diff options
Diffstat (limited to 'src/cdp')
| -rw-r--r-- | src/cdp/discovery.ts | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/cdp/discovery.ts b/src/cdp/discovery.ts index 3de6e97..41bcfe5 100644 --- a/src/cdp/discovery.ts +++ b/src/cdp/discovery.ts @@ -63,10 +63,10 @@ const waitForPort = async (port: number): Promise<Result<void, CdpError>> => { const isPortLive = async (port: number): Promise<boolean> => (await probePort(port)).success; // /json/version is authoritative: a stale DevToolsActivePort file may name a browser that has since exited while another now owns the port. -const queryLiveWsUrl = (port: number, timeoutMs = 1_500): Promise<string | null> => +const queryLiveWsUrl = (port: number, timeoutMs = 1_500, host = "127.0.0.1"): Promise<string | null> => new Promise<string | null>((resolve) => { const req = httpRequest( - { host: "127.0.0.1", port, path: "/json/version", method: "GET", timeout: timeoutMs }, + { host, port, path: "/json/version", method: "GET", timeout: timeoutMs }, (res) => { if (res.statusCode !== 200) { res.resume(); @@ -114,6 +114,35 @@ export const discoverEndpoint = async (): Promise<Result<CdpEndpoint, CdpError>> const override = endpointFromEnv(); if (override) return ok(override); + // LOCAL PATCH: direct remote browser support. + // + // BU_CDP_HTTP="host:port" points at a CDP *HTTP* endpoint on another machine. + // We ask it for its canonical webSocketDebuggerUrl on every discovery call, + // so the browser UUID is re-read after the remote Chrome restarts. That is + // the difference from the stock BU_CDP_WS (checked just above, so an explicit + // WebSocket URL still wins), which pins a UUID that Chrome re-mints on every + // launch. + // + // Returns no userDataDir: the profile registry lives on the remote host, so + // profile enumeration correctly degrades to the pre-profile behaviour. + const remote = process.env["BU_CDP_HTTP"]?.trim(); + if (remote) { + const sep = remote.lastIndexOf(":"); + const host = sep === -1 ? remote : remote.slice(0, sep); + const port = sep === -1 ? 9222 : Number(remote.slice(sep + 1)); + if (!host || !Number.isInteger(port) || port <= 0 || port >= 65536) { + return err(cdpError("discovery_failed", `BU_CDP_HTTP is not a valid host:port — got "${remote}"`)); + } + const ws = await queryLiveWsUrl(port, 5_000, host); + if (!ws) { + return err(cdpError( + "discovery_failed", + `No CDP endpoint answered at http://${host}:${port}/json/version (BU_CDP_HTTP). Is the remote browser running and reachable?`, + )); + } + return ok({ wsUrl: ws }); + } + const dirs = userDataDirCandidates(); const candidates: Candidate[] = []; |
