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

clusterstream

v0.1.4

Published

Piping streams across a cluster of Workers

Downloads

52

Readme

Clusterstream is a duplex stream that distributes any incoming data to a pool of workers and pipes down the output of the workers. Clusterstream uses the stdin/stdout to manage backpressure: when a worker buffer is full the worker is no longer considered available until it emits a drain event. When no workers are available the Clusterstream will pause until any worker becomes available again.

Clusterstream([options],fn)

Each worker is initialized with a supplied function and executes it on the incoming stream. The first argument of the function will be the incoming stream (sharing all data passed to that worker) and the function itself has to return a stream as well. The returned stream is then piped back to the parent.

The function argument can either be a pure javascript function or a full path to a module containing a function. The function will be executed in the context of the child process, so it will not have access to any variables on the parent scope. So if you need any required packages you will have to explicitly require them inside the function, even if they are available in parent. The function will receive a stream as the first argument and will have to return a stream (that is piped back to the parent).

Options are optional (can be omitted completely):

  • children - defines the number of workers to be created
  • global - an object of variables that should be put into the global scope for the function
  • argv - a standard variable of arguments that will be made available to the function
  • require - an array of strings representing modules that should be required (no paths allowed)

By default, Clusterstream creates as many workers as there are cpus. If options is a number (not an object), then that number will determine the number of workers created.

Here is a quick and dirty example where each worker waits 500ms before returning each object:

const Stream = require('stream');
const Clusterstream = require('../index');

const feeder = Stream.PassThrough({objectMode:true});
[...Array(100)].forEach( (d,i) => feeder.write({data:i}));
feeder.end();

feeder.pipe(Clusterstream(incoming => {
  const Stream = require('stream');
  return incoming.pipe(Stream.Transform({
    objectMode: true,
    transform: (d,e,cb) => setTimeout(() =>
      cb(null,{data:d.data,worker:incoming.workerId}),
      500
    )
  }));
}))
.on('error',console.log)
.pipe(Stream.Transform({
  objectMode: true,
  transform: (d,e,cb) => {
    console.log('received',d);
    cb();
  }
}));

Same example using etl instead of Stream.Transform:

const etl = require('etl');
const Clusterstream = require('../index');

etl.toStream([...Array(100)].map( (d,i) => ({data:i})))
.pipe(Clusterstream(incoming => incoming
  .pipe(require('etl').map(d =>
    require('bluebird').delay(500,{data: d.data,worker:incoming.workerId})
  ))
))
.pipe(etl.map(d => console.log('received',d)));

Here is an example of how global variables can be passed into 10 workers:

const etl = require('etl');
const Clusterstream = require('../index');

const multiplier = 2;
const options = {
  children: 10,
  global: {multiplier},
  require: ['etl','bluebird']
};

etl.toStream([...Array(100)].map( (d,i) => ({data:i})))
.pipe(Clusterstream(options, incoming => incoming
  .pipe(etl.map(d =>
    bluebird.delay(500,{data: d.data* multiplier,worker:incoming.workerId})
  ))
))
.pipe(etl.map(d => console.log('received',d)));

Clusterstream.map([options],fn)

Clusterstream.map is a simple wrapper where you supply a transform function that receives each packet as an argument and returns the transformed values (instead of stream input/output)