npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

multi-part-lite

v1.0.0

Published

Simple and fast multipart/form-data implementation without any dependencies. With Stream and Buffer modes. Supports: Strings, Numbers, Arrays, Streams, Buffers and Vinyl.

Downloads

78,989

Readme

multi-part-lite License npm

Build Status node Test Coverage

A multi-part-lite allows you to create multipart/form-data Stream and Buffer, which can be used to submit forms and file uploads to other web applications.

There are no external dependencies, so it as fast and simple as possible.

Supports: Strings, Numbers, Arrays, Streams, Buffers and Vinyl.

Install

$ npm install multi-part-lite --save

Usage

Usage with got as Stream:

const got = require('got');
const Multipart = require('multi-part-lite');
const form = new Multipart();

form.append('photo', got.stream('https://avatars1.githubusercontent.com/u/2401029'), { filename: 'image.jpg', contentType: 'image/jpeg' });
form.append('field', 'multi-part test');

got.post('127.0.0.1:3000', { headers: form.getHeaders(), body: form.stream() });

Usage with got as Buffer:

const got = require('got');
const Multipart = require('multi-part-lite');
const form = new Multipart();

form.append('photo', got.stream('https://avatars1.githubusercontent.com/u/2401029'), { filename: 'image.jpg', contentType: 'image/jpeg' });
form.append('field', 'multi-part test');

(async () => {
  const body = await form.buffer();
  got.post('127.0.0.1:3000', { headers: form.getHeaders(false), body });
})()

Usage with http/https as Stream:

const http = require('http');
const https = require('https');
const Multipart = require('multi-part-lite');
const form = new Multipart();

form.append('photo', https.request('https://avatars1.githubusercontent.com/u/2401029'), { filename: 'image.jpg', contentType: 'image/jpeg' });

form.stream().pipe(http.request({ headers: form.getHeaders(), hostname: '127.0.0.1', port: 3000, method: 'POST' }));

Usage with http/https as Buffer:

const http = require('http');
const https = require('https');
const Multipart = require('multi-part-lite');
const form = new Multipart();

form.append('photo', https.request('https://avatars1.githubusercontent.com/u/2401029'), { filename: 'image.jpg', contentType: 'image/jpeg' });

(async () => {
  const body = await form.buffer();
  const req = http.request({ headers: form.getHeaders(false), hostname: '127.0.0.1', port: 3000, method: 'POST' });
  req.end(body);
})()

API

new Multipart([options])

Constructor.

Params:

  • [options] (Object) - Object with options:
    • [boundary] (String|Number) - Custom boundary for multipart data. Ex: if equal CustomBoundary, boundary will be equal exactly CustomBoundary.
    • [boundaryPrefix] (String|Number) - Custom boundary prefix for multipart data. Ex: if equal CustomBoundary, boundary will be equal something like --CustomBoundary567689371204.
    • [defaults] (Object) - Object with defaults values:
      • [name] (String) - File name which will be used, if filename is not specified in the options of .append method. By default file.
      • [ext] (String) - File extension which will be used, if filename is not specified in the options of .append method. By default bin.
      • [type] (String) - File content-type which will be used, if contentType is not specified in the options of .append method. By default application/octet-stream.

.append(name, value, [options])

Adds a new data to the multipart/form-data stream.

Params:

  • name (String|Number) - Field name. Ex: photo.
  • value (Mixed) - Value can be String, Number, Array, Buffer, ReadableStream or even Vynil.
  • [options] (Object) - Additional options:
    • filename (String) - File name. You should always specify file name with extension, otherwise file.bin will be set. Ex: anonim.jpg.
    • contentType (String) - File content type. You should always specify content-type, otherwise application/octet-stream will be set. Ex: image/jpeg.

If value is an array, append will be called for each value:

form.append('array', [0, [2, 3], 1]);

// similar to

form.append('array', 0);
form.append('array', 2);
form.append('array', 3);
form.append('array', 1);

Null, false and true will be converted to '0', '0' and '1'. Numbers will be converted to strings also.

Warning: You must specify the correct contentType and filename. This library doesn't validate them. You can use multi-part library which can handle it for you.

.stream()

Returns a multipart/form-data stream.

.buffer()

Returns a Promise with a buffer of the multipart/form-data stream data.

.getBoundary()

Returns the form boundary used in the multipart/form-data stream.

form.getBoundary(); // -> '--MultipartBoundary352840693617'

.getLength()

Returns the length of a buffer of the multipart/form-data stream data.

Should be called after .buffer();

For .stream() it's always 0.

await form.buffer();
form.getLength(); // -> 12345

.getHeaders(chunked = true)

Returns the headers.

If you want to get correct content-length, you should call it after .buffer(). There is no way to know content-length of the .stream(), so it will be always 0.

Params:

  • chunked (Boolean) - If false - headers will include content-length header, otherwise there will be transfer-encoding: 'chunked'.
form.getHeaders(); // ->
//{
//  'transfer-encoding': 'chunked',
//  'content-type': 'multipart/form-data; boundary="--MultipartBoundary352840693617"'
//}

With .buffer():

form.getHeaders(false); // ->
//{
//  'content-length': '0',
//  'content-type': 'multipart/form-data; boundary="--MultipartBoundary352840693617"'
//}

await form.buffer();
form.getHeaders(false); // ->
//{
//  'content-length': '12345',
//  'content-type': 'multipart/form-data; boundary="--MultipartBoundary352840693617"'
//}

License

The MIT License (MIT) Copyright (c) 2019 Alexey Bystrov