{"name":"meros","version":"1.1.0-beta.2","description":"A fast 633B utility that makes reading multipart responses simple","keywords":["defer","fetch","graphql","multipart mixed","multipart","reader","stream","utility"],"repository":{"type":"git","url":"git+https://github.com/maraisr/meros.git"},"license":"MIT","author":{"name":"Marais Rossouw","email":"me@marais.dev","url":"https://marais.io"},"sideEffects":false,"exports":{".":{"import":"./node/index.mjs","browser":"./browser/index.mjs","require":"./node/index.js"},"./browser":{"import":"./browser/index.mjs","require":"./browser/index.js"},"./node":{"import":"./node/index.mjs","require":"./node/index.js"},"./package.json":"./package.json"},"main":"node/index.js","unpkg":"browser/index.min.js","module":"node/index.mjs","browser":"browser/index.mjs","types":"index.d.ts","scripts":{"bench":"node -r ts-node/register -r ./test/_polyfill.js bench/index.ts","build":"rollup -c","format":"prettier  --write --list-different \"{*,.github/**/*}.+(json|yml|md)\"","prepublishOnly":"yarn build","test":"uvu -r ts-node/register -r ./test/_polyfill.js -i _polyfill -i mocks test","typecheck":"tsc --noEmit"},"prettier":{"bracketSpacing":true,"printWidth":80,"proseWrap":"always","singleQuote":true,"tabWidth":4,"trailingComma":"all","useTabs":true,"overrides":[{"files":["*.md"],"options":{"tabWidth":2,"useTabs":false}}]},"devDependencies":{"@marais/tsconfig":"0.0.1","@rollup/plugin-node-resolve":"11.1.1","@types/benchmark":"2.1.0","@types/node":"14.14.22","benchmark":"2.1.4","fetch-multipart-graphql":"3.0.0","it-multipart":"1.0.6","prettier":"2.2.1","rollup":"2.38.4","rollup-plugin-dts":"2.0.1","rollup-plugin-filesize":"9.1.0","rollup-plugin-terser":"7.0.2","rollup-plugin-typescript2":"0.29.0","ts-node":"9.1.1","typescript":"4.1.3","uvu":"0.5.1"},"volta":{"node":"15.8.0","yarn":"1.22.5"},"readme":"<div align=\"center\">\n\t<h1 align=\"center\">meros</h1>\n\t<p align=\"center\"><code>yarn add meros</code> makes reading multipart responses simple</p>\n\t<hr />\n\t<span>\n\t\t<a href=\"https://github.com/maraisr/meros/actions?query=workflow:CI+branch:main\">\n\t\t\t<img src=\"https://github.com/maraisr/meros/workflows/CI/badge.svg?query=branch:main\"/>\n\t\t</a>\n\t\t<a href=\"https://npm.im/meros\">\n\t\t\t<img src=\"https://img.shields.io/npm/dm/meros\" alt=\"downloads\"/>\n\t\t</a>\n\t\t<a href=\"https://bundlephobia.com/result?p=meros\">\n\t\t\t<img src=\"https://badgen.net/bundlephobia/minzip/meros\" alt=\"size\"/>\n\t\t</a>\n\t</span>\n</div>\n\n## ⚡ Features\n\n- No dependencies\n- Super [performant](#-benchmark)\n- Supports _any_<sup>1</sup> `content-type`\n- _preamble_ and _epilogue_ don't yield\n- Browser-Compatible\n- Plugs into existing libraries like Relay and rxjs\n\n## ⚙️ Install\n\n```sh\nyarn add meros\n```\n\n## 🚀 Usage\n\n```ts\n// Rely on bundler/environment dection\nimport { meros } from 'meros';\n\nconst parts = await fetch('/api').then(meros);\n\n// As a simple Async Generator\nfor await (const part of parts) {\n  // Do something with this part\n}\n\n// Used with rxjs streams\nfrom(parts).pipe(\n  tap((part) => {\n    // Do something with it\n  }),\n);\n```\n\n## _Specific Environment_\n\n```ts\n// Browser\nimport { meros } from 'meros/browser';\n// import { meros } from 'https://cdn.skypack.dev/meros';\n\nconst parts = await fetch('/api').then(meros);\n\n// Node\nimport http from 'http';\nimport { meros } from 'meros/node';\n\nconst response = await new Promise((resolve) => {\n  const request = http.get(`http://my-domain/mock-ep`, (response) => {\n    resolve(response);\n  });\n  request.end();\n});\n\nconst parts = await meros(response);\n```\n\n## 🎒 Notes\n\nThis library aims to implement [RFC1341] in its entirety, however we aren't\nthere yet. That being said, you may very well use this library in other\nscenarios like streaming in file form uploads.\n\nPlease note; be sure to define a boundary that can be guaranteed to never\ncollide with things from the body:\n\n> _Because encapsulation boundaries must not appear in the body parts being\n> encapsulated, a user agent must exercise care to choose a unique boundary._\n>\n> <small>~ [RFC1341] 7.2.1</small>\n\n- `meros` comes from Ancient Greek μέρος méros, meaning \"part\".\n\n### _Caveats_\n\n- No support the `/alternative` , `/digest` _or_ `/parallel` subtype at this\n  time.\n- No support for\n  [nested multiparts](https://tools.ietf.org/html/rfc1341#appendix-C)\n\n## 🔎 API\n\nMeros offers two flavours, both for the browser and for node; but their api's\nare fundamentally the same.\n\n> **Note:** The type `Response` is used loosely here and simply alludes to\n> Node's `IncomingMessage` or the browser's `Response` type.\n\n### `meros(response: Response, options?: Options)`\n\nReturns: `Promise<Response | AsyncGenerator<Part | Part[]>`\n\nMeros returns a promise that will resolve to an `AsyncGenerator` if the response\nis of `multipart/mixed` mime, or simply returns the `Response` if something\nelse; helpful for middlewares. The idea here being that you run meros as a chain\noff fetch.\n\n```ts\nfetch('/api').then(meros);\n```\n\n> If the `content-type` is **NOT** a multipart, then meros will resolve with the\n> response argument.\n>\n> <details>\n> <summary>Example on how to handle this case</summary>\n>\n> ```ts\n> import { meros } from 'meros';\n>\n> const response = await fetch('/api'); // Assume this returns json\n> const parts = await meros(response);\n>\n> if (parts[Symbol.asyncIterator] < 'u') {\n>   for await (const part of parts) {\n>     // Do something with this part\n>   }\n> } else {\n>   const data = await parts.json();\n> }\n> ```\n>\n> </details>\n\neach `Part` gives you access to:\n\n- `json: boolean` ~ Tells you the `body` would be a JavaScript object of your\n  defined generic `T`.\n- `headers: object` ~ A key-value pair of all headers discovered from this part.\n- `body: T | Fallback` ~ Is the _body_ of the part, either as a JavaScript\n  object (noted by `json`) _or_ the base type of the environment\n  (`Buffer | string`, for Node and Browser respectively).\n\n#### `options.multiple: boolean`\n\nDefault: `false`\n\nSetting this to `true` will yield once for all available parts of a chunk,\nrather than yielding once per part. This is an optimization technique for\ntechnologies like GraphQL where rather than commit the payload to the store, to\nbe added-to in the next process-tick we can simply do that synchronously.\n\n> **Important:** This will alter the behaviour and yield arrays—than yield\n> payloads.\n\n```ts\nconst chunks = await fetch('/api').then((response) =>\n  meros(response, { multiple: true }),\n);\n\n// As a simple Async Generator\nfor await (const parts of chunks) {\n  for (const part of parts) {\n    // Do something with this part, maybe aggregate?\n  }\n}\n```\n\n## 💨 Benchmark\n\n```\nValidation :: node\n✔ meros\n✘ it-multipart (FAILED @ \"should match reference patch set\")\n\nBenchmark :: node\n  meros                     x 28,232 ops/sec ±0.91% (77 runs sampled)\n  it-multipart              x 16,909 ops/sec ±0.55% (79 runs sampled)\n\nValidation :: browser\n✔ meros\n✘ fetch-multipart-graphql (FAILED @ \"should match reference patch set\")\n\nBenchmark :: browser\n  meros                     x 29,291 ops/sec ±0.50% (76 runs sampled)\n  fetch-multipart-graphql   x 27,804 ops/sec ±0.55% (82 runs sampled)\n```\n\n> Ran with Node v15.8.0\n\n## ❤ Thanks\n\nSpecial thanks to [Luke Edwards](https://github.com/lukeed) for performance\nguidance and high level api design.\n\n## License\n\nMIT © [Marais Rossouw](https://marais.io)\n\n<details>\n<summary>Footnote</summary>\n\n> 1: By default, we'll look for JSON, and parse that for you. If not, we'll give\n> you the body as what was streamed.\n\n</details>\n\n[rfc1341]: https://tools.ietf.org/html/rfc1341 'The Multipart Content-Type'\n","readmeFilename":"readme.md","gitHead":"a87ef6917ce939fc1de65281354d20f442733059","bugs":{"url":"https://github.com/maraisr/meros/issues"},"homepage":"https://github.com/maraisr/meros#readme","_id":"meros@1.1.0-beta.2","_nodeVersion":"15.8.0","_npmVersion":"7.5.1","dist":{"integrity":"sha512-XimPTn4kehyFVEPTyPpyiXGM+VD5QO8y5yAvnEl74Pn4xaOt1O243zBkWJps/xkxyIyRLBBRN/rPGXNOoKd8IQ==","shasum":"5a23f20689234527b4dded3178792b5c9ca571e5","tarball":"http://registry-npm.ywwl.com/meros/download/meros-1.1.0-beta.2.tgz","fileCount":11,"unpackedSize":30700,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgGmRJCRA9TVsSAnZWagAACAkP/jWKcw5OgC1MSQoz7lO2\na/UJXSKYgAf/P656d8DdgKB324DLveAk9vIbr5ARfF2Ufb00Hc/AEv6b8619\nIyOmMz4TEt16s1GJwWezjq2Fr2Ulv6fBPN4TwZzkEEMDQtqUDdZc/Q9FWWSY\nCYCsENSu+Nh+ZqO5ZKtMUBjWSp3FPpiX6mlZcMIOHb7TvL1xYt0EYlrnmU3D\n3wZak9kKX+7eobMGRUMu2hNW9zKtWEGwqUqsFEKmjmmvRQRIDbUFyG5nHqwB\nqJY/O34asFgpzz1jKPK8rAyGqtckNXB1Pxkq4+24KK6iQcXzQD7QP3g2Onj7\nUeo1gGKSK/XGLj8EgrBvetN++OQOsm7uj8DJ9P7eyOs/sky6HsWy2ygw/Bvv\nVdT1Rv7TyXKifVs2oJzyNPtCXq4fIe8d/gJl0hdnzQeMBzL3gLy9++Jkc63p\nTOLXSDmrq2oad7291f5Mh13SG9AgcMYugl4UHRkVrL9VI6PN/cl/R+GyN3hc\n/Emhr5dFySjtUXOOw08gCEdEMH44nQTIPA7U6XjkOlu+lgeG/oocM3LDQ3gt\n4egzTOlCWnOMHE9rRDLiuHFs9qON56/l8+24SX4zQCqU+w8LgAkT8txs70BR\n84kQ6cjM78nizK2ico7EAgy09zOJrpmOCXiOQJdd/m04NUeWOwPHXuH521JX\nTHYX\r\n=Jxew\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDXShwARSCa+hZDTzdlgo8sfwmYtN0DQgQzphgz0/QQ4gIhAJTVbUIzQreMBHRGlOuMyh431XBobYqt5au0tOjUY0Km"}],"size":7274,"noattachment":false,"key":"/meros/-/meros-1.1.0-beta.2.tgz"},"_npmUser":{"name":"marais","email":"me@maraisr.com"},"directories":{},"maintainers":[{"name":"marais","email":"me@maraisr.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/meros_1.1.0-beta.2_1612342345403_0.5831655694728015"},"_hasShrinkwrap":false,"publish_time":1612342345523,"_cnpm_publish_time":1612342345523,"dist-tags":{"beta":"1.1.0-beta.2","latest":"1.3.2","next":"1.2.0-next.4"}}