@@ -61,20 +61,14 @@ export type FetchResponseDiagnosticsMessage = {
6161} ;
6262
6363export class FetchFactory {
64- static #dispatcher: Dispatcher . ComposedDispatcher ;
65- static #opaqueLocalStorage = new AsyncLocalStorage < FetchOpaque > ( ) ;
64+ #dispatcher? : Dispatcher . ComposedDispatcher ;
65+ #opaqueLocalStorage = new AsyncLocalStorage < FetchOpaque > ( ) ;
6666
67- static getDispatcher ( ) {
68- return FetchFactory . #dispatcher ?? getGlobalDispatcher ( ) ;
69- }
67+ static #instance = new FetchFactory ( ) ;
7068
71- static setDispatcher ( dispatcher : Agent ) {
72- FetchFactory . #dispatcher = dispatcher ;
73- }
74-
75- static setClientOptions ( clientOptions : ClientOptions ) {
69+ setClientOptions ( clientOptions : ClientOptions ) {
7670 let dispatcherOption : BaseAgentOptions = {
77- opaqueLocalStorage : FetchFactory . #opaqueLocalStorage,
71+ opaqueLocalStorage : this . #opaqueLocalStorage,
7872 } ;
7973 let dispatcherClazz : new ( options : BaseAgentOptions ) => BaseAgent = BaseAgent ;
8074 if ( clientOptions ?. lookup || clientOptions ?. checkAddress ) {
@@ -101,12 +95,20 @@ export class FetchFactory {
10195 } as HttpAgentOptions ;
10296 dispatcherClazz = BaseAgent ;
10397 }
104- FetchFactory . #dispatcher = new dispatcherClazz ( dispatcherOption ) ;
98+ this . #dispatcher = new dispatcherClazz ( dispatcherOption ) ;
10599 initDiagnosticsChannel ( ) ;
106100 }
107101
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 ( ) ;
110112 // origin => Pool Instance
111113 const clients : Map < string , WeakRef < Pool > > | undefined = Reflect . get ( agent , undiciSymbols . kClients ) ;
112114 const poolStatsMap : Record < string , PoolStat > = { } ;
@@ -131,10 +133,18 @@ export class FetchFactory {
131133 return poolStatsMap ;
132134 }
133135
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 > {
135145 const requestStartTime = performance . now ( ) ;
136146 init = init ?? { } ;
137- init . dispatcher = init . dispatcher ?? FetchFactory . #dispatcher;
147+ init . dispatcher = init . dispatcher ?? this . #dispatcher;
138148 const request = new Request ( input , init ) ;
139149 const requestId = globalId ( 'HttpClientRequest' ) ;
140150 // https://developer.chrome.com/docs/devtools/network/reference/?utm_source=devtools#timing-explanation
@@ -219,7 +229,7 @@ export class FetchFactory {
219229 socketErrorRetries : 0 ,
220230 } as any as RawResponseWithMeta ;
221231 try {
222- await FetchFactory . #opaqueLocalStorage. run ( internalOpaque , async ( ) => {
232+ await this . #opaqueLocalStorage. run ( internalOpaque , async ( ) => {
223233 res = await UndiciFetch ( request ) ;
224234 } ) ;
225235 } catch ( e : any ) {
@@ -262,6 +272,18 @@ export class FetchFactory {
262272 } as ResponseDiagnosticsMessage ) ;
263273 return res ! ;
264274 }
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+ }
265287}
266288
267289export const fetch = FetchFactory . fetch ;
0 commit comments