1.0.0 • Published 4 years ago

readable-stream-buffer-stream v1.0.0

Weekly downloads
2,021
License
ISC
Repository
github
Last release
4 years ago

readable-stream-buffer-stream

Build status Coverage Status Dependencies Status

An async iterator that emits buffers containing bytes up to a certain length

Install

$ npm install --save readable-stream-buffer-stream

Usage

const totalLength = //... a big number

// all options are optional, defaults are shown
const options = {
  chunkSize: 4096, // how many bytes will be in each buffer
  generator: (size, callback) => {
    // call the passed callback with a Buffer object `size` bytes long.
    //
    // if omitted, `Promise.resolve(crypto.randomBytes(size))` will be used
  }
}

let buffers = []

const stream = bufferStream(totalLength, options)
stream.on('data', (buf) => {
  buffers.push(buf)
})
stream.on('end', (buf) => {
  if (buf) {
    buffers.push(buf)
  }

  // `buffers` is an array of Buffers the combined length of which === totalLength
})