Skip to content

🏁 Streaming length prefixed buffers with pull-streams

License

Notifications You must be signed in to change notification settings

jayd2446/it-length-prefixed

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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.

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

About

🏁 Streaming length prefixed buffers with pull-streams

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%