refactor(desktop): extract hidden Playwright CDP target helper

Move CDPTargetDescriptor and the reclaim predicate out of
playwright-cdp.ts into a dedicated module so the reclamation rule can be
unit-tested in isolation and reused without pulling in the whole hidden
browser manager.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-24 09:39:03 +08:00
parent f6e553dcc3
commit b48729e4a2
3 changed files with 66 additions and 18 deletions
+5 -18
View File
@@ -2,6 +2,10 @@ import { BrowserWindow, nativeTheme } from "electron/main";
import type { BrowserWindow as ElectronBrowserWindow, Session } from "electron/main";
import type { Browser as PlaywrightBrowser, Page as PlaywrightPage } from "playwright-core";
import {
isReclaimableHiddenPlaywrightTarget,
type CDPTargetDescriptor,
} from "./playwright-cdp-targets";
import { STANDARD_USER_AGENT } from "./user-agent";
const hiddenPlaywrightIdleTTLms = 5 * 60_000;
@@ -13,13 +17,6 @@ const cdpPort = Number.parseInt(process.env.GEO_ELECTRON_CDP_PORT ?? "9339", 10)
const cdpEndpointURL = `http://127.0.0.1:${cdpPort}`;
const cdpTargetsListURL = `${cdpEndpointURL}/json/list`;
interface CDPTargetDescriptor {
id?: string;
type?: string;
title?: string;
url?: string;
}
interface HiddenPlaywrightRecord {
key: string;
accountId: string;
@@ -347,7 +344,7 @@ async function pruneStaleCDPTargets(): Promise<number> {
let closedCount = 0;
for (const target of targets) {
if (!isStaleCDPTarget(target)) {
if (!isReclaimableHiddenPlaywrightTarget(target)) {
continue;
}
@@ -368,16 +365,6 @@ async function pruneStaleCDPTargets(): Promise<number> {
return closedCount;
}
function isStaleCDPTarget(target: CDPTargetDescriptor): boolean {
if ((target.type ?? "").trim() !== "page") {
return false;
}
if (!(target.id ?? "").trim()) {
return false;
}
return !(target.title ?? "").trim() && !(target.url ?? "").trim();
}
function isConnectOverCDPTimeout(error: unknown): boolean {
return error instanceof Error
&& error.name === "TimeoutError"