Skip to content

Commit 36cd3a9

Browse files
authored
fix(logger-plugin): handle undefined protocol and hostname (#1036)
1 parent 45364e4 commit 36cd3a9

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/plugins/default/logger-plugin.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,30 @@ export const loggerPlugin: Plugin = (proxyServer, options) => {
4444
const originalUrl = req.originalUrl ?? `${req.baseUrl || ''}${req.url}`;
4545

4646
// construct targetUrl
47-
const port = getPort(proxyRes.req?.agent?.sockets);
47+
let target: URL;
4848

49-
const obj = {
50-
protocol: proxyRes.req.protocol,
51-
host: proxyRes.req.host,
52-
pathname: proxyRes.req.path,
53-
} as URL;
49+
try {
50+
const port = getPort(proxyRes.req?.agent?.sockets);
5451

55-
const target = new URL(`${obj.protocol}//${obj.host}${obj.pathname}`);
52+
const obj = {
53+
protocol: proxyRes.req.protocol,
54+
host: proxyRes.req.host,
55+
pathname: proxyRes.req.path,
56+
} as URL;
5657

57-
if (port) {
58-
target.port = port;
58+
target = new URL(`${obj.protocol}//${obj.host}${obj.pathname}`);
59+
60+
if (port) {
61+
target.port = port;
62+
}
63+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
64+
} catch (err) {
65+
// nock issue (https://github.com/chimurai/http-proxy-middleware/issues/1035)
66+
// fallback to old implementation (less correct - without port)
67+
target = new URL(options.target as URL);
68+
target.pathname = proxyRes.req.path;
5969
}
70+
6071
const targetUrl = target.toString();
6172

6273
const exchange = `[HPM] ${req.method} ${originalUrl} -> ${targetUrl} [${proxyRes.statusCode}]`;

0 commit comments

Comments
 (0)