Skip to content

Instantly share code, notes, and snippets.

@donabrams
Last active August 29, 2015 14:24
Show Gist options
  • Save donabrams/883e1e6e097ffe1edc9e to your computer and use it in GitHub Desktop.
Save donabrams/883e1e6e097ffe1edc9e to your computer and use it in GitHub Desktop.
Node Streams (through and though2)
var through2 = require('through2');
var through = require('through');
var fs = require('fs');
//This is what most people use
fs.createReadStream('testStreams.js')
.pipe(through2(function(chunk, enc, cb) {
console.log('t2', 's1');
cb(null, chunk);
}, function(cb) {
console.log('t2', 'e1');
cb();
}))
.pipe(through2(function(chunk, enc, cb) {
console.log('t2', 's2');
//Since I don't call back with the chunk, no data is streamed to the next one!
//s3 is never fired!
cb();
}, function(cb) {
console.log('t2', 'e2');
//Since I don't cb, e3 is never fired!
//cb();
}))
.pipe(through2(function(chunk, enc, cb) {
console.log('t2', 's3');
cb();
}, function(cb) {
console.log('t2', 'e3');
cb();
}));
//This is what mold-source-map uses
fs.createReadStream('testStreams.js')
.pipe(through(function(data) {
console.log('t1', 's1');
//s2 only fires if data is queued here
this.queue(data);
}, function() {
console.log('t1', 'e1');
//e2 only fires if end is emitted here
this.emit('end');
}))
.pipe(through(function(data) {
console.log('t1', 's2');
//s3 is not fired because data is NOT queued here
}, function() {
console.log('t1', 'e2');
//e3 is not fired next end is NOT emitted here
fs.writeFile('test.test', 'test', 'utf8', function(err) {
console.log(err);
//However, this will pass data and trigger s3
this.queue('blue');
//and this should trigger e3
this.queue(null);
}.bind(this));
}))
.pipe(through(function(data) {
console.log('t1', 's3');
}, function() {
console.log('t1', 'e3');
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment