GithubHelp home page GithubHelp logo

isabella232 / stream-splicer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from browserify/stream-splicer

0.0 0.0 0.0 41 KB

streaming pipeline with a mutable configuration

License: MIT License

JavaScript 100.00%

stream-splicer's Introduction

stream-splicer

streaming pipeline with a mutable configuration

This module is similar to stream-combiner, but with a pipeline configuration that can be changed at runtime.

build status

example

This example begins with an HTTP header parser that waits for an empty line to signify the end of the header. At that point, it switches to a streaming json parser to operate on the HTTP body.

var splicer = require('stream-splicer');
var through = require('through2');
var jsonStream = require('jsonstream2');
var split = require('split2');

var headerData = {};
var headers = through.obj(function (buf, enc, next) {
    var line = buf.toString('utf8');
    if (line === '') {
        this.push(headerData);
        pipeline.splice(1, 1, jsonStream.parse([ 'rows', true ]));
    }
    else {
        var m = /^(\S+):(.+)/.exec(line);
        var key = m && m[1].trim();
        var value = m && m[2].trim();
        if (m) headerData[key] = value;
    }
    next();
});
var pipeline = splicer([ split(), headers, jsonStream.stringify() ]);
process.stdin.pipe(pipeline).pipe(process.stdout);

intput:

GET / HTTP/1.1
Host: substack.net
User-Agent: echo

{"rows":["beep","boop"]}

output:

$ echo -ne 'GET / HTTP/1.1\nHost: substack.net\nUser-Agent: echo\n\n{"rows":["beep","boop"]}\n' | node example/header.js
[
{"Host":"substack.net","User-Agent":"echo"}
,
"beep"
,
"boop"
]

methods

var splicer = require('stream-splicer')

var pipeline = splicer(streams, opts)

Create a pipeline duplex stream given an array of streams. Each stream will be piped to the next. Writes to pipeline get written to the first stream and data for reads from pipeline come from the last stream.

For example, for streams [ a, b, c, d ], this pipeline is constructed internally:

a.pipe(b).pipe(c).pipe(d)

Input will get written into a. Output will be read from d.

If any of the elements in streams are arrays, they will be converted into nested pipelines. This is useful if you want to expose a hookable pipeline with grouped insertion points.

var pipeline = splicer.obj(streams, opts)

Create a pipeline with opts.objectMode set to true for convenience.

var removed = pipeline.splice(index, howMany, stream, ...)

Splice the pipeline starting at index, removing howMany streams and replacing them with each additional stream argument provided.

The streams that were removed from the splice and returned.

pipeline.push(stream, ...)

Push one or more streams to the end of the pipeline.

var stream = pipeline.pop()

Pop a stream from the end of the pipeline.

pipeline.unshift(stream, ...)

Unshift one or more streams to the begining of the pipeline.

var stream = pipeline.shift()

Shift a stream from the begining of the pipeline.

var stream = pipeline.get(index, ...)

Return the stream at index index, .... Indexes can be negative.

Multiple indexes will traverse into nested pipelines.

attributes

pipeline.length

The number of streams in the pipeline

install

With npm do:

npm install stream-splicer

license

MIT

stream-splicer's People

Contributors

goto-bus-stop avatar jeromew avatar mafintosh avatar zertosh avatar zoubin avatar zxqx 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.