From 25ec4c40875b1dbfc938113f690d0a3b68bb903d Mon Sep 17 00:00:00 2001 From: Calvin Metcalf Date: Mon, 8 Aug 2016 13:43:28 -0400 Subject: [PATCH] fix prepend --- build/files.js | 15 +++++++++++++++ lib/_stream_readable.js | 11 ++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/build/files.js b/build/files.js index 57438569e5..030e2bfd39 100644 --- a/build/files.js +++ b/build/files.js @@ -175,6 +175,20 @@ const headRegexp = /(^module.exports = \w+;?)/m /require\('internal\/streams\/BufferList'\)/, 'require(\'./internal/streams/BufferList\')' ] + , prependFix = [ + /var prependListener;\n(?:.|\n)+\[fn, emitter\._events\[event]];\n\s\s};\n}/m, + `function prependListener(emitter, event, fn) { + if (typeof emitter.prependListener === 'function') { + return emitter.prependListener(event, fn); + } else { + // This is a hack to make sure that our error handler is attached before any + // userland ones. NEVER DO THIS. This is here only because this code needs + // to continue to work with older versions of Node.js that do not include + // the prependListener() method. The goal is to eventually remove this hack. + if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]]; + } + }` + ] module.exports['_stream_duplex.js'] = [ requireReplacement @@ -220,6 +234,7 @@ module.exports['_stream_readable.js'] = [ , bufferShimFix , bufferStaticMethods , internalDirectory + , prependFix ] module.exports['_stream_transform.js'] = [ diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index ddc30bb9e1..208cc65f1c 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -56,19 +56,16 @@ var StringDecoder; util.inherits(Readable, Stream); -var prependListener; -if (typeof EE.prototype.prependListener === 'function') { - prependListener = function prependListener(emitter, event, fn) { +function prependListener(emitter, event, fn) { + if (typeof emitter.prependListener === 'function') { return emitter.prependListener(event, fn); - }; -} else { - prependListener = function prependListener(emitter, event, fn) { + } else { // This is a hack to make sure that our error handler is attached before any // userland ones. NEVER DO THIS. This is here only because this code needs // to continue to work with older versions of Node.js that do not include // the prependListener() method. The goal is to eventually remove this hack. if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]]; - }; + } } var Duplex;