@@ -94,7 +94,7 @@ module.exports = class BodyReadable extends Readable {
9494 }
9595
9696 push ( chunk ) {
97- if ( this [ kConsume ] && chunk !== null && this . readableLength === 0 ) {
97+ if ( this [ kConsume ] && chunk !== null ) {
9898 consumePush ( this [ kConsume ] , chunk )
9999 return this [ kReading ] ? super . push ( chunk ) : true
100100 }
@@ -215,26 +215,28 @@ async function consume (stream, type) {
215215 reject ( rState . errored ?? new TypeError ( 'unusable' ) )
216216 }
217217 } else {
218- stream [ kConsume ] = {
219- type,
220- stream,
221- resolve,
222- reject,
223- length : 0 ,
224- body : [ ]
225- }
218+ queueMicrotask ( ( ) => {
219+ stream [ kConsume ] = {
220+ type,
221+ stream,
222+ resolve,
223+ reject,
224+ length : 0 ,
225+ body : [ ]
226+ }
226227
227- stream
228- . on ( 'error' , function ( err ) {
229- consumeFinish ( this [ kConsume ] , err )
230- } )
231- . on ( 'close' , function ( ) {
232- if ( this [ kConsume ] . body !== null ) {
233- consumeFinish ( this [ kConsume ] , new RequestAbortedError ( ) )
234- }
235- } )
228+ stream
229+ . on ( 'error' , function ( err ) {
230+ consumeFinish ( this [ kConsume ] , err )
231+ } )
232+ . on ( 'close' , function ( ) {
233+ if ( this [ kConsume ] . body !== null ) {
234+ consumeFinish ( this [ kConsume ] , new RequestAbortedError ( ) )
235+ }
236+ } )
236237
237- queueMicrotask ( ( ) => consumeStart ( stream [ kConsume ] ) )
238+ consumeStart ( stream [ kConsume ] )
239+ } )
238240 }
239241 } )
240242}
@@ -246,8 +248,16 @@ function consumeStart (consume) {
246248
247249 const { _readableState : state } = consume . stream
248250
249- for ( const chunk of state . buffer ) {
250- consumePush ( consume , chunk )
251+ if ( state . bufferIndex ) {
252+ const start = state . bufferIndex
253+ const end = state . buffer . length
254+ for ( let n = start ; n < end ; n ++ ) {
255+ consumePush ( consume , state . buffer [ n ] )
256+ }
257+ } else {
258+ for ( const chunk of state . buffer ) {
259+ consumePush ( consume , chunk )
260+ }
251261 }
252262
253263 if ( state . endEmitted ) {
0 commit comments