Skip to content

Commit 6c2780b

Browse files
committed
feat: support https_proxy
Based on nodejs/node#43187 (comment)
1 parent ddc4679 commit 6c2780b

7 files changed

+55
-11
lines changed

index.cjs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
const { fetch } = require("undici");
1+
const { fetch, setGlobalDispatcher, ProxyAgent } = require("undici");
2+
3+
const proxyServer =
4+
process.env['https_proxy'] || process.env['HTTPS_PROXY'];
5+
if (proxyServer) {
6+
setGlobalDispatcher(new ProxyAgent(proxyServer));
7+
}
28

39
if (!Object.keys(global).includes("fetch")) {
410
Object.defineProperty(global, "fetch", { value: fetch });
511
}
12+

index.mjs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { fetch } from "undici";
1+
import { fetch, setGlobalDispatcher, ProxyAgent } from "undici";
2+
3+
const proxyServer = process.env["https_proxy"] || process.env["HTTPS_PROXY"];
4+
if (proxyServer) {
5+
setGlobalDispatcher(new ProxyAgent(proxyServer));
6+
}
27

38
if (!Object.keys(global).includes("fetch")) {
49
Object.defineProperty(global, "fetch", { value: fetch });

package-lock.json

+2-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"index.*"
1212
],
1313
"scripts": {
14-
"test": "node tests/with-polyfill.mjs && node tests/without-polyfill.mjs"
14+
"test": "node tests/index.mjs"
1515
},
1616
"repository": {
1717
"type": "git",

tests/index.mjs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { run } from "node:test";
2+
import { resolve, dirname } from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
5+
const __dirname = dirname(fileURLToPath(import.meta.url));
6+
7+
const files = ["with-proxy.mjs", "with-polyfill.mjs", "without-polyfill.mjs"].map(
8+
(testFile) => resolve(__dirname, testFile)
9+
);
10+
11+
run({ files }).pipe(process.stdout);

tests/with-polyfill.mjs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import "fetch-undici-polyfill";
21
import { test } from "node:test";
32
import assert from "node:assert";
3+
import { getGlobalDispatcher, ProxyAgent } from "undici";
44

5+
// Act
6+
await import("fetch-undici-polyfill");
7+
8+
// Assert
59
test("fetch exists", () => {
610
assert(typeof fetch !== "undefined");
711
});
12+
13+
test("proxy is not set", () => {
14+
assert(!(getGlobalDispatcher() instanceof ProxyAgent));
15+
});

tests/with-proxy.mjs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { test } from "node:test";
2+
import assert from "node:assert";
3+
import { getGlobalDispatcher, ProxyAgent } from "undici";
4+
5+
// Arrange
6+
process.env["HTTPS_PROXY"] = "https://foo.com";
7+
8+
// Act
9+
await import("fetch-undici-polyfill");
10+
11+
// Assert
12+
test("fetch exists", () => {
13+
assert(typeof fetch !== "undefined");
14+
});
15+
16+
test("proxy is set", () => {
17+
assert(getGlobalDispatcher() instanceof ProxyAgent);
18+
});

0 commit comments

Comments
 (0)