@@ -49,9 +49,17 @@ function resolveDispatcherKey(params: {
4949 return `${ params . kind } :${ params . timeoutMs } :${ autoSelectToken } ` ;
5050}
5151
52+ 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 (
5361 runtime : Pick < UndiciGlobalDispatcherDeps , "getGlobalDispatcher" > ,
54- ) : DispatcherKind | null {
62+ ) : Exclude < DispatcherKind , "unsupported" > | null {
5563 let dispatcher : unknown ;
5664 try {
5765 dispatcher = runtime . getGlobalDispatcher ( ) ;
@@ -92,19 +100,54 @@ export function ensureGlobalUndiciEnvProxyDispatcher(): void {
92100 }
93101}
94102
103+ 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 ) {
98143 return ;
99144 }
100- const timeoutMs = Math . max ( DEFAULT_UNDICI_STREAM_TIMEOUT_MS , Math . floor ( timeoutMsRaw ) ) ;
101145 _globalUndiciStreamTimeoutMs = timeoutMs ;
102146 if ( ! hasEnvHttpProxyAgentConfigured ( ) ) {
103147 lastAppliedTimeoutKey = null ;
104148 return ;
105149 }
106150 const runtime = loadUndiciGlobalDispatcherDeps ( ) ;
107- const { EnvHttpProxyAgent, setGlobalDispatcher } = runtime ;
108151 const kind = resolveCurrentDispatcherKind ( runtime ) ;
109152 if ( kind === null ) {
110153 return ;
@@ -113,25 +156,21 @@ export function ensureGlobalUndiciStreamTimeouts(opts?: { timeoutMs?: number }):
113156 return ;
114157 }
115158
116- 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 ) {
119165 return ;
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}
136175
137176export function resetGlobalUndiciStreamTimeoutsForTests ( ) : void {
0 commit comments