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' ;
21
2
22
3
module . exports = Readable ;
23
4
@@ -41,16 +22,12 @@ if (!EE.listenerCount) EE.listenerCount = function(emitter, type) {
41
22
/*</replacement>*/
42
23
43
24
var Stream = require ( 'stream' ) ;
25
+ var util = require ( 'util' ) ;
26
+ var Duplex ;
44
27
45
28
/*<replacement>*/
46
29
var util = require ( 'core-util-is' ) ;
47
30
util . inherits = require ( 'inherits' ) ;
48
- /*</replacement>*/
49
-
50
- var StringDecoder ;
51
-
52
-
53
- /*<replacement>*/
54
31
var debug = require ( 'util' ) ;
55
32
if ( debug && debug . debuglog ) {
56
33
debug = debug . debuglog ( 'stream' ) ;
@@ -59,18 +36,25 @@ if (debug && debug.debuglog) {
59
36
}
60
37
/*</replacement>*/
61
38
39
+ var StringDecoder ;
62
40
63
41
util . inherits ( Readable , Stream ) ;
64
42
65
43
function ReadableState ( options , stream ) {
66
- var Duplex = require ( './_stream_duplex' ) ;
67
-
44
+ Duplex = Duplex || require ( './_stream_duplex' ) ;
68
45
options = options || { } ;
69
46
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
+
70
54
// the point at which it stops calling _read() to fill the buffer
71
55
// Note: 0 is a valid value, means "don't call _read preemptively ever"
72
56
var hwm = options . highWaterMark ;
73
- var defaultHwm = options . objectMode ? 16 : 16 * 1024 ;
57
+ var defaultHwm = this . objectMode ? 16 : 16 * 1024 ;
74
58
this . highWaterMark = ( hwm || hwm === 0 ) ? hwm : defaultHwm ;
75
59
76
60
// cast to ints.
@@ -97,14 +81,6 @@ function ReadableState(options, stream) {
97
81
this . emittedReadable = false ;
98
82
this . readableListening = false ;
99
83
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
-
108
84
// Crypto is kind of old and crusty. Historically, its default string
109
85
// encoding is 'binary' so we have to make this configurable.
110
86
// Everything else in the universe uses 'utf8', though.
@@ -124,15 +100,13 @@ function ReadableState(options, stream) {
124
100
this . encoding = null ;
125
101
if ( options . encoding ) {
126
102
if ( ! StringDecoder )
127
- StringDecoder = require ( 'string_decoder/ ' ) . StringDecoder ;
103
+ StringDecoder = require ( 'string_decoder' ) . StringDecoder ;
128
104
this . decoder = new StringDecoder ( options . encoding ) ;
129
105
this . encoding = options . encoding ;
130
106
}
131
107
}
132
108
133
109
function Readable ( options ) {
134
- var Duplex = require ( './_stream_duplex' ) ;
135
-
136
110
if ( ! ( this instanceof Readable ) )
137
111
return new Readable ( options ) ;
138
112
@@ -168,11 +142,15 @@ Readable.prototype.unshift = function(chunk) {
168
142
return readableAddChunk ( this , state , chunk , '' , true ) ;
169
143
} ;
170
144
145
+ Readable . prototype . isPaused = function ( ) {
146
+ return this . _readableState . flowing === false ;
147
+ } ;
148
+
171
149
function readableAddChunk ( stream , state , chunk , encoding , addToFront ) {
172
150
var er = chunkInvalid ( state , chunk ) ;
173
151
if ( er ) {
174
152
stream . emit ( 'error' , er ) ;
175
- } else if ( util . isNullOrUndefined ( chunk ) ) {
153
+ } else if ( chunk === null ) {
176
154
state . reading = false ;
177
155
if ( ! state . ended )
178
156
onEofChunk ( stream , state ) ;
@@ -234,7 +212,7 @@ function needMoreData(state) {
234
212
// backwards compatibility.
235
213
Readable . prototype . setEncoding = function ( enc ) {
236
214
if ( ! StringDecoder )
237
- StringDecoder = require ( 'string_decoder/ ' ) . StringDecoder ;
215
+ StringDecoder = require ( 'string_decoder' ) . StringDecoder ;
238
216
this . _readableState . decoder = new StringDecoder ( enc ) ;
239
217
this . _readableState . encoding = enc ;
240
218
return this ;
@@ -261,7 +239,7 @@ function howMuchToRead(n, state) {
261
239
if ( state . objectMode )
262
240
return n === 0 ? 0 : 1 ;
263
241
264
- if ( isNaN ( n ) || util . isNull ( n ) ) {
242
+ if ( util . isNull ( n ) || isNaN ( n ) ) {
265
243
// only flow one buffer at a time
266
244
if ( state . flowing && state . buffer . length )
267
245
return state . buffer [ 0 ] . length ;
@@ -737,10 +715,6 @@ Readable.prototype.resume = function() {
737
715
if ( ! state . flowing ) {
738
716
debug ( 'resume' ) ;
739
717
state . flowing = true ;
740
- if ( ! state . reading ) {
741
- debug ( 'resume read 0' ) ;
742
- this . read ( 0 ) ;
743
- }
744
718
resume ( this , state ) ;
745
719
}
746
720
return this ;
@@ -756,6 +730,11 @@ function resume(stream, state) {
756
730
}
757
731
758
732
function resume_ ( stream , state ) {
733
+ if ( ! state . reading ) {
734
+ debug ( 'resume read 0' ) ;
735
+ stream . read ( 0 ) ;
736
+ }
737
+
759
738
state . resumeScheduled = false ;
760
739
stream . emit ( 'resume' ) ;
761
740
flow ( stream ) ;
@@ -806,7 +785,12 @@ Readable.prototype.wrap = function(stream) {
806
785
debug ( 'wrapped data' ) ;
807
786
if ( state . decoder )
808
787
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 ) )
810
794
return ;
811
795
812
796
var ret = self . push ( chunk ) ;
0 commit comments