@@ -2,12 +2,11 @@ import { ReadableStream, ReadableStreamLike } from 'web-streams-polyfill';
22import { parseJSON } from 'web-utility' ;
33
44import {
5- emitStreamProgress ,
6- parseDocument ,
5+ DataRuntime ,
6+ DataToolkit ,
7+ defaultDataRuntime ,
78 ProgressData ,
8- ProgressEventTarget ,
9- readBytes ,
10- streamFromProgress
9+ ProgressEventTarget
1110} from './utility' ;
1211
1312export enum BodyRequestMethods {
@@ -80,7 +79,7 @@ export function parseBody<T>(raw: string, contentType: string): T {
8079
8180 if ( contentType . match ( / h t m l | x m l / ) )
8281 try {
83- return parseDocument ( raw , contentType ) as T ;
82+ return this . parseDocument ( raw , contentType ) as T ;
8483 } catch { }
8584
8685 if ( contentType . includes ( 'text' ) ) return raw as T ;
@@ -107,26 +106,25 @@ export interface HeadResponse {
107106 body ?: ArrayBuffer ;
108107}
109108
110- export type HTTPRuntime = Partial <
111- Pick <
112- typeof globalThis ,
113- 'EventTarget' | 'XMLHttpRequest' | 'Headers' | 'Blob' | 'fetch'
114- > & {
115- ReadableStream : typeof ReadableStream ;
116- }
117- > ;
118-
119- export const defaultRuntime : HTTPRuntime = {
109+ export type HTTPRuntime = DataRuntime &
110+ Partial <
111+ Pick <
112+ typeof globalThis ,
113+ 'EventTarget' | 'XMLHttpRequest' | 'Headers' | 'fetch'
114+ >
115+ > ;
116+ export const defaultHTTPRuntime : HTTPRuntime = {
117+ ...defaultDataRuntime ,
120118 EventTarget : globalThis . EventTarget ,
121119 XMLHttpRequest : globalThis . XMLHttpRequest ,
122120 Headers : globalThis . Headers ,
123- Blob : globalThis . Blob ,
124- ReadableStream,
125121 fetch : globalThis . fetch
126122} ;
127123
128- export class HTTPToolkit {
129- constructor ( public runtime : HTTPRuntime = defaultRuntime ) { }
124+ export class HTTPToolkit extends DataToolkit {
125+ constructor ( public runtime : HTTPRuntime = defaultHTTPRuntime ) {
126+ super ( runtime ) ;
127+ }
130128
131129 requestXHR = < B > ( {
132130 method = 'GET' ,
@@ -136,15 +134,13 @@ export class HTTPToolkit {
136134 signal,
137135 ...rest
138136 } : Request ) : RequestResult < B > => {
139- const { XMLHttpRequest, Headers, Blob } = this . runtime ;
137+ const { XMLHttpRequest, Headers, Blob, ReadableStream } = this . runtime ;
140138
141139 const request = new XMLHttpRequest ( ) ;
142140 const header = new Headers ( headers ) ;
143141 const bodyPromise =
144- body instanceof globalThis . ReadableStream
145- ? Array . fromAsync (
146- ReadableStream . from ( body as ReadableStreamLike )
147- ) . then ( parts => new Blob ( parts ) )
142+ body instanceof ReadableStream
143+ ? Array . fromAsync ( body ) . then ( parts => new Blob ( parts ) )
148144 : Promise . resolve ( body ) ;
149145 const abort = ( ) => request . abort ( ) ;
150146
@@ -201,8 +197,8 @@ export class HTTPToolkit {
201197
202198 return {
203199 response,
204- upload : streamFromProgress ( request . upload ) ,
205- download : streamFromProgress ( request )
200+ upload : this . streamFromProgress ( request . upload ) ,
201+ download : this . streamFromProgress ( request )
206202 } ;
207203 } ;
208204
@@ -241,20 +237,20 @@ export class HTTPToolkit {
241237 : responseType === 'arraybuffer' || responseType === 'blob'
242238 ? { ...headers , Accept : 'application/octet-stream' }
243239 : headers ;
244- const isStream = body instanceof globalThis . ReadableStream ;
240+ const isStream = body instanceof ReadableStream ;
245241 var upload : AsyncGenerator < ProgressData > | undefined ;
246242
247243 if ( isStream ) {
248244 const uploadProgress = new EventTarget ( ) ;
249245
250246 body = ReadableStream . from (
251- emitStreamProgress (
252- ReadableStream . from ( body ) ,
247+ this . emitStreamProgress (
248+ body ,
253249 + headers [ 'Content-Length' ] ,
254250 uploadProgress
255251 )
256252 ) ;
257- upload = streamFromProgress ( uploadProgress ) ;
253+ upload = this . streamFromProgress ( uploadProgress ) ;
258254 }
259255 const downloadProgress = new EventTarget ( ) ;
260256
@@ -269,7 +265,7 @@ export class HTTPToolkit {
269265 } ) . then ( response =>
270266 this . parseResponse < B > ( response , responseType , downloadProgress )
271267 ) ;
272- const download = streamFromProgress ( downloadProgress ) ;
268+ const download = this . streamFromProgress ( downloadProgress ) ;
273269
274270 return { response, upload, download } ;
275271 } ;
@@ -289,7 +285,7 @@ export class HTTPToolkit {
289285 return { status, statusText, headers : header , body : undefined } ;
290286
291287 const stream = ReadableStream . from (
292- emitStreamProgress (
288+ this . emitStreamProgress (
293289 ReadableStream . from ( body as ReadableStreamLike ) ,
294290 + headers . get ( 'Content-Length' ) ,
295291 downloadProgress
@@ -404,7 +400,7 @@ export class HTTPToolkit {
404400 ...fetchOptions ,
405401 headers : normalizedHeaders
406402 } ) ;
407- const body = await readBytes (
403+ const body = await this . readBytes (
408404 ReadableStream . from ( response . body as ReadableStreamLike ) ,
409405 FILE_TYPE_MAGIC_NUMBER_SIZE
410406 ) ;
0 commit comments