-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathindex.ts
More file actions
128 lines (113 loc) · 3.61 KB
/
Copy pathindex.ts
File metadata and controls
128 lines (113 loc) · 3.61 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
import { LRU } from 'ylru';
import { HttpClient, HEADER_USER_AGENT } from './HttpClient.js';
import type { RequestOptions, RequestURL } from './Request.js';
import type { HttpClientResponse } from './Response.js';
let httpClient: HttpClient;
let allowH2HttpClient: HttpClient;
let allowUnauthorizedHttpClient: HttpClient;
let allowH2AndUnauthorizedHttpClient: HttpClient;
const domainSocketHttpClients = new LRU(50);
export function getDefaultHttpClient(rejectUnauthorized?: boolean, allowH2?: boolean): HttpClient {
if (rejectUnauthorized === false) {
if (allowH2) {
if (!allowH2AndUnauthorizedHttpClient) {
allowH2AndUnauthorizedHttpClient = new HttpClient({
allowH2,
connect: {
rejectUnauthorized,
},
});
}
return allowH2AndUnauthorizedHttpClient;
}
if (!allowUnauthorizedHttpClient) {
allowUnauthorizedHttpClient = new HttpClient({
connect: {
rejectUnauthorized,
},
});
}
return allowUnauthorizedHttpClient;
}
if (allowH2) {
if (!allowH2HttpClient) {
allowH2HttpClient = new HttpClient({
allowH2,
});
}
return allowH2HttpClient;
}
if (!httpClient) {
httpClient = new HttpClient();
}
return httpClient;
}
interface UrllibRequestOptions extends RequestOptions {
/** Allow to use HTTP2 first. Default is `false` */
allowH2?: boolean;
}
export async function request<T = any>(
url: RequestURL,
options?: UrllibRequestOptions,
): Promise<HttpClientResponse<T>> {
if (options?.socketPath) {
let domainSocketHttpclient = domainSocketHttpClients.get<HttpClient>(options.socketPath);
if (!domainSocketHttpclient) {
domainSocketHttpclient = new HttpClient({
connect: { socketPath: options.socketPath },
});
domainSocketHttpClients.set(options.socketPath, domainSocketHttpclient);
}
return await domainSocketHttpclient.request<T>(url, options);
}
return await getDefaultHttpClient(options?.rejectUnauthorized, options?.allowH2).request<T>(url, options);
}
// export curl method is keep compatible with urllib.curl()
// ```ts
// import * as urllib from 'urllib';
// urllib.curl(url);
// ```
export async function curl<T = any>(url: RequestURL, options?: UrllibRequestOptions): Promise<HttpClientResponse<T>> {
return await request<T>(url, options);
}
export {
MockAgent,
ProxyAgent,
Agent,
Dispatcher,
setGlobalDispatcher,
getGlobalDispatcher,
Request,
Response,
Headers,
FormData,
} from 'undici';
export type { RequestInfo, RequestInit, BodyInit, ResponseInit } from 'undici';
// HttpClient2 is keep compatible with urllib@2 HttpClient2
export { HttpClient, HttpClient as HttpClient2, HEADER_USER_AGENT as USER_AGENT } from './HttpClient.js';
export type { RequestDiagnosticsMessage, ResponseDiagnosticsMessage, ClientOptions } from './HttpClient.js';
// RequestOptions2 is keep compatible with urllib@2 RequestOptions2
export type {
RequestOptions,
RequestOptions as RequestOptions2,
RequestURL,
HttpMethod,
FixJSONCtlCharsHandler,
FixJSONCtlChars,
} from './Request.js';
export type { CheckAddressFunction } from './HttpAgent.js';
export type { SocketInfo, Timing, RawResponseWithMeta, HttpClientResponse } from './Response.js';
export type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
export * from './HttpClientError.js';
export { FetchFactory, fetch } from './fetch.js';
export { FormData as WebFormData } from './FormData.js';
const urllib: {
request: typeof request;
curl: typeof curl;
USER_AGENT: string;
} = {
request,
curl,
USER_AGENT: HEADER_USER_AGENT,
};
export default urllib;