Last Updated: February 25, 2016
·
885
· logicalparadox

No-inheritance Transform Streams for Node.js

TLDR;

Intro

Streams are a very important part of a node.js developer's daily regiment. Transform streams can be the most powerful for one-off uses but, unfortunately, require inheritance of require('stream').Transform.

transmute makes this much easier by providing a transform stream factory. It supports full options merging for both sides of the stream as well as flush support if needed.

Simple Example: toUpperCase

A simple transmute stream is just the transform function:

var transmute = require('transmute');

var toUpper = transmute(function(chunk, enc, cb) {
  cb(null, chunk.toString().toUpperCase());
});

input.pipe(toUpper).pipe(output);

Full Example: Simple Logger

A more feature-complete example is a simple logger that takes written objects, filters out the types of log message we don't want, then pipes to display (process.stdout):

/*!
 * Require transmute
 */

var transmute = require('transmute');

/*!
 * Base logger
 */

var log = transmute({
    options: { objectMode: true, highWaterMark: 1 }
  , transform: function(obj, enc, cb) {
      obj.timestamp = new Date();
      cb(null, obj);
    }
  , flush: function(cb) {
      var ended = {};
      ended.timestamp = new Date();
      ended.type = 'info';
      ended.msg = 'Log ended';
      this.push(ended);
      cb();
    }
});

/*!
 * Debug filter
 */

var nodebug = transmute({
    options: { objectMode: true, highWaterMark: 1 }
  , transform: function(obj, enc, cb) {
      if (obj.type === 'debug') return cb();
      cb(null, obj);
    }
});

/*!
 * Display stream
 */

var display = transmute({
    writable: { objectMode: true, highWaterMark: 1 }
  , transform: function(obj, enc, cb) {
      cb(null, obj.timestamp.toUTCString() + ' [' + obj.type + '] ' + obj.msg + '\n');
    }
});

/*!
 * Plumbing
 */

log.pipe(nodebug).pipe(display).pipe(process.stdout);

/*!
 * Log stuff
 */

log.write({ type: 'info', msg: 'hello universe' });
log.write({ type: 'debug', msg: 'debug the universe' });
log.write({ type: 'info', msg: 'did you see the debug, thought not.' });
log.end();

Stream all the things!