{"name":"it-length-prefixed","version":"1.0.0","description":"Streaming length prefixed buffers with async iterables","main":"src/index.js","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":"^3.0.0","varint":"^5.0.0"},"devDependencies":{"aegir":"^18.2.1","chai":"^4.2.0","it-block":"^1.0.1","it-pipe":"^1.0.1","it-pushable":"^1.2.1","random-bytes":"^1.0.0","random-int":"^2.0.0","streaming-iterables":"^4.1.0"},"contributors":[{"name":"Alan Shaw","email":"alan.shaw@protocol.ai"},{"name":"Dmitriy Ryajov","email":"dryajov@gmail.com"},{"name":"Jacob Heun","email":"jacobheun@gmail.com"},{"name":"Jacob Heun","email":"jake@andyet.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":"dignifiedquire","email":"dignifiedquire@users.noreply.github.com"},{"name":"dignifiedquire","email":"dignifiedquire@gmail.com"}],"gitHead":"572145ef20b4819bfec4f1d1db00acf47521efca","_id":"it-length-prefixed@1.0.0","_nodeVersion":"11.15.0","_npmVersion":"6.7.0","_npmUser":{"name":"alanshaw","email":"alan138@gmail.com"},"dist":{"integrity":"sha512-K+VD1dQtaeTqYs8kLoxJ4dyvykiEUgOtMXgvWbqLEgD8F7MqC4KsN2IeVg6nkje2nM/61blGJrMwmgKz0OGiKQ==","shasum":"8a783914d8971e5cac61425317935925845c87a6","tarball":"http://registry-npm.ywwl.com/it-length-prefixed/download/it-length-prefixed-1.0.0.tgz","fileCount":13,"unpackedSize":1066013,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJc0gNFCRA9TVsSAnZWagAAKG8P/j0IiscH/cpiC9nBduaj\nu0TwebVrOakSyUaaJO/MDsuEh1wE401/duMiwz0FPJrEva9MdwfJxVtQSsCq\nsZiVhpAaHyOSyJ+hFRiMCrEwrw5LVzATGiQdgNeI0hWgKignVPn4rz056coC\naEp3ZK+w2pIuF8bZwL2p3yaT9MhUlkVT/FiEU+Y8U3Aqk+t57bhpOWibT3XW\nLBifjhwQci9+huBYG7WN+MvRHcncI9y2z0BCZQVGMPlXjaMnO+OkmJuQoIUp\nt2QKrSuMxrzabKh0IRQ8H4aUqZQBK1Y2B7lspsjTur8KsPBOBdqZWxrIvED9\nva4LYdeead+Ax5jiycFTrV4gqI2VvqnMT2o+sS0hz8HtStbBtaA0UZOtYnvx\nNImiajeFc6YQGaD5rg7o9CcrKy5Ao5mOdtJsg58QYr4qAzGj5DEQB63Ri8dq\nGa2v086JkHyaDjuNX/4JOV7+2QfVAqe3CIc6EmgUVNLSj0bFoZSDcz3DSDGz\nNOlTCMV1sPnteIwO1hmtA0wLxNWBj88HgIJ2nj3DvMgn6ujn3g9SnTfY6TGr\ndYpAy6cKTkvRTfC7awA/SaVOpYglvaWD9yEZ39iPC/3Blf9cldtlRoDZmc6D\ntSgesRtFFzjnmIOE6L+fX4axAQImLoSY+y0rWzNBTlCuKUV8am7K09GxieXz\n+Zpb\r\n=MObo\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDAtR+cIlLTxrFd0Q4SC49mCy1NES+l9osoEZVaio/XrgIhAJwdA+al7pTTDKBQZWEjqugExWXhzjeL7lYr25MrjX0v"}],"size":271414,"noattachment":false,"key":"/it-length-prefixed/-/it-length-prefixed-1.0.0.tgz"},"maintainers":[{"name":"alanshaw","email":"alan138@gmail.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/it-length-prefixed_1.0.0_1557267268244_0.18238386625778746"},"_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":1557267268378,"_cnpm_publish_time":1557267268378,"dist-tags":{"latest":"11.0.1"}}