Skip to content

Commit

Permalink
feat: support https_proxy (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-bompart authored Mar 2, 2023
1 parent ddc4679 commit 86552b5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
9 changes: 8 additions & 1 deletion index.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const { fetch } = require("undici");
const { fetch, setGlobalDispatcher, ProxyAgent } = require("undici");

const proxyServer =
process.env['https_proxy'] || process.env['HTTPS_PROXY'];
if (proxyServer) {
setGlobalDispatcher(new ProxyAgent(proxyServer));
}

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

7 changes: 6 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { fetch } from "undici";
import { fetch, setGlobalDispatcher, ProxyAgent } from "undici";

const proxyServer = process.env["https_proxy"] || process.env["HTTPS_PROXY"];
if (proxyServer) {
setGlobalDispatcher(new ProxyAgent(proxyServer));
}

if (!Object.keys(global).includes("fetch")) {
Object.defineProperty(global, "fetch", { value: fetch });
Expand Down
9 changes: 2 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"index.*"
],
"scripts": {
"test": "node tests/with-polyfill.mjs && node tests/without-polyfill.mjs"
"test": "node tests/index.mjs"
},
"repository": {
"type": "git",
Expand Down
11 changes: 11 additions & 0 deletions tests/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { run } from "node:test";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = dirname(fileURLToPath(import.meta.url));

const files = ["with-proxy.mjs", "with-polyfill.mjs", "without-polyfill.mjs"].map(
(testFile) => resolve(__dirname, testFile)
);

run({ files }).pipe(process.stdout);
10 changes: 9 additions & 1 deletion tests/with-polyfill.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import "fetch-undici-polyfill";
import { test } from "node:test";
import assert from "node:assert";
import { getGlobalDispatcher, ProxyAgent } from "undici";

// Act
await import("fetch-undici-polyfill");

// Assert
test("fetch exists", () => {
assert(typeof fetch !== "undefined");
});

test("proxy is not set", () => {
assert(!(getGlobalDispatcher() instanceof ProxyAgent));
});
18 changes: 18 additions & 0 deletions tests/with-proxy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { test } from "node:test";
import assert from "node:assert";
import { getGlobalDispatcher, ProxyAgent } from "undici";

// Arrange
process.env["HTTPS_PROXY"] = "https://foo.com";

// Act
await import("fetch-undici-polyfill");

// Assert
test("fetch exists", () => {
assert(typeof fetch !== "undefined");
});

test("proxy is set", () => {
assert(getGlobalDispatcher() instanceof ProxyAgent);
});

0 comments on commit 86552b5

Please sign in to comment.