{"name":"meros","version":"1.2.0-next.4","description":"A fast 626B 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":{".":{"browser":{"import":"./browser/index.mjs","require":"./browser/index.js"},"node":{"import":"./node/index.mjs","require":"./node/index.js"},"default":{"import":"./node/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 tsm -r ./test/_polyfill.ts bench/index.ts","build":"rollup -c","format":"prettier --write --list-different \"{*,examples/*/**,.github/**/*}.+(ts|tsx|json|yml|md)\"","prepublishOnly":"pnpm run build","test":"uvu test \".spec.ts$\" -r tsm -r test/_polyfill.ts -i suites","typecheck":"tsc --noEmit"},"devDependencies":{"@marais/tsconfig":"0.0.2","@n1ru4l/push-pull-async-iterable-iterator":"3.2.0","@rollup/plugin-node-resolve":"13.1.1","@types/node":"17.0.2","prettier":"2.5.1","rollup":"2.61.1","rollup-plugin-dts":"4.0.1","rollup-plugin-terser":"7.0.2","tsm":"2.2.1","typescript":"4.5.4","uvu":"0.5.2"},"peerDependencies":{"@types/node":">=12"},"peerDependenciesMeta":{"@types/node":{"optional":true}},"engines":{"node":">=12"},"volta":{"node":"17.3.0"},"readme":"<img src=\"logo.svg\" alt=\"meros\">\n<br />\n<br />\n\n<div align=\"right\">\n\t<p><code>npm add meros</code> makes reading multipart responses simple</p>\n\t<span>\n\t\t<a href=\"https://github.com/maraisr/meros/actions/workflows/ci.yml\">\n\t\t\t<img src=\"https://github.com/maraisr/meros/actions/workflows/ci.yml/badge.svg\"/>\n\t\t</a>\n\t\t<a href=\"https://npm-stat.com/charts.html?package=meros\">\n    \t\t<img src=\"https://badgen.net/npm/dw/meros?labelColor=black&color=black&cache=600\" 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?labelColor=black&color=black\" alt=\"size\"/>\n\t\t</a>\n\t</span>\n  <br />\n  <br />\n</div>\n\n## ⚡ Features\n\n- No dependencies\n- Seemless api\n- Super [performant](#-benchmark)\n- Supports _any_<sup>1</sup> `content-type`\n- _preamble_ and _epilogue_ don't yield\n- Browser/Node Compatible\n- Plugs into existing libraries like Relay and rxjs\n\n## 🚀 Usage\n\n> Visit [/examples](/examples) for more info!\n\n```ts\n// Relies on bundler/environment detection\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).subscribe((part) => {\n  // Do something with it\n});\n```\n\n## _Specific Environment_\n\n#### _Browser_\n\n```ts\nimport { meros } from 'meros/browser';\n// import { meros } from 'https://cdn.skypack.dev/meros';\n\nconst parts = await fetch('/api').then(meros);\n```\n\n#### _Node_\n\n```ts\nimport http from 'http';\nimport { meros } from 'meros/node';\n\nconst response = await new Promise((resolve) => {\n  const request = http.get(`http://example.com/api`, (response) => {\n    resolve(response);\n  });\n  request.end();\n});\n\nconst parts = await meros(response);\n```\n\n## 🔎 API\n\nMeros offers two flavours, both for the browser and for node; but their api's are fundamentally the same.\n\n> **Note:** The type `Response` is used loosely here and simply alludes to Node's `IncomingMessage` or the browser's\n> `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 is of `multipart/mixed` mime, or simply\nreturns the `Response` if something else; helpful for middlewares. The idea here being that you run meros as a chain off\nfetch.\n\n```ts\nfetch('/api').then(meros);\n```\n\n> If the `content-type` is **NOT** a multipart, then meros will resolve with the 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 isnt multipart\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 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 object (noted by `json`) _or_ the base type\n  of the environment (`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, rather than yielding once per part. This is\nan optimization technique for technologies like GraphQL where rather than commit the payload to the store, to be\nadded-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 payloads.\n\n```ts\nconst chunks = await fetch('/api').then((response) => meros(response, { multiple: true }));\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> via the [`/bench`](/bench) directory with Node v16.11.0\n\n```\nValidation :: node\n✔ meros\n✘ it-multipart (FAILED @ \"should match reference patch set\")\n\nBenchmark :: node\n  meros                     x 262,515 ops/sec ±2.45% (77 runs sampled)\n  it-multipart              x 165,562 ops/sec ±0.99% (79 runs sampled)\n\nValidation :: browser\n✔ meros\n✘ fetch-multipart-graphql (FAILED @ \"should match reference patch set\")\n\nBenchmark :: browser\n  meros                     x 864,124 ops/sec ±0.77% (80 runs sampled)\n  fetch-multipart-graphql   x 283,296 ops/sec ±0.96% (82 runs sampled)\n```\n\n## 🎒 Notes\n\nWhy the name? _meros_ comes from Ancient Greek μέρος méros, meaning \"part\".\n\nThis library aims to implement [RFC1341] in its entirety, however we aren't there yet. That being said, you may very\nwell use this library in other scenarios like streaming in file form uploads.\n\nAnother goal here is to aide in being the defacto standard transport library to support\n[`@defer` and `@stream` GraphQL directives](https://foundation.graphql.org/news/2020/12/08/improving-latency-with-defer-and-stream-directives/)\n\n### _Caveats_\n\n- No support the `/alternative` , `/digest` _or_ `/parallel` subtype at this time.\n- No support for [nested multiparts](https://tools.ietf.org/html/rfc1341#appendix-C)\n\n## ❤ Thanks\n\nSpecial thanks to [Luke Edwards](https://github.com/lukeed) for performance guidance 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 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":"922cfe244297a807734c0450f1102c5dfaf8dc28","bugs":{"url":"https://github.com/maraisr/meros/issues"},"homepage":"https://github.com/maraisr/meros#readme","_id":"meros@1.2.0-next.4","_nodeVersion":"17.3.0","_npmVersion":"8.3.0","dist":{"integrity":"sha512-Cf/wlu3IA8QhOAXwRR3KI//SUTSu4U+QGNaJ4tk278m6Lu1UtOBnAOIDYI5D3W+dDT74q6zQr8VMRGnCebjRBg==","shasum":"480c91d76d409d6a32ee5a633954aae04b93b03a","tarball":"http://registry-npm.ywwl.com/meros/download/meros-1.2.0-next.4.tgz","fileCount":11,"unpackedSize":29149,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhxYinCRA9TVsSAnZWagAA5X4QAJrUAoy5BBhD1BH3KhCp\ne4+vKMaUetxFzyQQeis9IwgAmzQWK3ah8YG/PbAmQ5FFBAjj5bdTs7Krqeja\nlXYLWp+CSdUXMM6VaXLEOeWn8D++iUV/HfMi8DNvEfO2NFhUYQuJlBTYi7kb\n7G0Vsijp04k1trVYHB2P/KGBWabnPQzcsV6jkizQcP1UtqqFZYXDrYkndyGL\nEQWZw0jkmaFk81KdqTd3E4gnGigZ0peqmvF6qtrLqxhQucF/FyZyGE28JNId\n/or9BP7vmEjyBYFVzaSYcU+9jjMqBWZF/NMP3wHbGnVQ5C2/BYSjxT7ZC7Y2\nk3YL90fN0EISaYZKrbMBWAeReg1NdeaWY1x393Vhz86JpNEM+PxaXJrXx+XD\nXr/FXxtdvRct3CuS99ia4uKNzR4nnM8punjhw2My672yBw/mSxWNloNG+6fL\n/qnEuRfScgMrkaeswH15TiulUn9ldvWgpOloec4Mplexn3OvMNg0TIVDMBIp\nrsW/RXUCtClPE2G+tz1k69GsVacObHr+skFPDPoMP13PDy62oJ9hiZTbYZO/\n6Llfjcc/o4rS3NkVAdry4VtHOEgcYoHLE5GneBZbx2EqPFXDr/aWQuNE4sIx\nnSIHtnIi6PC0hTtPGcQkIm1l74NwEV0pGGAJSfdTBkB2Y9ZT4BJZkj85JMax\n2AoT\r\n=e9dt\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBvkli/BqB1XSAD0QslZ1V3FYpugBvhdAcXkIy/67ehvAiEAuyp1Mlnh46mLK2RLr1/9a34eEuf6FI8gQgnY29CImlE="}],"size":7014,"noattachment":false,"key":"/meros/-/meros-1.2.0-next.4.tgz"},"_npmUser":{"name":"marais","email":"me@marais.co"},"directories":{},"maintainers":[{"name":"marais","email":"me@marais.co"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/meros_1.2.0-next.4_1640335527831_0.5562149100667162"},"_hasShrinkwrap":false,"publish_time":1640335527989,"_cnpm_publish_time":1640335527989,"dist-tags":{"beta":"1.1.0-beta.2","latest":"1.3.2","next":"1.2.0-next.4"}}