
























@@ -1,9 +1,12 @@
1-import { Agent, EnvHttpProxyAgent, getGlobalDispatcher, setGlobalDispatcher } from "undici";
21import { hasEnvHttpProxyAgentConfigured, resolveEnvHttpProxyAgentOptions } from "./proxy-env.js";
32import {
43createUndiciAutoSelectFamilyConnectOptions,
54resolveUndiciAutoSelectFamily,
65} from "./undici-family-policy.js";
6+import {
7+loadUndiciGlobalDispatcherDeps,
8+type UndiciGlobalDispatcherDeps,
9+} from "./undici-runtime.js";
710811export const DEFAULT_UNDICI_STREAM_TIMEOUT_MS = 30 * 60 * 1000;
912@@ -46,10 +49,12 @@ function resolveDispatcherKey(params: {
4649return `${params.kind}:${params.timeoutMs}:${autoSelectToken}`;
4750}
485149-function resolveCurrentDispatcherKind(): DispatcherKind | null {
52+function resolveCurrentDispatcherKind(
53+runtime: Pick<UndiciGlobalDispatcherDeps, "getGlobalDispatcher">,
54+): DispatcherKind | null {
5055let dispatcher: unknown;
5156try {
52-dispatcher = getGlobalDispatcher();
57+dispatcher = runtime.getGlobalDispatcher();
5358} catch {
5459return null;
5560}
@@ -63,13 +68,15 @@ export function ensureGlobalUndiciEnvProxyDispatcher(): void {
6368if (!shouldUseEnvProxy) {
6469return;
6570}
71+const runtime = loadUndiciGlobalDispatcherDeps();
72+const { EnvHttpProxyAgent, setGlobalDispatcher } = runtime;
6673if (lastAppliedProxyBootstrap) {
67-if (resolveCurrentDispatcherKind() === "env-proxy") {
74+if (resolveCurrentDispatcherKind(runtime) === "env-proxy") {
6875return;
6976}
7077lastAppliedProxyBootstrap = false;
7178}
72-const currentKind = resolveCurrentDispatcherKind();
79+const currentKind = resolveCurrentDispatcherKind(runtime);
7380if (currentKind === null) {
7481return;
7582}
@@ -92,10 +99,19 @@ export function ensureGlobalUndiciStreamTimeouts(opts?: { timeoutMs?: number }):
9299}
93100const timeoutMs = Math.max(DEFAULT_UNDICI_STREAM_TIMEOUT_MS, Math.floor(timeoutMsRaw));
94101_globalUndiciStreamTimeoutMs = timeoutMs;
95-const kind = resolveCurrentDispatcherKind();
102+if (!hasEnvHttpProxyAgentConfigured()) {
103+lastAppliedTimeoutKey = null;
104+return;
105+}
106+const runtime = loadUndiciGlobalDispatcherDeps();
107+const { EnvHttpProxyAgent, setGlobalDispatcher } = runtime;
108+const kind = resolveCurrentDispatcherKind(runtime);
96109if (kind === null) {
97110return;
98111}
112+if (kind !== "env-proxy") {
113+return;
114+}
99115100116const autoSelectFamily = resolveUndiciAutoSelectFamily();
101117const nextKey = resolveDispatcherKey({ kind, timeoutMs, autoSelectFamily });
@@ -105,23 +121,13 @@ export function ensureGlobalUndiciStreamTimeouts(opts?: { timeoutMs?: number }):
105121106122const connect = createUndiciAutoSelectFamilyConnectOptions(autoSelectFamily);
107123try {
108-if (kind === "env-proxy") {
109-const proxyOptions = {
110- ...resolveEnvHttpProxyAgentOptions(),
111-bodyTimeout: timeoutMs,
112-headersTimeout: timeoutMs,
113- ...(connect ? { connect } : {}),
114-} as ConstructorParameters<typeof EnvHttpProxyAgent>[0];
115-setGlobalDispatcher(new EnvHttpProxyAgent(proxyOptions));
116-} else {
117-setGlobalDispatcher(
118-new Agent({
119-bodyTimeout: timeoutMs,
120-headersTimeout: timeoutMs,
121- ...(connect ? { connect } : {}),
122-}),
123-);
124-}
124+const proxyOptions = {
125+ ...resolveEnvHttpProxyAgentOptions(),
126+bodyTimeout: timeoutMs,
127+headersTimeout: timeoutMs,
128+ ...(connect ? { connect } : {}),
129+} as ConstructorParameters<UndiciGlobalDispatcherDeps["EnvHttpProxyAgent"]>[0];
130+setGlobalDispatcher(new EnvHttpProxyAgent(proxyOptions));
125131lastAppliedTimeoutKey = nextKey;
126132} catch {
127133// Best-effort hardening only.
@@ -140,17 +146,29 @@ export function resetGlobalUndiciStreamTimeoutsForTests(): void {
140146 */
141147export function forceResetGlobalDispatcher(): void {
142148lastAppliedTimeoutKey = null;
149+if (!hasEnvHttpProxyAgentConfigured()) {
150+if (!lastAppliedProxyBootstrap) {
151+return;
152+}
153+lastAppliedProxyBootstrap = false;
154+try {
155+const { Agent, setGlobalDispatcher } = loadUndiciGlobalDispatcherDeps();
156+setGlobalDispatcher(new Agent());
157+} catch {
158+// Best-effort reset only.
159+}
160+return;
161+}
143162lastAppliedProxyBootstrap = false;
144163try {
164+const { EnvHttpProxyAgent, setGlobalDispatcher } = loadUndiciGlobalDispatcherDeps();
145165const proxyOptions = resolveEnvHttpProxyAgentOptions();
146-if (hasEnvHttpProxyAgentConfigured()) {
147-setGlobalDispatcher(
148-new EnvHttpProxyAgent(proxyOptions as ConstructorParameters<typeof EnvHttpProxyAgent>[0]),
149-);
150-lastAppliedProxyBootstrap = true;
151-} else {
152-setGlobalDispatcher(new Agent());
153-}
166+setGlobalDispatcher(
167+new EnvHttpProxyAgent(
168+proxyOptions as ConstructorParameters<UndiciGlobalDispatcherDeps["EnvHttpProxyAgent"]>[0],
169+),
170+);
171+lastAppliedProxyBootstrap = true;
154172} catch {
155173// Best-effort reset only.
156174}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。