Created
September 12, 2013 03:22
-
-
Save calvinfo/6532738 to your computer and use it in GitHub Desktop.
Streams continue running
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will output 'ended' multiple times, as the readstream will read to completion