Skip to content

Instantly share code, notes, and snippets.

@json2d
Last active September 13, 2017 05:46
Show Gist options
  • Save json2d/05cef71538f8c752590fc14b96eabc0f to your computer and use it in GitHub Desktop.
Save json2d/05cef71538f8c752590fc14b96eabc0f to your computer and use it in GitHub Desktop.
//normally to pipe a -> b -> c:
a.pipe(b).pipe(c)
//could also compose pipings, this below is the same as above, (or is it?):
a.pipe(b.pipe(c))
//makes more sense when using a variable to define a composed piping instance
const bc = b.pipe(c)
a.pipe(bc)
d.pipe(bc)
const through = require('through');
const a = through(function write(chunk) {this.push({data:`you said ${chunk}`})})
const b = through(function write(chunk) {chunk.data+='...at ';this.push(chunk)})
const c = through(function write(chunk) {chunk.data+=(new Date)+'\n';this.push(chunk)})
const d = through(function write(chunk) {this.push(chunk.data)})
const astream = process.stdin.pipe(a)
//splitting the transform and output to different streams
astream.pipe(b).pipe(c)
astream.pipe(d).pipe(process.stdout)
// $ echo hi | node streamy.js
// => you said hi
// => ...at Sun Jul 09 2017 00:47:34 GMT-0400 (EDT)
//order matters here
astream.pipe(d).pipe(process.stdout)
astream.pipe(b).pipe(c)
// $ echo hi | node streamy.js
// => hi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment