Skip to content

Commit 625bf71

Browse files
committed
stream: move legacy to sep file
1 parent f43ea2a commit 625bf71

File tree

3 files changed

+101
-92
lines changed

3 files changed

+101
-92
lines changed

lib/_stream_legacy.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
2+
3+
module.exports = function(Stream) {
4+
// old-style streams. Note that the pipe method (the only relevant
5+
// part of this class) is overridden in the Readable class.
6+
7+
Stream.prototype.pipe = function(dest, options) {
8+
var source = this;
9+
10+
function ondata(chunk) {
11+
if (dest.writable) {
12+
if (false === dest.write(chunk) && source.pause) {
13+
source.pause();
14+
}
15+
}
16+
}
17+
18+
source.on('data', ondata);
19+
20+
function ondrain() {
21+
if (source.readable && source.resume) {
22+
source.resume();
23+
}
24+
}
25+
26+
dest.on('drain', ondrain);
27+
28+
// If the 'end' option is not supplied, dest.end() will be called when
29+
// source gets the 'end' or 'close' events. Only dest.end() once.
30+
if (!dest._isStdio && (!options || options.end !== false)) {
31+
source.on('end', onend);
32+
source.on('close', onclose);
33+
}
34+
35+
var didOnEnd = false;
36+
function onend() {
37+
if (didOnEnd) return;
38+
didOnEnd = true;
39+
40+
dest.end();
41+
}
42+
43+
44+
function onclose() {
45+
if (didOnEnd) return;
46+
didOnEnd = true;
47+
48+
if (typeof dest.destroy === 'function') dest.destroy();
49+
}
50+
51+
// don't leave dangling pipes when there are errors.
52+
function onerror(er) {
53+
cleanup();
54+
if (EE.listenerCount(this, 'error') === 0) {
55+
throw er; // Unhandled stream error in pipe.
56+
}
57+
}
58+
59+
source.on('error', onerror);
60+
dest.on('error', onerror);
61+
62+
// remove all the event listeners that were added.
63+
function cleanup() {
64+
source.removeListener('data', ondata);
65+
dest.removeListener('drain', ondrain);
66+
67+
source.removeListener('end', onend);
68+
source.removeListener('close', onclose);
69+
70+
source.removeListener('error', onerror);
71+
dest.removeListener('error', onerror);
72+
73+
source.removeListener('end', cleanup);
74+
source.removeListener('close', cleanup);
75+
76+
dest.removeListener('close', cleanup);
77+
}
78+
79+
source.on('end', cleanup);
80+
source.on('close', cleanup);
81+
82+
dest.on('close', cleanup);
83+
84+
dest.emit('pipe', source);
85+
86+
// Allow for unix-like usage: A.pipe(B).pipe(C)
87+
return dest;
88+
};
89+
90+
return Stream;
91+
};

lib/stream.js

+9-92
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
'use strict';
22

3-
module.exports = Stream;
4-
53
const EE = require('events');
64
const util = require('util');
75

6+
function Stream() {
7+
EE.call(this);
8+
}
89
util.inherits(Stream, EE);
10+
11+
// wrap the old-style stream
12+
require('_stream_legacy')(Stream);
13+
914
Stream.Readable = require('_stream_readable');
1015
Stream.Writable = require('_stream_writable');
1116
Stream.Duplex = require('_stream_duplex');
@@ -15,93 +20,5 @@ Stream.PassThrough = require('_stream_passthrough');
1520
// Backwards-compat with node 0.4.x
1621
Stream.Stream = Stream;
1722

18-
19-
// old-style streams. Note that the pipe method (the only relevant
20-
// part of this class) is overridden in the Readable class.
21-
22-
function Stream() {
23-
EE.call(this);
24-
}
25-
26-
Stream.prototype.pipe = function(dest, options) {
27-
var source = this;
28-
29-
function ondata(chunk) {
30-
if (dest.writable) {
31-
if (false === dest.write(chunk) && source.pause) {
32-
source.pause();
33-
}
34-
}
35-
}
36-
37-
source.on('data', ondata);
38-
39-
function ondrain() {
40-
if (source.readable && source.resume) {
41-
source.resume();
42-
}
43-
}
44-
45-
dest.on('drain', ondrain);
46-
47-
// If the 'end' option is not supplied, dest.end() will be called when
48-
// source gets the 'end' or 'close' events. Only dest.end() once.
49-
if (!dest._isStdio && (!options || options.end !== false)) {
50-
source.on('end', onend);
51-
source.on('close', onclose);
52-
}
53-
54-
var didOnEnd = false;
55-
function onend() {
56-
if (didOnEnd) return;
57-
didOnEnd = true;
58-
59-
dest.end();
60-
}
61-
62-
63-
function onclose() {
64-
if (didOnEnd) return;
65-
didOnEnd = true;
66-
67-
if (typeof dest.destroy === 'function') dest.destroy();
68-
}
69-
70-
// don't leave dangling pipes when there are errors.
71-
function onerror(er) {
72-
cleanup();
73-
if (EE.listenerCount(this, 'error') === 0) {
74-
throw er; // Unhandled stream error in pipe.
75-
}
76-
}
77-
78-
source.on('error', onerror);
79-
dest.on('error', onerror);
80-
81-
// remove all the event listeners that were added.
82-
function cleanup() {
83-
source.removeListener('data', ondata);
84-
dest.removeListener('drain', ondrain);
85-
86-
source.removeListener('end', onend);
87-
source.removeListener('close', onclose);
88-
89-
source.removeListener('error', onerror);
90-
dest.removeListener('error', onerror);
91-
92-
source.removeListener('end', cleanup);
93-
source.removeListener('close', cleanup);
94-
95-
dest.removeListener('close', cleanup);
96-
}
97-
98-
source.on('end', cleanup);
99-
source.on('close', cleanup);
100-
101-
dest.on('close', cleanup);
102-
103-
dest.emit('pipe', source);
104-
105-
// Allow for unix-like usage: A.pipe(B).pipe(C)
106-
return dest;
107-
};
23+
// export
24+
module.exports = Stream;

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
'lib/_stream_transform.js',
6262
'lib/_stream_passthrough.js',
6363
'lib/_stream_wrap.js',
64+
'lib/_stream_legacy.js',
6465
'lib/string_decoder.js',
6566
'lib/sys.js',
6667
'lib/timers.js',

0 commit comments

Comments
 (0)