From 625bf712607083ecaf61e9ab7663edc370f575df Mon Sep 17 00:00:00 2001 From: yorkie Date: Tue, 9 Aug 2016 23:34:23 +0800 Subject: [PATCH 1/5] stream: move legacy to sep file --- lib/_stream_legacy.js | 91 +++++++++++++++++++++++++++++++++++++ lib/stream.js | 101 ++++-------------------------------------- node.gyp | 1 + 3 files changed, 101 insertions(+), 92 deletions(-) create mode 100644 lib/_stream_legacy.js diff --git a/lib/_stream_legacy.js b/lib/_stream_legacy.js new file mode 100644 index 00000000000000..bd1af6d0cf2d9d --- /dev/null +++ b/lib/_stream_legacy.js @@ -0,0 +1,91 @@ +'use strict'; + +module.exports = function(Stream) { + // old-style streams. Note that the pipe method (the only relevant + // part of this class) is overridden in the Readable class. + + Stream.prototype.pipe = function(dest, options) { + var source = this; + + function ondata(chunk) { + if (dest.writable) { + if (false === dest.write(chunk) && source.pause) { + source.pause(); + } + } + } + + source.on('data', ondata); + + function ondrain() { + if (source.readable && source.resume) { + source.resume(); + } + } + + dest.on('drain', ondrain); + + // If the 'end' option is not supplied, dest.end() will be called when + // source gets the 'end' or 'close' events. Only dest.end() once. + if (!dest._isStdio && (!options || options.end !== false)) { + source.on('end', onend); + source.on('close', onclose); + } + + var didOnEnd = false; + function onend() { + if (didOnEnd) return; + didOnEnd = true; + + dest.end(); + } + + + function onclose() { + if (didOnEnd) return; + didOnEnd = true; + + if (typeof dest.destroy === 'function') dest.destroy(); + } + + // don't leave dangling pipes when there are errors. + function onerror(er) { + cleanup(); + if (EE.listenerCount(this, 'error') === 0) { + throw er; // Unhandled stream error in pipe. + } + } + + source.on('error', onerror); + dest.on('error', onerror); + + // remove all the event listeners that were added. + function cleanup() { + source.removeListener('data', ondata); + dest.removeListener('drain', ondrain); + + source.removeListener('end', onend); + source.removeListener('close', onclose); + + source.removeListener('error', onerror); + dest.removeListener('error', onerror); + + source.removeListener('end', cleanup); + source.removeListener('close', cleanup); + + dest.removeListener('close', cleanup); + } + + source.on('end', cleanup); + source.on('close', cleanup); + + dest.on('close', cleanup); + + dest.emit('pipe', source); + + // Allow for unix-like usage: A.pipe(B).pipe(C) + return dest; + }; + + return Stream; +}; diff --git a/lib/stream.js b/lib/stream.js index 047fbceb4bdb30..0860cf7ac7d6c7 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -1,11 +1,16 @@ 'use strict'; -module.exports = Stream; - const EE = require('events'); const util = require('util'); +function Stream() { + EE.call(this); +} util.inherits(Stream, EE); + +// wrap the old-style stream +require('_stream_legacy')(Stream); + Stream.Readable = require('_stream_readable'); Stream.Writable = require('_stream_writable'); Stream.Duplex = require('_stream_duplex'); @@ -15,93 +20,5 @@ Stream.PassThrough = require('_stream_passthrough'); // Backwards-compat with node 0.4.x Stream.Stream = Stream; - -// old-style streams. Note that the pipe method (the only relevant -// part of this class) is overridden in the Readable class. - -function Stream() { - EE.call(this); -} - -Stream.prototype.pipe = function(dest, options) { - var source = this; - - function ondata(chunk) { - if (dest.writable) { - if (false === dest.write(chunk) && source.pause) { - source.pause(); - } - } - } - - source.on('data', ondata); - - function ondrain() { - if (source.readable && source.resume) { - source.resume(); - } - } - - dest.on('drain', ondrain); - - // If the 'end' option is not supplied, dest.end() will be called when - // source gets the 'end' or 'close' events. Only dest.end() once. - if (!dest._isStdio && (!options || options.end !== false)) { - source.on('end', onend); - source.on('close', onclose); - } - - var didOnEnd = false; - function onend() { - if (didOnEnd) return; - didOnEnd = true; - - dest.end(); - } - - - function onclose() { - if (didOnEnd) return; - didOnEnd = true; - - if (typeof dest.destroy === 'function') dest.destroy(); - } - - // don't leave dangling pipes when there are errors. - function onerror(er) { - cleanup(); - if (EE.listenerCount(this, 'error') === 0) { - throw er; // Unhandled stream error in pipe. - } - } - - source.on('error', onerror); - dest.on('error', onerror); - - // remove all the event listeners that were added. - function cleanup() { - source.removeListener('data', ondata); - dest.removeListener('drain', ondrain); - - source.removeListener('end', onend); - source.removeListener('close', onclose); - - source.removeListener('error', onerror); - dest.removeListener('error', onerror); - - source.removeListener('end', cleanup); - source.removeListener('close', cleanup); - - dest.removeListener('close', cleanup); - } - - source.on('end', cleanup); - source.on('close', cleanup); - - dest.on('close', cleanup); - - dest.emit('pipe', source); - - // Allow for unix-like usage: A.pipe(B).pipe(C) - return dest; -}; +// export +module.exports = Stream; diff --git a/node.gyp b/node.gyp index abba4c3c767b0e..fa402a177cc345 100644 --- a/node.gyp +++ b/node.gyp @@ -61,6 +61,7 @@ 'lib/_stream_transform.js', 'lib/_stream_passthrough.js', 'lib/_stream_wrap.js', + 'lib/_stream_legacy.js', 'lib/string_decoder.js', 'lib/sys.js', 'lib/timers.js', From 876c3adafa1965dd2781334b1060ca6bb28191bd Mon Sep 17 00:00:00 2001 From: yorkie Date: Sat, 7 Jan 2017 15:06:21 +0800 Subject: [PATCH 2/5] stream: move legacy to lib/internal dir --- lib/{_stream_legacy.js => internal/streams/legacy.js} | 3 ++- lib/stream.js | 9 +++++---- node.gyp | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) rename lib/{_stream_legacy.js => internal/streams/legacy.js} (98%) diff --git a/lib/_stream_legacy.js b/lib/internal/streams/legacy.js similarity index 98% rename from lib/_stream_legacy.js rename to lib/internal/streams/legacy.js index bd1af6d0cf2d9d..c8769173b7f964 100644 --- a/lib/_stream_legacy.js +++ b/lib/internal/streams/legacy.js @@ -1,5 +1,7 @@ 'use strict'; +const EE = require('events'); + module.exports = function(Stream) { // old-style streams. Note that the pipe method (the only relevant // part of this class) is overridden in the Readable class. @@ -80,7 +82,6 @@ module.exports = function(Stream) { source.on('close', cleanup); dest.on('close', cleanup); - dest.emit('pipe', source); // Allow for unix-like usage: A.pipe(B).pipe(C) diff --git a/lib/stream.js b/lib/stream.js index 0860cf7ac7d6c7..b6e0cc5e22d93d 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -9,7 +9,11 @@ function Stream() { util.inherits(Stream, EE); // wrap the old-style stream -require('_stream_legacy')(Stream); +require('internal/streams/legacy')(Stream); + +// Note: export Stream before Readable/Writable/Duplex/... +// to avoid a cross-reference(require) issues +module.exports = Stream; Stream.Readable = require('_stream_readable'); Stream.Writable = require('_stream_writable'); @@ -19,6 +23,3 @@ Stream.PassThrough = require('_stream_passthrough'); // Backwards-compat with node 0.4.x Stream.Stream = Stream; - -// export -module.exports = Stream; diff --git a/node.gyp b/node.gyp index fa402a177cc345..8caa8db355f9bc 100644 --- a/node.gyp +++ b/node.gyp @@ -61,7 +61,6 @@ 'lib/_stream_transform.js', 'lib/_stream_passthrough.js', 'lib/_stream_wrap.js', - 'lib/_stream_legacy.js', 'lib/string_decoder.js', 'lib/sys.js', 'lib/timers.js', @@ -97,6 +96,7 @@ 'lib/internal/v8_prof_processor.js', 'lib/internal/streams/lazy_transform.js', 'lib/internal/streams/BufferList.js', + 'lib/internal/streams/legacy.js', 'deps/v8/tools/splaytree.js', 'deps/v8/tools/codemap.js', 'deps/v8/tools/consarray.js', From 1005ccb1497197fc07362ceb5eb4e55a8a587279 Mon Sep 17 00:00:00 2001 From: yorkie Date: Tue, 17 Jan 2017 16:06:29 +0800 Subject: [PATCH 3/5] fix nit --- lib/internal/streams/legacy.js | 2 ++ lib/stream.js | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/streams/legacy.js b/lib/internal/streams/legacy.js index c8769173b7f964..a1adee2b4fa593 100644 --- a/lib/internal/streams/legacy.js +++ b/lib/internal/streams/legacy.js @@ -1,11 +1,13 @@ 'use strict'; +const util = require('util'); const EE = require('events'); module.exports = function(Stream) { // old-style streams. Note that the pipe method (the only relevant // part of this class) is overridden in the Readable class. + util.inherits(Stream, EE); Stream.prototype.pipe = function(dest, options) { var source = this; diff --git a/lib/stream.js b/lib/stream.js index b6e0cc5e22d93d..2ed46558a0d070 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -1,12 +1,10 @@ 'use strict'; -const EE = require('events'); const util = require('util'); function Stream() { EE.call(this); } -util.inherits(Stream, EE); // wrap the old-style stream require('internal/streams/legacy')(Stream); From ece3060015c6c3803f246f09ee5d1a55809ab89d Mon Sep 17 00:00:00 2001 From: yorkie Date: Tue, 17 Jan 2017 16:39:15 +0800 Subject: [PATCH 4/5] fix nits --- lib/internal/streams/legacy.js | 2 -- lib/internal/streams/stream.js | 11 +++++++++++ lib/stream.js | 6 +----- 3 files changed, 12 insertions(+), 7 deletions(-) create mode 100644 lib/internal/streams/stream.js diff --git a/lib/internal/streams/legacy.js b/lib/internal/streams/legacy.js index a1adee2b4fa593..c8769173b7f964 100644 --- a/lib/internal/streams/legacy.js +++ b/lib/internal/streams/legacy.js @@ -1,13 +1,11 @@ 'use strict'; -const util = require('util'); const EE = require('events'); module.exports = function(Stream) { // old-style streams. Note that the pipe method (the only relevant // part of this class) is overridden in the Readable class. - util.inherits(Stream, EE); Stream.prototype.pipe = function(dest, options) { var source = this; diff --git a/lib/internal/streams/stream.js b/lib/internal/streams/stream.js new file mode 100644 index 00000000000000..45bc0e91d4c2d7 --- /dev/null +++ b/lib/internal/streams/stream.js @@ -0,0 +1,11 @@ +'use strict'; + +const EE = require('events'); +const util = require('util'); + +function Stream() { + EE.call(this); +} +util.inherits(Stream, EE); + +exports.Stream = Stream; diff --git a/lib/stream.js b/lib/stream.js index 2ed46558a0d070..c5cb0cbc9f0784 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -1,10 +1,6 @@ 'use strict'; -const util = require('util'); - -function Stream() { - EE.call(this); -} +var Stream = require('internal/streams/stream').Stream; // wrap the old-style stream require('internal/streams/legacy')(Stream); From 4e14f31e7b3f04db9e65187caca6f3e6a8b52624 Mon Sep 17 00:00:00 2001 From: yorkie Date: Tue, 17 Jan 2017 23:19:54 +0800 Subject: [PATCH 5/5] fix nit --- lib/internal/streams/legacy.js | 125 +++++++++++++++++---------------- lib/internal/streams/stream.js | 11 --- lib/stream.js | 7 +- 3 files changed, 64 insertions(+), 79 deletions(-) delete mode 100644 lib/internal/streams/stream.js diff --git a/lib/internal/streams/legacy.js b/lib/internal/streams/legacy.js index c8769173b7f964..3242b15eabdb0d 100644 --- a/lib/internal/streams/legacy.js +++ b/lib/internal/streams/legacy.js @@ -1,92 +1,93 @@ 'use strict'; const EE = require('events'); +const util = require('util'); -module.exports = function(Stream) { - // old-style streams. Note that the pipe method (the only relevant - // part of this class) is overridden in the Readable class. +function Stream() { + EE.call(this); +} +util.inherits(Stream, EE); - Stream.prototype.pipe = function(dest, options) { - var source = this; +Stream.prototype.pipe = function(dest, options) { + var source = this; - function ondata(chunk) { - if (dest.writable) { - if (false === dest.write(chunk) && source.pause) { - source.pause(); - } + function ondata(chunk) { + if (dest.writable) { + if (false === dest.write(chunk) && source.pause) { + source.pause(); } } + } - source.on('data', ondata); + source.on('data', ondata); - function ondrain() { - if (source.readable && source.resume) { - source.resume(); - } + function ondrain() { + if (source.readable && source.resume) { + source.resume(); } + } - dest.on('drain', ondrain); + dest.on('drain', ondrain); - // If the 'end' option is not supplied, dest.end() will be called when - // source gets the 'end' or 'close' events. Only dest.end() once. - if (!dest._isStdio && (!options || options.end !== false)) { - source.on('end', onend); - source.on('close', onclose); - } + // If the 'end' option is not supplied, dest.end() will be called when + // source gets the 'end' or 'close' events. Only dest.end() once. + if (!dest._isStdio && (!options || options.end !== false)) { + source.on('end', onend); + source.on('close', onclose); + } - var didOnEnd = false; - function onend() { - if (didOnEnd) return; - didOnEnd = true; + var didOnEnd = false; + function onend() { + if (didOnEnd) return; + didOnEnd = true; - dest.end(); - } + dest.end(); + } - function onclose() { - if (didOnEnd) return; - didOnEnd = true; + function onclose() { + if (didOnEnd) return; + didOnEnd = true; - if (typeof dest.destroy === 'function') dest.destroy(); - } + if (typeof dest.destroy === 'function') dest.destroy(); + } - // don't leave dangling pipes when there are errors. - function onerror(er) { - cleanup(); - if (EE.listenerCount(this, 'error') === 0) { - throw er; // Unhandled stream error in pipe. - } + // don't leave dangling pipes when there are errors. + function onerror(er) { + cleanup(); + if (EE.listenerCount(this, 'error') === 0) { + throw er; // Unhandled stream error in pipe. } + } - source.on('error', onerror); - dest.on('error', onerror); - - // remove all the event listeners that were added. - function cleanup() { - source.removeListener('data', ondata); - dest.removeListener('drain', ondrain); + source.on('error', onerror); + dest.on('error', onerror); - source.removeListener('end', onend); - source.removeListener('close', onclose); + // remove all the event listeners that were added. + function cleanup() { + source.removeListener('data', ondata); + dest.removeListener('drain', ondrain); - source.removeListener('error', onerror); - dest.removeListener('error', onerror); + source.removeListener('end', onend); + source.removeListener('close', onclose); - source.removeListener('end', cleanup); - source.removeListener('close', cleanup); + source.removeListener('error', onerror); + dest.removeListener('error', onerror); - dest.removeListener('close', cleanup); - } + source.removeListener('end', cleanup); + source.removeListener('close', cleanup); - source.on('end', cleanup); - source.on('close', cleanup); + dest.removeListener('close', cleanup); + } - dest.on('close', cleanup); - dest.emit('pipe', source); + source.on('end', cleanup); + source.on('close', cleanup); - // Allow for unix-like usage: A.pipe(B).pipe(C) - return dest; - }; + dest.on('close', cleanup); + dest.emit('pipe', source); - return Stream; + // Allow for unix-like usage: A.pipe(B).pipe(C) + return dest; }; + +module.exports = Stream; diff --git a/lib/internal/streams/stream.js b/lib/internal/streams/stream.js deleted file mode 100644 index 45bc0e91d4c2d7..00000000000000 --- a/lib/internal/streams/stream.js +++ /dev/null @@ -1,11 +0,0 @@ -'use strict'; - -const EE = require('events'); -const util = require('util'); - -function Stream() { - EE.call(this); -} -util.inherits(Stream, EE); - -exports.Stream = Stream; diff --git a/lib/stream.js b/lib/stream.js index c5cb0cbc9f0784..a6b25a212508ab 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -1,13 +1,8 @@ 'use strict'; -var Stream = require('internal/streams/stream').Stream; - -// wrap the old-style stream -require('internal/streams/legacy')(Stream); - // Note: export Stream before Readable/Writable/Duplex/... // to avoid a cross-reference(require) issues -module.exports = Stream; +const Stream = module.exports = require('internal/streams/legacy'); Stream.Readable = require('_stream_readable'); Stream.Writable = require('_stream_writable');