/** * Read-only view of the target pi-browser-harness persists for `/browser-target`. * * The stored value is the raw string a user would have put in BU_CDP_HTTP * ("local", "10.88.0.25:9223", …) — kept in that vocabulary deliberately, so * there is one resolution path in endpoint.ts and a stored value can never mean * something the variable could not. * * Every read failure degrades to "nothing stored" rather than throwing: a * corrupt or absent file must fall back to the built-in default, not break search. */ import { readFile } from "node:fs/promises"; import { targetFilePath } from "./paths.ts"; const CURRENT_VERSION = 1; /** * The persisted target, or null when nothing is stored, the file is missing, or * it cannot be parsed. A file written by a newer version is treated as "nothing * stored" rather than guessed at. */ export const readStoredTarget = async (): Promise => { let raw: string; try { raw = await readFile(targetFilePath(), "utf8"); } catch { return null; } try { const parsed: unknown = JSON.parse(raw); if (typeof parsed !== "object" || parsed === null) return null; const file = parsed as Record; if (file["version"] !== CURRENT_VERSION) return null; const target = file["target"]; if (typeof target !== "string") return null; const trimmed = target.trim(); return trimmed.length > 0 ? trimmed : null; } catch { return null; } };