Skip to content

xingrz/multipartist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multipartist

Build multipart stream, with stream.

Unlike form-data, this library provides more lower-level APIs to build not only multipart/form-data but also multipart/related and any other similar structures.

Requirements

Node >= 4, tested on latest Node and latest LTS Node.

Installation

npm install --save multipartist

Usage

import Multipart from 'multipartist';
import request from 'request';
import { createReadStream } from 'fs';

const multipart = new Multipart('form-data');

multipart.append(JSON.stringify({ 'text': 'Hello world!' }), {
  'Content-Disposition': 'form-data; name="metadata"',
  'Content-Type': 'application/json; charset=UTF-8',
});

multipart.append(createReadStream('audio.wav'), {
  'Content-Disposition': 'form-data; name="audio"',
  'Content-Type': 'application/octet-stream',
  'X-Speaker-Name': 'XiNGRZ',
});

multipart.pipe(request.post('https://api.example.com/recognize', {
  headers: multipart.headers({
    'Authorization': 'Bearer YOUR_OWN_API_KEY_HERE',
  }),
}, (req, res, body) => {
  // ...
}));

API

Class: Multipartist(type[, options])

The Multipartist class, which is an subclass of Readable Stream.

Arguments

  • type String - Multipart type (e.g. form-data, related, etc...). Defaults to form-data.
  • options Object
    • endOnEmpty=true - Whether the Multipartist readable should end when it's empty. Otherwise, it won't end until explicitly call flush()

Multipartist#append(content[, length][, headers])

Add a part.

Arguments

  • content String | Buffer | Readable - Content of this part
  • length Number - Optional. Length of the content of this part. It's better to specific the length of a Readable part explicitly. Otherwise, the Content-Length of the whole multipart payload would not be caculated.
  • headers Object - Optional. Additional headers of this part

Returns

Multipartist - the Multipartist instance itself

Multipartist#headers([additions])

Returns all auto-generated headers.

Arguments

  • headers Object - Optional. Additional headers

Returns

  • Object - Headers

Multipartist#flush()

Flush the ending data and end the stream. Only required if endOnEmpty is explicitly set to false.

Test

npm test

License

This project is released under the terms of MIT License.

About

A library to build multipart data, with streaming APIs.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published