/** * The running worker answers on 127.0.0.1: so the CLI (in an * SSH session) can ask for its status, stop it, or take snapshots. The token * changes every run and is kept in runtime.json in the user's own profile. */ import { readJson } from './config'; export interface Runtime { pid: number; watchdogPid: number | null; port: number; token: string; startedAt: string; version: string; } export async function callWorker( runtimePath: string, path: string, init: { method?: string; timeoutMs?: number } = {} ): Promise { const runtime = await readJson(runtimePath); if (!runtime) return null; try { const response = await fetch(`http://127.0.0.1:${runtime.port}${path}`, { method: init.method ?? 'GET', headers: { 'x-vash-token': runtime.token }, signal: AbortSignal.timeout(init.timeoutMs ?? 4000) }); if (!response.ok) return null; return (await response.json()) as T; } catch { return null; } } export function authorized(request: Request, token: string) { const given = request.headers.get('x-vash-token') ?? ''; if (given.length !== token.length) return false; let diff = 0; for (let index = 0; index < token.length; index++) diff |= given.charCodeAt(index) ^ token.charCodeAt(index); return diff === 0; }