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 🙏

© 2025 – Pkg Stats / Ryan Hefner

banzai

v0.5.12

Published

Library for creating document processing pipelines

Readme

Intro

Banzai (short for BanzaiETL) is a document processing framework. It operates by defining pipelines. We have a PubSub model where publishers (e.g. our api) place things in a priority queue that then gets consumed by a pipeline worker. A pipeline worker subscribes to the work queue and when it finds a task that needs to be done starts working on it. A pipeline is basically a state machine that allows you to execute multiple operations on a single file. You can use it to enrich documents with stuff from the internet, convert images, replicate a database to another datastore, etc. In our case we use the pipelines to process emails, convert pdf attachments to images, resize images, process information we get from mechanical turk, etc.

Terminology

Pipeline

You can define one or more pipelines like this:

var banzai = require('banzai');
var pipeline = banzai.pipeline('order pipeline');

The pipeline object has a chainable configuration API which you can use to later configure this object.

To start with, a pipeline processes documents in a document store.

States and Transitions

Each document that enters a pipeline has a state at each moment in time.

You can trigger a handle when a document state changes. This handle is a function that receives the document and then transforms it.

You can define the pipeline states and transitions like this:

pipeline
    .on('initial', initialHandler, {
        next: 'order received email sent'
    })
    .on('order received email sent', orderEmailSentHandler, {
        priority: 2
      , condition: allItemsAvailable
      , next: 'items available'
    })
    .on('order received email sent', confirmationEmailSentHandler, {
        priority: 1
      , next: 'items not available'
    })
    .on('items not available', itemsNotAvailableHandler)
    .on('items available', itemsAvailableHandler, {
      next: 'order placed'
    })
    .on('order placed', orderPlacedHandler, {
      next: 'order placed email sent'
    });

State handlers

initlalHandler, orderEmailSentHandler, itemsNotAvailableHandler and all the others are functions that handle the state entry. They could look something like this;

var initialHandler = function(orderDoc, done) {
  email.sendOrderReceivedEmail(orderDoc, done);
};

var confirmationEmailSentHandler = function(orderDoc, done) {
  done();
};

var itemsNotAvailableHandler = function(orderDoc, done) {
  email.sendItemsNotAvailable(orderDoc, done);
};

It is necessary that the handlers call the done function once they are done. If an error occurs, done should be called with an error on the first argument. If not, the first argument should be null.

You can see that the confirmationEmailSentHandler handler does not pass the document to the callback function. If that happens, the previous version of the document is used.

Condition functions

allItemsAvailable is a condition function, which could look something like this;

var allItemsAvailable = function(orderDoc, done) {
  inventory.checkAvailabilityAndReserveItems(orderDoc.items, function(err, availability) {
    if (err) { return done(err); }
    done(null, availability.allAvailable);
  });
};

A condition function should either return a boolean or call the done function with (err, boolean). The first argument should be null if there is no error and the second argument should be a boolean, telling if the condition was met or not.

Error handling

TODO!!!!

Logging

TODO!!!!

Meta-data

TODO!!!!

Document Store

Each pipeline has a document store, which is where the docs that are processed are stored.

You can define the document store on a pipeline like this:

pipline.docStore(docStore);

A doc store is an object that has to have these 2 functions:

  • load(docId, callback) - callback is a function with the signature (err, doc);
  • save(doc, callback) - callback is a function with the signature (err, doc);

By convention, you should expect doc.id or doc._id if the document exists and update it.

There already exists a store for CouchDB named banzai-docstore-couchdb. You can install it like this:

$ cd <PROJECT ROOT DIR>
$ npm install banzai-docstore-couchdb

... and use it like this:

var banzaiDocstoreCouchdb = require('banzai-docstore-couchdb');
var docStore = banzaiDocstoreCouchdb({
    url: 'http://localhost:5984'
  , db: 'docs'
});
pipeline.docStore(docStore);

State Store

Each pipeline has a state store, which is where the document states are stored.

(If you don't define a state store, the document state will be stored inside a "state" property of your document).

A state store is an object that has to have these 2 functions:

  • load(stateDocId, callback) - callback is a function with the signature (err, doc);
  • save(stateDoc, callback) - callback is a function with the signature (err, doc);

By convention, you should expect doc.id or doc._id if the document exists and update it.

There already exists a state store for CouchDB named banzai-statestore-couchdb. You can install it like this:

$ cd <PROJECT ROOT DIR>
$ npm install banzai-statestore-couchdb

... and use it like this:

var banzaiStatestoreCouchdb = require('banzai-statestore-couchdb');
var stateStore = banzaiDocstoreCouchdb({
    url: 'http://localhost:5984'
  , db: 'states'
});
pipeline.stateStore(stateStore);

Work Queue

A work queue is a queue where pending transitions are put so that they are picked up by interested workers.

Each pipeline has a work queue, which is an object that has to have these functions:

  • push(state_name, jobData, callback)
    • state_name: a string to identify the state;
    • jobData: an object to be passed onto the worker
    • callback: a function with the signature (err).
  • pop(state_name, callback)
    • state_name
    • callback: a function with the signature (err, jobData).