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

v0.2.0

Published

Make transform stream chain easier

Downloads

10

Readme

stream-train

NPM version Downloads Build Status

Make stream pipeline a little easier to use(objectMode only).

Advantages

Forward errors along the pipeline

Consider the following code snippet:

stream1
  .pipe(stream2)
  .pipe(stream3)
  .on('error', e => console.error(e));

This is a normal use case of stream pipelines. You would only catch the error emitted by stream3, because errors do not forward in node official stream implementation. This package makes a little effort and forward all the errors for you.

new Train()
  .push(stream1)
  .push(stream2)
  .on('error', e => {
    // all errors emitted by stream1, stream2, stream3 would come here
    console.error(e);
  })
  .run();

Won't break pipeline when error occurs

When the target stream encounts an error, the source stream will invoke .unpipe() method that will disconnect itself from the target stream, then the whole pipeline breaks. But in real world, we use streams with { objectMode: true } to process object that is completely isolated from each other. If one fails, we just do some logs and expect the pipeline to go on. This package does the trick for you.

new Train()
  .push(through2.obj(function(chunk, enc, cb) {
    this.emit('error', new Error('oops!'));
    this.push('haha');
    cb();
  }))
  .push(through2.obj(function(chunk, enc, cb) {
    console.log(chunk); // haha
    cb();
  }))
  .on('error', e => {
    console.error(e.message);  // oops!
  })
  .run();

Installation

$ npm i stream-train --save

API

constructor(options)

  • options.seed: Original object you want to pass down to the stream pipeline, optional.
const Train = require('stream-train');
const file = { path: '/Users/foo/a.js' };
new Train({ seed: file })
  .push(through2.obj(function(file, enc, cb) {
    console.log(file.path);  // /Users/foo/a.js
  }))
  .push(stream2)
  .run();

Notice: Seed is optional, think of what you want to do. If you use a stream that can read by itself(such as vfs.src), then there is no need to set seed.

.push(stream)

Append a stream instance to internal list, it's chainable.

.unshift(stream)

Prepend a stream instance to internal list, it's chainable.

.delete(stream)

Delete a stream instance from internal list, it's chainable.

.if(condition)

Sometimes we may add additional steps according to some condition, this api allows you to add stream conditionally.

new Train()
  .push(stream1)
  .if(condition)
    .push(stream2)
  .endif()
  .push(stream3)
  .run();

.endif()

Invoked when condition block need to close. It has to be paired with .if().

.run(callback)

Start the pipeline, accept optional argument callback which is invoked when pipeline finish. Return a Promise.

Events

error

Emitted when any stream in the pipeline emit error event.

info

Emitted when any stream in the pipeline emit info event.

finish

Emitted when pipeline finish piping.

LICENSE

MIT