urllib 4.8.2 → 4.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -42
- package/dist/commonjs/BaseAgent.d.ts +2 -2
- package/dist/commonjs/BaseAgent.js +1 -1
- package/dist/commonjs/FetchOpaqueInterceptor.d.ts +4 -0
- package/dist/commonjs/FetchOpaqueInterceptor.js +1 -1
- package/dist/commonjs/FormData.js +2 -2
- package/dist/commonjs/HttpAgent.d.ts +3 -2
- package/dist/commonjs/HttpAgent.js +1 -1
- package/dist/commonjs/HttpClient.d.ts +22 -22
- package/dist/commonjs/HttpClient.js +46 -25
- package/dist/commonjs/HttpClientError.d.ts +1 -1
- package/dist/commonjs/IncomingHttpHeaders.d.ts +1 -1
- package/dist/commonjs/Request.d.ts +5 -5
- package/dist/commonjs/diagnosticsChannel.js +3 -4
- package/dist/commonjs/fetch.d.ts +6 -5
- package/dist/commonjs/fetch.js +6 -8
- package/dist/commonjs/index.d.ts +14 -11
- package/dist/commonjs/index.js +3 -2
- package/dist/commonjs/symbols.d.ts +2 -2
- package/dist/commonjs/symbols.js +3 -2
- package/dist/commonjs/utils.d.ts +3 -3
- package/dist/commonjs/utils.js +17 -11
- package/dist/esm/BaseAgent.d.ts +2 -2
- package/dist/esm/BaseAgent.js +2 -2
- package/dist/esm/FetchOpaqueInterceptor.d.ts +4 -0
- package/dist/esm/FetchOpaqueInterceptor.js +1 -1
- package/dist/esm/FormData.js +2 -2
- package/dist/esm/HttpAgent.d.ts +3 -2
- package/dist/esm/HttpAgent.js +1 -1
- package/dist/esm/HttpClient.d.ts +22 -22
- package/dist/esm/HttpClient.js +47 -26
- package/dist/esm/HttpClientError.d.ts +1 -1
- package/dist/esm/IncomingHttpHeaders.d.ts +1 -1
- package/dist/esm/Request.d.ts +5 -5
- package/dist/esm/diagnosticsChannel.js +3 -4
- package/dist/esm/fetch.d.ts +6 -5
- package/dist/esm/fetch.js +7 -9
- package/dist/esm/index.d.ts +14 -11
- package/dist/esm/index.js +4 -3
- package/dist/esm/symbols.d.ts +2 -2
- package/dist/esm/symbols.js +3 -2
- package/dist/esm/utils.d.ts +3 -3
- package/dist/esm/utils.js +17 -11
- package/dist/package.json +1 -1
- package/package.json +92 -73
- package/src/BaseAgent.ts +4 -5
- package/src/FetchOpaqueInterceptor.ts +1 -0
- package/src/FormData.ts +3 -2
- package/src/HttpAgent.ts +9 -9
- package/src/HttpClient.ts +150 -88
- package/src/HttpClientError.ts +1 -1
- package/src/IncomingHttpHeaders.ts +2 -1
- package/src/Request.ts +15 -7
- package/src/Response.ts +1 -0
- package/src/diagnosticsChannel.ts +55 -21
- package/src/fetch.ts +36 -44
- package/src/formstream.d.ts +5 -1
- package/src/index.ts +38 -24
- package/src/symbols.ts +24 -1
- package/src/utils.ts +34 -26
package/src/fetch.ts
CHANGED
|
@@ -1,46 +1,34 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
import { debuglog } from 'node:util';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
RequestInit,
|
|
7
|
-
Request,
|
|
8
|
-
Response,
|
|
9
|
-
Agent,
|
|
10
|
-
getGlobalDispatcher,
|
|
11
|
-
Pool,
|
|
12
|
-
Dispatcher,
|
|
13
|
-
} from 'undici';
|
|
3
|
+
|
|
4
|
+
import { fetch as UndiciFetch, Request, Response, Agent, getGlobalDispatcher, Pool, Dispatcher } from 'undici';
|
|
5
|
+
import type { RequestInfo, RequestInit } from 'undici';
|
|
14
6
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
15
7
|
// @ts-ignore
|
|
16
8
|
import undiciSymbols from 'undici/lib/core/symbols.js';
|
|
17
9
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
18
10
|
// @ts-ignore
|
|
19
11
|
import { getResponseState } from 'undici/lib/web/fetch/response.js';
|
|
20
|
-
|
|
21
|
-
|
|
12
|
+
|
|
13
|
+
import { BaseAgent } from './BaseAgent.js';
|
|
14
|
+
import type { BaseAgentOptions } from './BaseAgent.js';
|
|
15
|
+
import { initDiagnosticsChannel } from './diagnosticsChannel.js';
|
|
16
|
+
import type { FetchOpaque } from './FetchOpaqueInterceptor.js';
|
|
17
|
+
import { HttpAgent } from './HttpAgent.js';
|
|
18
|
+
import type { HttpAgentOptions } from './HttpAgent.js';
|
|
19
|
+
import { channels } from './HttpClient.js';
|
|
20
|
+
import type {
|
|
22
21
|
ClientOptions,
|
|
23
22
|
PoolStat,
|
|
24
23
|
RequestDiagnosticsMessage,
|
|
25
24
|
ResponseDiagnosticsMessage,
|
|
26
25
|
UndiciTimingInfo,
|
|
27
26
|
} from './HttpClient.js';
|
|
28
|
-
import {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
} from './HttpAgent.js';
|
|
32
|
-
import { initDiagnosticsChannel } from './diagnosticsChannel.js';
|
|
33
|
-
import { convertHeader, globalId, performanceTime, updateSocketInfo } from './utils.js';
|
|
27
|
+
import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
|
|
28
|
+
import type { FetchMeta, HttpMethod, RequestMeta } from './Request.js';
|
|
29
|
+
import type { RawResponseWithMeta, SocketInfo } from './Response.js';
|
|
34
30
|
import symbols from './symbols.js';
|
|
35
|
-
import {
|
|
36
|
-
FetchMeta,
|
|
37
|
-
HttpMethod,
|
|
38
|
-
RequestMeta,
|
|
39
|
-
} from './Request.js';
|
|
40
|
-
import { FetchOpaque } from './FetchOpaqueInterceptor.js';
|
|
41
|
-
import { RawResponseWithMeta, SocketInfo } from './Response.js';
|
|
42
|
-
import { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
|
|
43
|
-
import { BaseAgent, BaseAgentOptions } from './BaseAgent.js';
|
|
31
|
+
import { convertHeader, globalId, performanceTime, updateSocketInfo } from './utils.js';
|
|
44
32
|
|
|
45
33
|
const debug = debuglog('urllib/fetch');
|
|
46
34
|
|
|
@@ -68,7 +56,7 @@ export class FetchFactory {
|
|
|
68
56
|
|
|
69
57
|
static #instance = new FetchFactory();
|
|
70
58
|
|
|
71
|
-
setClientOptions(clientOptions: ClientOptions) {
|
|
59
|
+
setClientOptions(clientOptions: ClientOptions): void {
|
|
72
60
|
let dispatcherOption: BaseAgentOptions = {
|
|
73
61
|
opaqueLocalStorage: this.#opaqueLocalStorage,
|
|
74
62
|
};
|
|
@@ -101,15 +89,15 @@ export class FetchFactory {
|
|
|
101
89
|
initDiagnosticsChannel();
|
|
102
90
|
}
|
|
103
91
|
|
|
104
|
-
getDispatcher() {
|
|
92
|
+
getDispatcher(): Dispatcher {
|
|
105
93
|
return this.#dispatcher ?? getGlobalDispatcher();
|
|
106
94
|
}
|
|
107
95
|
|
|
108
|
-
setDispatcher(dispatcher: Agent) {
|
|
96
|
+
setDispatcher(dispatcher: Agent): void {
|
|
109
97
|
this.#dispatcher = dispatcher;
|
|
110
98
|
}
|
|
111
99
|
|
|
112
|
-
getDispatcherPoolStats() {
|
|
100
|
+
getDispatcherPoolStats(): Record<string, PoolStat> {
|
|
113
101
|
const agent = this.getDispatcher();
|
|
114
102
|
// origin => Pool Instance
|
|
115
103
|
const clients: Map<string, WeakRef<Pool>> | undefined = Reflect.get(agent, undiciSymbols.kClients);
|
|
@@ -117,8 +105,8 @@ export class FetchFactory {
|
|
|
117
105
|
if (!clients) {
|
|
118
106
|
return poolStatsMap;
|
|
119
107
|
}
|
|
120
|
-
for (const [
|
|
121
|
-
const pool = (typeof ref.deref === 'function' ? ref.deref() : ref) as unknown as
|
|
108
|
+
for (const [key, ref] of clients) {
|
|
109
|
+
const pool = (typeof ref.deref === 'function' ? ref.deref() : ref) as unknown as Pool & { dispatcher: Pool };
|
|
122
110
|
// NOTE: pool become to { dispatcher: Pool } in undici@v7
|
|
123
111
|
const stats = pool?.stats ?? pool?.dispatcher?.stats;
|
|
124
112
|
if (!stats) continue;
|
|
@@ -135,11 +123,11 @@ export class FetchFactory {
|
|
|
135
123
|
return poolStatsMap;
|
|
136
124
|
}
|
|
137
125
|
|
|
138
|
-
static setClientOptions(clientOptions: ClientOptions) {
|
|
126
|
+
static setClientOptions(clientOptions: ClientOptions): void {
|
|
139
127
|
FetchFactory.#instance.setClientOptions(clientOptions);
|
|
140
128
|
}
|
|
141
129
|
|
|
142
|
-
static getDispatcherPoolStats() {
|
|
130
|
+
static getDispatcherPoolStats(): Record<string, PoolStat> {
|
|
143
131
|
return FetchFactory.#instance.getDispatcherPoolStats();
|
|
144
132
|
}
|
|
145
133
|
|
|
@@ -225,9 +213,7 @@ export class FetchFactory {
|
|
|
225
213
|
aborted: false,
|
|
226
214
|
rt: 0,
|
|
227
215
|
keepAliveSocket: true,
|
|
228
|
-
requestUrls: [
|
|
229
|
-
request.url,
|
|
230
|
-
],
|
|
216
|
+
requestUrls: [request.url],
|
|
231
217
|
timing,
|
|
232
218
|
socket: socketInfo,
|
|
233
219
|
retries: 0,
|
|
@@ -267,8 +253,14 @@ export class FetchFactory {
|
|
|
267
253
|
urllibResponse.size = parseInt(urllibResponse.headers['content-length']);
|
|
268
254
|
}
|
|
269
255
|
urllibResponse.rt = performanceTime(requestStartTime);
|
|
270
|
-
debug(
|
|
271
|
-
|
|
256
|
+
debug(
|
|
257
|
+
'Request#%d got response, status: %s, headers: %j, timing: %j, socket: %j',
|
|
258
|
+
requestId,
|
|
259
|
+
urllibResponse.status,
|
|
260
|
+
urllibResponse.headers,
|
|
261
|
+
timing,
|
|
262
|
+
urllibResponse.socket,
|
|
263
|
+
);
|
|
272
264
|
channels.fetchResponse.publish({
|
|
273
265
|
fetch: fetchMeta,
|
|
274
266
|
timingInfo: state.timingInfo,
|
|
@@ -284,11 +276,11 @@ export class FetchFactory {
|
|
|
284
276
|
return res!;
|
|
285
277
|
}
|
|
286
278
|
|
|
287
|
-
static getDispatcher() {
|
|
279
|
+
static getDispatcher(): Dispatcher {
|
|
288
280
|
return FetchFactory.#instance.getDispatcher();
|
|
289
281
|
}
|
|
290
282
|
|
|
291
|
-
static setDispatcher(dispatcher: Agent) {
|
|
283
|
+
static setDispatcher(dispatcher: Agent): void {
|
|
292
284
|
FetchFactory.#instance.setDispatcher(dispatcher);
|
|
293
285
|
}
|
|
294
286
|
|
|
@@ -297,4 +289,4 @@ export class FetchFactory {
|
|
|
297
289
|
}
|
|
298
290
|
}
|
|
299
291
|
|
|
300
|
-
export const fetch = FetchFactory.fetch;
|
|
292
|
+
export const fetch: (input: RequestInfo, init?: UrllibRequestInit) => Promise<Response> = FetchFactory.fetch;
|
package/src/formstream.d.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { LRU } from 'ylru';
|
|
2
|
-
import { patchForNode16 } from './utils.js';
|
|
3
2
|
|
|
3
|
+
import { patchForNode16 } from './utils.js';
|
|
4
4
|
|
|
5
5
|
patchForNode16();
|
|
6
6
|
|
|
7
7
|
import { HttpClient, HEADER_USER_AGENT } from './HttpClient.js';
|
|
8
|
-
import { RequestOptions, RequestURL } from './Request.js';
|
|
8
|
+
import type { RequestOptions, RequestURL } from './Request.js';
|
|
9
|
+
import type { HttpClientResponse } from './Response.js';
|
|
9
10
|
|
|
10
11
|
let httpClient: HttpClient;
|
|
11
12
|
let allowH2HttpClient: HttpClient;
|
|
@@ -63,7 +64,10 @@ interface UrllibRequestOptions extends RequestOptions {
|
|
|
63
64
|
allowH2?: boolean;
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
export async function request<T = any>(
|
|
67
|
+
export async function request<T = any>(
|
|
68
|
+
url: RequestURL,
|
|
69
|
+
options?: UrllibRequestOptions,
|
|
70
|
+
): Promise<HttpClientResponse<T>> {
|
|
67
71
|
if (options?.socketPath) {
|
|
68
72
|
let domainSocketHttpclient = domainSocketHttpClients.get<HttpClient>(options.socketPath);
|
|
69
73
|
if (!domainSocketHttpclient) {
|
|
@@ -83,42 +87,52 @@ export async function request<T = any>(url: RequestURL, options?: UrllibRequestO
|
|
|
83
87
|
// import * as urllib from 'urllib';
|
|
84
88
|
// urllib.curl(url);
|
|
85
89
|
// ```
|
|
86
|
-
export async function curl<T = any>(url: RequestURL, options?: UrllibRequestOptions) {
|
|
90
|
+
export async function curl<T = any>(url: RequestURL, options?: UrllibRequestOptions): Promise<HttpClientResponse<T>> {
|
|
87
91
|
return await request<T>(url, options);
|
|
88
92
|
}
|
|
89
93
|
|
|
90
94
|
export {
|
|
91
|
-
MockAgent,
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
95
|
+
MockAgent,
|
|
96
|
+
ProxyAgent,
|
|
97
|
+
Agent,
|
|
98
|
+
Dispatcher,
|
|
99
|
+
setGlobalDispatcher,
|
|
100
|
+
getGlobalDispatcher,
|
|
101
|
+
Request,
|
|
102
|
+
Response,
|
|
103
|
+
Headers,
|
|
104
|
+
FormData,
|
|
96
105
|
} from 'undici';
|
|
106
|
+
export type { RequestInfo, RequestInit, BodyInit, ResponseInit } from 'undici';
|
|
97
107
|
// HttpClient2 is keep compatible with urllib@2 HttpClient2
|
|
98
|
-
export {
|
|
99
|
-
|
|
100
|
-
RequestDiagnosticsMessage, ResponseDiagnosticsMessage, ClientOptions,
|
|
101
|
-
} from './HttpClient.js';
|
|
108
|
+
export { HttpClient, HttpClient as HttpClient2, HEADER_USER_AGENT as USER_AGENT } from './HttpClient.js';
|
|
109
|
+
export type { RequestDiagnosticsMessage, ResponseDiagnosticsMessage, ClientOptions } from './HttpClient.js';
|
|
102
110
|
// RequestOptions2 is keep compatible with urllib@2 RequestOptions2
|
|
103
|
-
export {
|
|
104
|
-
RequestOptions,
|
|
105
|
-
|
|
111
|
+
export type {
|
|
112
|
+
RequestOptions,
|
|
113
|
+
RequestOptions as RequestOptions2,
|
|
114
|
+
RequestURL,
|
|
115
|
+
HttpMethod,
|
|
116
|
+
FixJSONCtlCharsHandler,
|
|
117
|
+
FixJSONCtlChars,
|
|
106
118
|
} from './Request.js';
|
|
107
119
|
|
|
108
|
-
export { CheckAddressFunction } from './HttpAgent.js';
|
|
120
|
+
export type { CheckAddressFunction } from './HttpAgent.js';
|
|
109
121
|
|
|
110
|
-
export {
|
|
111
|
-
|
|
112
|
-
} from './Response.js';
|
|
113
|
-
export {
|
|
114
|
-
IncomingHttpHeaders,
|
|
115
|
-
} from './IncomingHttpHeaders.js';
|
|
122
|
+
export type { SocketInfo, Timing, RawResponseWithMeta, HttpClientResponse } from './Response.js';
|
|
123
|
+
export type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
|
|
116
124
|
export * from './HttpClientError.js';
|
|
117
125
|
export { FetchFactory, fetch } from './fetch.js';
|
|
118
126
|
export { FormData as WebFormData } from './FormData.js';
|
|
119
127
|
|
|
120
|
-
|
|
128
|
+
const urllib: {
|
|
129
|
+
request: typeof request;
|
|
130
|
+
curl: typeof curl;
|
|
131
|
+
USER_AGENT: string;
|
|
132
|
+
} = {
|
|
121
133
|
request,
|
|
122
134
|
curl,
|
|
123
135
|
USER_AGENT: HEADER_USER_AGENT,
|
|
124
136
|
};
|
|
137
|
+
|
|
138
|
+
export default urllib;
|
package/src/symbols.ts
CHANGED
|
@@ -1,4 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
const symbols: {
|
|
2
|
+
kSocketId: symbol;
|
|
3
|
+
kSocketStartTime: symbol;
|
|
4
|
+
kSocketConnectedTime: symbol;
|
|
5
|
+
kSocketConnectErrorTime: symbol;
|
|
6
|
+
kSocketRequestEndTime: symbol;
|
|
7
|
+
kSocketLocalAddress: symbol;
|
|
8
|
+
kSocketLocalPort: symbol;
|
|
9
|
+
kSocketConnectHost: symbol;
|
|
10
|
+
kSocketConnectPort: symbol;
|
|
11
|
+
kSocketConnectProtocol: symbol;
|
|
12
|
+
kHandledRequests: symbol;
|
|
13
|
+
kHandledResponses: symbol;
|
|
14
|
+
kRequestSocket: symbol;
|
|
15
|
+
kRequestId: symbol;
|
|
16
|
+
kRequestStartTime: symbol;
|
|
17
|
+
kEnableRequestTiming: symbol;
|
|
18
|
+
kRequestTiming: symbol;
|
|
19
|
+
kRequestOriginalOpaque: symbol;
|
|
20
|
+
kRequestInternalOpaque: symbol;
|
|
21
|
+
kErrorSocket: symbol;
|
|
22
|
+
} = {
|
|
2
23
|
kSocketId: Symbol('socket id'),
|
|
3
24
|
kSocketStartTime: Symbol('socket start time'),
|
|
4
25
|
kSocketConnectedTime: Symbol('socket connected time'),
|
|
@@ -20,3 +41,5 @@ export default {
|
|
|
20
41
|
kRequestInternalOpaque: Symbol('request internal opaque'),
|
|
21
42
|
kErrorSocket: Symbol('socket of error'),
|
|
22
43
|
};
|
|
44
|
+
|
|
45
|
+
export default symbols;
|
package/src/utils.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
import { Blob, File } from 'node:buffer';
|
|
1
2
|
import { randomBytes, createHash } from 'node:crypto';
|
|
2
|
-
import { Readable } from 'node:stream';
|
|
3
3
|
import { performance } from 'node:perf_hooks';
|
|
4
|
+
import { Readable } from 'node:stream';
|
|
4
5
|
import { ReadableStream, TransformStream } from 'node:stream/web';
|
|
5
|
-
import { Blob, File } from 'node:buffer';
|
|
6
6
|
import { toUSVString } from 'node:util';
|
|
7
|
+
|
|
8
|
+
import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
|
|
7
9
|
import type { FixJSONCtlChars } from './Request.js';
|
|
8
|
-
import { SocketInfo } from './Response.js';
|
|
10
|
+
import type { SocketInfo } from './Response.js';
|
|
9
11
|
import symbols from './symbols.js';
|
|
10
|
-
import { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
|
|
11
12
|
|
|
12
13
|
const JSONCtlCharsMap: Record<string, string> = {
|
|
13
14
|
'"': '\\"', // \u0022
|
|
@@ -29,7 +30,7 @@ function replaceJSONCtlChars(value: string) {
|
|
|
29
30
|
return value.replace(JSONCtlCharsRE, replaceOneChar);
|
|
30
31
|
}
|
|
31
32
|
|
|
32
|
-
export function parseJSON(data: string, fixJSONCtlChars?: FixJSONCtlChars) {
|
|
33
|
+
export function parseJSON(data: string, fixJSONCtlChars?: FixJSONCtlChars): unknown {
|
|
33
34
|
if (typeof fixJSONCtlChars === 'function') {
|
|
34
35
|
data = fixJSONCtlChars(data);
|
|
35
36
|
} else if (fixJSONCtlChars) {
|
|
@@ -45,8 +46,12 @@ export function parseJSON(data: string, fixJSONCtlChars?: FixJSONCtlChars) {
|
|
|
45
46
|
}
|
|
46
47
|
if (data.length > 1024) {
|
|
47
48
|
// show 0~512 ... -512~end data
|
|
48
|
-
err.message +=
|
|
49
|
-
|
|
49
|
+
err.message +=
|
|
50
|
+
' (data json format: ' +
|
|
51
|
+
JSON.stringify(data.slice(0, 512)) +
|
|
52
|
+
' ...skip... ' +
|
|
53
|
+
JSON.stringify(data.slice(data.length - 512)) +
|
|
54
|
+
')';
|
|
50
55
|
} else {
|
|
51
56
|
err.message += ' (data json format: ' + JSON.stringify(data) + ')';
|
|
52
57
|
}
|
|
@@ -55,7 +60,7 @@ export function parseJSON(data: string, fixJSONCtlChars?: FixJSONCtlChars) {
|
|
|
55
60
|
return data;
|
|
56
61
|
}
|
|
57
62
|
|
|
58
|
-
function md5(s: string) {
|
|
63
|
+
function md5(s: string): string {
|
|
59
64
|
const sum = createHash('md5');
|
|
60
65
|
sum.update(s, 'utf8');
|
|
61
66
|
return sum.digest('hex');
|
|
@@ -65,7 +70,7 @@ const AUTH_KEY_VALUE_RE = /(\w{1,100})=["']?([^'"]+)["']?/;
|
|
|
65
70
|
let NC = 0;
|
|
66
71
|
const NC_PAD = '00000000';
|
|
67
72
|
|
|
68
|
-
export function digestAuthHeader(method: string, uri: string, wwwAuthenticate: string, userpass: string) {
|
|
73
|
+
export function digestAuthHeader(method: string, uri: string, wwwAuthenticate: string, userpass: string): string {
|
|
69
74
|
// WWW-Authenticate: Digest realm="testrealm@host.com",
|
|
70
75
|
// qop="auth,auth-int",
|
|
71
76
|
// nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
|
@@ -134,31 +139,33 @@ export function digestAuthHeader(method: string, uri: string, wwwAuthenticate: s
|
|
|
134
139
|
const MAX_ID_VALUE = Math.pow(2, 31) - 10;
|
|
135
140
|
const globalIds: Record<string, number> = {};
|
|
136
141
|
|
|
137
|
-
export function globalId(category: string) {
|
|
142
|
+
export function globalId(category: string): number {
|
|
138
143
|
if (!globalIds[category] || globalIds[category] >= MAX_ID_VALUE) {
|
|
139
144
|
globalIds[category] = 0;
|
|
140
145
|
}
|
|
141
146
|
return ++globalIds[category];
|
|
142
147
|
}
|
|
143
148
|
|
|
144
|
-
export function performanceTime(startTime: number, now?: number) {
|
|
149
|
+
export function performanceTime(startTime: number, now?: number): number {
|
|
145
150
|
return Math.floor(((now ?? performance.now()) - startTime) * 1000) / 1000;
|
|
146
151
|
}
|
|
147
152
|
|
|
148
|
-
export function isReadable(stream: any) {
|
|
153
|
+
export function isReadable(stream: any): boolean {
|
|
149
154
|
if (typeof Readable.isReadable === 'function') return Readable.isReadable(stream);
|
|
150
155
|
// patch from node
|
|
151
156
|
// https://github.com/nodejs/node/blob/1287530385137dda1d44975063217ccf90759475/lib/internal/streams/utils.js#L119
|
|
152
157
|
// simple way https://github.com/sindresorhus/is-stream/blob/main/index.js
|
|
153
|
-
return
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
158
|
+
return (
|
|
159
|
+
stream !== null &&
|
|
160
|
+
typeof stream === 'object' &&
|
|
161
|
+
typeof stream.pipe === 'function' &&
|
|
162
|
+
stream.readable !== false &&
|
|
163
|
+
typeof stream._read === 'function' &&
|
|
164
|
+
typeof stream._readableState === 'object'
|
|
165
|
+
);
|
|
159
166
|
}
|
|
160
167
|
|
|
161
|
-
export function updateSocketInfo(socketInfo: SocketInfo, internalOpaque: any, err?: any) {
|
|
168
|
+
export function updateSocketInfo(socketInfo: SocketInfo, internalOpaque: any, err?: any): void {
|
|
162
169
|
const socket = internalOpaque[symbols.kRequestSocket] ?? err?.[symbols.kErrorSocket];
|
|
163
170
|
|
|
164
171
|
if (socket) {
|
|
@@ -197,10 +204,10 @@ export function updateSocketInfo(socketInfo: SocketInfo, internalOpaque: any, er
|
|
|
197
204
|
|
|
198
205
|
export function convertHeader(headers: Headers): IncomingHttpHeaders {
|
|
199
206
|
const res: IncomingHttpHeaders = {};
|
|
200
|
-
for (const [
|
|
207
|
+
for (const [key, value] of headers.entries()) {
|
|
201
208
|
if (res[key]) {
|
|
202
209
|
if (!Array.isArray(res[key])) {
|
|
203
|
-
res[key] = [
|
|
210
|
+
res[key] = [res[key]];
|
|
204
211
|
}
|
|
205
212
|
res[key].push(value);
|
|
206
213
|
} else {
|
|
@@ -211,7 +218,7 @@ export function convertHeader(headers: Headers): IncomingHttpHeaders {
|
|
|
211
218
|
}
|
|
212
219
|
|
|
213
220
|
// support require from Node.js 16
|
|
214
|
-
export function patchForNode16() {
|
|
221
|
+
export function patchForNode16(): void {
|
|
215
222
|
if (typeof global.ReadableStream === 'undefined') {
|
|
216
223
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
217
224
|
// @ts-ignore
|
|
@@ -244,14 +251,15 @@ export function patchForNode16() {
|
|
|
244
251
|
if (String.prototype.toWellFormed === undefined) {
|
|
245
252
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
246
253
|
// @ts-ignore
|
|
254
|
+
// eslint-disable-next-line no-extend-native
|
|
247
255
|
Object.defineProperty(String.prototype, 'toWellFormed', {
|
|
248
|
-
value: function() {
|
|
256
|
+
value: function () {
|
|
249
257
|
return toUSVString(this);
|
|
250
258
|
},
|
|
251
259
|
enumerable: false,
|
|
252
260
|
configurable: true,
|
|
253
261
|
writable: true,
|
|
254
|
-
})
|
|
262
|
+
});
|
|
255
263
|
}
|
|
256
264
|
|
|
257
265
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
@@ -259,8 +267,9 @@ export function patchForNode16() {
|
|
|
259
267
|
if (String.prototype.isWellFormed === undefined) {
|
|
260
268
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
261
269
|
// @ts-ignore
|
|
270
|
+
// eslint-disable-next-line no-extend-native
|
|
262
271
|
Object.defineProperty(String.prototype, 'isWellFormed', {
|
|
263
|
-
value: function() {
|
|
272
|
+
value: function () {
|
|
264
273
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
265
274
|
// @ts-ignore
|
|
266
275
|
return toUSVString(this) === this;
|
|
@@ -270,7 +279,6 @@ export function patchForNode16() {
|
|
|
270
279
|
writable: true,
|
|
271
280
|
});
|
|
272
281
|
}
|
|
273
|
-
|
|
274
282
|
}
|
|
275
283
|
|
|
276
284
|
// https://github.com/jimmywarting/node-domexception/blob/main/index.js
|