Skip to content

Commit 7a9cd20

Browse files
committed
refactor(api): read proxy host and scheme from WHATWG URL fields
1 parent a4eb358 commit 7a9cd20

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎src/api/proxy.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ export function getProxyForUrl(
5353
noProxyFallback?: string | null,
5454
): string {
5555
const parsedUrl = URL.parse(url);
56-
if (!parsedUrl?.host) {
56+
if (!parsedUrl?.hostname) {
5757
return ""; // Don't proxy URLs without a valid scheme or host.
5858
}
5959

60-
const proto = parsedUrl.protocol.split(":", 1)[0];
61-
// Stripping ports in this way instead of using parsedUrl.hostname to make
62-
// sure that the brackets around IPv6 addresses are kept.
63-
const hostname = parsedUrl.host.replace(/:\d*$/, "");
60+
const proto = parsedUrl.protocol.slice(0, -1);
61+
// WHATWG keeps the brackets around IPv6 hosts, so no-proxy entries like
62+
// "[::1]" still match.
63+
const hostname = parsedUrl.hostname;
6464
const port = Number.parseInt(parsedUrl.port) || DEFAULT_PORTS[proto] || 0;
6565
if (!shouldProxy(hostname, port, noProxy, noProxyFallback)) {
6666
return ""; // Don't proxy URLs that match NO_PROXY.

‎test/unit/api/proxy.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ describe("proxy", () => {
9898
noProxy: "example.com:8443",
9999
url: "https://example.com:8443",
100100
},
101+
{
102+
name: "bracketed IPv6",
103+
noProxy: "[::1]",
104+
url: "http://[::1]:8080",
105+
},
101106
])(
102107
"bypasses proxy when hostname matches noProxy ($name)",
103108
({ noProxy, url }) => {

0 commit comments

Comments
 (0)