it-length-prefixed
Version:
Streaming length prefixed buffers with async iterables
80 lines (54 loc) • 2.35 kB
Markdown
//img.shields.io/codecov/c/github/alanshaw/it-length-prefixed.svg?style=flat-square)](https://codecov.io/gh/alanshaw/it-length-prefixed)
[](https://github.com/alanshaw/it-length-prefixed/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)
> Streaming length prefixed buffers with async iterables
<!--
!IMPORTANT!
Everything in this README between "# About" and "# Install" is automatically
generated and will be overwritten the next time the doc generator is run.
To make changes to this section, please update the @packageDocumentation section
of src/index.js or src/index.ts
To experiment with formatting, please run "npm run docs" from the root of this
repo and examine the changes made.
-->
Encode/decode streams of bytes with length-prefixes.
```js
import { pipe } from 'it-pipe'
import * as lp from 'it-length-prefixed'
const encoded = []
// encode
await pipe(
[ ],
(source) => lp.encode(source),
async source => {
for await (const chunk of source) {
encoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array)
}
}
)
console.log(encoded)
// => [Buffer <0b 68 65 6c 6c 6f 20 77 6f 72 6c 64>]
const decoded = []
// decode
await pipe(
encoded, // e.g. from above
(source) => lp.decode(source),
async source => {
for await (const chunk of source) {
decoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array)
}
}
)
console.log(decoded)
// => [Buffer <68 65 6c 6c 6f 20 77 6f 72 6c 64>]
```
- <https://alanshaw.github.io/it-length-prefixed>
Licensed under either of
- Apache 2.0, ([LICENSE-APACHE](https://github.com/alanshaw/it-length-prefixed/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](https://github.com/alanshaw/it-length-prefixed/LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
Unless 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.
[![codecov](https: