1
1
import 'core-js/es/object/from-entries' ;
2
- import 'core-js/es/string/match-all' ;
3
2
import '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' ;
5
5
import { parseJSON } from 'web-utility' ;
6
6
7
- import {
8
- parseDocument ,
9
- ProgressData ,
10
- readAs ,
11
- streamFromProgress
12
- } from './utility' ;
7
+ import { parseDocument , ProgressData , streamFromProgress } from './utility' ;
13
8
14
9
export enum BodyRequestMethods {
15
10
POST = 'POST' ,
@@ -81,15 +76,15 @@ export const parseHeaders = (raw: string): Response['headers'] =>
81
76
)
82
77
) ;
83
78
export function parseBody < T > ( raw : string , contentType : string ) : T {
84
- if ( contentType . includes ( 'text' ) ) return raw as T ;
85
-
86
79
if ( contentType . includes ( 'json' ) ) return parseJSON ( raw ) ;
87
80
88
81
if ( contentType . match ( / h t m l | x m l / ) )
89
82
try {
90
83
return parseDocument ( raw , contentType ) as T ;
91
84
} catch { }
92
85
86
+ if ( contentType . includes ( 'text' ) ) return raw as T ;
87
+
93
88
return new TextEncoder ( ) . encode ( raw ) . buffer as T ;
94
89
}
95
90
@@ -201,46 +196,35 @@ export function requestFetch<B>({
201
196
body,
202
197
signal : signals [ 0 ] && AbortSignal . any ( signals )
203
198
} ) ;
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
- } ) ;
218
199
219
200
return {
220
- response : parseResponse ( responsePromise , stream1 . promise , responseType ) ,
221
- download : iterateFetchBody ( responsePromise , stream2 . promise )
201
+ response : parseResponse ( responsePromise , responseType ) ,
202
+ download : iterateFetchBody ( responsePromise )
222
203
} ;
223
204
}
224
205
225
206
export async function parseResponse < B > (
226
207
responsePromise : Promise < globalThis . Response > ,
227
- streamPromise : Promise < ReadableStream < Uint8Array > > ,
228
208
responseType : Request [ 'responseType' ]
229
209
) : 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
+
232
214
const contentType = headers . get ( 'Content-Type' ) || '' ;
233
215
234
216
const header = parseHeaders (
235
217
[ ...headers ] . map ( ( [ key , value ] ) => `${ key } : ${ value } ` ) . join ( '\n' )
236
218
) ;
237
- const stream = await streamPromise ;
238
- const body =
219
+ const rBody =
239
220
status === 204
240
221
? 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 } ;
244
228
}
245
229
246
230
export async function parseFetchBody < B > (
@@ -258,21 +242,22 @@ export async function parseFetchBody<B>(
258
242
259
243
if ( responseType === 'arraybuffer' ) return blob . arrayBuffer ( ) as B ;
260
244
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 ) ;
264
246
}
265
247
266
248
export async function * iterateFetchBody (
267
- responsePromise : Promise < globalThis . Response > ,
268
- bodyPromise : Promise < ReadableStream < Uint8Array > >
249
+ responsePromise : Promise < globalThis . Response >
269
250
) {
270
- const response = await responsePromise ,
271
- body = await bodyPromise ;
272
- const total = + response . headers . get ( 'Content-Length' ) ;
251
+ const { headers, body } = ( await responsePromise ) . clone ( ) ;
273
252
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
+ }
276
261
}
277
262
278
263
export const request = < B > ( options : Request ) : RequestResult < B > =>
0 commit comments