stream.pipe should remove listeners on dest close

This commit is contained in:
Ryan Dahl 2010-11-20 23:08:45 -08:00
parent 2320497992
commit c321e9893d

View File

@ -10,13 +10,17 @@ exports.Stream = Stream;
Stream.prototype.pipe = function (dest, options) { Stream.prototype.pipe = function (dest, options) {
var source = this; var source = this;
source.on("data", function (chunk) { function ondata (chunk) {
if (false === dest.write(chunk)) source.pause(); if (false === dest.write(chunk)) source.pause();
}); }
dest.on("drain", function () { source.on("data", ondata);
function ondrain () {
if (source.readable) source.resume(); if (source.readable) source.resume();
}); }
dest.on("drain", ondrain);
/* /*
* If the 'end' option is not supplied, dest.end() will be called when * If the 'end' option is not supplied, dest.end() will be called when
@ -24,11 +28,20 @@ Stream.prototype.pipe = function (dest, options) {
*/ */
if (!options || options.end !== false) { if (!options || options.end !== false) {
source.on("end", function () { function onend () {
dest.end(); dest.end();
}); }
source.on("end", onend);
} }
dest.on('close', function () {
dest.removeListener('data', ondata);
dest.removeListener('drain', ondrain);
dest.removeListener('end', onend);
});
/* /*
* Questionable: * Questionable:
*/ */