Skip to content

Commit 070c31c

Browse files
fix(browser): preserve explicit cdpPort when cdpUrl omits port
1 parent 989fc9f commit 070c31c

2 files changed

Lines changed: 9 additions & 15 deletions

File tree

extensions/browser/src/browser/cdp.helpers.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ export { isLoopbackHost };
2828
function hasRawExplicitPort(raw: string): boolean {
2929
// Strip scheme (e.g. "http://") and take only the authority portion
3030
// (everything before the first /, ?, or #).
31-
const authority =
32-
raw
33-
.replace(/^[a-z][a-z0-9+.-]*:\/\//i, "")
34-
.split(/[/?#]/, 1)[0] ?? "";
31+
const authority = raw.replace(/^[a-z][a-z0-9+.-]*:\/\//i, "").split(/[/?#]/, 1)[0] ?? "";
3532

36-
// Strip userinfo (user:pass@)the colon there is NOT a port separator.
33+
// Strip userinfo (user:pass@); the colon there is not a port separator.
3734
const hostPort = authority.includes("@")
3835
? authority.slice(authority.lastIndexOf("@") + 1)
3936
: authority;
4037

41-
// IPv6: [::1]:9222 port after closing bracket
38+
// IPv6: [::1]:9222 has a port after the closing bracket.
4239
if (hostPort.startsWith("[")) {
4340
return /^\[[^\]]+\]:\d+$/.test(hostPort);
4441
}
@@ -88,16 +85,15 @@ export function parseBrowserHttpUrl(raw: string, label: string) {
8885
const atIdx = rest.indexOf("@");
8986
const hostStart = atIdx >= 0 ? atIdx + 1 : 0;
9087
const hostPart = rest.slice(hostStart);
91-
// Find end of host: IPv6 brackets or first : / /
88+
// Find the end of the host: IPv6 brackets, a path slash, or a port colon.
9289
const hostLen = hostPart.startsWith("[")
9390
? hostPart.indexOf("]") + 1
9491
: (() => {
9592
const idx = hostPart.search(/[:/]/);
9693
return idx < 0 ? hostPart.length : idx;
9794
})();
9895
const insertAt = hostStart + hostLen;
99-
normalizedWithPort =
100-
proto + rest.slice(0, insertAt) + ":" + port + rest.slice(insertAt);
96+
normalizedWithPort = proto + rest.slice(0, insertAt) + ":" + port + rest.slice(insertAt);
10197
} else {
10298
normalizedWithPort = normalized;
10399
}

extensions/browser/src/browser/config.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -621,9 +621,7 @@ describe("browser config", () => {
621621

622622
const websocket = resolveProfile(resolved, "websocket");
623623
expect(websocket?.cdpPort).toBe(443);
624-
expect(websocket?.cdpUrl).toBe(
625-
"wss://remote-browser.example.com:443/json/version?token=abc",
626-
);
624+
expect(websocket?.cdpUrl).toBe("wss://remote-browser.example.com:443/json/version?token=abc");
627625

628626
const ipv6 = resolveProfile(resolved, "ipv6");
629627
expect(ipv6?.cdpPort).toBe(80);
@@ -716,9 +714,7 @@ describe("browser config", () => {
716714
},
717715
},
718716
});
719-
expect(() => resolveProfile(resolved, "bad")).toThrow(
720-
'must define cdpPort or cdpUrl',
721-
);
717+
expect(() => resolveProfile(resolved, "bad")).toThrow("must define cdpPort or cdpUrl");
722718
});
723719

724720
it("stale WS devtools URL + cdpPort drops path and uses cdpPort", () => {
@@ -735,6 +731,8 @@ describe("browser config", () => {
735731
const profile = resolveProfile(resolved, "chrome-cdp");
736732
expect(profile?.cdpUrl).toBe("http://127.0.0.1:9222");
737733
expect(profile?.cdpPort).toBe(9222);
734+
expect(profile?.cdpIsLoopback).toBe(true);
735+
expect(profile?.attachOnly).toBe(true);
738736
});
739737

740738
it("IPv6 URL without port defers to cdpPort", () => {

0 commit comments

Comments
 (0)