Skip to content

Commit e09a140

Browse files
author
vhess
committed
updated forced undici/fetch path handling
1 parent db6dc43 commit e09a140

File tree

7 files changed

+289
-281
lines changed

7 files changed

+289
-281
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ async function stream2(
248248
requestOptions.body = options.buffer as Stream.Readable;
249249
} else if (req.method !== "GET" && req.method !== "HEAD") {
250250
requestOptions.body = req;
251+
requestOptions.duplex
251252
}
252253

253254
// Call onBeforeRequest callback before making the forward request

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import type * as http from 'node:http';
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> {
3636
// Check if we're in forced undici mode
3737
if (process.env.FORCE_FETCH_PATH === 'true' && options.fetch === undefined) {
38-
options = { ...options, fetch: { dispatcher: new Agent({ allowH2: true, connect: { rejectUnauthorized: true } }) as any } };
38+
options = { ...options, fetch: { dispatcher: new Agent({ allowH2: true, connect: { rejectUnauthorized: false } }) as any } };
3939
}
4040

4141
return new ProxyServer<TIncomingMessage, TServerResponse, TError>(options);

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

Lines changed: 129 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -129,79 +129,81 @@ describe("#createProxyServer.web() using own http server", () => {
129129
.end();
130130
}));
131131

132-
it.skipIf(() => process.env.FORCE_UNDICI_PATH === "true")("should detect a proxyReq event and modify headers", () => new Promise<void>(done => {
133-
const proxy = httpProxy.createProxyServer({
134-
target: address(8080),
135-
});
132+
it.skipIf(() => process.env.FORCE_FETCH_PATH
133+
=== "true")("should detect a proxyReq event and modify headers", () => new Promise<void>(done => {
134+
const proxy = httpProxy.createProxyServer({
135+
target: address(8080),
136+
});
136137

137-
proxy.on("proxyReq", (proxyReq, _req, _res, _options) => {
138-
proxyReq.setHeader("X-Special-Proxy-Header", "foobar");
139-
});
138+
proxy.on("proxyReq", (proxyReq, _req, _res, _options) => {
139+
proxyReq.setHeader("X-Special-Proxy-Header", "foobar");
140+
});
140141

141-
function requestHandler(req: http.IncomingMessage, res: http.ServerResponse) {
142-
proxy.web(req, res);
143-
}
142+
function requestHandler(req: http.IncomingMessage, res: http.ServerResponse) {
143+
proxy.web(req, res);
144+
}
144145

145-
const proxyServer = http.createServer(requestHandler);
146+
const proxyServer = http.createServer(requestHandler);
146147

147-
const source = http.createServer((req, res) => {
148-
res.end();
149-
source.close();
150-
proxyServer.close();
151-
expect(req.headers["x-special-proxy-header"]).toEqual("foobar");
152-
done();
153-
});
148+
const source = http.createServer((req, res) => {
149+
res.end();
150+
source.close();
151+
proxyServer.close();
152+
expect(req.headers["x-special-proxy-header"]).toEqual("foobar");
153+
done();
154+
});
154155

155-
proxyServer.listen(ports["8081"]);
156-
source.listen(ports["8080"]);
156+
proxyServer.listen(ports["8081"]);
157+
source.listen(ports["8080"]);
157158

158-
http.request(address(8081), () => { }).end();
159-
}));
159+
http.request(address(8081), () => { }).end();
160+
}));
160161

161-
it.skipIf(() => process.env.FORCE_UNDICI_PATH === "true")('should skip proxyReq event when handling a request with header "expect: 100-continue" [https://www.npmjs.com/advisories/1486]', () => new Promise<void>(done => {
162-
const proxy = httpProxy.createProxyServer({
163-
target: address(8080),
164-
});
162+
it.skipIf(() => process.env.FORCE_FETCH_PATH
163+
=== "true")('should skip proxyReq event when handling a request with header "expect: 100-continue" [https://www.npmjs.com/advisories/1486]', () => new Promise<void>(done => {
164+
const proxy = httpProxy.createProxyServer({
165+
target: address(8080),
166+
});
165167

166-
proxy.on("proxyReq", (proxyReq, _req, _res, _options) => {
167-
proxyReq.setHeader("X-Special-Proxy-Header", "foobar");
168-
});
168+
proxy.on("proxyReq", (proxyReq, _req, _res, _options) => {
169+
proxyReq.setHeader("X-Special-Proxy-Header", "foobar");
170+
});
169171

170-
function requestHandler(req: http.IncomingMessage, res: http.ServerResponse) {
171-
proxy.web(req, res);
172-
}
172+
function requestHandler(req: http.IncomingMessage, res: http.ServerResponse) {
173+
proxy.web(req, res);
174+
}
173175

174-
const proxyServer = http.createServer(requestHandler);
176+
const proxyServer = http.createServer(requestHandler);
175177

176-
const source = http.createServer((req, res) => {
177-
res.end();
178-
source.close();
179-
proxyServer.close();
180-
expect(req.headers["x-special-proxy-header"]).not.toEqual("foobar");
181-
done();
182-
});
178+
const source = http.createServer((req, res) => {
179+
res.end();
180+
source.close();
181+
proxyServer.close();
182+
expect(req.headers["x-special-proxy-header"]).not.toEqual("foobar");
183+
done();
184+
});
183185

184-
proxyServer.listen(ports["8081"]);
185-
source.listen(ports["8080"]);
186+
proxyServer.listen(ports["8081"]);
187+
source.listen(ports["8080"]);
186188

187-
const postData = "".padStart(1025, "x");
189+
const postData = "".padStart(1025, "x");
188190

189-
const postOptions = {
190-
hostname: "127.0.0.1",
191-
port: ports["8081"],
192-
path: "/",
193-
method: "POST",
194-
headers: {
195-
"Content-Type": "application/x-www-form-urlencoded",
196-
"Content-Length": Buffer.byteLength(postData),
197-
expect: "100-continue",
198-
},
199-
};
191+
const postOptions = {
192+
hostname: "127.0.0.1",
193+
port: ports["8081"],
194+
path: "/",
195+
method: "POST",
196+
headers: {
197+
"Content-Type": "application/x-www-form-urlencoded",
198+
"Content-Length": Buffer.byteLength(postData),
199+
expect: "100-continue",
200+
},
201+
};
200202

201-
const req = http.request(postOptions, () => { });
202-
req.write(postData);
203-
req.end();
204-
}));
203+
const req = http.request(postOptions, () => { });
204+
req.write(postData);
205+
req.end();
206+
}));
205207

206208
it("should proxy the request and handle error via callback", () => new Promise<void>(done => {
207209
const proxy = httpProxy.createProxyServer({
@@ -390,85 +392,87 @@ describe("#createProxyServer.web() using own http server", () => {
390392
req.end();
391393
}));
392394

393-
it.skipIf(() => process.env.FORCE_UNDICI_PATH === "true")("should proxy the request and provide a proxyRes event with the request and response parameters", () => new Promise<void>(done => {
394-
const proxy = httpProxy.createProxyServer({
395-
target: address(8080),
396-
});
397-
398-
function requestHandler(req: http.IncomingMessage, res: http.ServerResponse) {
399-
proxy.once("proxyRes", (proxyRes, pReq, pRes) => {
400-
source.close();
401-
proxyServer.close();
402-
expect(proxyRes != null).toBe(true);
403-
expect(pReq).toEqual(req);
404-
expect(pRes).toEqual(res);
405-
done();
395+
it.skipIf(() => process.env.FORCE_FETCH_PATH
396+
=== "true")("should proxy the request and provide a proxyRes event with the request and response parameters", () => new Promise<void>(done => {
397+
const proxy = httpProxy.createProxyServer({
398+
target: address(8080),
406399
});
407400

408-
proxy.web(req, res);
409-
}
401+
function requestHandler(req: http.IncomingMessage, res: http.ServerResponse) {
402+
proxy.once("proxyRes", (proxyRes, pReq, pRes) => {
403+
source.close();
404+
proxyServer.close();
405+
expect(proxyRes != null).toBe(true);
406+
expect(pReq).toEqual(req);
407+
expect(pRes).toEqual(res);
408+
done();
409+
});
410410

411-
const proxyServer = http.createServer(requestHandler);
411+
proxy.web(req, res);
412+
}
412413

413-
const source = http.createServer((_req, res) => {
414-
res.end("Response");
415-
});
414+
const proxyServer = http.createServer(requestHandler);
416415

417-
proxyServer.listen(port(8086));
418-
source.listen(port(8080));
419-
http.request(address(8086), () => { }).end();
420-
}));
416+
const source = http.createServer((_req, res) => {
417+
res.end("Response");
418+
});
421419

422-
it.skipIf(() => process.env.FORCE_UNDICI_PATH === "true")("should proxy the request and provide and respond to manual user response when using modifyResponse", () => new Promise(done => {
423-
const proxy = httpProxy.createProxyServer({
424-
target: address(8080),
425-
selfHandleResponse: true,
426-
});
420+
proxyServer.listen(port(8086));
421+
source.listen(port(8080));
422+
http.request(address(8086), () => { }).end();
423+
}));
427424

428-
function requestHandler(req: http.IncomingMessage, res: http.ServerResponse) {
429-
proxy.once("proxyRes", (proxyRes, _pReq, pRes) => {
430-
proxyRes.pipe(
431-
concat((body) => {
432-
expect(body.toString("utf8")).toEqual("Response");
433-
pRes.end(Buffer.from("my-custom-response"));
434-
}),
435-
);
425+
it.skipIf(() => process.env.FORCE_FETCH_PATH
426+
=== "true")("should proxy the request and provide and respond to manual user response when using modifyResponse", () => new Promise(done => {
427+
const proxy = httpProxy.createProxyServer({
428+
target: address(8080),
429+
selfHandleResponse: true,
436430
});
437431

438-
proxy.web(req, res);
439-
}
432+
function requestHandler(req: http.IncomingMessage, res: http.ServerResponse) {
433+
proxy.once("proxyRes", (proxyRes, _pReq, pRes) => {
434+
proxyRes.pipe(
435+
concat((body) => {
436+
expect(body.toString("utf8")).toEqual("Response");
437+
pRes.end(Buffer.from("my-custom-response"));
438+
}),
439+
);
440+
});
440441

441-
const proxyServer = http.createServer(requestHandler);
442+
proxy.web(req, res);
443+
}
442444

443-
const source = http.createServer((_req, res) => {
444-
res.end("Response");
445-
});
445+
const proxyServer = http.createServer(requestHandler);
446446

447-
async.parallel(
448-
[
449-
(next) => proxyServer.listen(port(8086), next),
450-
(next) => source.listen(port(8080), next),
451-
],
452-
(_err) => {
453-
http
454-
.get(address(8086), (res) => {
455-
res.pipe(
456-
concat((body) => {
457-
expect(body.toString("utf8")).toEqual("my-custom-response");
458-
source.close();
459-
proxyServer.close();
460-
done(undefined);
461-
}),
462-
);
463-
})
464-
.once("error", (err) => {
465-
source.close();
466-
proxyServer.close();
467-
done(err);
468-
});
469-
},
470-
);
471-
}));
447+
const source = http.createServer((_req, res) => {
448+
res.end("Response");
449+
});
450+
451+
async.parallel(
452+
[
453+
(next) => proxyServer.listen(port(8086), next),
454+
(next) => source.listen(port(8080), next),
455+
],
456+
(_err) => {
457+
http
458+
.get(address(8086), (res) => {
459+
res.pipe(
460+
concat((body) => {
461+
expect(body.toString("utf8")).toEqual("my-custom-response");
462+
source.close();
463+
proxyServer.close();
464+
done(undefined);
465+
}),
466+
);
467+
})
468+
.once("error", (err) => {
469+
source.close();
470+
proxyServer.close();
471+
done(err);
472+
});
473+
},
474+
);
475+
}));
472476

473477
it("should proxy the request and handle changeOrigin option", () => new Promise<void>(done => {
474478
const proxy = httpProxy

0 commit comments

Comments
 (0)