Skip to content

Instantly share code, notes, and snippets.

@polotek
Created May 27, 2010 15:54
Show Gist options
  • Save polotek/415972 to your computer and use it in GitHub Desktop.
Save polotek/415972 to your computer and use it in GitHub Desktop.
exports.pump = function (readStream, writeStream, processChunk, callback) {
if(!callback) {
callback = processChunk;
processChunk = undefined;
}
readStream.addListener("data", function (chunk) {
var newChunk = processChunk ? processChunk(chunk) : chunk;
if (newChunk != null && writeStream.write(newChunk) === false) {
var l = function () {
writeStream.removeListener("drain", l);
readStream.resume();
}
writeStream.addListener("drain", l);
readStream.pause();
}
});
readStream.addListener("end", function () {
writeStream.end();
});
if (callback) writeStream.addListener("close", callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment