Skip to content

Commit 13ade91

Browse files
committed
fix(runtime): ignore unsupported lowercase proxy env vars
1 parent bd4b0ad commit 13ade91

3 files changed

Lines changed: 19 additions & 40 deletions

File tree

packages/cli/tests/e2e/proxy.e2e.test.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { cliPackageRoot } from "./helpers.ts";
1111
const execFileAsync = promisify(execFile);
1212

1313
/**
14-
* 代理支持 E2E(issue #35):只验证 `setupProxyFromEnv()` 是否把代理 dispatcher
14+
* 代理支持 E2E:只验证 `setupProxyFromEnv()` 是否把代理 dispatcher
1515
* 正确装到全局 fetch 上——设了 HTTPS_PROXY 后裸 `fetch()` 走代理,未设置时直连,
1616
* NO_PROXY 命中时跳过,非法代理值给出明确报错。
1717
*
@@ -66,11 +66,8 @@ afterAll(async () => {
6666
/** 清空所有代理相关环境变量,确保每个用例只受自身设置影响 */
6767
const PROXY_ENV_CLEARED = {
6868
HTTPS_PROXY: "",
69-
https_proxy: "",
7069
HTTP_PROXY: "",
71-
http_proxy: "",
7270
NO_PROXY: "",
73-
no_proxy: "",
7471
};
7572

7673
/** 以给定代理环境变量运行探针脚本,返回 { exitCode, stderr } */
@@ -97,12 +94,6 @@ describe("e2e: proxy", () => {
9794
expect(connectTargets).toContain(`${FAKE_HOST}:443`);
9895
});
9996

100-
test("空字符串小写变量不屏蔽大写 HTTPS_PROXY(undici ?? 取值回归)", async () => {
101-
connectTargets.length = 0;
102-
await runProbe({ https_proxy: "", HTTPS_PROXY: proxyUrl });
103-
expect(connectTargets).toContain(`${FAKE_HOST}:443`);
104-
});
105-
10697
test("NO_PROXY 命中目标主机时不走代理", async () => {
10798
connectTargets.length = 0;
10899
await runProbe({ HTTPS_PROXY: proxyUrl, NO_PROXY: FAKE_HOST });

packages/runtime/src/proxy.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,22 @@ export interface ProxyEnv {
77
noProxy?: string;
88
}
99

10-
function pick(env: NodeJS.ProcessEnv, ...keys: string[]): string | undefined {
11-
for (const key of keys) {
12-
const value = env[key]?.trim();
13-
if (value) return value;
14-
}
15-
return undefined;
10+
function pick(env: NodeJS.ProcessEnv, key: string): string | undefined {
11+
const value = env[key]?.trim();
12+
return value || undefined;
1613
}
1714

18-
/**
19-
* 读取代理环境变量(小写优先,与 curl 约定一致)。
20-
* 空白值视为未设置——undici 自身用 `??` 取值,空字符串的小写变量会屏蔽
21-
* 已设置的大写变量,这里统一清洗后显式传入,绕开该坑。
22-
*/
15+
/** 读取代理环境变量,空白值视为未设置。 */
2316
export function readProxyEnv(env: NodeJS.ProcessEnv = process.env): ProxyEnv {
2417
return {
25-
httpProxy: pick(env, "http_proxy", "HTTP_PROXY"),
26-
httpsProxy: pick(env, "https_proxy", "HTTPS_PROXY"),
27-
noProxy: pick(env, "no_proxy", "NO_PROXY"),
18+
httpProxy: pick(env, "HTTP_PROXY"),
19+
httpsProxy: pick(env, "HTTPS_PROXY"),
20+
noProxy: pick(env, "NO_PROXY"),
2821
};
2922
}
3023

3124
// Node 内置 fetch(undici)默认不读取代理环境变量,VPN / 公司代理环境下会
32-
// 绕过代理直连而被拦截(见 issue #35)。仅当用户显式设置了 HTTP_PROXY /
33-
// HTTPS_PROXY 时才安装代理 dispatcher(同时支持 NO_PROXY),未设置时不触碰
25+
// 绕过代理直连而被拦截。仅当用户配置了代理时才安装 dispatcher,未配置时不触碰
3426
// 全局 dispatcher,行为与之前完全一致。
3527
export function setupProxyFromEnv(): void {
3628
const { httpProxy, httpsProxy, noProxy } = readProxyEnv();

packages/runtime/tests/proxy.test.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,17 @@ test("readProxyEnv: 空白值视为未设置", () => {
1717
});
1818
});
1919

20-
test("readProxyEnv: 大小写变量均可识别,小写优先", () => {
21-
expect(readProxyEnv({ HTTPS_PROXY: "http://upper:1" }).httpsProxy).toBe("http://upper:1");
22-
expect(readProxyEnv({ https_proxy: "http://lower:1" }).httpsProxy).toBe("http://lower:1");
20+
test("readProxyEnv: 读取代理变量", () => {
2321
expect(
24-
readProxyEnv({ https_proxy: "http://lower:1", HTTPS_PROXY: "http://upper:1" }).httpsProxy,
25-
).toBe("http://lower:1");
26-
});
27-
28-
test("readProxyEnv: 空字符串小写变量不屏蔽已设置的大写变量", () => {
29-
expect(readProxyEnv({ https_proxy: "", HTTPS_PROXY: "http://upper:1" }).httpsProxy).toBe(
30-
"http://upper:1",
31-
);
32-
expect(readProxyEnv({ http_proxy: "", HTTP_PROXY: "http://upper:2" }).httpProxy).toBe(
33-
"http://upper:2",
34-
);
22+
readProxyEnv({
23+
HTTP_PROXY: "http://proxy.example.com:8080",
24+
HTTPS_PROXY: "http://secure-proxy.example.com:8080",
25+
}),
26+
).toEqual({
27+
httpProxy: "http://proxy.example.com:8080",
28+
httpsProxy: "http://secure-proxy.example.com:8080",
29+
noProxy: undefined,
30+
});
3531
});
3632

3733
test("readProxyEnv: NO_PROXY 独立读取", () => {

0 commit comments

Comments
 (0)