JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 55131
  • Score
    100M100P100Q158505F
  • License MIT

Streaming length prefixed buffers with async iterables

Package Exports

  • it-length-prefixed

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (it-length-prefixed) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

it-length-prefixed

Travis (.org) Codecov Dependency Status js-standard-style

Streaming length prefixed buffers with async iterators

Install

npm install it-length-prefixed

Usage

const pipe = require('it-pipe')
const lp = require('it-length-prefixed')

const encoded = []

// encode
await pipe(
  [Buffer.from('hello world')],
  lp.encode(),
  async source => {
    for await (const chunk of source) {
      encoded.push(chunk.slice()) // (.slice converts BufferList to Buffer)
    }
  }
)

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
  lp.decode(),
  async source => {
    for await (const chunk of source) {
      decoded.push(chunk.slice()) // (.slice converts BufferList to Buffer)
    }
  }
)

console.log(decoded)
// => [Buffer <68 65 6c 6c 6f 20 77 6f 72 6c 64>]

API

encode([opts])

  • opts: Object, optional
    • poolSize: 10 * 1024: Buffer pool size to allocate up front

All messages will be prefixed with a varint.

Returns a transform that yields BufferList objects.

encode.single(chunk)

  • chunk: Buffer|BufferList chunk to encode

Returns a BufferList containing the encoded chunk.

decode([opts])

  • opts: Object, optional
    • maxDataLength: If provided, will not decode messages longer than the size specified, if omitted will use the current default of 4MB.
    • onLength(len: Number): Called for every length prefix that is decoded from the stream
    • onData(data: BufferList): Called for every chunk of data that is decoded from the stream

All messages will be prefixed with a varint.

Returns a transform that yields BufferList objects.

Contribute

PRs and issues gladly accepted! Check out the issues.

License

MIT © 2016 Friedel Ziegelmayer