Run a nested collection of streams in pipe or merge configurations
Combine merge and pipe streams into stream architectures that can bend packets to your will.
A merge stream will duplicate the input across all members and merge the output from all members (based on combine-stream.
A pipe stream will pass the output from each member as the input to the next (based on stream-combiner.
Both types accepts streams in their list - you can combine merge and pipes with each other - it's stream inception!
$ npm install streamworks
There are 2 main methods:
Create each type of stream by passing an array of either:
- functions - these are turned into streams using through2
- streams - these are used as is
var from = require'from';var streamworks = require'streamworks';// a merge is a stream that splits the input across each function and merges the output back into one streamvar mergestream = streamworksmerge// order lotsthispushchunk + ':10'callback// order somethispushchunk + ':2'callback// a pipe is a stream that passes values through each functionvar pipestream = streamworkspipe// filter anything that does not start with pifchunktoStringindexOf'p'!=0thispushchunk;callback;// uppercase to inputthispushchunktoStringtoUpperCase// run some data through the merge stream (which will duplicate it) and then through the pipe stream (which will filter it)from'apple' 'pie' 'custard'pipemergestreampipepipestreampipeprocessstdout
Because streamworks streams are, umm, streams - you can create complex nested stream-structures:
var bigAssStream = streamworkspipeifchunk!='world'thispushchunk;callback;streamworksmergethispush'A1:' + chunk;callback;thispush'A2:' + chunk;callback;thispush'A3:' + chunk;callback;streamworkspipeifchunktoStringindexOf'A2'!=0thispushchunk;callback;streamworksmergethispush'sub1:' + chunk;callback;thispush'sub2:' + chunk;callback;ifchunktoStringindexOf'sub2:A1:'!=0thispushchunk;callbackvar arr = ;from'hello''world''apple'pipebigAssStreamon'data'arrpushchunktoStringon'end'consoledirarr;/*sub1:A1:hellosub1:A3:hellosub2:A3:hellosub1:A1:applesub1:A3:applesub2:A3:apple*/
If you pass true or:
as the first argument to pipe or merge - the stream will be in object mode.
this means that the 'chunks' will be what you sent and not buffers/strings.
You can also use:
Which are shortcuts for:
- pipe(true, )
- merge(true, )
create a new readable/writable stream that will pipe each value through the array of streams/functions
shorthand for pipe(true, )
create a new readable/writable stream that will duplicate each value into each of the streams/functions and merge the results back into the output
shorthand for merge(true, )