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-join

v1.0.1

Published

stream-join is the micro-module to join values of multiple streams together.

Downloads

15

Readme

stream-join NPM version

stream-join is a function, which takes an array of object mode Readable streams and returns a combined object mode Readable stream, which pack together corresponding values from input streams, while properly handling backpressure.

Originally stream-join was used with stream-json to create and eventually join side-channels but can be used stand-alone.

stream-join is a lightweight, no-dependencies micro-package. It is distributed under New BSD license.

Intro

By default stream-join creates a stream of arrays of values. The first array contains the first values of all streams and the Nth array value comes from the Nth stream. Their respective order doesn't matter. The second array will contain the second values of all streams. And so on. If the corresponding stream has ended, null is going to be used as its value (object mode streams cannot use null values because it indicates the end-of-stream). The resulting stream will end when all streams have ended.

const join = require('stream-join');

const {PassThrough} = require('stream-join/tests/helpers');

const s1 = new PassThrough(), s2 = new PassThrough(),
  result = join([s1, s2]);

result.on('data', data => console.log(data));

// all streams are written asynchronously
s2.write('a');
s1.write(1);
s1.write(2);
s2.write('b');
s1.write(3);
s2.end();
s1.write(4);
s1.end();

// prints:
// [1, 'a']
// [2, 'b']
// [3, null] // s2 has ended
// [4, null]

The output can be controlled by a custom joining function. Given the setup above:

const s1 = new PassThrough(), s2 = new PassThrough(),
  result = join([s1, s2], {
    joinItems(output, items) {
      // a variable number of values is pushed out
      items.forEach(item => {
        // we should push only non-null values
        if (item !== null) output.push(item);
      });
    }
  });

result.on('data', data => console.log(data));

// all streams are written asynchronously
s2.write('a');
s1.write(1);
s1.write(2);
s2.write('b');
s1.write(3);
s2.end();
s1.write(4);
s1.end();

// now we normalized the order of values
// prints: 1, 'a', 2, 'b', 3, 4

Installation

npm i --save stream-join
# or: yarn add stream-join

Documentation

The module returns a function, whose prototype is:

const join = require('stream-join');

const result = join(streams[, options]);

Where:

  • streams is an array of object mode Readable streams.
  • options is an optional object detailed in the Node's documentation used to create result.
    • The following properties are always overridden:
      • objectMode is always true.
      • read() is replaced with an internal implementation.
    • The following custom properties are recognized:
      • skipEvents is an optional flag. If it is falsy (the default), 'error' events from all streams are forwarded to result. If it is truthy, no event forwarding is made. A user can always do so manually.
      • joinItems(output, items) is an optional function. It can be used to combine individual values together. It may push to the output 0 or more values. It returns no value and takes two arguments:
        • output is a result object described below. It can be used to push values with a method push().
          • Warning: never push out null values because they indicate that a stream has been finished and should be closed.
        • items is an array of values. It has the same length as streams and contains values from corresponding streams. If a corresponding stream has ended, null is going to be used as a value.
  • result is an object mode Readable stream, which produces combined values.

See the Introduction above for examples of how to use stream-join.

Release History

  • 1.0.1 technical release, no need to upgrade.
  • 1.0.0 the initial release.