Skip to content

Commit d27564b

Browse files
author
vhess
committed
fix path logic and duplex option
1 parent b6ef353 commit d27564b

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

lib/http-proxy/passes/web-incoming.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

lib/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ import type * as http from 'node:http';
3333
*/
3434

3535
function createProxyServer<TIncomingMessage extends typeof http.IncomingMessage = typeof http.IncomingMessage, TServerResponse extends typeof http.ServerResponse = typeof http.ServerResponse, TError = Error>(options: ServerOptions = {}): ProxyServer<TIncomingMessage, TServerResponse, TError> {
36-
// Check if we're in forced undici mode
37-
if (process.env.FORCE_FETCH_PATH === 'true' && options.fetch === undefined) {
38-
options = { ...options, fetch: { dispatcher: new Agent({ allowH2: true, connect: { rejectUnauthorized: false } }) as any } };
39-
}
40-
4136
return new ProxyServer<TIncomingMessage, TServerResponse, TError>(options);
4237
}
4338

lib/test/setup.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,3 @@
22

33
// so we can test https using our self-signed example cert
44
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
5-
6-
// Global test configuration for undici code path
7-
// When FORCE_UNDICI_PATH=true, all proxy servers will use undici by default
8-
// if (process.env.FORCE_FETCH_PATH
9-
// === "true") {
10-
// const { Agent, setGlobalDispatcher } = await import("undici");
11-
// // Enable HTTP/2 for all fetch operations
12-
// setGlobalDispatcher(new Agent({ allowH2: true }));
13-
// }

0 commit comments

Comments
 (0)