0.0.1 • Published 6 years ago

streaming-middleware v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

streaming-middleware

Build Status Coverage Status

Compose a chain of Transform Streams with Express-like middleware.

Installation

npm install

Usage

An instance of StreamingMiddleware is a type of Duplex Stream which internally writes data to and reads data from a chain of Transform streams that are piped through dynamically in the order you add them.

var StreamingMiddleware = require("../StreamingMiddleware.js");
var app = StreamingMiddleware();

app
  .use(function Uppercase(chunk, enc, next){
    next(null, chunk.toString().toUpperCase() );
  })
  .use(function Reverse(chunk, enc, next){
    next(null, chunk.toString().split("").reverse().join("").trim("") + "\n" );
  })

process.stdin.pipe(app.stream()).pipe(process.stdout);
echo "pickle rick" | node app.js      // # KCIR ELKCIP

Test

npm test

# or
npm install -g mocha
mocha test/**/*spec.js --reporter spec
# or
mocha test/**/*spec.js --reporter=nyan

Test Documentation

The test documentation was generated with Mocha’s “doc” reporter, and directly reflects the test suite.

Coverage

npm run coverage

API

StreamingMiddleware

Transform stream builder

use

Add a Transform Function to the stack

Parameters

  • fn Function An instanceof {Transform} or a plain Function with 3 args

Examples

Simple use

 var app = StreamingMiddleware();
 app.use(function Uppercase(chunk, enc, next){
   next(null, chunk.toString().toUpperCase() );
 })
Chaining

 var app = StreamingMiddleware();
 app
   .use(function Uppercase(chunk, enc, next){
     next(null, chunk.toString().toUpperCase() );
   })
   .use(function Reverse(chunk, enc, next){
     next(null, chunk.toString().split("").reverse().join("").trim("") + "\n" );
   })

Returns StreamingMiddleware

stream

Return a Duplex stream built by connecting all the Transform streams which were added via StreamingMiddleware.use()

Parameters

  • options Object options passed to each Transform e.g. "objectMode"

Examples

Stream of 2 Transforms in non object mode

 var app = StreamingMiddleware();
 app
   .use(function Uppercase(chunk, enc, next){
     next(null, chunk.toString().toUpperCase() );
   })
   .use(function Uppercase(chunk, enc, next){
     next(null, chunk.toString().split("").reverse().join("").trim("") + "\n" );
   })
 process.stdin.pipe(app.stream()).pipe(process.stdout);

Returns DuplexStream

More Examples

See more examples in the Example folder

Why

  • Experiment creating an easily extendable CLI for processing streamed data
  • Experiment using a test driven approach
  • Experiment using various Tools e.g. travis-ci, jshint, instanbul, autodoc etc