Skip to content

Instantly share code, notes, and snippets.

@calvinmetcalf
Forked from jcoglan/lazy_stream.js
Last active August 29, 2015 14:04
Show Gist options
  • Save calvinmetcalf/7ec2b9a45cd4db8a8a08 to your computer and use it in GitHub Desktop.
Save calvinmetcalf/7ec2b9a45cd4db8a8a08 to your computer and use it in GitHub Desktop.
// In this example, I want to create a stream that pipes a file through a transform,
// *without* beginning to read data from the file. I want the whole pipeline to be
// lazy until I call read() on the end of the pipeline.
//
// Instead, the console.log() call fires unexpectedly with every line of the file.
var fs = require('fs'),
split = require('split'),
stream = require('stream');
var dest = new stream.Transform({highWaterMark: 1});
dest._transform = function(chunk, encoding, callback) {
console.log(chunk);
callback(null, chunk);//<- need to emit something
};
var result = fs.createReadStream('./foo.txt').pipe(split()).pipe(dest);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment