From f6680267714ca0b6882c1efb19183e44e50f5ec6 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 29 May 2014 07:43:29 -0400 Subject: [PATCH] fix(pipe) create: EventEmitter memory leak --- lib/server/pipe.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/server/pipe.js b/lib/server/pipe.js index 070c798e..0e7a4c06 100644 --- a/lib/server/pipe.js +++ b/lib/server/pipe.js @@ -47,29 +47,37 @@ if (p.gzip || p.gunzip) { if (p.gzip) - gzip = zlib.createGzip(); + gzip = zlib.createGzip(); else - gzip = zlib.createGunzip(); + gzip = zlib.createGunzip(); - read.on('error', func); - read = read.pipe(gzip); + on('error', read, func); + read = read.pipe(gzip); } - write.on('error', func); - read.on('error', func); + on('error', write, func); + on('error', read, func); Util.exec.if(!isFsWrite, function() { read.pipe(write, { end: !p.notEnd }); - read.on('end', func); + read.once('end', func); }, function(callback) { - write.on('open', callback); + on('open', write, callback); }); } } + function on(event, emitter, callback) { + var listeners = emitter.listeners(event), + length = listeners.length; + + if (length <= 1) + emitter.once(event, callback); + } + /** * get body of readStream * @@ -83,11 +91,11 @@ body += chunk; }); - readStream.on('error', function(error) { + readStream.once('error', function(error) { Util.exec.ret(callback, error); }); - readStream.on('end', function() { + readStream.once('end', function() { Util.exec(callback, null, body); }); }