




















@@ -49,9 +49,17 @@ function resolveDispatcherKey(params: {
4949return `${params.kind}:${params.timeoutMs}:${autoSelectToken}`;
5050}
515152+function resolveStreamTimeoutMs(opts?: { timeoutMs?: number }): number | null {
53+const timeoutMsRaw = opts?.timeoutMs ?? DEFAULT_UNDICI_STREAM_TIMEOUT_MS;
54+if (!Number.isFinite(timeoutMsRaw)) {
55+return null;
56+}
57+return Math.max(DEFAULT_UNDICI_STREAM_TIMEOUT_MS, Math.floor(timeoutMsRaw));
58+}
59+5260function resolveCurrentDispatcherKind(
5361runtime: Pick<UndiciGlobalDispatcherDeps, "getGlobalDispatcher">,
54-): DispatcherKind | null {
62+): Exclude<DispatcherKind, "unsupported"> | null {
5563let dispatcher: unknown;
5664try {
5765dispatcher = runtime.getGlobalDispatcher();
@@ -92,19 +100,54 @@ export function ensureGlobalUndiciEnvProxyDispatcher(): void {
92100}
93101}
94102103+function applyGlobalDispatcherStreamTimeouts(params: {
104+runtime: UndiciGlobalDispatcherDeps;
105+kind: Exclude<DispatcherKind, "unsupported">;
106+timeoutMs: number;
107+}): void {
108+const { runtime, kind, timeoutMs } = params;
109+const autoSelectFamily = resolveUndiciAutoSelectFamily();
110+const nextKey = resolveDispatcherKey({ kind, timeoutMs, autoSelectFamily });
111+if (lastAppliedTimeoutKey === nextKey) {
112+return;
113+}
114+115+const connect = createUndiciAutoSelectFamilyConnectOptions(autoSelectFamily);
116+try {
117+if (kind === "env-proxy") {
118+const proxyOptions = {
119+ ...resolveEnvHttpProxyAgentOptions(),
120+bodyTimeout: timeoutMs,
121+headersTimeout: timeoutMs,
122+ ...(connect ? { connect } : {}),
123+} as ConstructorParameters<UndiciGlobalDispatcherDeps["EnvHttpProxyAgent"]>[0];
124+runtime.setGlobalDispatcher(new runtime.EnvHttpProxyAgent(proxyOptions));
125+} else {
126+runtime.setGlobalDispatcher(
127+new runtime.Agent({
128+bodyTimeout: timeoutMs,
129+headersTimeout: timeoutMs,
130+ ...(connect ? { connect } : {}),
131+}),
132+);
133+}
134+lastAppliedTimeoutKey = nextKey;
135+} catch {
136+// Best-effort hardening only.
137+}
138+}
139+95140export function ensureGlobalUndiciStreamTimeouts(opts?: { timeoutMs?: number }): void {
96-const timeoutMsRaw = opts?.timeoutMs ?? DEFAULT_UNDICI_STREAM_TIMEOUT_MS;
97-if (!Number.isFinite(timeoutMsRaw)) {
141+const timeoutMs = resolveStreamTimeoutMs(opts);
142+if (timeoutMs === null) {
98143return;
99144}
100-const timeoutMs = Math.max(DEFAULT_UNDICI_STREAM_TIMEOUT_MS, Math.floor(timeoutMsRaw));
101145_globalUndiciStreamTimeoutMs = timeoutMs;
102146if (!hasEnvHttpProxyAgentConfigured()) {
103147lastAppliedTimeoutKey = null;
104148return;
105149}
106150const runtime = loadUndiciGlobalDispatcherDeps();
107-const { EnvHttpProxyAgent, setGlobalDispatcher } = runtime;
108151const kind = resolveCurrentDispatcherKind(runtime);
109152if (kind === null) {
110153return;
@@ -113,25 +156,21 @@ export function ensureGlobalUndiciStreamTimeouts(opts?: { timeoutMs?: number }):
113156return;
114157}
115158116-const autoSelectFamily = resolveUndiciAutoSelectFamily();
117-const nextKey = resolveDispatcherKey({ kind, timeoutMs, autoSelectFamily });
118-if (lastAppliedTimeoutKey === nextKey) {
159+applyGlobalDispatcherStreamTimeouts({ runtime, kind, timeoutMs });
160+}
161+162+export function ensureGlobalUndiciDispatcherStreamTimeouts(opts?: { timeoutMs?: number }): void {
163+const timeoutMs = resolveStreamTimeoutMs(opts);
164+if (timeoutMs === null) {
119165return;
120166}
121-122-const connect = createUndiciAutoSelectFamilyConnectOptions(autoSelectFamily);
123-try {
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));
131-lastAppliedTimeoutKey = nextKey;
132-} catch {
133-// Best-effort hardening only.
167+_globalUndiciStreamTimeoutMs = timeoutMs;
168+const runtime = loadUndiciGlobalDispatcherDeps();
169+const kind = resolveCurrentDispatcherKind(runtime);
170+if (kind === null) {
171+return;
134172}
173+applyGlobalDispatcherStreamTimeouts({ runtime, kind, timeoutMs });
135174}
136175137176export function resetGlobalUndiciStreamTimeoutsForTests(): void {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。