Skip to content

Commit 1268b34

Browse files
backport iojs v1.0.0 streams changes
1 parent b1036fb commit 1268b34

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+316
-1018
lines changed

lib/_stream_duplex.js

+2-21
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1-
// Copyright Joyent, Inc. and other Node contributors.
2-
//
3-
// Permission is hereby granted, free of charge, to any person obtaining a
4-
// copy of this software and associated documentation files (the
5-
// "Software"), to deal in the Software without restriction, including
6-
// without limitation the rights to use, copy, modify, merge, publish,
7-
// distribute, sublicense, and/or sell copies of the Software, and to permit
8-
// persons to whom the Software is furnished to do so, subject to the
9-
// following conditions:
10-
//
11-
// The above copyright notice and this permission notice shall be included
12-
// in all copies or substantial portions of the Software.
13-
//
14-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15-
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17-
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18-
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19-
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20-
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21-
221
// a duplex stream is just a stream that is both readable and writable.
232
// Since JS doesn't have multiple prototypal inheritance, this class
243
// prototypally inherits from Readable, and then parasitically from
254
// Writable.
265

6+
'use strict';
7+
278
module.exports = Duplex;
289

2910
/*<replacement>*/

lib/_stream_readable.js

+32-48
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
1-
// Copyright Joyent, Inc. and other Node contributors.
2-
//
3-
// Permission is hereby granted, free of charge, to any person obtaining a
4-
// copy of this software and associated documentation files (the
5-
// "Software"), to deal in the Software without restriction, including
6-
// without limitation the rights to use, copy, modify, merge, publish,
7-
// distribute, sublicense, and/or sell copies of the Software, and to permit
8-
// persons to whom the Software is furnished to do so, subject to the
9-
// following conditions:
10-
//
11-
// The above copyright notice and this permission notice shall be included
12-
// in all copies or substantial portions of the Software.
13-
//
14-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15-
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17-
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18-
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19-
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20-
// USE OR OTHER DEALINGS IN THE SOFTWARE.
1+
'use strict';
212

223
module.exports = Readable;
234

@@ -41,16 +22,12 @@ if (!EE.listenerCount) EE.listenerCount = function(emitter, type) {
4122
/*</replacement>*/
4223

4324
var Stream = require('stream');
25+
var util = require('util');
26+
var Duplex;
4427

4528
/*<replacement>*/
4629
var util = require('core-util-is');
4730
util.inherits = require('inherits');
48-
/*</replacement>*/
49-
50-
var StringDecoder;
51-
52-
53-
/*<replacement>*/
5431
var debug = require('util');
5532
if (debug && debug.debuglog) {
5633
debug = debug.debuglog('stream');
@@ -59,18 +36,25 @@ if (debug && debug.debuglog) {
5936
}
6037
/*</replacement>*/
6138

39+
var StringDecoder;
6240

6341
util.inherits(Readable, Stream);
6442

6543
function ReadableState(options, stream) {
66-
var Duplex = require('./_stream_duplex');
67-
44+
Duplex = Duplex || require('./_stream_duplex');
6845
options = options || {};
6946

47+
// object stream flag. Used to make read(n) ignore n and to
48+
// make all the buffer merging and length checks go away
49+
this.objectMode = !!options.objectMode;
50+
51+
if (stream instanceof Duplex)
52+
this.objectMode = this.objectMode || !!options.readableObjectMode;
53+
7054
// the point at which it stops calling _read() to fill the buffer
7155
// Note: 0 is a valid value, means "don't call _read preemptively ever"
7256
var hwm = options.highWaterMark;
73-
var defaultHwm = options.objectMode ? 16 : 16 * 1024;
57+
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
7458
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
7559

7660
// cast to ints.
@@ -97,14 +81,6 @@ function ReadableState(options, stream) {
9781
this.emittedReadable = false;
9882
this.readableListening = false;
9983

100-
101-
// object stream flag. Used to make read(n) ignore n and to
102-
// make all the buffer merging and length checks go away
103-
this.objectMode = !!options.objectMode;
104-
105-
if (stream instanceof Duplex)
106-
this.objectMode = this.objectMode || !!options.readableObjectMode;
107-
10884
// Crypto is kind of old and crusty. Historically, its default string
10985
// encoding is 'binary' so we have to make this configurable.
11086
// Everything else in the universe uses 'utf8', though.
@@ -124,15 +100,13 @@ function ReadableState(options, stream) {
124100
this.encoding = null;
125101
if (options.encoding) {
126102
if (!StringDecoder)
127-
StringDecoder = require('string_decoder/').StringDecoder;
103+
StringDecoder = require('string_decoder').StringDecoder;
128104
this.decoder = new StringDecoder(options.encoding);
129105
this.encoding = options.encoding;
130106
}
131107
}
132108

133109
function Readable(options) {
134-
var Duplex = require('./_stream_duplex');
135-
136110
if (!(this instanceof Readable))
137111
return new Readable(options);
138112

@@ -168,11 +142,15 @@ Readable.prototype.unshift = function(chunk) {
168142
return readableAddChunk(this, state, chunk, '', true);
169143
};
170144

145+
Readable.prototype.isPaused = function() {
146+
return this._readableState.flowing === false;
147+
};
148+
171149
function readableAddChunk(stream, state, chunk, encoding, addToFront) {
172150
var er = chunkInvalid(state, chunk);
173151
if (er) {
174152
stream.emit('error', er);
175-
} else if (util.isNullOrUndefined(chunk)) {
153+
} else if (chunk === null) {
176154
state.reading = false;
177155
if (!state.ended)
178156
onEofChunk(stream, state);
@@ -234,7 +212,7 @@ function needMoreData(state) {
234212
// backwards compatibility.
235213
Readable.prototype.setEncoding = function(enc) {
236214
if (!StringDecoder)
237-
StringDecoder = require('string_decoder/').StringDecoder;
215+
StringDecoder = require('string_decoder').StringDecoder;
238216
this._readableState.decoder = new StringDecoder(enc);
239217
this._readableState.encoding = enc;
240218
return this;
@@ -261,7 +239,7 @@ function howMuchToRead(n, state) {
261239
if (state.objectMode)
262240
return n === 0 ? 0 : 1;
263241

264-
if (isNaN(n) || util.isNull(n)) {
242+
if (util.isNull(n) || isNaN(n)) {
265243
// only flow one buffer at a time
266244
if (state.flowing && state.buffer.length)
267245
return state.buffer[0].length;
@@ -737,10 +715,6 @@ Readable.prototype.resume = function() {
737715
if (!state.flowing) {
738716
debug('resume');
739717
state.flowing = true;
740-
if (!state.reading) {
741-
debug('resume read 0');
742-
this.read(0);
743-
}
744718
resume(this, state);
745719
}
746720
return this;
@@ -756,6 +730,11 @@ function resume(stream, state) {
756730
}
757731

758732
function resume_(stream, state) {
733+
if (!state.reading) {
734+
debug('resume read 0');
735+
stream.read(0);
736+
}
737+
759738
state.resumeScheduled = false;
760739
stream.emit('resume');
761740
flow(stream);
@@ -806,7 +785,12 @@ Readable.prototype.wrap = function(stream) {
806785
debug('wrapped data');
807786
if (state.decoder)
808787
chunk = state.decoder.write(chunk);
809-
if (!chunk || !state.objectMode && !chunk.length)
788+
789+
// don't skip over falsy values in objectMode
790+
//if (state.objectMode && util.isNullOrUndefined(chunk))
791+
if (state.objectMode && (chunk === null || chunk === undefined))
792+
return;
793+
else if (!state.objectMode && (!chunk || !chunk.length))
810794
return;
811795

812796
var ret = self.push(chunk);

lib/_stream_transform.js

+4-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
// Copyright Joyent, Inc. and other Node contributors.
2-
//
3-
// Permission is hereby granted, free of charge, to any person obtaining a
4-
// copy of this software and associated documentation files (the
5-
// "Software"), to deal in the Software without restriction, including
6-
// without limitation the rights to use, copy, modify, merge, publish,
7-
// distribute, sublicense, and/or sell copies of the Software, and to permit
8-
// persons to whom the Software is furnished to do so, subject to the
9-
// following conditions:
10-
//
11-
// The above copyright notice and this permission notice shall be included
12-
// in all copies or substantial portions of the Software.
13-
//
14-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15-
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17-
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18-
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19-
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20-
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21-
22-
231
// a transform stream is a readable/writable stream where you do
242
// something with the data. Sometimes it's called a "filter",
253
// but that's not a great name for it, since that implies a thing where
@@ -62,6 +40,8 @@
6240
// would be consumed, and then the rest would wait (un-transformed) until
6341
// the results of the previous transformed chunk were consumed.
6442

43+
'use strict';
44+
6545
module.exports = Transform;
6646

6747
var Duplex = require('./_stream_duplex');
@@ -74,7 +54,7 @@ util.inherits = require('inherits');
7454
util.inherits(Transform, Duplex);
7555

7656

77-
function TransformState(options, stream) {
57+
function TransformState(stream) {
7858
this.afterTransform = function(er, data) {
7959
return afterTransform(stream, er, data);
8060
};
@@ -117,7 +97,7 @@ function Transform(options) {
11797

11898
Duplex.call(this, options);
11999

120-
this._transformState = new TransformState(options, this);
100+
this._transformState = new TransformState(this);
121101

122102
// when the writable side finishes, then flush out anything remaining.
123103
var stream = this;

0 commit comments

Comments
 (0)