Skip to content

Latest commit

 

History

History
41 lines (31 loc) · 1.37 KB

README.md

File metadata and controls

41 lines (31 loc) · 1.37 KB

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
})