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