Skip to content

Commit

Permalink
stream: move legacy to sep file
Browse files Browse the repository at this point in the history
  • Loading branch information
yorkie committed Jan 6, 2017
1 parent f43ea2a commit 625bf71
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 92 deletions.
91 changes: 91 additions & 0 deletions lib/_stream_legacy.js
Original file line number Diff line number Diff line change
@@ -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;
};
101 changes: 9 additions & 92 deletions lib/stream.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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;
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 625bf71

Please sign in to comment.