@@ -21,66 +21,52 @@ export enum FileMethod {
2121
2222const DataURI = / ^ d a t a : ( .+ ?\/ ( .+ ?) ) ? ( ; b a s e 6 4 ) ? , ( [ \s \S ] + ) / ;
2323
24- export class HTTPUtility {
25- constructor (
26- public runtime : {
27- Blob : typeof globalThis . Blob ;
28- DOMParser : typeof globalThis . DOMParser ;
29- FormData : typeof globalThis . FormData ;
30- FileReader : typeof globalThis . FileReader ;
31- URLSearchParams : typeof globalThis . URLSearchParams ;
32- ReadableStream : typeof globalThis . ReadableStream ;
33- ProgressEvent : typeof globalThis . ProgressEvent ;
34- } = {
35- get Blob ( ) {
36- return globalThis . Blob ;
37- } ,
38- get DOMParser ( ) {
39- return globalThis . DOMParser ;
40- } ,
41- get FormData ( ) {
42- return globalThis . FormData ;
43- } ,
44- get FileReader ( ) {
45- return globalThis . FileReader ;
46- } ,
47- get URLSearchParams ( ) {
48- return globalThis . URLSearchParams ;
49- } ,
50- get ReadableStream ( ) {
51- return globalThis . ReadableStream ;
52- } ,
53- get ProgressEvent ( ) {
54- return ( globalThis . ProgressEvent ||= class ProgressEvent <
55- T extends EventTarget = EventTarget
56- > extends Event {
57- declare target : T | null ;
58-
59- lengthComputable : boolean ;
60- total : number ;
61- loaded : number ;
62-
63- constructor (
64- type : string ,
65- {
66- lengthComputable,
67- total,
68- loaded,
69- ...meta
70- } : ProgressEventInit = { }
71- ) {
72- super ( type , meta ) ;
73-
74- this . lengthComputable = lengthComputable ;
75- this . total = total ;
76- this . loaded = loaded ;
77- }
78- } ) ;
79- }
24+ export function polyfillProgressEvent ( ) {
25+ return ( globalThis . ProgressEvent ||= class ProgressEvent <
26+ T extends EventTarget = EventTarget
27+ > extends Event {
28+ declare target : T | null ;
29+
30+ lengthComputable : boolean ;
31+ total : number ;
32+ loaded : number ;
33+
34+ constructor (
35+ type : string ,
36+ { lengthComputable, total, loaded, ...meta } : ProgressEventInit = { }
37+ ) {
38+ super ( type , meta ) ;
39+
40+ this . lengthComputable = lengthComputable ;
41+ this . total = total ;
42+ this . loaded = loaded ;
8043 }
81- ) { }
44+ } ) ;
45+ }
8246
83- polyfillProgressEvent = ( ) => this . runtime . ProgressEvent ;
47+ export type UtilityRuntime = Partial <
48+ Pick <
49+ typeof globalThis ,
50+ | 'Blob'
51+ | 'DOMParser'
52+ | 'FormData'
53+ | 'FileReader'
54+ | 'URLSearchParams'
55+ | 'ProgressEvent'
56+ >
57+ > ;
58+
59+ export const defaultUtilityRuntime : UtilityRuntime = {
60+ Blob : globalThis . Blob ,
61+ DOMParser : globalThis . DOMParser ,
62+ FormData : globalThis . FormData ,
63+ FileReader : globalThis . FileReader ,
64+ URLSearchParams : globalThis . URLSearchParams ,
65+ ProgressEvent : globalThis . ProgressEvent
66+ } ;
67+
68+ export class HTTPUtility {
69+ constructor ( public runtime : UtilityRuntime = defaultUtilityRuntime ) { }
8470
8571 parseDocument = async ( text : string , contentType = '' ) => {
8672 const [ type ] = contentType ?. split ( ';' ) || [ ] ;
@@ -116,7 +102,7 @@ export class HTTPUtility {
116102 data : string | URLSearchParams | FormData ;
117103 } => {
118104 const { FormData, URLSearchParams } = this . runtime ;
119- let contentType : string ;
105+ var contentType : string ;
120106
121107 if ( ! ( root instanceof HTMLFormElement ) )
122108 return {
@@ -162,8 +148,7 @@ export class HTTPUtility {
162148 data : T | BodyInit ;
163149 contentType ?: string ;
164150 } => {
165- const { URLSearchParams, FormData, Blob, ReadableStream } =
166- this . runtime ;
151+ const { URLSearchParams, FormData } = this . runtime ;
167152 const [ type ] = contentType ?. split ( ';' ) || [ ] ;
168153
169154 switch ( type ) {
@@ -204,8 +189,8 @@ export class HTTPUtility {
204189 isTypedArray ( data ) ||
205190 data instanceof ArrayBuffer ||
206191 data instanceof DataView ||
207- data instanceof Blob ||
208- data instanceof ReadableStream
192+ data instanceof globalThis . Blob ||
193+ data instanceof globalThis . ReadableStream
209194 )
210195 return {
211196 contentType : 'application/octet-stream' ,
@@ -287,14 +272,16 @@ export class HTTPUtility {
287272 const self = this ;
288273
289274 return ( async function * ( ) {
290- let loaded = 0 ;
275+ var loaded = 0 ;
276+ const ProgressEvent =
277+ self . runtime . ProgressEvent ?? polyfillProgressEvent ( ) ;
291278
292279 for await ( const chunk of stream ) {
293280 yield chunk ;
294281
295282 loaded += ( chunk as Uint8Array ) . byteLength ;
296283
297- const event = new self . runtime . ProgressEvent ( 'progress' , {
284+ const event = new ProgressEvent ( 'progress' , {
298285 lengthComputable : isNaN ( total ) ,
299286 loaded,
300287 total
@@ -341,7 +328,6 @@ export class HTTPUtility {
341328
342329const _defaultUtility = new HTTPUtility ( ) ;
343330
344- export const polyfillProgressEvent = _defaultUtility . polyfillProgressEvent ;
345331export const parseDocument = _defaultUtility . parseDocument ;
346332export const makeFormData = _defaultUtility . makeFormData ;
347333export const serializeNode = _defaultUtility . serializeNode ;
0 commit comments