From c321e9893d9eabefb125d9e4efa964603fed9dfe Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Sat, 20 Nov 2010 23:08:45 -0800 Subject: [PATCH] stream.pipe should remove listeners on dest close --- lib/stream.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/stream.js b/lib/stream.js index 97f4fa60222..dfe5380adfb 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -10,13 +10,17 @@ exports.Stream = Stream; Stream.prototype.pipe = function (dest, options) { var source = this; - source.on("data", function (chunk) { + function ondata (chunk) { if (false === dest.write(chunk)) source.pause(); - }); + } - dest.on("drain", function () { + source.on("data", ondata); + + function ondrain () { if (source.readable) source.resume(); - }); + } + + dest.on("drain", ondrain); /* * 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) { - source.on("end", function () { + function onend () { dest.end(); - }); + } + + source.on("end", onend); } + dest.on('close', function () { + dest.removeListener('data', ondata); + dest.removeListener('drain', ondrain); + dest.removeListener('end', onend); + }); + + /* * Questionable: */