INNER CODE UNIT · TypeScript

readRunState

JuliusBrussee/caveman · packages/pi-extension/src/index.ts:57

function readRunState(gateway: string): RunState | undefined {
  let port: string;
  try {
    const url = new URL(gateway);
    if (!isLoopbackUrl(gateway) || url.pathname !== "/" || url.search || url.hash || url.username || url.password) return undefined;
    port = url.port || (url.protocol === "https:" ? "443" : "80");
  } catch {
    return undefined;
  }
  try {
    const state = JSON.parse(readFileSync(join(cavemanHome(), "run", `${port}.json`), "utf8"));
    // A SIGKILL'd proxy leaves its run-state file behind by design; mirror the
    // CLI's field validation and require the recorded pid to still be alive so
    // a stale file (or a stranger on the port) can never open the gate.
    if (state?.schema !== "caveman.proxy.run.v1") return undefined;
    if (!Number.isSafeInteger(state.pid) || state.pid < 1 || state.port !== Number(port)
      || typeof state.instance_token !== "string" || !state.instance_token) return undefined;
    if (state.owner !== "wrap" && state.owner !== "start") return undefined;

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…