Skip to content

Instantly share code, notes, and snippets.

@finnp
Last active August 29, 2015 14:23
Show Gist options
  • Save finnp/1f9d7c3d24d3b2d836d7 to your computer and use it in GitHub Desktop.
Save finnp/1f9d7c3d24d3b2d836d7 to your computer and use it in GitHub Desktop.
// convention for streams with ports
// object with two properties 'in' and 'out'
// 'in' is an object with keys as port names and values which are Writable streams
// 'out' is an object with keys as port and values which are Readable streams
// if the length of in- our outports is 1, 'in' and 'out' can be Readable or Writable streams directly
// if both the length of the in- and outports is 1, it's a Duplex stream
// through-like helper module
var multi = require('stream-with-ports')
var example = multi(['stdin', 'errors'], ['stdout', 'stderr'], function(inport, chunk, enc, cb) {
if(inport === 'stdin' && !chunk.error) this.stdout.push(chunk)
else this.stderr.push(chunk)
})
a.pipe(example.in.stdin)
opts.pipe(example.in.errors)
example.out.stderr.pipe(errorlog)
example.out.stdout.pipe(process.stdout)
// only multiple outports
var multi = require('multi-through')
var split = multi(['true', 'false'], function(chunk, enc, cb) {
if(chunk.length > 10) this.true.push(chunk)
else this.false.push(chunk)
cb()
})
objectStream.pipe(stream.in)
stream.out.true.pipe(longObjects)
stream.out.false.pipe(shortObjects)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment