Skip to content

Instantly share code, notes, and snippets.

@calvinfo
Created September 12, 2013 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvinfo/6532738 to your computer and use it in GitHub Desktop.
Save calvinfo/6532738 to your computer and use it in GitHub Desktop.
Streams continue running
var fs = require('fs')
, stream = require('stream');
function Through () {
this.processed = 0;
}
Through.prototype = new stream.Transform();
function End () {}
End.prototype = new stream.Transform();
Through.prototype._transform = function (chunk, encoding, cb) {
this.processed += chunk.length;
console.log('Passing through', this.processed);
this.push(chunk);
cb();
};
End.prototype._transform = function (chunk, encoding, cb) {
console.log('Ended');
this.push(null);
cb();
};
fs.createReadStream('some-large-file.txt')
.pipe(new Through())
.pipe(new End());
@calvinfo
Copy link
Author

This will output 'ended' multiple times, as the readstream will read to completion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment