Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thenorthmemory/multipart

Simple and lite of the multipart/form-data implementation.
https://github.com/thenorthmemory/multipart

Last synced: about 2 months ago
JSON representation

Simple and lite of the multipart/form-data implementation.

Awesome Lists containing this project

README

        

# Multipart

Simple and lite of the `multipart/form-data` implementation. Split from `wechatpay-axios-plugin` project for general usages.

[![release](https://img.shields.io/npm/v/@thenorthmemory/multipart)](https://github.com/TheNorthMemory/multipart/releases)
[![vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/@thenorthmemory/multipart?label=snyk.io)](https://snyk.io/advisor/npm-package/@thenorthmemory/multipart)
[![types](https://img.shields.io/badge/types-included-blue)](https://www.npmjs.com/package/@thenorthmemory/multipart)
[![requirement](https://img.shields.io/node/v/@thenorthmemory/multipart)](https://www.npmjs.com/package/@thenorthmemory/multipart)
[![downloads](https://img.shields.io/npm/dm/@thenorthmemory/multipart)](https://www.npmjs.com/package/@thenorthmemory/multipart)
[![license](https://img.shields.io/npm/l/@thenorthmemory/multipart)](https://www.npmjs.com/package/@thenorthmemory/multipart)

## Usage

`npm i @thenorthmemory/multipart`

```js
import Multipart from '@thenorthmemory/multipart';

// buffer style(Synchronous)
(new Multipart())
.append('a', 1)
.append('b', '2')
.append('c', Buffer.from('31'))
.append('d', JSON.stringify({}), 'any.json')
.append('e', require('fs').readFileSync('/path/your/file.jpg'), 'file.jpg')
.getBuffer();

// stream style(Asynchronous)
(new Multipart())
.append('f', require('fs').createReadStream('/path/your/file2.jpg'), 'file2.jpg')
.pipe(require('fs').createWriteStream('./file3.jpg'));
```

## API

- [Multipart](#multipart)
- [Usage](#usage)
- [API](#api)
- [new Multipart()](#new-multipart)
- [multipart.mimeTypes](#multipartmimetypes)
- [multipart.boundary](#multipartboundary)
- [multipart.data](#multipartdata)
- [multipart.indices](#multipartindices)
- [multipart.getBuffer() ⇒ Buffer](#multipartgetbuffer--buffer)
- [multipart.getHeaders() ⇒ object.<string, string>](#multipartgetheaders--objectstring-string)
- [multipart.appendMimeTypes(things) ⇒ this](#multipartappendmimetypesthings--this)
- [multipart.append(name, value, [filename]) ⇒ this](#multipartappendname-value-filename--this)
- [multipart.formed(name, value, [filename]) ⇒ Array.<(Buffer\|ReadStream)>](#multipartformedname-value-filename--arraybufferreadstream)
- [multipart.set(name, value, [filename]) ⇒ this](#multipartsetname-value-filename--this)
- [multipart.delete(name) ⇒ this](#multipartdeletename--this)
- [multipart.get(name) ⇒ Buffer \| ReadStream \| undefined](#multipartgetname--buffer--readstream--undefined)
- [multipart.getAll(name) ⇒ Array.<(Buffer\|ReadStream)>](#multipartgetallname--arraybufferreadstream)
- [multipart.has(name) ⇒ boolean](#multiparthasname--boolean)
- [multipart.entries() ⇒ Iterator.<Array.<EntryTuple.<(string\|undefined), (Buffer\|ReadStream)>>>](#multipartentries--iteratorarrayentrytuplestringundefined-bufferreadstream)
- [multipart.keys() ⇒ Iterator.<(string\|undefined)>](#multipartkeys--iteratorstringundefined)
- [multipart.values() ⇒ Iterator.<(Buffer\|ReadStream)>](#multipartvalues--iteratorbufferreadstream)
- [multipart.toString() ⇒ string](#multiparttostring--string)
- [multipart.flowing([end]) ⇒ Promise.<this>](#multipartflowingend--promisethis)
- [multipart.pipe(destination, [options]) ⇒ stream.Writable](#multipartpipedestination-options--streamwritable)
- [License](#license)

### new Multipart()
Create a `multipart/form-data` buffer container for the media(image/video) file uploading.

### multipart.mimeTypes
**Kind**: instance property of [Multipart](#Multipart)
**Access**: protected
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| mimeTypes | object.<string, string> | Built-in mime-type mapping |

### multipart.boundary
**Kind**: instance property of [Multipart](#Multipart)
**Read only**: true
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| boundary | Buffer | The boundary buffer. |

### multipart.data
**Kind**: instance property of [Multipart](#Multipart)
**Access**: protected
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| data | Array.<(Buffer\|ReadStream)> | The Multipart's instance data storage |

### multipart.indices
**Kind**: instance property of [Multipart](#Multipart)
**Access**: protected
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| indices | Array.<IndexTuple.<(string\|undefined), number>> | The entities' value indices whose were in [data](#Multipart+data) |

### multipart.getBuffer() ⇒ Buffer
To retrieve the [Miltipart#data](Miltipart#data) buffer

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: Buffer - - The payload buffer

### multipart.getHeaders() ⇒ object.<string, string>
To retrieve the `Content-Type` multipart/form-data header

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: object.<string, string> - - The `Content-Type` header With [boundary](#Multipart+boundary)

### multipart.appendMimeTypes(things) ⇒ this
Append a customized [Multipart#mimeType](Multipart#mimeType)

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: this - - The `Multipart` class instance self

| Param | Type | Description |
| --- | --- | --- |
| things | object.<string, string> | The mime-type |

**Example**
```js
.appendMimeTypes({p12: 'application/x-pkcs12'})
.appendMimeTypes({txt: 'text/plain'})
```

### multipart.append(name, value, [filename]) ⇒ this
Append data wrapped by [boundary](#Multipart+boundary)

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: this - - The `Multipart` class instance self

| Param | Type | Description |
| --- | --- | --- |
| name | string | The field name |
| value | string \| Buffer \| ReadStream | The value |
| [filename] | string | Optional filename, when provided, then append the `Content-Type` after of the `Content-Disposition` |

### multipart.formed(name, value, [filename]) ⇒ Array.<(Buffer\|ReadStream)>
Formed a named value, a filename reported to the server, when a Buffer or FileStream is passed as the second parameter.

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: Array.<(Buffer\|ReadStream)> - - The part of data

| Param | Type | Description |
| --- | --- | --- |
| name | string | The field name |
| value | string \| Buffer \| ReadStream | The value |
| [filename] | string | Optional filename, when provided, then append the `Content-Type` after of the `Content-Disposition` |

### multipart.set(name, value, [filename]) ⇒ this
Sets a new value for an existing key inside a [data](#Multipart+data) instance, or adds the key/value if it does not already exist.

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: this - - The Multipart instance

| Param | Type | Description |
| --- | --- | --- |
| name | string | The field name |
| value | string \| Buffer \| ReadStream | The value |
| [filename] | string | Optional filename, when provided, then append the `Content-Type` after of the `Content-Disposition` |

### multipart.delete(name) ⇒ this
Deletes a key and its value(s) from a [data](#Multipart+data) instance

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: this - - The Multipart instance

| Param | Type | Description |
| --- | --- | --- |
| name | string | The field name |

### multipart.get(name) ⇒ Buffer \| ReadStream \| undefined
Returns the first value associated with a given key from within a [data](#Multipart+data) instance

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: Buffer \| ReadStream \| undefined - value - The value, undefined means none named key exists

| Param | Type | Description |
| --- | --- | --- |
| name | string | The field name |

### multipart.getAll(name) ⇒ Array.<(Buffer\|ReadStream)>
Returns all values associated with a given key from within a [data](#Multipart+data) instance

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: Array.<(Buffer\|ReadStream)> - value(s) - The value(s)

| Param | Type | Description |
| --- | --- | --- |
| name | string | The field name |

### multipart.has(name) ⇒ boolean
Returns a boolean stating whether a [data](#Multipart+data) instance contains a certain key.

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: boolean - - True for contains

| Param | Type | Description |
| --- | --- | --- |
| name | string | The field name |

### multipart.entries() ⇒ Iterator.<Array.<EntryTuple.<(string\|undefined), (Buffer\|ReadStream)>>>
To go through all key/value pairs contained in this [data](#Multipart+data) instance

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: Iterator.<Array.<EntryTuple.<(string\|undefined), (Buffer\|ReadStream)>>> - - An Array Iterator key/value pairs.

### multipart.keys() ⇒ Iterator.<(string\|undefined)>
To go through all keys contained in [data](#Multipart+data) instance

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: Iterator.<(string\|undefined)> - - An Array Iterator key pairs.

### multipart.values() ⇒ Iterator.<(Buffer\|ReadStream)>
To go through all values contained in [data](#Multipart+data) instance

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: Iterator.<(Buffer\|ReadStream)> - - An Array Iterator value pairs.

### multipart.toString() ⇒ string
**Kind**: instance method of [Multipart](#Multipart)
**Returns**: string - - FormData string

### multipart.flowing([end]) ⇒ Promise.<this>
Pushing [data](#Multipart+data) into the readable BufferList

**Kind**: instance method of [Multipart](#Multipart)
**Returns**: Promise.<this> - - The Multipart instance

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [end] | boolean | true | End the writer when the reader ends. Default: `true`. Available {@since v0.8.0} |

### multipart.pipe(destination, [options]) ⇒ stream.Writable
Attaches a Writable stream to the [Multipart](#Multipart) instance

## License

[MIT](LICENSE)