blob: 6ff0db476cb75ae05d9c82c19740658a62a52dfb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
/**
* 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<string | null> => {
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<string, unknown>;
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;
}
};
|