Skip to content
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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions lib/internal/streams/legacy.js
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;
};
106 changes: 11 additions & 95 deletions lib/stream.js
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused.


util.inherits(Stream, EE);
function Stream() {
EE.call(this);
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it :)


// wrap the old-style stream
require('internal/streams/legacy')(Stream);
Copy link
Member

@mcollina mcollina Jan 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not clear to me. Basically the definition of Stream is divided into two files, whicn is not easy to understand in my opinion. If we also move the definition of Stream, I think internal/streams/stream should also include the 'legacy' bits.

How about we move all _stream prefixed files into internal/streams? That might be a better solution overall.
cc @nodejs/streams.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 require() them? Unless you mean leave behind similarly-named files that simple re-export?

Also, I think the point of this PR was to separate the actual old Stream implementation from the other things, like the re-exporting of the various stream types.

Copy link
Member

Choose a reason for hiding this comment

The 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?
Either we just move the legacy bits, we move the full definition, or we move all the streams parts.

I'm 👎 in having both internal/streams/stream and internal/streams/legacy, if we are not moving along readable, writable, etc.


// 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');
Expand All @@ -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;
};
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -96,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',
Expand Down