@npy/fetch 0.1.0 → 0.1.2
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/_internal/consts.cjs +4 -0
- package/_internal/consts.d.cts +3 -0
- package/_internal/consts.d.ts +3 -0
- package/_internal/consts.js +4 -0
- package/_internal/decode-stream-error.cjs +18 -0
- package/_internal/decode-stream-error.d.cts +11 -0
- package/_internal/decode-stream-error.d.ts +11 -0
- package/_internal/decode-stream-error.js +18 -0
- package/_internal/error-mapping.cjs +44 -0
- package/_internal/error-mapping.d.cts +15 -0
- package/_internal/error-mapping.d.ts +15 -0
- package/_internal/error-mapping.js +41 -0
- package/_internal/guards.cjs +23 -0
- package/_internal/guards.d.cts +15 -0
- package/_internal/guards.d.ts +15 -0
- package/_internal/guards.js +15 -0
- package/_internal/net.cjs +95 -0
- package/_internal/net.d.cts +11 -0
- package/_internal/net.d.ts +11 -0
- package/_internal/net.js +92 -0
- package/_internal/promises.cjs +18 -0
- package/_internal/promises.d.cts +1 -0
- package/_internal/promises.d.ts +1 -0
- package/_internal/promises.js +18 -0
- package/_internal/streams.cjs +37 -0
- package/_internal/streams.d.cts +21 -0
- package/_internal/streams.d.ts +21 -0
- package/_internal/streams.js +36 -0
- package/_internal/symbols.cjs +4 -0
- package/_internal/symbols.d.cts +1 -0
- package/_internal/symbols.d.ts +1 -0
- package/_internal/symbols.js +4 -0
- package/_virtual/_rolldown/runtime.cjs +23 -0
- package/agent-pool.cjs +96 -0
- package/agent-pool.d.cts +2 -0
- package/agent-pool.d.ts +2 -0
- package/agent-pool.js +95 -0
- package/agent.cjs +260 -0
- package/agent.d.cts +3 -0
- package/agent.d.ts +3 -0
- package/agent.js +259 -0
- package/body.cjs +105 -0
- package/body.d.cts +12 -0
- package/body.d.ts +12 -0
- package/body.js +102 -0
- package/dialers/index.d.cts +3 -0
- package/dialers/index.d.ts +3 -0
- package/dialers/proxy.cjs +56 -0
- package/dialers/proxy.d.cts +27 -0
- package/dialers/proxy.d.ts +27 -0
- package/dialers/proxy.js +55 -0
- package/dialers/tcp.cjs +92 -0
- package/dialers/tcp.d.cts +57 -0
- package/dialers/tcp.d.ts +57 -0
- package/dialers/tcp.js +89 -0
- package/encoding.cjs +114 -0
- package/encoding.d.cts +35 -0
- package/encoding.d.ts +35 -0
- package/encoding.js +110 -0
- package/errors.cjs +275 -0
- package/errors.d.cts +110 -0
- package/errors.d.ts +110 -0
- package/errors.js +259 -0
- package/fetch.cjs +353 -0
- package/fetch.d.cts +58 -0
- package/fetch.d.ts +58 -0
- package/fetch.js +350 -0
- package/http-client.cjs +75 -0
- package/http-client.d.cts +39 -0
- package/http-client.d.ts +39 -0
- package/http-client.js +75 -0
- package/index.cjs +49 -0
- package/index.d.cts +14 -0
- package/index.d.ts +14 -0
- package/index.js +11 -0
- package/io/_utils.cjs +56 -0
- package/io/_utils.d.cts +10 -0
- package/io/_utils.d.ts +10 -0
- package/io/_utils.js +51 -0
- package/io/buf-writer.cjs +149 -0
- package/io/buf-writer.d.cts +13 -0
- package/io/buf-writer.d.ts +13 -0
- package/io/buf-writer.js +148 -0
- package/io/io.cjs +199 -0
- package/io/io.d.cts +5 -0
- package/io/io.d.ts +5 -0
- package/io/io.js +198 -0
- package/io/readers.cjs +337 -0
- package/io/readers.d.cts +69 -0
- package/io/readers.d.ts +69 -0
- package/io/readers.js +333 -0
- package/io/writers.cjs +196 -0
- package/io/writers.d.cts +22 -0
- package/io/writers.d.ts +22 -0
- package/io/writers.js +195 -0
- package/package.json +23 -10
- package/types/agent.d.cts +72 -0
- package/types/agent.d.ts +72 -0
- package/types/dialer.d.cts +30 -0
- package/types/dialer.d.ts +30 -0
- package/types/index.d.cts +2 -0
- package/types/index.d.ts +2 -0
package/fetch.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ProxyInfo } from '@npy/proxy-kit';
|
|
2
|
+
import { BodyInit as FetchBodyInit } from './body';
|
|
3
|
+
import { HttpClientOptions, HttpClient } from './http-client';
|
|
4
|
+
export type FetchProxyInput = string | ProxyInfo | null;
|
|
5
|
+
export interface RequestInit extends Omit<globalThis.RequestInit, "body" | "headers"> {
|
|
6
|
+
body?: FetchBodyInit | null;
|
|
7
|
+
headers?: HeadersInit;
|
|
8
|
+
client?: HttpClient;
|
|
9
|
+
proxy?: FetchProxyInput;
|
|
10
|
+
}
|
|
11
|
+
export type FetchRequestInit = RequestInit;
|
|
12
|
+
export interface FetchOptions extends HttpClientOptions {
|
|
13
|
+
}
|
|
14
|
+
export interface FetchLike {
|
|
15
|
+
(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
|
|
16
|
+
close(): Promise<void>;
|
|
17
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
18
|
+
readonly client: HttpClient;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Normalizes any supported header input into a {@link Headers} instance.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* If the input is already a {@link Headers} object, the same instance is returned.
|
|
25
|
+
* Tuple arrays and plain records are copied into a new {@link Headers}.
|
|
26
|
+
*/
|
|
27
|
+
export declare function normalizeHeaders(headers?: HeadersInit): Headers;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a fetch-compatible client backed by {@link HttpClient}.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* The returned function follows the standard fetch shape, but uses this library's
|
|
33
|
+
* connection pooling, proxy support and body/error mapping rules.
|
|
34
|
+
*
|
|
35
|
+
* When no client is provided, an internal {@link HttpClient} is created and owned
|
|
36
|
+
* by the returned fetch-like function. Calling {@link FetchLike.close} closes that
|
|
37
|
+
* internal client and any proxy-specific clients created on demand.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const fetchLike = createFetch();
|
|
42
|
+
* const response = await fetchLike("https://httpbin.org/anything");
|
|
43
|
+
* const data = await response.json();
|
|
44
|
+
* await fetchLike.close();
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function createFetch(client?: HttpClient): FetchLike;
|
|
48
|
+
export type { HttpClientOptions, ProxyInfo };
|
|
49
|
+
export { HttpClient };
|
|
50
|
+
/**
|
|
51
|
+
* Default fetch-compatible client created with {@link createFetch}.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* This singleton owns its internal {@link HttpClient}. Call {@link FetchLike.close}
|
|
55
|
+
* when you want to release pooled connections explicitly.
|
|
56
|
+
*/
|
|
57
|
+
export declare const fetch: FetchLike;
|
|
58
|
+
export default fetch;
|
package/fetch.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { toWebBodyReadError, toWebFetchError } from "./_internal/error-mapping.js";
|
|
2
|
+
import { bodyErrorMapperSymbol } from "./_internal/symbols.js";
|
|
3
|
+
import { isBlob, isFormData, isIterable, isMultipartFormDataStream, isReadable, isReadableStream, isURLSearchParameters } from "./_internal/guards.js";
|
|
4
|
+
import { fromRequestBody } from "./body.js";
|
|
5
|
+
import { ProxyDialer } from "./dialers/proxy.js";
|
|
6
|
+
import { AutoDialer } from "./dialers/tcp.js";
|
|
7
|
+
import { HttpClient } from "./http-client.js";
|
|
8
|
+
import { parse, stringify } from "@npy/proxy-kit";
|
|
9
|
+
//#region src/fetch.ts
|
|
10
|
+
var MAX_REDIRECTS = 20;
|
|
11
|
+
var REDIRECT_STATUSES = new Set([
|
|
12
|
+
301,
|
|
13
|
+
302,
|
|
14
|
+
303,
|
|
15
|
+
307,
|
|
16
|
+
308
|
|
17
|
+
]);
|
|
18
|
+
var SENSITIVE_REDIRECT_HEADERS = [
|
|
19
|
+
"authorization",
|
|
20
|
+
"cookie",
|
|
21
|
+
"proxy-authorization"
|
|
22
|
+
];
|
|
23
|
+
var BODY_HEADERS = [
|
|
24
|
+
"content-encoding",
|
|
25
|
+
"content-language",
|
|
26
|
+
"content-length",
|
|
27
|
+
"content-location",
|
|
28
|
+
"content-type",
|
|
29
|
+
"transfer-encoding"
|
|
30
|
+
];
|
|
31
|
+
function createDefaultHttpClient(options = {}) {
|
|
32
|
+
return new HttpClient({
|
|
33
|
+
...options,
|
|
34
|
+
dialer: options.dialer ?? new AutoDialer()
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Normalizes any supported header input into a {@link Headers} instance.
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* If the input is already a {@link Headers} object, the same instance is returned.
|
|
42
|
+
* Tuple arrays and plain records are copied into a new {@link Headers}.
|
|
43
|
+
*/
|
|
44
|
+
function normalizeHeaders(headers) {
|
|
45
|
+
if (headers instanceof Headers) return headers;
|
|
46
|
+
const result = new Headers();
|
|
47
|
+
if (!headers) return result;
|
|
48
|
+
if (Array.isArray(headers)) {
|
|
49
|
+
for (const [key, value] of headers) result.append(key, value);
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
for (const [key, value] of Object.entries(headers)) if (Array.isArray(value)) for (const entry of value) result.append(key, entry);
|
|
53
|
+
else if (value !== void 0) result.append(key, value);
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
function resolveUrl(input) {
|
|
57
|
+
if (input instanceof URL) return input;
|
|
58
|
+
return new URL(input instanceof Request ? input.url : String(input));
|
|
59
|
+
}
|
|
60
|
+
function resolveMethod(input, init) {
|
|
61
|
+
return (init.method ?? (input instanceof Request ? input.method : void 0))?.toUpperCase().trim() ?? "GET";
|
|
62
|
+
}
|
|
63
|
+
function resolveHeaders(input, init) {
|
|
64
|
+
return normalizeHeaders(init.headers !== void 0 ? init.headers : input instanceof Request ? input.headers : void 0);
|
|
65
|
+
}
|
|
66
|
+
function resolveSignal(input, init) {
|
|
67
|
+
return init.signal ?? (input instanceof Request ? input.signal : void 0);
|
|
68
|
+
}
|
|
69
|
+
function resolveBody(input, init) {
|
|
70
|
+
if (init.body !== void 0) return init.body;
|
|
71
|
+
return input instanceof Request ? fromRequestBody(input) : void 0;
|
|
72
|
+
}
|
|
73
|
+
function resolveRedirectMode(input, init) {
|
|
74
|
+
return init.redirect ?? (input instanceof Request ? input.redirect : "follow");
|
|
75
|
+
}
|
|
76
|
+
function getBodyReplaySource(input, init, method, body, redirect) {
|
|
77
|
+
if (redirect !== "follow" || init.body !== void 0 || body == null || method === "GET" || method === "HEAD" || !(input instanceof Request)) return;
|
|
78
|
+
return input.clone();
|
|
79
|
+
}
|
|
80
|
+
function assertValidFetchUrl(url) {
|
|
81
|
+
if (url.username || url.password) throw new TypeError("Request URL must not include embedded credentials");
|
|
82
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") throw new TypeError(`fetch failed: unsupported scheme ${url.protocol}`);
|
|
83
|
+
}
|
|
84
|
+
function assertValidFetchBody(method, body) {
|
|
85
|
+
if (body == null) return;
|
|
86
|
+
if (method === "GET" || method === "HEAD") throw new TypeError(`Request with ${method} method cannot have a body`);
|
|
87
|
+
}
|
|
88
|
+
function lookupEnvProxy(...names) {
|
|
89
|
+
for (const name of names) {
|
|
90
|
+
const normalized = (process.env[name] ?? process.env[name.toLowerCase()])?.trim();
|
|
91
|
+
if (normalized) return normalized;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function resolveProxyFromEnv(url) {
|
|
95
|
+
const socksProxy = lookupEnvProxy("SOCKS5_PROXY", "SOCKS_PROXY");
|
|
96
|
+
if (socksProxy) return socksProxy;
|
|
97
|
+
if (url.protocol === "https:") return lookupEnvProxy("HTTPS_PROXY", "HTTP_PROXY");
|
|
98
|
+
return lookupEnvProxy("HTTP_PROXY");
|
|
99
|
+
}
|
|
100
|
+
function normalizeProxyConfig(proxy) {
|
|
101
|
+
const parsed = typeof proxy === "string" ? parse(proxy, { strict: true }) : proxy;
|
|
102
|
+
if (parsed == null) throw new TypeError(`Invalid proxy string: ${String(proxy)}`);
|
|
103
|
+
const key = stringify(parsed, {
|
|
104
|
+
strict: true,
|
|
105
|
+
format: "user:pass@ip:port"
|
|
106
|
+
});
|
|
107
|
+
if (!key) throw new TypeError("Failed to normalize proxy configuration");
|
|
108
|
+
return {
|
|
109
|
+
key,
|
|
110
|
+
proxy: parsed
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function resolveNormalizedProxy(url, proxy) {
|
|
114
|
+
if (proxy === null) return null;
|
|
115
|
+
if (proxy !== void 0) return normalizeProxyConfig(proxy);
|
|
116
|
+
const envProxy = resolveProxyFromEnv(url);
|
|
117
|
+
return envProxy ? normalizeProxyConfig(envProxy) : null;
|
|
118
|
+
}
|
|
119
|
+
function annotateResponse(response, url, redirected) {
|
|
120
|
+
try {
|
|
121
|
+
if (redirected) Object.defineProperties(response, {
|
|
122
|
+
redirected: {
|
|
123
|
+
configurable: true,
|
|
124
|
+
enumerable: true,
|
|
125
|
+
value: true,
|
|
126
|
+
writable: false
|
|
127
|
+
},
|
|
128
|
+
url: {
|
|
129
|
+
configurable: true,
|
|
130
|
+
enumerable: true,
|
|
131
|
+
value: url,
|
|
132
|
+
writable: false
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
else Object.defineProperty(response, "url", {
|
|
136
|
+
configurable: true,
|
|
137
|
+
enumerable: true,
|
|
138
|
+
value: url,
|
|
139
|
+
writable: false
|
|
140
|
+
});
|
|
141
|
+
} catch {}
|
|
142
|
+
return response;
|
|
143
|
+
}
|
|
144
|
+
async function discardResponse(response) {
|
|
145
|
+
try {
|
|
146
|
+
await response.body?.cancel();
|
|
147
|
+
} catch {}
|
|
148
|
+
}
|
|
149
|
+
function isRedirectResponse(response) {
|
|
150
|
+
return REDIRECT_STATUSES.has(response.status) && response.headers.get("location") != null;
|
|
151
|
+
}
|
|
152
|
+
function isReplayableBody(body) {
|
|
153
|
+
if (body == null || typeof body === "string" || body instanceof Uint8Array || isBlob(body) || isFormData(body) || isURLSearchParameters(body)) return true;
|
|
154
|
+
if (isReadableStream(body) || isReadable(body) || isMultipartFormDataStream(body) || isIterable(body)) return false;
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
async function resolveReplayableBody(current) {
|
|
158
|
+
if (isReplayableBody(current.body)) return current.body;
|
|
159
|
+
if (current.replayBodyRequest) {
|
|
160
|
+
const buffer = await current.replayBodyRequest.arrayBuffer();
|
|
161
|
+
return buffer.byteLength > 0 ? new Uint8Array(buffer) : null;
|
|
162
|
+
}
|
|
163
|
+
throw new TypeError("Cannot follow redirect with a non-replayable request body");
|
|
164
|
+
}
|
|
165
|
+
function shouldDropBodyOnRedirect(status, method) {
|
|
166
|
+
if (status === 303) return method !== "HEAD";
|
|
167
|
+
return (status === 301 || status === 302) && method === "POST";
|
|
168
|
+
}
|
|
169
|
+
function resolveRedirectMethod(status, method) {
|
|
170
|
+
if (status === 303 && method !== "HEAD" || (status === 301 || status === 302) && method === "POST") return "GET";
|
|
171
|
+
return method;
|
|
172
|
+
}
|
|
173
|
+
function createRedirectHeaders(current, nextUrl, dropBody) {
|
|
174
|
+
const headers = new Headers(current.headers);
|
|
175
|
+
const isCrossOrigin = current.url.origin !== nextUrl.origin;
|
|
176
|
+
const isDowngrade = current.url.protocol === "https:" && nextUrl.protocol === "http:";
|
|
177
|
+
headers.delete("host");
|
|
178
|
+
if (isCrossOrigin) for (const name of SENSITIVE_REDIRECT_HEADERS) headers.delete(name);
|
|
179
|
+
if (dropBody) for (const name of BODY_HEADERS) headers.delete(name);
|
|
180
|
+
if (!headers.has("referer") && !isDowngrade) headers.set("referer", current.url.toString());
|
|
181
|
+
return headers;
|
|
182
|
+
}
|
|
183
|
+
async function buildRedirectRequest(current, response) {
|
|
184
|
+
const location = response.headers.get("location");
|
|
185
|
+
if (!location) return current;
|
|
186
|
+
const nextUrl = new URL(location, current.url);
|
|
187
|
+
const nextMethod = resolveRedirectMethod(response.status, current.method);
|
|
188
|
+
const dropBody = shouldDropBodyOnRedirect(response.status, current.method);
|
|
189
|
+
const nextHeaders = createRedirectHeaders(current, nextUrl, dropBody);
|
|
190
|
+
const nextBody = dropBody ? void 0 : await resolveReplayableBody(current);
|
|
191
|
+
return {
|
|
192
|
+
...current,
|
|
193
|
+
url: nextUrl,
|
|
194
|
+
method: nextMethod,
|
|
195
|
+
headers: nextHeaders,
|
|
196
|
+
body: nextBody,
|
|
197
|
+
replayBodyRequest: void 0
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
async function sendPreparedRequest(prepared, getProxyClient) {
|
|
201
|
+
assertValidFetchUrl(prepared.url);
|
|
202
|
+
assertValidFetchBody(prepared.method, prepared.body);
|
|
203
|
+
const normalizedProxy = resolveNormalizedProxy(prepared.url, prepared.proxy);
|
|
204
|
+
const client = normalizedProxy ? getProxyClient(prepared.baseClient, normalizedProxy) : prepared.baseClient;
|
|
205
|
+
try {
|
|
206
|
+
const sendOptions = Object.defineProperty({
|
|
207
|
+
url: prepared.url,
|
|
208
|
+
method: prepared.method,
|
|
209
|
+
headers: prepared.headers,
|
|
210
|
+
body: prepared.body ?? null,
|
|
211
|
+
signal: prepared.signal
|
|
212
|
+
}, bodyErrorMapperSymbol, {
|
|
213
|
+
configurable: false,
|
|
214
|
+
enumerable: false,
|
|
215
|
+
writable: false,
|
|
216
|
+
value: (error) => toWebBodyReadError(error, prepared.signal)
|
|
217
|
+
});
|
|
218
|
+
return await client.send(sendOptions);
|
|
219
|
+
} catch (error) {
|
|
220
|
+
throw toWebFetchError(error, prepared.signal);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function prepareRequest(input, init, defaultHttpClient) {
|
|
224
|
+
const url = resolveUrl(input);
|
|
225
|
+
const method = resolveMethod(input, init);
|
|
226
|
+
const headers = resolveHeaders(input, init);
|
|
227
|
+
const body = resolveBody(input, init);
|
|
228
|
+
const signal = resolveSignal(input, init);
|
|
229
|
+
const redirect = resolveRedirectMode(input, init);
|
|
230
|
+
return {
|
|
231
|
+
url,
|
|
232
|
+
method,
|
|
233
|
+
headers,
|
|
234
|
+
body,
|
|
235
|
+
signal,
|
|
236
|
+
redirect,
|
|
237
|
+
baseClient: init.client ?? defaultHttpClient,
|
|
238
|
+
proxy: init.proxy,
|
|
239
|
+
replayBodyRequest: getBodyReplaySource(input, init, method, body, redirect)
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Creates a fetch-compatible client backed by {@link HttpClient}.
|
|
244
|
+
*
|
|
245
|
+
* @remarks
|
|
246
|
+
* The returned function follows the standard fetch shape, but uses this library's
|
|
247
|
+
* connection pooling, proxy support and body/error mapping rules.
|
|
248
|
+
*
|
|
249
|
+
* When no client is provided, an internal {@link HttpClient} is created and owned
|
|
250
|
+
* by the returned fetch-like function. Calling {@link FetchLike.close} closes that
|
|
251
|
+
* internal client and any proxy-specific clients created on demand.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```ts
|
|
255
|
+
* const fetchLike = createFetch();
|
|
256
|
+
* const response = await fetchLike("https://httpbin.org/anything");
|
|
257
|
+
* const data = await response.json();
|
|
258
|
+
* await fetchLike.close();
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
function createFetch(client) {
|
|
262
|
+
const defaultHttpClient = client ?? createDefaultHttpClient();
|
|
263
|
+
const proxyClientsByBase = /* @__PURE__ */ new WeakMap();
|
|
264
|
+
const ownedProxyClients = /* @__PURE__ */ new Set();
|
|
265
|
+
let closePromise;
|
|
266
|
+
const getProxyClient = (baseClient, proxy) => {
|
|
267
|
+
let clients = proxyClientsByBase.get(baseClient);
|
|
268
|
+
if (!clients) {
|
|
269
|
+
clients = /* @__PURE__ */ new Map();
|
|
270
|
+
proxyClientsByBase.set(baseClient, clients);
|
|
271
|
+
}
|
|
272
|
+
const existing = clients.get(proxy.key);
|
|
273
|
+
if (existing) return existing;
|
|
274
|
+
const proxyClient = new HttpClient({
|
|
275
|
+
...baseClient.options,
|
|
276
|
+
dialer: new ProxyDialer(proxy.proxy)
|
|
277
|
+
});
|
|
278
|
+
clients.set(proxy.key, proxyClient);
|
|
279
|
+
ownedProxyClients.add(proxyClient);
|
|
280
|
+
return proxyClient;
|
|
281
|
+
};
|
|
282
|
+
const fetchLike = (async (input, init = {}) => {
|
|
283
|
+
let prepared = prepareRequest(input, init, defaultHttpClient);
|
|
284
|
+
let redirected = false;
|
|
285
|
+
let redirects = 0;
|
|
286
|
+
for (;;) {
|
|
287
|
+
const response = await sendPreparedRequest(prepared, getProxyClient);
|
|
288
|
+
if (!isRedirectResponse(response) || prepared.redirect === "manual") return annotateResponse(response, prepared.url.toString(), redirected);
|
|
289
|
+
if (prepared.redirect === "error") {
|
|
290
|
+
await discardResponse(response);
|
|
291
|
+
throw new TypeError(`fetch failed: redirect mode is set to "error"`);
|
|
292
|
+
}
|
|
293
|
+
if (redirects >= MAX_REDIRECTS) {
|
|
294
|
+
await discardResponse(response);
|
|
295
|
+
throw new TypeError(`fetch failed: maximum redirect count exceeded`);
|
|
296
|
+
}
|
|
297
|
+
prepared = await buildRedirectRequest(prepared, response);
|
|
298
|
+
await discardResponse(response);
|
|
299
|
+
redirected = true;
|
|
300
|
+
redirects += 1;
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
const close = async () => {
|
|
304
|
+
if (closePromise) return closePromise;
|
|
305
|
+
const promise = (async () => {
|
|
306
|
+
const proxyClients = Array.from(ownedProxyClients);
|
|
307
|
+
ownedProxyClients.clear();
|
|
308
|
+
const errors = (await Promise.allSettled([...proxyClients.map((c) => c.close()), ...client == null ? [defaultHttpClient.close()] : []])).flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
309
|
+
if (errors.length === 1) throw errors[0];
|
|
310
|
+
if (errors.length > 1) throw new AggregateError(errors, "Failed to close one or more fetch clients");
|
|
311
|
+
})();
|
|
312
|
+
closePromise = promise;
|
|
313
|
+
try {
|
|
314
|
+
await promise;
|
|
315
|
+
} finally {
|
|
316
|
+
if (closePromise === promise) closePromise = void 0;
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
Object.defineProperties(fetchLike, {
|
|
320
|
+
client: {
|
|
321
|
+
configurable: false,
|
|
322
|
+
enumerable: false,
|
|
323
|
+
value: defaultHttpClient,
|
|
324
|
+
writable: false
|
|
325
|
+
},
|
|
326
|
+
close: {
|
|
327
|
+
configurable: false,
|
|
328
|
+
enumerable: false,
|
|
329
|
+
value: close,
|
|
330
|
+
writable: false
|
|
331
|
+
},
|
|
332
|
+
[Symbol.asyncDispose]: {
|
|
333
|
+
configurable: false,
|
|
334
|
+
enumerable: false,
|
|
335
|
+
value: close,
|
|
336
|
+
writable: false
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
return fetchLike;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Default fetch-compatible client created with {@link createFetch}.
|
|
343
|
+
*
|
|
344
|
+
* @remarks
|
|
345
|
+
* This singleton owns its internal {@link HttpClient}. Call {@link FetchLike.close}
|
|
346
|
+
* when you want to release pooled connections explicitly.
|
|
347
|
+
*/
|
|
348
|
+
var fetch = createFetch();
|
|
349
|
+
//#endregion
|
|
350
|
+
export { createFetch, fetch as default, normalizeHeaders };
|
package/http-client.cjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const require_agent_pool = require("./agent-pool.cjs");
|
|
2
|
+
//#region src/http-client.ts
|
|
3
|
+
/**
|
|
4
|
+
* Advanced HTTP client with per-origin pooling and explicit lifecycle control.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Use this API when you want direct access to the library's richer error model and
|
|
8
|
+
* transport options instead of the fetch-like compatibility layer.
|
|
9
|
+
*
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const client = new HttpClient();
|
|
14
|
+
* const response = await client.send({
|
|
15
|
+
* url: "https://httpbin.org/anything",
|
|
16
|
+
* method: "GET",
|
|
17
|
+
* });
|
|
18
|
+
* await client.close();
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
var HttpClient = class {
|
|
22
|
+
#agentPools = /* @__PURE__ */ new Map();
|
|
23
|
+
#agentPoolOptions;
|
|
24
|
+
#closePromise;
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
this.#agentPoolOptions = { ...options };
|
|
27
|
+
}
|
|
28
|
+
get options() {
|
|
29
|
+
return this.#agentPoolOptions;
|
|
30
|
+
}
|
|
31
|
+
async send(options) {
|
|
32
|
+
return this.#getOrCreateAgentPool(options.url).send(options);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Closes all pooled connections owned by this client.
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
* After closing, future requests may recreate pools as needed.
|
|
39
|
+
*/
|
|
40
|
+
async close() {
|
|
41
|
+
if (this.#closePromise) return this.#closePromise;
|
|
42
|
+
const promise = (async () => {
|
|
43
|
+
const entries = Array.from(this.#agentPools.entries());
|
|
44
|
+
const errors = (await Promise.allSettled(entries.map(async ([origin, agentPool]) => {
|
|
45
|
+
try {
|
|
46
|
+
await agentPool.close();
|
|
47
|
+
} finally {
|
|
48
|
+
this.#agentPools.delete(origin);
|
|
49
|
+
}
|
|
50
|
+
}))).flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
51
|
+
if (errors.length === 1) throw errors[0];
|
|
52
|
+
if (errors.length > 1) throw new AggregateError(errors, "Failed to close one or more agent pools");
|
|
53
|
+
})();
|
|
54
|
+
this.#closePromise = promise;
|
|
55
|
+
try {
|
|
56
|
+
await promise;
|
|
57
|
+
} finally {
|
|
58
|
+
if (this.#closePromise === promise) this.#closePromise = void 0;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async [Symbol.asyncDispose]() {
|
|
62
|
+
await this.close();
|
|
63
|
+
}
|
|
64
|
+
#getOrCreateAgentPool(url) {
|
|
65
|
+
const origin = typeof url === "string" ? new URL(url).origin : url.origin;
|
|
66
|
+
let agentPool = this.#agentPools.get(origin);
|
|
67
|
+
if (!agentPool) {
|
|
68
|
+
agentPool = require_agent_pool.createAgentPool(origin, this.#agentPoolOptions);
|
|
69
|
+
this.#agentPools.set(origin, agentPool);
|
|
70
|
+
}
|
|
71
|
+
return agentPool;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
//#endregion
|
|
75
|
+
exports.HttpClient = HttpClient;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Agent, AgentPool } from './types/agent';
|
|
2
|
+
/**
|
|
3
|
+
* Advanced HTTP client with per-origin pooling and explicit lifecycle control.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Use this API when you want direct access to the library's richer error model and
|
|
7
|
+
* transport options instead of the fetch-like compatibility layer.
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const client = new HttpClient();
|
|
13
|
+
* const response = await client.send({
|
|
14
|
+
* url: "https://httpbin.org/anything",
|
|
15
|
+
* method: "GET",
|
|
16
|
+
* });
|
|
17
|
+
* await client.close();
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class HttpClient implements AsyncDisposable {
|
|
21
|
+
#private;
|
|
22
|
+
constructor(options?: HttpClient.Options);
|
|
23
|
+
get options(): Readonly<HttpClient.Options>;
|
|
24
|
+
send(options: Agent.SendOptions): Promise<Response>;
|
|
25
|
+
/**
|
|
26
|
+
* Closes all pooled connections owned by this client.
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* After closing, future requests may recreate pools as needed.
|
|
30
|
+
*/
|
|
31
|
+
close(): Promise<void>;
|
|
32
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export declare namespace HttpClient {
|
|
35
|
+
interface Options extends AgentPool.Options {
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export interface HttpClientOptions extends HttpClient.Options {
|
|
39
|
+
}
|
package/http-client.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Agent, AgentPool } from './types/agent';
|
|
2
|
+
/**
|
|
3
|
+
* Advanced HTTP client with per-origin pooling and explicit lifecycle control.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Use this API when you want direct access to the library's richer error model and
|
|
7
|
+
* transport options instead of the fetch-like compatibility layer.
|
|
8
|
+
*
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const client = new HttpClient();
|
|
13
|
+
* const response = await client.send({
|
|
14
|
+
* url: "https://httpbin.org/anything",
|
|
15
|
+
* method: "GET",
|
|
16
|
+
* });
|
|
17
|
+
* await client.close();
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class HttpClient implements AsyncDisposable {
|
|
21
|
+
#private;
|
|
22
|
+
constructor(options?: HttpClient.Options);
|
|
23
|
+
get options(): Readonly<HttpClient.Options>;
|
|
24
|
+
send(options: Agent.SendOptions): Promise<Response>;
|
|
25
|
+
/**
|
|
26
|
+
* Closes all pooled connections owned by this client.
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* After closing, future requests may recreate pools as needed.
|
|
30
|
+
*/
|
|
31
|
+
close(): Promise<void>;
|
|
32
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
export declare namespace HttpClient {
|
|
35
|
+
interface Options extends AgentPool.Options {
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export interface HttpClientOptions extends HttpClient.Options {
|
|
39
|
+
}
|
package/http-client.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { createAgentPool } from "./agent-pool.js";
|
|
2
|
+
//#region src/http-client.ts
|
|
3
|
+
/**
|
|
4
|
+
* Advanced HTTP client with per-origin pooling and explicit lifecycle control.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Use this API when you want direct access to the library's richer error model and
|
|
8
|
+
* transport options instead of the fetch-like compatibility layer.
|
|
9
|
+
*
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* const client = new HttpClient();
|
|
14
|
+
* const response = await client.send({
|
|
15
|
+
* url: "https://httpbin.org/anything",
|
|
16
|
+
* method: "GET",
|
|
17
|
+
* });
|
|
18
|
+
* await client.close();
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
var HttpClient = class {
|
|
22
|
+
#agentPools = /* @__PURE__ */ new Map();
|
|
23
|
+
#agentPoolOptions;
|
|
24
|
+
#closePromise;
|
|
25
|
+
constructor(options = {}) {
|
|
26
|
+
this.#agentPoolOptions = { ...options };
|
|
27
|
+
}
|
|
28
|
+
get options() {
|
|
29
|
+
return this.#agentPoolOptions;
|
|
30
|
+
}
|
|
31
|
+
async send(options) {
|
|
32
|
+
return this.#getOrCreateAgentPool(options.url).send(options);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Closes all pooled connections owned by this client.
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
* After closing, future requests may recreate pools as needed.
|
|
39
|
+
*/
|
|
40
|
+
async close() {
|
|
41
|
+
if (this.#closePromise) return this.#closePromise;
|
|
42
|
+
const promise = (async () => {
|
|
43
|
+
const entries = Array.from(this.#agentPools.entries());
|
|
44
|
+
const errors = (await Promise.allSettled(entries.map(async ([origin, agentPool]) => {
|
|
45
|
+
try {
|
|
46
|
+
await agentPool.close();
|
|
47
|
+
} finally {
|
|
48
|
+
this.#agentPools.delete(origin);
|
|
49
|
+
}
|
|
50
|
+
}))).flatMap((result) => result.status === "rejected" ? [result.reason] : []);
|
|
51
|
+
if (errors.length === 1) throw errors[0];
|
|
52
|
+
if (errors.length > 1) throw new AggregateError(errors, "Failed to close one or more agent pools");
|
|
53
|
+
})();
|
|
54
|
+
this.#closePromise = promise;
|
|
55
|
+
try {
|
|
56
|
+
await promise;
|
|
57
|
+
} finally {
|
|
58
|
+
if (this.#closePromise === promise) this.#closePromise = void 0;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async [Symbol.asyncDispose]() {
|
|
62
|
+
await this.close();
|
|
63
|
+
}
|
|
64
|
+
#getOrCreateAgentPool(url) {
|
|
65
|
+
const origin = typeof url === "string" ? new URL(url).origin : url.origin;
|
|
66
|
+
let agentPool = this.#agentPools.get(origin);
|
|
67
|
+
if (!agentPool) {
|
|
68
|
+
agentPool = createAgentPool(origin, this.#agentPoolOptions);
|
|
69
|
+
this.#agentPools.set(origin, agentPool);
|
|
70
|
+
}
|
|
71
|
+
return agentPool;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
//#endregion
|
|
75
|
+
export { HttpClient };
|
package/index.cjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_net = require("./_internal/net.cjs");
|
|
3
|
+
const require_errors = require("./errors.cjs");
|
|
4
|
+
const require_encoding = require("./encoding.cjs");
|
|
5
|
+
const require_body = require("./body.cjs");
|
|
6
|
+
const require_agent = require("./agent.cjs");
|
|
7
|
+
const require_proxy = require("./dialers/proxy.cjs");
|
|
8
|
+
const require_tcp = require("./dialers/tcp.cjs");
|
|
9
|
+
const require_agent_pool = require("./agent-pool.cjs");
|
|
10
|
+
const require_http_client = require("./http-client.cjs");
|
|
11
|
+
const require_fetch = require("./fetch.cjs");
|
|
12
|
+
exports.AgentBusyError = require_errors.AgentBusyError;
|
|
13
|
+
exports.AgentClosedError = require_errors.AgentClosedError;
|
|
14
|
+
exports.AutoDialer = require_tcp.AutoDialer;
|
|
15
|
+
exports.ConnectTimeoutError = require_errors.ConnectTimeoutError;
|
|
16
|
+
exports.ConnectionError = require_errors.ConnectionError;
|
|
17
|
+
exports.ErrorType = require_errors.ErrorType;
|
|
18
|
+
exports.FetchError = require_errors.FetchError;
|
|
19
|
+
exports.FetchErrorCode = require_errors.FetchErrorCode;
|
|
20
|
+
exports.HttpClient = require_http_client.HttpClient;
|
|
21
|
+
exports.HttpStatusError = require_errors.HttpStatusError;
|
|
22
|
+
exports.OriginMismatchError = require_errors.OriginMismatchError;
|
|
23
|
+
exports.ProxyDialer = require_proxy.ProxyDialer;
|
|
24
|
+
exports.RequestAbortedError = require_errors.RequestAbortedError;
|
|
25
|
+
exports.RequestWriteError = require_errors.RequestWriteError;
|
|
26
|
+
exports.ResponseBodyError = require_errors.ResponseBodyError;
|
|
27
|
+
exports.ResponseDecodeError = require_errors.ResponseDecodeError;
|
|
28
|
+
exports.ResponseHeaderError = require_errors.ResponseHeaderError;
|
|
29
|
+
exports.TcpDialer = require_tcp.TcpDialer;
|
|
30
|
+
exports.TlsDialer = require_tcp.TlsDialer;
|
|
31
|
+
exports.UnsupportedAlpnProtocolError = require_errors.UnsupportedAlpnProtocolError;
|
|
32
|
+
exports.UnsupportedMethodError = require_errors.UnsupportedMethodError;
|
|
33
|
+
exports.UnsupportedProtocolError = require_errors.UnsupportedProtocolError;
|
|
34
|
+
exports.connectTcp = require_net.connectTcp;
|
|
35
|
+
exports.connectTls = require_net.connectTls;
|
|
36
|
+
exports.createAgent = require_agent.createAgent;
|
|
37
|
+
exports.createAgentPool = require_agent_pool.createAgentPool;
|
|
38
|
+
exports.createDecoders = require_encoding.createDecoders;
|
|
39
|
+
exports.createEncoders = require_encoding.createEncoders;
|
|
40
|
+
exports.createFetch = require_fetch.createFetch;
|
|
41
|
+
exports.decodeStream = require_encoding.decodeStream;
|
|
42
|
+
exports.encodeStream = require_encoding.encodeStream;
|
|
43
|
+
exports.extractBody = require_body.extractBody;
|
|
44
|
+
exports.fetch = require_fetch;
|
|
45
|
+
exports.fromRequestBody = require_body.fromRequestBody;
|
|
46
|
+
exports.getFormDataLength = require_body.getFormDataLength;
|
|
47
|
+
exports.normalizeHeaders = require_fetch.normalizeHeaders;
|
|
48
|
+
exports.resolveHostPort = require_tcp.resolveHostPort;
|
|
49
|
+
exports.upgradeTls = require_net.upgradeTls;
|