$ npm install meros
meros · From Ancient Greek μέρος (méros, "part").A fast (761B) utility for reading streamed multipart/mixed responses.
content-typecontent-type'scontent-length3yarn add meros
// 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
}),
);
// 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);
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
/alternative , /digest or /parallel subtype at this
time.function meros<T = unknown>(
response: Response,
): Promise<Response | AsyncGenerator<T>>;
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-typeis a multipart, then it will resolve with the response argument.
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)
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"}
Special thanks to Luke Edwards for performance guidance and high level api design.
MIT © Marais Rossouw
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-streamfor untyped chunks, we assume whatevercontent-typewe last encountered. See RFC1341 Section 43: If not given, everything from the body through boundary will yield
© 2010 - cnpmjs.org x YWFE | Home | YWFE