Coder Social home page Coder Social logo

lmammino / stream-accumulator Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 1.0 113 KB

Accumulate all the data flowing through a stream and emit it as a single chunk or as a promise

License: MIT License

JavaScript 100.00%
streams stream nodejs node data-processing data-pipeline buffer accumulator library module

stream-accumulator's Introduction

πŸ₯’ stream-accumulator

npm version CircleCI codecov.io JavaScript Style Guide

Accumulate all the data flowing through a stream and emit it as a single chunk or as a promise.

⚠️ Warning: This module will buffer all the data from the source stream in memory, so watch out for infinite (or very large) streams. Using this library might not be ideal in such cases!

πŸ’½ Install & quick usage

Requirements: this library is written for Node.js >= 6.0

As usual, this happens through NPM:

npm install --save stream-accumulator

Then, in your code:

const StreamAccumulator = require('stream-accumulator')

// sourceStream is some readable stream

// ... inside an async function
const streamContent = await StreamAccumulator.promise(sourceStream)

// do stuff with streamContent (which would be a buffer)

πŸ€” Rationale

Oftentimes you are receiving data through a stream, but you have to accumulate all the data (waiting the stream to end) before you can use it. For instance, when you have to apply a transformation that requires the full data to be loaded in memory for the transformation to be possible.

This library allows you to accumulate the data of a stream into a single Buffer and allows you to consume the resulting Buffer through a stream or a promise based interface.

😲 An example

Let's say you are receiving some JavaScript source code through a streaming interface and you want to minify the code using UglifyJS. This is how you generally solve the problem:

const { createReadStream } = require('fs')
const { minify } = require('uglify-js')

let data = ''
createReadStream('source.js')
  .on('data', (d) => data += d.toString())
  .on('error', (e) => {
    console.error(e)
    process.exit(1)
  })
  .on('end', () => {
    const { code: minifiedCode } = minify(data)
    console.log('Minified code', minifiedCode)
  })

While this implementation is fine and it's not particularly complicated, it might not be the most composable one it's a bit verbose to read.

Using StreamAccumulator you can solve the same problem as follows:

const { createReadStream } = require('fs')
const StreamAccumulator = require('stream-accumulator')

const source = createReadStream('source.js')
source
  .pipe(new StreamAccumulator())
  .on('end', (data) => {
    const { code: minifiedCode } = minify(data)
    console.log('Minified code', minifiedCode)
  })

And, if you use the promise based interface you can even use async/await and have a more streamlined and easy to read implementation:

const { createReadStream } = require('fs')
const StreamAccumulator = require('stream-accumulator')

// ... inside an async function
const source = createReadStream('source.js')
const code = await StreamAccumulator.promise(source)
const { code: minifiedCode } = minify(data)
console.log('Minified code', minifiedCode)

πŸ•Ή Usage

This library offers 2 APIs, a stream based one (transform stream) and a promise based one.

🌊 Stream based API

The stream based API allows you to pipe a Readable stream into StreamAccumulator. StreamAccumulator will buffer the entire readable stream and will emit a single chunk when the source stream is ended.

Example:

const StreamAccumulator = require('stream-accumulator')

// initialize someReadableStream

someReadableStream
  .pipe(new StreamAccumulator())
  .on('end', (data) => {
    // data is a buffer
  })

🀞Promise based API

The promise based API allows you to wait for the source stream to finish (or emit an error.

Example:

const StreamAccumulator = require('stream-accumulator')

// initialize someSourceStream

// ... inside an async function
const data = await StreamAccumulator.promise(someSourceStream)
// data is a buffer

πŸ‘―β€ Contributing

Everyone is very welcome to contribute to this project. You can contribute just by submitting bugs or suggesting improvements by opening an issue on GitHub.

πŸ€¦β€ License

Licensed under MIT License. Β© Luciano Mammino.

stream-accumulator's People

Contributors

lmammino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.