@@ -61,20 +61,14 @@ export type FetchResponseDiagnosticsMessage = {
61
61
} ;
62
62
63
63
export class FetchFactory {
64
- static #dispatcher: Dispatcher . ComposedDispatcher ;
65
- static #opaqueLocalStorage = new AsyncLocalStorage < FetchOpaque > ( ) ;
64
+ #dispatcher? : Dispatcher . ComposedDispatcher ;
65
+ #opaqueLocalStorage = new AsyncLocalStorage < FetchOpaque > ( ) ;
66
66
67
- static getDispatcher ( ) {
68
- return FetchFactory . #dispatcher ?? getGlobalDispatcher ( ) ;
69
- }
67
+ static #instance = new FetchFactory ( ) ;
70
68
71
- static setDispatcher ( dispatcher : Agent ) {
72
- FetchFactory . #dispatcher = dispatcher ;
73
- }
74
-
75
- static setClientOptions ( clientOptions : ClientOptions ) {
69
+ setClientOptions ( clientOptions : ClientOptions ) {
76
70
let dispatcherOption : BaseAgentOptions = {
77
- opaqueLocalStorage : FetchFactory . #opaqueLocalStorage,
71
+ opaqueLocalStorage : this . #opaqueLocalStorage,
78
72
} ;
79
73
let dispatcherClazz : new ( options : BaseAgentOptions ) => BaseAgent = BaseAgent ;
80
74
if ( clientOptions ?. lookup || clientOptions ?. checkAddress ) {
@@ -101,12 +95,20 @@ export class FetchFactory {
101
95
} as HttpAgentOptions ;
102
96
dispatcherClazz = BaseAgent ;
103
97
}
104
- FetchFactory . #dispatcher = new dispatcherClazz ( dispatcherOption ) ;
98
+ this . #dispatcher = new dispatcherClazz ( dispatcherOption ) ;
105
99
initDiagnosticsChannel ( ) ;
106
100
}
107
101
108
- static getDispatcherPoolStats ( ) {
109
- const agent = FetchFactory . getDispatcher ( ) ;
102
+ getDispatcher ( ) {
103
+ return this . #dispatcher ?? getGlobalDispatcher ( ) ;
104
+ }
105
+
106
+ setDispatcher ( dispatcher : Agent ) {
107
+ this . #dispatcher = dispatcher ;
108
+ }
109
+
110
+ getDispatcherPoolStats ( ) {
111
+ const agent = this . getDispatcher ( ) ;
110
112
// origin => Pool Instance
111
113
const clients : Map < string , WeakRef < Pool > > | undefined = Reflect . get ( agent , undiciSymbols . kClients ) ;
112
114
const poolStatsMap : Record < string , PoolStat > = { } ;
@@ -131,10 +133,18 @@ export class FetchFactory {
131
133
return poolStatsMap ;
132
134
}
133
135
134
- static async fetch ( input : RequestInfo , init ?: UrllibRequestInit ) : Promise < Response > {
136
+ static setClientOptions ( clientOptions : ClientOptions ) {
137
+ FetchFactory . #instance. setClientOptions ( clientOptions ) ;
138
+ }
139
+
140
+ static getDispatcherPoolStats ( ) {
141
+ return FetchFactory . #instance. getDispatcherPoolStats ( ) ;
142
+ }
143
+
144
+ async fetch ( input : RequestInfo , init ?: UrllibRequestInit ) : Promise < Response > {
135
145
const requestStartTime = performance . now ( ) ;
136
146
init = init ?? { } ;
137
- init . dispatcher = init . dispatcher ?? FetchFactory . #dispatcher;
147
+ init . dispatcher = init . dispatcher ?? this . #dispatcher;
138
148
const request = new Request ( input , init ) ;
139
149
const requestId = globalId ( 'HttpClientRequest' ) ;
140
150
// https://developer.chrome.com/docs/devtools/network/reference/?utm_source=devtools#timing-explanation
@@ -219,7 +229,7 @@ export class FetchFactory {
219
229
socketErrorRetries : 0 ,
220
230
} as any as RawResponseWithMeta ;
221
231
try {
222
- await FetchFactory . #opaqueLocalStorage. run ( internalOpaque , async ( ) => {
232
+ await this . #opaqueLocalStorage. run ( internalOpaque , async ( ) => {
223
233
res = await UndiciFetch ( request ) ;
224
234
} ) ;
225
235
} catch ( e : any ) {
@@ -262,6 +272,18 @@ export class FetchFactory {
262
272
} as ResponseDiagnosticsMessage ) ;
263
273
return res ! ;
264
274
}
275
+
276
+ static getDispatcher ( ) {
277
+ return FetchFactory . #instance. getDispatcher ( ) ;
278
+ }
279
+
280
+ static setDispatcher ( dispatcher : Agent ) {
281
+ FetchFactory . #instance. setDispatcher ( dispatcher ) ;
282
+ }
283
+
284
+ static async fetch ( input : RequestInfo , init ?: UrllibRequestInit ) : Promise < Response > {
285
+ return FetchFactory . #instance. fetch ( input , init ) ;
286
+ }
265
287
}
266
288
267
289
export const fetch = FetchFactory . fetch ;
0 commit comments