22import  {  Buffer  }  from  'node:buffer' ; 
33import  {  Readable  }  from  'node:stream' ; 
44import  busboy  from  'busboy' ; 
5+ import  {  handleMaybePromise ,  MaybePromise  }  from  '@whatwg-node/promise-helpers' ; 
56import  {  hasArrayBufferMethod ,  hasBufferMethod ,  hasBytesMethod ,  PonyfillBlob  }  from  './Blob.js' ; 
67import  {  PonyfillFile  }  from  './File.js' ; 
78import  {  getStreamFromFormData ,  PonyfillFormData  }  from  './FormData.js' ; 
@@ -140,36 +141,44 @@ export class PonyfillBody<TJSON = any> implements Body {
140141      return  fakePromise ( this . _chunks ) ; 
141142    } 
142143    if  ( this . bodyType  ===  BodyInitType . AsyncIterable )  { 
144+       if  ( Array . fromAsync )  { 
145+         return  handleMaybePromise ( 
146+           ( )  =>  Array . fromAsync ( this . bodyInit  as  AsyncIterable < Uint8Array > ) , 
147+           chunks  =>  { 
148+             this . _chunks  =  chunks ; 
149+             return  this . _chunks ; 
150+           } , 
151+         ) ; 
152+       } 
143153      const  iterator  =  ( this . bodyInit  as  AsyncIterable < Uint8Array > ) [ Symbol . asyncIterator ] ( ) ; 
144-       const  collectValue  =  ( ) : Promise < Uint8Array [ ] >  =>  { 
145-         return  iterator . next ( ) . then ( ( {  value,  done } )  =>  { 
146-           this . _chunks  ||=  [ ] ; 
147-           if  ( value )  { 
148-             this . _chunks . push ( value ) ; 
149-           } 
150-           if  ( ! done )  { 
151-             return  collectValue ( ) ; 
152-           } 
153-           return  this . _chunks ; 
154-         } ) ; 
155-       } ; 
154+       const  collectValue  =  ( ) : MaybePromise < Uint8Array [ ] >  => 
155+         handleMaybePromise ( 
156+           ( )  =>  iterator . next ( ) , 
157+           ( {  value,  done } )  =>  { 
158+             this . _chunks  ||=  [ ] ; 
159+             if  ( value )  { 
160+               this . _chunks . push ( value ) ; 
161+             } 
162+             if  ( ! done )  { 
163+               return  collectValue ( ) ; 
164+             } 
165+             return  this . _chunks ; 
166+           } , 
167+         ) ; 
156168      return  collectValue ( ) ; 
157169    } 
158170    const  _body  =  this . generateBody ( ) ; 
159171    if  ( ! _body )  { 
160-       return  fakePromise ( [ ] ) ; 
172+       this . _chunks  =  [ ] ; 
173+       return  fakePromise ( this . _chunks ) ; 
161174    } 
162175    this . _chunks  =  [ ] ; 
163176    _body . readable . on ( 'data' ,  chunk  =>  { 
164177      this . _chunks ! . push ( chunk ) ; 
165178    } ) ; 
166179    return  new  Promise < Uint8Array [ ] > ( ( resolve ,  reject )  =>  { 
167-       _body . readable . once ( 'end' ,  ( )  =>  { 
168-         resolve ( this . _chunks ! ) ; 
169-       } ) ; 
170-       _body . readable . once ( 'error' ,  e  =>  { 
171-         reject ( e ) ; 
172-       } ) ; 
180+       _body . readable . once ( 'end' ,  ( )  =>  resolve ( this . _chunks ! ) ) ; 
181+       _body . readable . once ( 'error' ,  reject ) ; 
173182    } ) ; 
174183  } 
175184
@@ -190,13 +199,18 @@ export class PonyfillBody<TJSON = any> implements Body {
190199      } ) ; 
191200      return  fakePromise ( this . _blob ) ; 
192201    } 
193-     return  this . _collectChunksFromReadable ( ) . then ( chunks  =>  { 
194-       this . _blob  =  new  PonyfillBlob ( chunks ,  { 
195-         type : this . contentType  ||  '' , 
196-         size : this . contentLength , 
197-       } ) ; 
198-       return  this . _blob ; 
199-     } ) ; 
202+     return  fakePromise ( 
203+       handleMaybePromise ( 
204+         ( )  =>  this . _collectChunksFromReadable ( ) , 
205+         chunks  =>  { 
206+           this . _blob  =  new  PonyfillBlob ( chunks ,  { 
207+             type : this . contentType  ||  '' , 
208+             size : this . contentLength , 
209+           } ) ; 
210+           return  this . _blob ; 
211+         } , 
212+       ) , 
213+     ) ; 
200214  } 
201215
202216  _formData : PonyfillFormData  |  null  =  null ; 
@@ -299,14 +313,19 @@ export class PonyfillBody<TJSON = any> implements Body {
299313        } ) ; 
300314      } 
301315    } 
302-     return  this . _collectChunksFromReadable ( ) . then ( chunks  =>  { 
303-       if  ( chunks . length  ===  1 )  { 
304-         this . _buffer  =  chunks [ 0 ]  as  Buffer ; 
305-         return  this . _buffer ; 
306-       } 
307-       this . _buffer  =  Buffer . concat ( chunks ) ; 
308-       return  this . _buffer ; 
309-     } ) ; 
316+     return  fakePromise ( 
317+       handleMaybePromise ( 
318+         ( )  =>  this . _collectChunksFromReadable ( ) , 
319+         chunks  =>  { 
320+           if  ( chunks . length  ===  1 )  { 
321+             this . _buffer  =  chunks [ 0 ]  as  Buffer ; 
322+             return  this . _buffer ; 
323+           } 
324+           this . _buffer  =  Buffer . concat ( chunks ) ; 
325+           return  this . _buffer ; 
326+         } , 
327+       ) , 
328+     ) ; 
310329  } 
311330
312331  bytes ( ) : Promise < Uint8Array >  { 
0 commit comments