{"name":"it-length-prefixed","version":"5.0.0","description":"Streaming length prefixed buffers with async iterables","main":"src/index.js","types":"src/index.d.ts","scripts":{"test":"aegir test -t node -t browser","test:browser":"aegir test -t browser","test:node":"aegir test -t node","lint":"aegir lint","release":"aegir release -t node -t browser","release-minor":"aegir release --type minor -t node -t browser","release-major":"aegir release --type major -t node -t browser","build":"aegir build","coverage":"npx nyc --reporter=text --reporter=lcov npm run test:node -- --bail","coverage-publish":"npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov"},"repository":{"type":"git","url":"git+https://github.com/alanshaw/it-length-prefixed.git"},"keywords":["varint","async","iterable","iterator","length-prefixed-stream","length-prefixed"],"author":{"name":"Alan Shaw"},"license":"MIT","bugs":{"url":"https://github.com/alanshaw/it-length-prefixed/issues"},"homepage":"https://github.com/alanshaw/it-length-prefixed#readme","dependencies":{"bl":"^5.0.0","buffer":"^6.0.3","varint":"^6.0.0"},"devDependencies":{"@types/bl":"^4.1.0","aegir":"^33.0.0","chai":"^4.2.0","iso-random-stream":"^2.0.0","it-block":"^3.0.0","it-pipe":"^1.1.0","it-pushable":"^1.4.0","it-reader":"^3.0.0","p-defer":"^3.0.0","random-int":"^2.0.0","streaming-iterables":"^5.0.2"},"contributors":[{"name":"Alan Shaw","email":"alan.shaw@protocol.ai"},{"name":"Friedel Ziegelmayer","email":"dignifiedquire@gmail.com"},{"name":"Jacob Heun","email":"jacobheun@gmail.com"},{"name":"Alex Potsides","email":"alex@achingbrain.net"},{"name":"Maciej Krüger","email":"mkg20001@gmail.com"},{"name":"Projjol Banerji","email":"probaner23@gmail.com"},{"name":"Richard Littauer","email":"richard.littauer@gmail.com"},{"name":"Dmitriy Ryajov","email":"dryajov@gmail.com"},{"name":"Hugo Dias","email":"mail@hugodias.me"}],"gitHead":"994b0dd00ee6fb022e85f043857059be01e56487","_id":"it-length-prefixed@5.0.0","_nodeVersion":"15.12.0","_npmVersion":"7.6.3","dist":{"integrity":"sha512-N5aUi2Fa3whwVGfiOiw14lvx957hwP0eFx8ExJlcbE9evtZJllvyZZNg77UlI8KrwESPl37z88UDzEq9+sT/Ng==","shasum":"86d40671ff3325dd59459478aa8fdfececb878c7","tarball":"http://registry-npm.ywwl.com/it-length-prefixed/download/it-length-prefixed-5.0.0.tgz","fileCount":15,"unpackedSize":81415,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgbuL4CRA9TVsSAnZWagAAmooP/RW18HxKv0uhIPCVPtWx\nLZb44tNX6OvIdgwWa75ZvOuHgGoXe/xGMOIl9oM+yskYttIVYJrWQ2LGUGpF\naVvTy3pHCCZm5J32zz3h1r15UADsbFDL41v/SutPsb6BtJ/4Mcg6f3IDZU1a\nXe6c0JiLnDZEdegE4IV+4piOsCwZkYqprCL8VvrnSbcQ5huE+SPntz96acbm\nsoeTDN4FGFiZZOzsF5sbFAv6tknR4/+drsojTu/rcVpIBm8abBhsL/e+w7oc\nU4a7enzMdYMWNyqSY7iRo2+e9hxp/uoy/PfmSxnICuEvI+s5ee1kKblkwyNG\nV7B2turmG/o7N76maeUPpYAGkXmclqLq7zC32ob6mDJBIy5ZVYnvmxAB6lyW\nfNvNF3f7AFs10m2XFVXSDC9LNsLJuMzmZ8+omYlBwkBQWeLsW5fi5QkkrIL0\nnfZW9kD5p7UyzdLWz61Efvllnfwm7i/rRr8G/MHw1WaQCPR9K8D3EeDQWqvB\nyv8o/2P//6Xe9fYcxVlDk+l9kxnUES84OtOZ7kvTpCsg7BuSzwcnjzhSGStN\neASLqnJbMuptr0HBZ0Z/1dVysUCNt4nBJB9S4fmsb2M04EGtwAKpRR3B87Sz\nCSYZdw5mb2s4iXUA9cQq9DwO3nimXMvj4WWur1APeHxaaRJ5bZ99TMlS3SqB\ndAsE\r\n=jAws\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC6VYtxHKoD2jk7Gjc7wwAQFBmGlbp/GQ6YrjKAFwZWLgIhAK6PcBSEQ9nO68GCzgk5wctSSOR8wCEd2pZoBjIARYyE"}],"size":21052,"noattachment":false,"key":"/it-length-prefixed/-/it-length-prefixed-5.0.0.tgz"},"_npmUser":{"name":"alanshaw","email":"alan138@gmail.com"},"directories":{},"maintainers":[{"name":"alanshaw","email":"alan138@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/it-length-prefixed_5.0.0_1617879799697_0.8208522377398977"},"_hasShrinkwrap":false,"readme":"# it-length-prefixed <!-- omit in toc -->\n\n[![codecov](https://img.shields.io/codecov/c/github/alanshaw/it-length-prefixed.svg?style=flat-square)](https://codecov.io/gh/alanshaw/it-length-prefixed)\n[![CI](https://img.shields.io/github/actions/workflow/status/alanshaw/it-length-prefixed/js-test-and-release.yml?branch=master\\&style=flat-square)](https://github.com/alanshaw/it-length-prefixed/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)\n\n> Streaming length prefixed buffers with async iterables\n\n## Table of contents <!-- omit in toc -->\n\n- [Install](#install)\n  - [Browser `<script>` tag](#browser-script-tag)\n- [Usage](#usage)\n- [API](#api)\n  - [`encode(source, [opts])`](#encodesource-opts)\n  - [`encode.single(chunk, [opts])`](#encodesinglechunk-opts)\n  - [`decode(source, [opts])`](#decodesource-opts)\n  - [`decode.fromReader(reader, [opts])`](#decodefromreaderreader-opts)\n- [Contribute](#contribute)\n- [API Docs](#api-docs)\n- [License](#license)\n- [Contribution](#contribution)\n\n## Install\n\n```console\n$ npm i it-length-prefixed\n```\n\n### Browser `<script>` tag\n\nLoading this module through a script tag will make it's exports available as `ItLengthPrefixed` in the global namespace.\n\n```html\n<script src=\"https://unpkg.com/it-length-prefixed/dist/index.min.js\"></script>\n```\n\n## Usage\n\n```js\nimport { pipe } from 'it-pipe'\nimport * as lp from 'it-length-prefixed'\n\nconst encoded = []\n\n// encode\nawait pipe(\n  [uint8ArrayFromString('hello world')],\n  (source) => lp.encode(source),\n  async source => {\n    for await (const chunk of source) {\n      encoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array)\n    }\n  }\n)\n\nconsole.log(encoded)\n// => [Buffer <0b 68 65 6c 6c 6f 20 77 6f 72 6c 64>]\n\nconst decoded = []\n\n// decode\nawait pipe(\n  encoded, // e.g. from above\n  (source) => lp.decode(source),\n  async source => {\n    for await (const chunk of source) {\n      decoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array)\n    }\n  }\n)\n\nconsole.log(decoded)\n// => [Buffer <68 65 6c 6c 6f 20 77 6f 72 6c 64>]\n```\n\n## API\n\n```js\nimport {\n  encode, decode\n} from 'it-length-prefixed'\n\nimport {\n  encode\n} from 'it-length-prefixed/encode'\n\nimport {\n  decode,\n  MAX_LENGTH_LENGTH,\n  MAX_DATA_LENGTH\n} from 'it-length-prefixed/decode'\n```\n\n### `encode(source, [opts])`\n\n- `source: Iterable<Uint8ArrayList | Uint8Array> | AsyncIterable<Uint8ArrayList | Uint8Array>` source to read bytes from\n- `opts: Object`, optional\n  - `lengthEncoder: Function`: A function that encodes the length that will prefix each message. By default this is a [`varint`](https://www.npmjs.com/package/varint) encoder. It is passed a `value` to encode, an (optional) `target` buffer to write to and an (optional) `offset` to start writing from. The function should encode the `value` into the `target` (or alloc a new Buffer if not specified), set the `lengthEncoder.bytes` value (the number of bytes written) and return the `target`.\n\nReturns `Generator` or `AsyncGenerator` that yields [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist) objects. All messages will be prefixed with a length, determined by the `lengthEncoder` function.\n\n### `encode.single(chunk, [opts])`\n\n- `chunk: Buffer|Uint8ArrayList` chunk to encode\n- `opts: Object`, optional\n  - `lengthEncoder: Function`: See description above. Note that this encoder will *not* be passed a `target` or `offset` and so will need to allocate a buffer to write to.\n\nReturns a `Uint8ArrayList` containing the encoded chunk.\n\n### `decode(source, [opts])`\n\n- `source: Iterable<Uint8ArrayList | Uint8Array> | AsyncIterable<Uint8ArrayList | Uint8Array>` source to read bytes from\n- `opts: Object`, optional\n  - `maxLengthLength`: If provided, will not decode messages whose length section exceeds the size specified, if omitted will use the default of 147 bytes.\n  - `maxDataLength`: If provided, will not decode messages whose data section exceeds the size specified, if omitted will use the default of 4MB.\n  - `onLength(len: Number)`: Called for every length prefix that is decoded from the stream\n  - `onData(data: Uint8ArrayList)`: Called for every chunk of data that is decoded from the stream\n  - `lengthDecoder: Function`: A function that decodes the length that prefixes each message. By default this is a [`varint`](https://www.npmjs.com/package/varint) decoder. It is passed some `data` to decode which is a [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist). The function should decode the length, set the `lengthDecoder.bytes` value (the number of bytes read) and return the length. If the length cannot be decoded, the function should throw a `RangeError`.\n\nReturns `Generator` or `AsyncGenerator` that yields [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist) objects.\n\n### `decode.fromReader(reader, [opts])`\n\nBehaves like `decode` except it only reads the exact number of bytes needed for each message in `reader`.\n\n- `reader: Reader`: An [it-reader](https://github.com/alanshaw/it-reader)\n- `opts: Object`, optional\n  - `maxLengthLength`: If provided, will not decode messages whose length section exceeds the size specified, if omitted will use the default of 147 bytes.\n  - `maxDataLength`: If provided, will not decode messages whose data section exceeds the size specified, if omitted will use the default of 4MB.\n  - `onData(data: Uint8ArrayList)`: Called for every chunk of data that is decoded from the stream\n  - `lengthEncoder: Function`: See description above.\n\nReturns `Generator` or `AsyncGenerator` that yields [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist) objects.\n\n## Contribute\n\nPRs and issues gladly accepted! Check out the [issues](https://github.com/alanshaw/it-length-prefixed/issues).\n\n## API Docs\n\n- <https://alanshaw.github.io/it-length-prefixed>\n\n## License\n\nLicensed under either of\n\n- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)\n- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)\n\n## Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.\n","publish_time":1617879799857,"_cnpm_publish_time":1617879799857,"dist-tags":{"latest":"11.0.1"}}