-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathfetch.ts
More file actions
298 lines (274 loc) · 9.49 KB
/
fetch.ts
File metadata and controls
298 lines (274 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { AsyncLocalStorage } from 'node:async_hooks';
import { debuglog } from 'node:util';
import { fetch as UndiciFetch, Request, Response, Agent, getGlobalDispatcher, Pool, Dispatcher } from 'undici';
import type { RequestInfo, RequestInit } from 'undici';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import undiciSymbols from 'undici/lib/core/symbols.js';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { getResponseState } from 'undici/lib/web/fetch/response.js';
import { BaseAgent } from './BaseAgent.js';
import type { BaseAgentOptions } from './BaseAgent.js';
import { initDiagnosticsChannel } from './diagnosticsChannel.js';
import type { FetchOpaque } from './FetchOpaqueInterceptor.js';
import { HttpAgent } from './HttpAgent.js';
import type { HttpAgentOptions } from './HttpAgent.js';
import { channels } from './HttpClient.js';
import type {
ClientOptions,
PoolStat,
RequestDiagnosticsMessage,
ResponseDiagnosticsMessage,
UndiciTimingInfo,
} from './HttpClient.js';
import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
import type { FetchMeta, HttpMethod, RequestMeta } from './Request.js';
import type { RawResponseWithMeta, SocketInfo } from './Response.js';
import symbols from './symbols.js';
import { convertHeader, globalId, performanceTime, updateSocketInfo } from './utils.js';
const debug = debuglog('urllib/fetch');
export interface UrllibRequestInit extends RequestInit {
// default is true
timing?: boolean;
}
export type FetchDiagnosticsMessage = {
fetch: FetchMeta;
fetchOpaque: FetchOpaque;
};
export type FetchResponseDiagnosticsMessage = {
fetch: FetchMeta;
fetchOpaque: FetchOpaque;
timingInfo?: UndiciTimingInfo;
response?: Response;
error?: Error;
};
export class FetchFactory {
#dispatcher?: Dispatcher.ComposedDispatcher;
#opaqueLocalStorage = new AsyncLocalStorage<FetchOpaque>();
static #instance = new FetchFactory();
setClientOptions(clientOptions: ClientOptions): void {
let dispatcherOption: BaseAgentOptions = {
opaqueLocalStorage: this.#opaqueLocalStorage,
};
let dispatcherClazz: new (options: BaseAgentOptions) => BaseAgent = BaseAgent;
if (clientOptions?.lookup || clientOptions?.checkAddress) {
dispatcherOption = {
...dispatcherOption,
lookup: clientOptions.lookup,
checkAddress: clientOptions.checkAddress,
connect: clientOptions.connect,
allowH2: clientOptions.allowH2,
} as HttpAgentOptions;
dispatcherClazz = HttpAgent as unknown as new (options: BaseAgentOptions) => BaseAgent;
} else if (clientOptions?.connect) {
dispatcherOption = {
...dispatcherOption,
connect: clientOptions.connect,
allowH2: clientOptions.allowH2,
} as HttpAgentOptions;
dispatcherClazz = BaseAgent;
} else if (clientOptions?.allowH2) {
// Support HTTP2
dispatcherOption = {
...dispatcherOption,
allowH2: clientOptions.allowH2,
} as HttpAgentOptions;
dispatcherClazz = BaseAgent;
}
this.#dispatcher = new dispatcherClazz(dispatcherOption);
initDiagnosticsChannel();
}
getDispatcher(): Dispatcher {
return this.#dispatcher ?? getGlobalDispatcher();
}
setDispatcher(dispatcher: Agent): void {
this.#dispatcher = dispatcher;
}
getDispatcherPoolStats(): Record<string, PoolStat> {
const agent = this.getDispatcher();
// origin => Pool Instance
const clients: Map<string, WeakRef<Pool>> | undefined = Reflect.get(agent, undiciSymbols.kClients);
const poolStatsMap: Record<string, PoolStat> = {};
if (!clients) {
return poolStatsMap;
}
for (const [key, ref] of clients) {
const pool = (typeof ref.deref === 'function' ? ref.deref() : ref) as unknown as Pool & { dispatcher: Pool };
// NOTE: pool become to { dispatcher: Pool } in undici@v7
const stats = pool?.stats ?? pool?.dispatcher?.stats;
if (!stats) continue;
poolStatsMap[key] = {
connected: stats.connected,
free: stats.free,
pending: stats.pending,
queued: stats.queued,
running: stats.running,
size: stats.size,
} satisfies PoolStat;
}
return poolStatsMap;
}
static setClientOptions(clientOptions: ClientOptions): void {
FetchFactory.#instance.setClientOptions(clientOptions);
}
static getDispatcherPoolStats(): Record<string, PoolStat> {
return FetchFactory.#instance.getDispatcherPoolStats();
}
async fetch(input: RequestInfo, init?: UrllibRequestInit): Promise<Response> {
const requestStartTime = performance.now();
init = init ?? {};
init.dispatcher = init.dispatcher ?? this.#dispatcher;
const request = new Request(input, init);
const requestId = globalId('HttpClientRequest');
// https://developer.chrome.com/docs/devtools/network/reference/?utm_source=devtools#timing-explanation
const timing = {
// socket assigned
queuing: 0,
// dns lookup time
dnslookup: 0,
// socket connected
connected: 0,
// request headers sent
requestHeadersSent: 0,
// request sent, including headers and body
requestSent: 0,
// Time to first byte (TTFB), the response headers have been received
waiting: 0,
// the response body and trailers have been received
contentDownload: 0,
};
// using opaque to diagnostics channel, binding request and socket
const internalOpaque = {
[symbols.kRequestId]: requestId,
[symbols.kRequestStartTime]: requestStartTime,
[symbols.kEnableRequestTiming]: !!(init.timing ?? true),
[symbols.kRequestTiming]: timing,
// [symbols.kRequestOriginalOpaque]: originalOpaque,
} as FetchOpaque;
const reqMeta: RequestMeta = {
requestId,
url: request.url,
args: {
method: request.method as HttpMethod,
type: request.method as HttpMethod,
data: request.body,
headers: convertHeader(request.headers),
},
retries: 0,
};
const fetchMeta: FetchMeta = {
requestId,
request,
};
const socketInfo: SocketInfo = {
id: 0,
localAddress: '',
localPort: 0,
remoteAddress: '',
remotePort: 0,
remoteFamily: '',
bytesWritten: 0,
bytesRead: 0,
handledRequests: 0,
handledResponses: 0,
};
channels.request.publish({
request: reqMeta,
isSentByFetch: true,
fetchOpaque: internalOpaque,
} as RequestDiagnosticsMessage);
channels.fetchRequest.publish({
fetch: fetchMeta,
fetchOpaque: internalOpaque,
} as FetchDiagnosticsMessage);
let res: Response;
// keep urllib createCallbackResponse style
const resHeaders: IncomingHttpHeaders = {};
const urllibResponse = {
status: -1,
statusCode: -1,
statusText: '',
statusMessage: '',
headers: resHeaders,
size: 0,
aborted: false,
rt: 0,
keepAliveSocket: true,
requestUrls: [request.url],
timing,
socket: socketInfo,
retries: 0,
socketErrorRetries: 0,
} as any as RawResponseWithMeta;
try {
await this.#opaqueLocalStorage.run(internalOpaque, async () => {
res = await UndiciFetch(request);
});
} catch (e: any) {
updateSocketInfo(socketInfo, internalOpaque, e);
urllibResponse.rt = performanceTime(requestStartTime);
debug('Request#%d throw error: %s', requestId, e);
channels.fetchResponse.publish({
fetch: fetchMeta,
error: e,
fetchOpaque: internalOpaque,
} as FetchResponseDiagnosticsMessage);
channels.response.publish({
request: reqMeta,
response: urllibResponse,
error: e,
isSentByFetch: true,
fetchOpaque: internalOpaque,
} as ResponseDiagnosticsMessage);
throw e;
}
// get undici internal response
// Bun's Response doesn't have the same internal state as npm undici's Response
let state: any;
try {
state = getResponseState(res!);
} catch {
state = {};
}
updateSocketInfo(socketInfo, internalOpaque);
urllibResponse.headers = convertHeader(res!.headers);
urllibResponse.status = urllibResponse.statusCode = res!.status;
urllibResponse!.statusMessage = res!.statusText;
if (urllibResponse.headers['content-length']) {
urllibResponse.size = parseInt(urllibResponse.headers['content-length']);
}
urllibResponse.rt = performanceTime(requestStartTime);
debug(
'Request#%d got response, status: %s, headers: %j, timing: %j, socket: %j',
requestId,
urllibResponse.status,
urllibResponse.headers,
timing,
urllibResponse.socket,
);
channels.fetchResponse.publish({
fetch: fetchMeta,
timingInfo: state.timingInfo,
response: res!,
fetchOpaque: internalOpaque,
} as FetchResponseDiagnosticsMessage);
channels.response.publish({
request: reqMeta,
response: urllibResponse,
isSentByFetch: true,
fetchOpaque: internalOpaque,
} as ResponseDiagnosticsMessage);
return res!;
}
static getDispatcher(): Dispatcher {
return FetchFactory.#instance.getDispatcher();
}
static setDispatcher(dispatcher: Agent): void {
FetchFactory.#instance.setDispatcher(dispatcher);
}
static async fetch(input: RequestInfo, init?: UrllibRequestInit): Promise<Response> {
return FetchFactory.#instance.fetch(input, init);
}
}
export const fetch: (input: RequestInfo, init?: UrllibRequestInit) => Promise<Response> = FetchFactory.fetch;