@@ -80,7 +80,7 @@ export function stream(req: Request, res: Response, options: NormalizedServerOpt
8080 // And we begin!
8181 server . emit ( "start" , req , res , options . target || options . forward ! ) ;
8282
83- if ( options . fetch ) {
83+ if ( options . fetch || process . env . FORCE_FETCH_PATH === 'true' ) {
8484 return stream2 ( req , res , options , _ , server , cb ) ;
8585 }
8686
@@ -200,6 +200,12 @@ async function stream2(
200200 cb ?: ErrorCallback ,
201201) {
202202
203+ if ( process . env . FORCE_FETCH_PATH === 'true' && ! options . fetch ) {
204+ const { Agent } = await import ( 'undici' ) ;
205+ console . log ( 'Setting undici dispatcher for fetch operations in stream2' ) ;
206+ options . fetch = { dispatcher : new Agent ( { allowH2 : true , connect : { rejectUnauthorized : false } } ) as any } ;
207+ }
208+
203209 // Helper function to handle errors consistently throughout the undici path
204210 // Centralizes the error handling logic to avoid repetition
205211 const handleError = ( err : Error , target ?: ProxyTargetUrl ) => {
@@ -248,7 +254,7 @@ async function stream2(
248254 requestOptions . body = options . buffer as Stream . Readable ;
249255 } else if ( req . method !== "GET" && req . method !== "HEAD" ) {
250256 requestOptions . body = req ;
251- requestOptions . duplex
257+ requestOptions . duplex = "half" ;
252258 }
253259
254260 // Call onBeforeRequest callback before making the forward request
@@ -262,7 +268,7 @@ async function stream2(
262268 }
263269
264270 try {
265- const result = await fetch ( outgoingOptions . url , requestOptions ) ;
271+ const result = await fetch ( new URL ( outgoingOptions . url ) . origin + outgoingOptions . path , requestOptions ) ;
266272
267273 // Call onAfterResponse callback for forward requests (though they typically don't expect responses)
268274 if ( fetchOptions . onAfterResponse ) {
@@ -303,6 +309,7 @@ async function stream2(
303309 requestOptions . headers = { ...requestOptions . headers , authorization : `Basic ${ Buffer . from ( options . auth ) . toString ( "base64" ) } ` } ;
304310 }
305311
312+
306313 if ( options . buffer ) {
307314 requestOptions . body = options . buffer as Stream . Readable ;
308315 } else if ( req . method !== "GET" && req . method !== "HEAD" ) {
@@ -320,7 +327,7 @@ async function stream2(
320327 }
321328
322329 try {
323- const response = await fetch ( new URL ( outgoingOptions . path ?? "/" , outgoingOptions . url ) , requestOptions ) ;
330+ const response = await fetch ( new URL ( outgoingOptions . url ) . origin + outgoingOptions . path , requestOptions ) ;
324331
325332 // Call onAfterResponse callback after receiving the response
326333 if ( fetchOptions . onAfterResponse ) {
0 commit comments