{"name":"it-length-prefixed","version":"8.0.3","description":"Streaming length prefixed buffers with async iterables","author":{"name":"Alan Shaw"},"license":"Apache-2.0 OR MIT","homepage":"https://github.com/alanshaw/it-length-prefixed#readme","repository":{"type":"git","url":"git+https://github.com/alanshaw/it-length-prefixed.git"},"bugs":{"url":"https://github.com/alanshaw/it-length-prefixed/issues"},"keywords":["async","iterable","iterator","length-prefixed","length-prefixed-stream","varint"],"engines":{"node":">=16.0.0","npm":">=7.0.0"},"type":"module","types":"./dist/src/index.d.ts","typesVersions":{"*":{"*":["*","dist/*","dist/src/*","dist/src/*/index"],"src/*":["*","dist/*","dist/src/*","dist/src/*/index"]}},"exports":{".":{"types":"./dist/src/index.d.ts","import":"./dist/src/index.js"},"./decode":{"types":"./dist/src/decode.d.ts","import":"./dist/src/decode.js"},"./encode":{"types":"./dist/src/encode.d.ts","import":"./dist/src/encode.js"}},"eslintConfig":{"extends":"ipfs","parserOptions":{"sourceType":"module"},"ignorePatterns":["benchmarks"]},"release":{"branches":["master"],"plugins":[["@semantic-release/commit-analyzer",{"preset":"conventionalcommits","releaseRules":[{"breaking":true,"release":"major"},{"revert":true,"release":"patch"},{"type":"feat","release":"minor"},{"type":"fix","release":"patch"},{"type":"docs","release":"patch"},{"type":"test","release":"patch"},{"type":"deps","release":"patch"},{"scope":"no-release","release":false}]}],["@semantic-release/release-notes-generator",{"preset":"conventionalcommits","presetConfig":{"types":[{"type":"feat","section":"Features"},{"type":"fix","section":"Bug Fixes"},{"type":"chore","section":"Trivial Changes"},{"type":"docs","section":"Documentation"},{"type":"deps","section":"Dependencies"},{"type":"test","section":"Tests"}]}}],"@semantic-release/changelog","@semantic-release/npm","@semantic-release/github","@semantic-release/git"]},"scripts":{"clean":"aegir clean","lint":"aegir lint","dep-check":"aegir dep-check","build":"aegir build","test":"aegir test","test:chrome":"aegir test -t browser --cov","test:chrome-webworker":"aegir test -t webworker","test:firefox":"aegir test -t browser -- --browser firefox","test:firefox-webworker":"aegir test -t webworker -- --browser firefox","test:node":"aegir test -t node --cov","test:electron-main":"aegir test -t electron-main","release":"aegir release"},"dependencies":{"err-code":"^3.0.1","it-stream-types":"^1.0.4","uint8-varint":"^1.0.1","uint8arraylist":"^2.0.0","uint8arrays":"^4.0.2"},"devDependencies":{"@types/varint":"^6.0.0","aegir":"^37.4.5","iso-random-stream":"^2.0.0","it-all":"^1.0.6","it-block":"^6.0.0","it-drain":"^1.0.5","it-foreach":"^0.1.1","it-map":"^1.0.6","it-pipe":"^2.0.2","it-pushable":"^3.0.0","it-reader":"^6.0.1","p-defer":"^4.0.0","random-int":"^3.0.0","varint":"^6.0.0"},"gitHead":"2d03408749543b74ada1d0f7642abe891dd7051c","_id":"it-length-prefixed@8.0.3","_nodeVersion":"16.17.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-qAMDAZHd9zuDH8UDjG9ISyF/L1BJfUmdqgOAo/15a7LE1EsZgDntV5/2ARlqaGSa1fzeEL0qdg6JDaUk0L//LA==","shasum":"a7774e3308892a1715a0cf6249465e9aecfe3a08","tarball":"http://registry-npm.ywwl.com/it-length-prefixed/download/it-length-prefixed-8.0.3.tgz","fileCount":23,"unpackedSize":32636,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHtGUU34v1WtMTcVvn09Xk3m7xCj1tCw2z/Nhu1eJmZAAiEA2EvuAJFKOj9lInwCqrgdAxFCqF12xpEvr3BqdI9PcQs="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjRuyfACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmoFrhAAhvMwXB7FB0i9rbcWr/k6d5lmPxXw/O+EHM3ChyQZTBNJEGpl\r\nUjgXEaP/XFSu1GMpoKMnD8vme/V4IuLytKNXF/KmMp9tSZLkIKh3DWgYTHS7\r\nDDzheiU3Uc6+JEK2Z8FhYNTSAQkXhB49QVnfHJcag0DRSYL0ibsJ99NEIcDb\r\nRJay6pKG7DcVSMrnPOCKmpREuHaCCK1Cp+OumN+PbBsZ0tiMGfqeXoU1eF7Y\r\n08/43PUQGm0IL7aSYH4Ye8WTVdtYNXm6mlxXELPBP9dxybcVHHFfH20yjwlF\r\nJ58cqVttFBK3DTwu/n/XvBA4cPkX5Z5pqPH4hgkiVxeBXiPxgtaSg5f20lk2\r\nZA3dozBEfrUI6t1tPVTpIHJstTNx5zLIrWfO+4+dpfnVBSuaxVUC4+Mq0DzV\r\n9h+8iT1qwRQPzV5muCM4DAxjIb+/lzW2TmcD37b9BkuIYZZc/znXGrw6jf1i\r\nLzOip5r9hqXCgGQ/N8CRNSn6BZHve5tA5XDMcfXgk6KQVdT3E1Ljcizh5OtV\r\n+dQQ3wIUIBsaLnjbgk3t6HTbjaOJgEKOM61m4J/MvJrOxNJV1/qDm2rsl06+\r\nsR8zSYGrXao/87FtUW/b36nZnoeCI5BUKIGi+9lSC1VPeh3EwG6XGKsT4Jwj\r\nBlrzDAlHVPUwFP0tJpt+6vLNwNAmNL3qZ4Q=\r\n=i8JM\r\n-----END PGP SIGNATURE-----\r\n","size":8807,"noattachment":false,"key":"/it-length-prefixed/-/it-length-prefixed-8.0.3.tgz"},"_npmUser":{"name":"alanshaw","email":"alan138@gmail.com"},"directories":{},"maintainers":[{"name":"achingbrain","email":"alex@achingbrain.net"},{"name":"alanshaw","email":"alan138@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/it-length-prefixed_8.0.3_1665592479586_0.09267564594161626"},"_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":1665592479794,"_cnpm_publish_time":1665592479794,"dist-tags":{"latest":"11.0.1"}}