var readStream = fs.createReadStream(__filename, {highWaterMark: 5}),
limitStream = new stream.Transform(),
limit = 5
// Just an example of a write stream that only wants a certain amount of data
limitStream._transform = function(chunk, encoding, cb) {
if (--limit >= 0)
return cb(null, chunk + '\n')
// All we want is 5 chunks, then we want to close
this.end()
// Wait a while and check if the file stream has closed
setTimeout(checkFd, 1000)
cb()
}
// fd is set to null when the file is closed
function checkFd() {
console.log(readStream.fd != null ? 'Bugger. File still open!' : 'All good')
}
readStream.on('unpipe', function() { console.log('unpipe emitted from readStream') })
readStream.on('end', function() { console.log('end emitted from readStream') })
readStream.on('close', function() { console.log('close emitted from readStream') })
limitStream.on('unpipe', function() { console.log('unpipe emitted from limitStream') })
limitStream.on('end', function() { console.log('end emitted from limitStream') })
limitStream.on('close', function() { console.log('close emitted from limitStream') })
readStream.pipe(limitStream).pipe(process.stdout)