Coder Social home page Coder Social logo

roccomuso / batched-stream Goto Github PK

View Code? Open in Web Editor NEW
9.0 4.0 0.0 9 KB

Transform stream which batches a bunch of input data into groups of specified size (supporting objectMode).

JavaScript 100.00%
batch stream nodejs transform objectmode byte chunks

batched-stream's Introduction

Batched Stream

Build Status JavaScript Style Guide NPM Version node Dependency Status

Transform stream supporting objectMode, which batches a bunch of input data into groups of specified size and will emit arrays, so that you can deal with pieces of input asynchronously.

Install

npm install --save batched-stream

Usage

const BatchStream = require('batched-stream')
const batch = new BatchStream({
  size : 5, // Bytes or N. of objects (when objectMode is true)
  objectMode: true, // false by default
  strictMode: false // return the rest of the batch when a stream contains a number of items that is not a strict multiply of the batch size
})

stream
  .pipe(batch)
  .pipe(new ArrayStream()) // In objectMode: true, deals with array input from pipe.

You can .pipe other streams to it or .write them yourself (if you .write don't forget to .end). The options are passed down to the transform stream (you can increase the highWaterMark for example).

  • If objectMode is enabled the emitted batches are Array of length equal to size. Otherwise Buffer with byte size-length are emitted.

  • The strictMode: false allows to get the rest of the batch when a stream contains a number of items that is not a strict multiply of the batch size.

Test

npm test

Author

Rocco Musolino (@roccomuso)

License

MIT

batched-stream's People

Contributors

roccomuso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

batched-stream's Issues

Suggestion: provide a maxWait param for cases where flush isn't enough

So, I was using this code (and thank you for making it) to convert a read Kafka stream into batches (to reduce concurrent load on the downstream destination) -

The issue is that it seems like some ReadableStreams don't actually ever invoke flush, making the strictMode irrelevant
as a replacement, i added a "maxWait" parameter that records the timestamp of the last update, and upon receiving a put into the BatchStream, checks whether the required batch size has been reached OR the maxWait has elapsed since the last put.

This way, there is an occasional internal "flushing" of the stream without waiting for an externally invoked flush...

wdyt?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.