@@ -283,9 +283,14 @@ async function stream2(
283283
284284 const outgoingOptions = common . setupOutgoing ( options . ssl || { } , options , req ) ;
285285
286+ // Remove symbols from headers as undici fetch does not like them
286287 const requestOptions : RequestInit = {
287288 method : outgoingOptions . method as Dispatcher . HttpMethod ,
288- headers : outgoingOptions . headers as Record < string , string > ,
289+ headers : Object . fromEntries (
290+ Object . entries ( outgoingOptions . headers || { } ) . filter ( ( [ key , _value ] ) => {
291+ return typeof key === "string" ;
292+ } )
293+ ) as RequestInit [ "headers" ] ,
289294 ...fetchOptions . requestOptions
290295 } ;
291296
@@ -314,7 +319,7 @@ async function stream2(
314319 }
315320
316321 try {
317- const response = await fetch ( outgoingOptions . url , requestOptions ) ;
322+ const response = await fetch ( new URL ( outgoingOptions . path ?? "/" , outgoingOptions . url ) , requestOptions ) ;
318323
319324 // Call onAfterResponse callback after receiving the response
320325 if ( fetchOptions . onAfterResponse ) {
@@ -331,14 +336,19 @@ async function stream2(
331336 // But since only certain properties are used, we can fake it here
332337 // to avoid having to refactor everything.
333338 const fakeProxyRes = {
334- ...response , rawHeaders : Object . entries ( response . headers ) . flatMap ( ( [ key , value ] ) => {
339+ statusCode : response . status ,
340+ statusMessage : response . statusText ,
341+ headers : Object . fromEntries ( response . headers . entries ( ) ) ,
342+ rawHeaders : Object . entries ( response . headers ) . flatMap ( ( [ key , value ] ) => {
335343 if ( Array . isArray ( value ) ) {
336344 return value . flatMap ( v => ( v != null ? [ key , v ] : [ ] ) ) ;
337345 }
338346 return value != null ? [ key , value ] : [ ] ;
339347 } ) as string [ ]
340348 } as unknown as ProxyResponse ;
341349
350+ server ?. emit ( "proxyRes" , fakeProxyRes , req , res ) ;
351+
342352 if ( ! res . headersSent && ! options . selfHandleResponse ) {
343353 for ( const pass of web_o ) {
344354 // note: none of these return anything
@@ -353,12 +363,19 @@ async function stream2(
353363 : null ;
354364
355365 if ( nodeStream ) {
366+ nodeStream . on ( "error" , ( err ) => {
367+ handleError ( err , options . target ) ;
368+ } ) ;
369+
356370 nodeStream . on ( "end" , ( ) => {
357371 server ?. emit ( "end" , req , res , fakeProxyRes ) ;
358372 } ) ;
373+
359374 // We pipe to the response unless its expected to be handled by the user
360375 if ( ! options . selfHandleResponse ) {
361- nodeStream . pipe ( res ) ;
376+ nodeStream . pipe ( res , { end : true } ) ;
377+ } else {
378+ nodeStream . resume ( ) ;
362379 }
363380 } else {
364381 server ?. emit ( "end" , req , res , fakeProxyRes ) ;
0 commit comments