@@ -235,27 +235,9 @@ async function stream2(
235235 const customFetch = options . fetch || fetch ;
236236 const fetchOptions = options . fetchOptions ?? { } as FetchOptions ;
237237
238- const controller = new AbortController ( ) ;
239- const { signal } = controller ;
240-
241- if ( options . proxyTimeout ) {
242- setTimeout ( ( ) => {
243- controller . abort ( ) ;
244- } , options . proxyTimeout ) ;
245- }
246-
247- // Ensure we abort proxy if request is aborted
248- res . on ( "close" , ( ) => {
249- const aborted = ! res . writableFinished ;
250- if ( aborted ) {
251- controller . abort ( ) ;
252- }
253- } ) ;
254-
255238 const prepareRequest = ( outgoing : common . Outgoing ) => {
256239 const requestOptions : RequestInit = {
257240 method : outgoing . method ,
258- signal,
259241 ...fetchOptions . requestOptions ,
260242 } ;
261243
@@ -279,6 +261,10 @@ async function stream2(
279261 headers . set ( "authorization" , `Basic ${ Buffer . from ( options . auth ) . toString ( "base64" ) } ` ) ;
280262 }
281263
264+ if ( options . proxyTimeout ) {
265+ requestOptions . signal = AbortSignal . timeout ( options . proxyTimeout ) ;
266+ }
267+
282268 requestOptions . headers = headers ;
283269
284270 if ( options . buffer ) {
@@ -322,16 +308,6 @@ async function stream2(
322308 }
323309 }
324310 } catch ( err ) {
325- if ( ( err as Error ) . name === "AbortError" ) {
326- // Handle aborts (timeout or client disconnect)
327- if ( options . proxyTimeout && signal . aborted ) {
328- const proxyTimeoutErr = new Error ( "Proxy timeout" ) ;
329- ( proxyTimeoutErr as any ) . code = "ECONNRESET" ;
330- handleError ( proxyTimeoutErr , options . forward ) ;
331- }
332- // If aborted by client (res.close), we might not want to emit an error or maybe just log it
333- return ;
334- }
335311 handleError ( err as Error , options . forward ) ;
336312 }
337313
@@ -427,14 +403,6 @@ async function stream2(
427403 server ?. emit ( "end" , req , res , fakeProxyRes ) ;
428404 }
429405 } catch ( err ) {
430- if ( ( err as Error ) . name === "AbortError" ) {
431- if ( options . proxyTimeout && signal . aborted ) {
432- const proxyTimeoutErr = new Error ( "Proxy timeout" ) ;
433- ( proxyTimeoutErr as any ) . code = "ECONNRESET" ;
434- handleError ( proxyTimeoutErr , options . target ) ;
435- }
436- return ;
437- }
438406 handleError ( err as Error , options . target ) ;
439407 }
440408}
0 commit comments