npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stream-accumulator

v0.0.2

Published

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

Downloads

135

Readme

🥒 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.