meros
A fast 699B utility that makes reading multipart responses simple
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

yarn add meros makes reading multipart responses simple


downloads size

⚡ Features

  • No dependencies
  • Super performant
  • Supports any1 content-type
  • 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

  • meros comes from Ancient Greek μέρος méros, meaning "part".

Caveats

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

🔎 API

Browser

function meros<T = object>(
  response: Response,
): Promise<
  | Response
  | AsyncGenerator<
      | { json: true; headers: Record<string, string>; body: T }
      | { json: false; headers: Record<string, string>; body: string }
    >
>;

Node

function meros<T = object>(
  response: IncomingMessage,
): Promise<
  | IncomingMessage
  | AsyncGenerator<
      | { json: true; headers: Record<string, string>; body: T }
      | { json: false; headers: Record<string, string>; body: Buffer }
    >
>;

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 NOT a multipart, then it will resolve with the response argument. Or really any unhandled cases, we'll resolve with the response.

<summary>Example on how to handle this case</summary>
import { meros } from 'meros';

const response = await fetch('/fetch-multipart'); // Assume this returns json
const parts = await meros(response);

if (parts[Symbol.asyncIterator] < 'u') {
  for await (const part of parts) {
    // Do something with this part
  }
} else {
  const data = await parts.json();
}

💨 Benchmark

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

Benchmark :: node
  meros                     x 27,567 ops/sec ±2.04% (77 runs sampled)
  it-multipart              x 16,064 ops/sec ±1.20% (79 runs sampled)

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

Benchmark :: browser
  meros                     x 30,077 ops/sec ±0.92% (80 runs sampled)
  fetch-multipart-graphql   x 25,551 ops/sec ±1.44% (81 runs sampled)

Ran with Node v15.1.0

❤ 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.

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