Skip to content

Commit 0063bc1

Browse files
committed
build(proxy): reorder to fix cjs build
1 parent fa902a2 commit 0063bc1

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

src/proxy.ts

+43-39
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,6 @@ import { HttpsProxyAgent } from "https-proxy-agent";
88
import type { ProxyOptions } from "../proxy";
99
import { fetch as _fetch } from "node-fetch-native";
1010

11-
export function createProxy(opts: ProxyOptions = {}) {
12-
const uri =
13-
opts.url ||
14-
process.env.https_proxy ||
15-
process.env.http_proxy ||
16-
process.env.HTTPS_PROXY ||
17-
process.env.HTTP_PROXY;
18-
19-
if (!uri) {
20-
return {
21-
agent: undefined,
22-
dispatcher: undefined,
23-
};
24-
}
25-
26-
const _noProxy = opts.noProxy || process.env.no_proxy || process.env.NO_PROXY;
27-
const noProxy = typeof _noProxy === "string" ? _noProxy.split(",") : _noProxy;
28-
29-
const nodeAgent = new NodeProxyAgent({ uri, noProxy });
30-
31-
// https://undici.nodejs.org/#/docs/api/ProxyAgent
32-
const undiciAgent = new UndiciProxyAgent({
33-
uri,
34-
noProxy,
35-
});
36-
37-
return {
38-
agent: nodeAgent,
39-
dispatcher: undiciAgent,
40-
};
41-
}
42-
43-
export function createFetch(proxyOptions: ProxyOptions = {}) {
44-
const proxy = createProxy(proxyOptions);
45-
return (url, fetchOptions) => _fetch(url, { ...proxy, ...fetchOptions });
46-
}
47-
48-
export const fetch = createFetch({});
49-
5011
// ----------------------------------------------
5112
// Utils
5213
// ----------------------------------------------
@@ -170,3 +131,46 @@ class NodeProxyAgent extends Agent {
170131
super.destroy();
171132
}
172133
}
134+
135+
// ----------------------------------------------
136+
// Main exports
137+
// ----------------------------------------------
138+
139+
export function createProxy(opts: ProxyOptions = {}) {
140+
const uri =
141+
opts.url ||
142+
process.env.https_proxy ||
143+
process.env.http_proxy ||
144+
process.env.HTTPS_PROXY ||
145+
process.env.HTTP_PROXY;
146+
147+
if (!uri) {
148+
return {
149+
agent: undefined,
150+
dispatcher: undefined,
151+
};
152+
}
153+
154+
const _noProxy = opts.noProxy || process.env.no_proxy || process.env.NO_PROXY;
155+
const noProxy = typeof _noProxy === "string" ? _noProxy.split(",") : _noProxy;
156+
157+
const nodeAgent = new NodeProxyAgent({ uri, noProxy });
158+
159+
// https://undici.nodejs.org/#/docs/api/ProxyAgent
160+
const undiciAgent = new UndiciProxyAgent({
161+
uri,
162+
noProxy,
163+
});
164+
165+
return {
166+
agent: nodeAgent,
167+
dispatcher: undiciAgent,
168+
};
169+
}
170+
171+
export function createFetch(proxyOptions: ProxyOptions = {}) {
172+
const proxy = createProxy(proxyOptions);
173+
return (url, fetchOptions) => _fetch(url, { ...proxy, ...fetchOptions });
174+
}
175+
176+
export const fetch = createFetch({});

0 commit comments

Comments
 (0)