@@ -218,8 +218,7 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
218
218
debug ( 'readableAddChunk' , chunk ) ;
219
219
const state = stream . _readableState ;
220
220
221
- let skipChunkCheck ;
222
-
221
+ let err ;
223
222
if ( ! state . objectMode ) {
224
223
if ( typeof chunk === 'string' ) {
225
224
encoding = encoding || state . defaultEncoding ;
@@ -231,54 +230,47 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
231
230
chunk = Buffer . from ( chunk , encoding ) ;
232
231
encoding = '' ;
233
232
}
234
- skipChunkCheck = true ;
233
+ } else if ( chunk instanceof Buffer ) {
234
+ encoding = '' ;
235
+ } else if ( Stream . _isUint8Array ( chunk ) ) {
236
+ chunk = Stream . _uint8ArrayToBuffer ( chunk ) ;
237
+ encoding = '' ;
238
+ } else if ( chunk != null ) {
239
+ err = new ERR_INVALID_ARG_TYPE (
240
+ 'chunk' , [ 'string' , 'Buffer' , 'Uint8Array' ] , chunk ) ;
235
241
}
236
- } else {
237
- skipChunkCheck = true ;
238
242
}
239
243
240
- if ( chunk === null ) {
244
+ if ( err ) {
245
+ errorOrDestroy ( stream , err ) ;
246
+ } else if ( chunk === null ) {
241
247
state . reading = false ;
242
248
onEofChunk ( stream , state ) ;
243
- } else {
244
- var er ;
245
- if ( ! skipChunkCheck )
246
- er = chunkInvalid ( state , chunk ) ;
247
- if ( er ) {
248
- errorOrDestroy ( stream , er ) ;
249
- } else if ( state . objectMode || ( chunk && chunk . length > 0 ) ) {
250
- if ( typeof chunk !== 'string' &&
251
- ! state . objectMode &&
252
- // Do not use Object.getPrototypeOf as it is slower since V8 7.3.
253
- ! ( chunk instanceof Buffer ) ) {
254
- chunk = Stream . _uint8ArrayToBuffer ( chunk ) ;
255
- }
256
-
257
- if ( addToFront ) {
258
- if ( state . endEmitted )
259
- errorOrDestroy ( stream , new ERR_STREAM_UNSHIFT_AFTER_END_EVENT ( ) ) ;
249
+ } else if ( state . objectMode || ( chunk && chunk . length > 0 ) ) {
250
+ if ( addToFront ) {
251
+ if ( state . endEmitted )
252
+ errorOrDestroy ( stream , new ERR_STREAM_UNSHIFT_AFTER_END_EVENT ( ) ) ;
253
+ else
254
+ addChunk ( stream , state , chunk , true ) ;
255
+ } else if ( state . ended ) {
256
+ errorOrDestroy ( stream , new ERR_STREAM_PUSH_AFTER_EOF ( ) ) ;
257
+ } else if ( state . destroyed ) {
258
+ return false ;
259
+ } else {
260
+ state . reading = false ;
261
+ if ( state . decoder && ! encoding ) {
262
+ chunk = state . decoder . write ( chunk ) ;
263
+ if ( state . objectMode || chunk . length !== 0 )
264
+ addChunk ( stream , state , chunk , false ) ;
260
265
else
261
- addChunk ( stream , state , chunk , true ) ;
262
- } else if ( state . ended ) {
263
- errorOrDestroy ( stream , new ERR_STREAM_PUSH_AFTER_EOF ( ) ) ;
264
- } else if ( state . destroyed ) {
265
- return false ;
266
+ maybeReadMore ( stream , state ) ;
266
267
} else {
267
- state . reading = false ;
268
- if ( state . decoder && ! encoding ) {
269
- chunk = state . decoder . write ( chunk ) ;
270
- if ( state . objectMode || chunk . length !== 0 )
271
- addChunk ( stream , state , chunk , false ) ;
272
- else
273
- maybeReadMore ( stream , state ) ;
274
- } else {
275
- addChunk ( stream , state , chunk , false ) ;
276
- }
268
+ addChunk ( stream , state , chunk , false ) ;
277
269
}
278
- } else if ( ! addToFront ) {
279
- state . reading = false ;
280
- maybeReadMore ( stream , state ) ;
281
270
}
271
+ } else if ( ! addToFront ) {
272
+ state . reading = false ;
273
+ maybeReadMore ( stream , state ) ;
282
274
}
283
275
284
276
// We can push more data if we are below the highWaterMark.
@@ -306,17 +298,6 @@ function addChunk(stream, state, chunk, addToFront) {
306
298
maybeReadMore ( stream , state ) ;
307
299
}
308
300
309
- function chunkInvalid ( state , chunk ) {
310
- if ( ! Stream . _isUint8Array ( chunk ) &&
311
- typeof chunk !== 'string' &&
312
- chunk !== undefined &&
313
- ! state . objectMode ) {
314
- return new ERR_INVALID_ARG_TYPE (
315
- 'chunk' , [ 'string' , 'Buffer' , 'Uint8Array' ] , chunk ) ;
316
- }
317
- }
318
-
319
-
320
301
Readable . prototype . isPaused = function ( ) {
321
302
const state = this . _readableState ;
322
303
return state [ kPaused ] === true || state . flowing === false ;
0 commit comments