11import 'core-js/es/object/from-entries' ;
2- import 'core-js/es/string/match-all' ;
32import 'core-js/es/promise/with-resolvers' ;
4- import 'web-streams-polyfill/polyfill/es5' ;
3+ import 'core-js/es/string/match-all' ;
4+ import type { ReadableStream } from 'web-streams-polyfill' ;
55import { parseJSON } from 'web-utility' ;
66
7- import {
8- parseDocument ,
9- ProgressData ,
10- readAs ,
11- streamFromProgress
12- } from './utility' ;
7+ import { parseDocument , ProgressData , streamFromProgress } from './utility' ;
138
149export enum BodyRequestMethods {
1510 POST = 'POST' ,
@@ -81,15 +76,15 @@ export const parseHeaders = (raw: string): Response['headers'] =>
8176 )
8277 ) ;
8378export function parseBody < T > ( raw : string , contentType : string ) : T {
84- if ( contentType . includes ( 'text' ) ) return raw as T ;
85-
8679 if ( contentType . includes ( 'json' ) ) return parseJSON ( raw ) ;
8780
8881 if ( contentType . match ( / h t m l | x m l / ) )
8982 try {
9083 return parseDocument ( raw , contentType ) as T ;
9184 } catch { }
9285
86+ if ( contentType . includes ( 'text' ) ) return raw as T ;
87+
9388 return new TextEncoder ( ) . encode ( raw ) . buffer as T ;
9489}
9590
@@ -201,46 +196,35 @@ export function requestFetch<B>({
201196 body,
202197 signal : signals [ 0 ] && AbortSignal . any ( signals )
203198 } ) ;
204- const stream1 = Promise . withResolvers < ReadableStream < Uint8Array > > ( ) ,
205- stream2 = Promise . withResolvers < ReadableStream < Uint8Array > > ( ) ;
206-
207- responsePromise
208- . then ( response => {
209- const streams = response . body . tee ( ) ;
210-
211- stream1 . resolve ( streams [ 0 ] ) ;
212- stream2 . resolve ( streams [ 1 ] ) ;
213- } )
214- . catch ( reason => {
215- stream1 . reject ( reason ) ;
216- stream2 . reject ( reason ) ;
217- } ) ;
218199
219200 return {
220- response : parseResponse ( responsePromise , stream1 . promise , responseType ) ,
221- download : iterateFetchBody ( responsePromise , stream2 . promise )
201+ response : parseResponse ( responsePromise , responseType ) ,
202+ download : iterateFetchBody ( responsePromise )
222203 } ;
223204}
224205
225206export async function parseResponse < B > (
226207 responsePromise : Promise < globalThis . Response > ,
227- streamPromise : Promise < ReadableStream < Uint8Array > > ,
228208 responseType : Request [ 'responseType' ]
229209) : Promise < Response < B > > {
230- const response = await responsePromise ;
231- const { status, statusText, headers } = response ;
210+ const { status, statusText, headers, body } = (
211+ await responsePromise
212+ ) . clone ( ) ;
213+
232214 const contentType = headers . get ( 'Content-Type' ) || '' ;
233215
234216 const header = parseHeaders (
235217 [ ...headers ] . map ( ( [ key , value ] ) => `${ key } : ${ value } ` ) . join ( '\n' )
236218 ) ;
237- const stream = await streamPromise ;
238- const body =
219+ const rBody =
239220 status === 204
240221 ? undefined
241- : await parseFetchBody < B > ( stream , contentType , responseType ) ;
242-
243- return { status, statusText, headers : header , body } ;
222+ : await parseFetchBody < B > (
223+ body as ReadableStream < Uint8Array > ,
224+ contentType ,
225+ responseType
226+ ) ;
227+ return { status, statusText, headers : header , body : rBody } ;
244228}
245229
246230export async function parseFetchBody < B > (
@@ -258,21 +242,22 @@ export async function parseFetchBody<B>(
258242
259243 if ( responseType === 'arraybuffer' ) return blob . arrayBuffer ( ) as B ;
260244
261- const text = ( await readAs ( blob , 'text' ) . result ) as string ;
262-
263- return parseBody < B > ( text , contentType ) ;
245+ return parseBody < B > ( await blob . text ( ) , contentType ) ;
264246}
265247
266248export async function * iterateFetchBody (
267- responsePromise : Promise < globalThis . Response > ,
268- bodyPromise : Promise < ReadableStream < Uint8Array > >
249+ responsePromise : Promise < globalThis . Response >
269250) {
270- const response = await responsePromise ,
271- body = await bodyPromise ;
272- const total = + response . headers . get ( 'Content-Length' ) ;
251+ const { headers, body } = ( await responsePromise ) . clone ( ) ;
273252
274- for await ( const { byteLength } of body )
275- yield { total, loaded : byteLength } ;
253+ const total = + headers . get ( 'Content-Length' ) ;
254+ var loaded = 0 ;
255+
256+ for await ( const { byteLength } of body as ReadableStream < Uint8Array > ) {
257+ loaded += byteLength ;
258+
259+ yield { total, loaded } ;
260+ }
276261}
277262
278263export const request = < B > ( options : Request ) : RequestResult < B > =>
0 commit comments