npm

Need private packages and team management tools?Check out npm Orgs. »

it-length-prefixed

1.1.0 • Public • Published

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

install

npm i it-length-prefixed

Downloadsweekly downloads

2

version

1.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability