Giter Club home page Giter Club logo

streaming-middleware's Introduction

streaming-middleware

build status Coverage Status npm license

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

Installation

npm install streaming-middleware

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("streaming-middleware");
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

streaming-middleware's People

Contributors

yoosername avatar

Stargazers

Paul47 avatar

Watchers

James Cloos avatar  avatar Paul47 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.