-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stream: move legacy to lib/internal dir #8197
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'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; | ||
|
||
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; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
'use strict'; | ||
|
||
module.exports = Stream; | ||
|
||
const EE = require('events'); | ||
const util = require('util'); | ||
|
||
util.inherits(Stream, EE); | ||
function Stream() { | ||
EE.call(this); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant previously is why can't we just move this into the new internal module and export it into this file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it :) |
||
|
||
// wrap the old-style stream | ||
require('internal/streams/legacy')(Stream); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not clear to me. Basically the definition of How about we move all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You couldn't just move those files because that would break someone trying to Also, I think the point of this PR was to separate the actual old There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but then why split the definition of it in one file, and the legacy part in another? I'm 👎 in having both |
||
|
||
// 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'); | ||
Stream.Duplex = require('_stream_duplex'); | ||
|
@@ -14,94 +21,3 @@ 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; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused.