Skip to content

Commit

Permalink
feat: support proxy for node.js with native fetch (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Dec 23, 2023
1 parent dc41245 commit 3f3ff11
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@
"citty": "^0.1.5",
"consola": "^3.2.3",
"defu": "^6.1.3",
"https-proxy-agent": "^7.0.2",
"node-fetch-native": "^1.4.1",
"node-fetch-native": "^1.5.1",
"ohash": "^1.1.3",
"pathe": "^1.1.1",
"tar": "^6.2.0"
Expand Down
21 changes: 11 additions & 10 deletions pnpm-lock.yaml

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

20 changes: 7 additions & 13 deletions src/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { promisify } from "node:util";
import type { Agent } from "node:http";
import { relative, resolve } from "pathe";
import { fetch } from "node-fetch-native";
import { createProxy } from "node-fetch-native/proxy";
import type { GitInfo } from "./types";

export async function download(
Expand Down Expand Up @@ -74,19 +75,12 @@ export async function sendFetch(
url: string,
options: InternalFetchOptions = {},
) {
if (!options.agent) {
const proxyEnv =
process.env.HTTPS_PROXY ||
process.env.https_proxy ||
process.env.HTTP_PROXY ||
process.env.http_proxy;
if (proxyEnv) {
const HttpsProxyAgent = await import("https-proxy-agent").then(
(r) => r.HttpsProxyAgent || r.default,
);
options.agent = new HttpsProxyAgent(proxyEnv);
}
}
// Add proxy support
// https://github.com/unjs/node-fetch-native?tab=readme-ov-file#proxy-support
options = {
...createProxy(),
...options,
};

// https://github.com/nodejs/undici/issues/1305
if (options.headers?.["sec-fetch-mode"]) {
Expand Down

0 comments on commit 3f3ff11

Please sign in to comment.