meros
A fast utility for reading streamed multipart responses.
Last updated 6 years ago by marais .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install meros 
SYNC missed versions from official npm registry.

meros · From Ancient Greek μέρος (méros, "part").

CI codecov

A fast (761B) utility for reading streamed multipart/mixed responses.

⚡ Features

  • ✅ No dependencies
  • ✅ Super performant
  • ✅ Supports any1 content-type
  • ✅ Supports fall through2 content-type's
  • ✅ Supports content-length3
  • ✅ preamble and epilogue don't yield
  • ✅ Browser-Compatible
  • ✅ Plugs into existing libraries like Relay and rxjs

⚙️ Install

yarn add meros

🚀 Usage

// Rely on bundler/environment dection
import { meros } from 'meros';

const parts = await fetch('/fetch-multipart').then(meros);

// As a simple Async Generator
for await (const part of parts) {
	// Do something with this part
}

// Used with rxjs streams
from(parts).pipe(
	tap((part) => {
		// Do something with it
	}),
);

Specific Environment

// Browser
import { meros } from 'meros/browser';
// import { meros } from 'https://cdn.skypack.dev/meros';

const parts = await fetch('/fetch-multipart').then(meros);

// Node
import http from 'http';
import { meros } from 'meros/node';

const response = await new Promise((resolve) => {
	const request = http.get(`http://my-domain/mock-ep`, (response) => {
		resolve(response);
	});
	request.end();
});

const parts = await meros(response);

🎒 Notes

This library aims to implement RFC1341 in its entirety, however we aren't there yet. That being said, you may very well use this library in other scenarios like streaming in file form uploads.

Please note; be sure to define a boundary that can be guaranteed to never collide with things from the body:

Because encapsulation boundaries must not appear in the body parts being encapsulated, a user agent must exercise care to choose a unique boundary.

~ RFC1341 7.2.1

Caveats

  • No support the /alternative , /digest or /parallel subtype at this time.
  • No support for nested multiparts

🔎 API

Browser

function meros<T = unknown>(
	response: Response,
): Promise<Response | AsyncGenerator<T>>;

Node

function meros<T = unknown>(
	response: IncomingMessage,
): Promise<IncomingMessage | AsyncGenerator<T>>;

Returns an async generator that yields on every part. Worth noting that if multiple parts are present in one chunk, each part will yield independently.

If the content-type is a multipart, then it will resolve with the response argument.

💨 Benchmark

Ran with Node v15.1.0

Validation :: node
✔ meros
✘ it-multipart (FAILED @ "should match reference patch set")

Benchmark :: node
  meros                     x 32,900 ops/sec ±0.89% (78 runs sampled)
  it-multipart              x 16,033 ops/sec ±0.98% (77 runs sampled)

Validation :: browser
✔ meros
✘ fetch-multipart-graphql (FAILED @ "should match reference patch set")

Benchmark :: browser
  meros                     x 28,226 ops/sec ±1.20% (80 runs sampled)
  fetch-multipart-graphql   x 14,835 ops/sec ±0.90% (78 runs sampled)
<summary>Reference patch set</summary>
content-type: "multipart/mixed; boundary=abc123"
preamble
--abc123
Content-Type: application/json
Content-Length: 17

{"hello":"world"}

--abc123
Content-Type: application/json
Content-Length: 17

{"other":"world"}

--abc123
Content-Type: application/json
Content-Length: 19

{"another":"world"}

--abc123
Content-Type: application/json
Content-Length: 39

{"massive":{"nested":{"world":"okay"}}}

--abc123--
epilogue
--abc123
Content-Type: application/json
Content-Length: 19

{"shouldnt":"work"}

❤ Thanks

Special thanks to Luke Edwards for performance guidance and high level api design.

License

MIT © Marais Rossouw

<summary>Footnote</summary>

1: By default, we'll look for JSON, and parse that for you. If not, we'll give you the body as what was streamed.

2: Unlike the spec assuming application/octet-stream for untyped chunks, we assume whatever content-type we last encountered. See RFC1341 Section 4

3: If not given, everything from the body through boundary will yield

Current Tags

  • 1.1.0-beta.2                                ...           beta (6 years ago)
  • 1.3.2                                ...           latest (a year ago)
  • 1.2.0-next.4                                ...           next (5 years ago)

31 Versions

  • 1.3.2                                ...           a year ago
  • 1.3.1                                ...           a year ago
  • 1.3.0                                ...           4 years ago
  • 1.2.1                                ...           4 years ago
  • 1.2.0                                ...           5 years ago
  • 1.2.0-next.4                                ...           5 years ago
  • 1.2.0-next.3                                ...           5 years ago
  • 1.2.0-next.2                                ...           5 years ago
  • 1.2.0-next.1                                ...           5 years ago
  • 1.1.4                                ...           6 years ago
  • 1.1.3                                ...           6 years ago
  • 1.1.2                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.1.0-beta.2                                ...           6 years ago
  • 1.1.0-beta.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
  • 1.0.0-beta.11                                ...           6 years ago
  • 1.0.0-beta.10                                ...           6 years ago
  • 1.0.0-beta.9                                ...           6 years ago
  • 1.0.0-beta.8                                ...           6 years ago
  • 1.0.0-beta.7                                ...           6 years ago
  • 1.0.0-beta.6                                ...           6 years ago
  • 1.0.0-beta.5                                ...           6 years ago
  • 1.0.0-beta.4                                ...           6 years ago
  • 1.0.0-beta.3                                ...           6 years ago
  • 1.0.0-beta.2                                ...           6 years ago
  • 1.0.0-beta.1                                ...           6 years ago
  • 0.0.3                                ...           6 years ago
  • 0.0.2                                ...           6 years ago
  • 0.0.1                                ...           6 years ago
Maintainers (1)
Downloads
Total 118
Today 0
This Week 0
This Month 66
Last Day 0
Last Week 41
Last Month 31
Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE