diff options
Diffstat (limited to 'src/daemon')
| -rw-r--r-- | src/daemon/bridge.ts | 16 | ||||
| -rw-r--r-- | src/daemon/protocol.ts | 9 |
2 files changed, 14 insertions, 11 deletions
diff --git a/src/daemon/bridge.ts b/src/daemon/bridge.ts index de6dec7..3f8b604 100644 --- a/src/daemon/bridge.ts +++ b/src/daemon/bridge.ts @@ -16,6 +16,7 @@ import { isCdpRawMessage } from "../cdp/types"; import type { CdpRawMessage } from "../cdp/types"; import type { WireRequest, WireResponse, WireEvent } from "./protocol"; import { CDP_CONNECT_TIMEOUT_MS, CDP_COMMAND_TIMEOUT_MS } from "./protocol"; +import { asString, isRecord } from "../util/guards"; // ── Types ────────────────────────────────────────────────────────────────────── @@ -179,9 +180,9 @@ export const createCdpBridge = (): CdpBridge => { } else { // Track session ownership for attach responses if (cb.isAttach && msg.result) { - const result = msg.result as { sessionId?: string }; - if (result.sessionId) { - router.record(cb.clientId, result.sessionId); + const sessionId = isRecord(msg.result) ? asString(msg.result["sessionId"]) : undefined; + if (sessionId) { + router.record(cb.clientId, sessionId); } } cb.send(cb.clientId, { type: "response", id: localId, result: msg.result }); @@ -238,12 +239,15 @@ export const createCdpBridge = (): CdpBridge => { if (ws && ws.readyState === WebSocket.OPEN) return; // Resolve URL (discover if not cached, or clear cache on retry) - let url = wsUrl; - if (!url || reconnectAttempt > 0) { + const cachedUrl = wsUrl; + let url: string; + if (cachedUrl === null || cachedUrl === "" || reconnectAttempt > 0) { const d = await discoverWsUrl(); if (!d.success) { scheduleRetry(); return; } url = d.data; wsUrl = url; + } else { + url = cachedUrl; } const settledPromise = new Promise<void>((settle) => { @@ -256,7 +260,7 @@ export const createCdpBridge = (): CdpBridge => { let sock: WebSocket; try { - sock = new WebSocket(url!, { perMessageDeflate: false }); + sock = new WebSocket(url, { perMessageDeflate: false }); } catch { scheduleRetry(); return; diff --git a/src/daemon/protocol.ts b/src/daemon/protocol.ts index 4fa4a2a..997a6c8 100644 --- a/src/daemon/protocol.ts +++ b/src/daemon/protocol.ts @@ -10,6 +10,8 @@ * - readline line-delimited: https://nodejs.org/docs/latest-v22.x/api/readline.html#event-line */ +import { isRecord } from "../util/guards"; + // ── Message types ────────────────────────────────────────────────────────────── /** A CDP command sent from a pi client to the daemon. */ @@ -60,15 +62,12 @@ export type WireMessage = WireRequest | WireResponse | WireEvent | WireControl; // ── Type guard ───────────────────────────────────────────────────────────────── -const isObject = (v: unknown): v is Readonly<Record<string, unknown>> => - typeof v === "object" && v !== null && !Array.isArray(v); - /** * Validates that an unknown parsed-JSON value is a well-formed WireMessage. * Does NOT validate the full CDP semantics — only structural shape. */ export const isWireMessage = (v: unknown): v is WireMessage => { - if (!isObject(v)) return false; + if (!isRecord(v)) return false; const t = v["type"]; if (typeof t !== "string") return false; @@ -84,7 +83,7 @@ export const isWireMessage = (v: unknown): v is WireMessage => { if (typeof v["id"] !== "number") return false; const errVal = v["error"]; if (errVal !== undefined) { - if (!isObject(errVal)) return false; + if (!isRecord(errVal)) return false; if (typeof errVal["message"] !== "string") return false; if (typeof errVal["code"] !== "number") return false; } |
