Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758/** * What the extension reads from `bridge.json` (written by the runner next to the extension files before Chrome starts): * where the bridge listens and the per-run token. Browser-safe, and strict on purpose: the extension must never * connect to anything but a bridge on this machine's loopback address. */export interface BridgeConfig { url: string; token: string; profile: string;}
export type BridgeConfigResult = { ok: true; config: BridgeConfig } | { ok: false; problems: string[] };
const TOKEN = /^[0-9a-f]{32}$/;
export function parseBridgeConfig(text: string): BridgeConfigResult { let raw: unknown; try { raw = JSON.parse(text); } catch { return { ok: false, problems: ["bridge.json: not valid JSON"] }; } if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return { ok: false, problems: ["bridge.json: must be a JSON object"] }; const o = raw as Record<string, unknown>; const problems: string[] = [];
let url = ""; if (o.url === undefined) problems.push("url: required"); else { try { const u = new URL(String(o.url)); const before = problems.length; if (u.protocol !== "ws:") problems.push("url: must use ws:// (the bridge is plain loopback)"); if (u.hostname !== "127.0.0.1") problems.push("url: host must be 127.0.0.1"); if (u.port === "") problems.push("url: needs a port"); if (u.pathname !== "/ext") problems.push("url: path must be /ext"); if (u.username !== "" || u.password !== "") problems.push("url: must not contain credentials"); if (u.search !== "" || u.hash !== "") problems.push("url: must not contain a query or fragment (the token travels separately)"); if (problems.length === before) url = u.toString(); } catch { problems.push("url: not a valid URL"); } }
let token = ""; if (o.token === undefined) problems.push("token: required"); else if (typeof o.token === "string" && TOKEN.test(o.token)) token = o.token; else problems.push("token: must be 32 hex characters");
let profile = "default"; if (o.profile !== undefined) { if (typeof o.profile === "string" && o.profile !== "") profile = o.profile; else problems.push("profile: must be a non-empty string"); }
return problems.length > 0 ? { ok: false, problems } : { ok: true, config: { url, token, profile } };}