From f9dcdf7f6dcdf2979693a9f179eda2b9995d57ae Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Tue, 28 Jul 2026 01:19:58 +0530 Subject: refactor: close the remaining unguarded type assertions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Types the event-queue iterator's TReturn as undefined so the terminal result needs no cast; declares the isError field the pi runtime reads by duck-typing; binds socket to a local const in the daemon transport; replaces the @ts-ignore on the optional sharp import with a non-literal specifier so src/ still typechecks where sharp was not installed. Adds src/util/guards.ts as the single home for narrowing over unknown — isRecord plus scalar/array readers — and converts the two private isObject copies in cdp/types.ts and daemon/protocol.ts along with every inline `as Record` in domains/js.ts and domains/render.ts. Gives Accessibility.getFullAXTree a real result schema, closing the last unvalidated CDP boundary: only nodeId is required, every object carries additionalProperties, and each AX value stays Unknown, so the tree decodes as a whole or fails as a whole rather than being filtered. Guards the five tool-result `details` readers, the daemon attach response, the persisted session state, the browser-namespace flag, and the fs errno narrowing, each feeding an already-present failure path. The daemon-transport socket binding is not a bug fix: send() runs synchronously inside sendWithTimeout's promise executor, so socket cannot be reassigned between the check and the write. It removes the assertion and makes the write immune to a later refactor that adds an await. Two assertions survive by design and are documented in the task report: the never->unknown re-widening in util/tool.ts, which the type-erased tool registry makes unavoidable, and the AsyncFunction constructor lookup in domains/js.ts, which JavaScript exposes no typed binding for. Co-Authored-By: Claude Opus 5 (1M context) --- src/cdp/daemon-transport.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'src/cdp/daemon-transport.ts') diff --git a/src/cdp/daemon-transport.ts b/src/cdp/daemon-transport.ts index 4b2fb78..819e581 100644 --- a/src/cdp/daemon-transport.ts +++ b/src/cdp/daemon-transport.ts @@ -67,21 +67,23 @@ export const createDaemonTransport = (clientId: string): CdpTransport => { resolve(r); }; + let sock: Socket; try { - socket = createConnection(DAEMON_SOCKET_PATH); + sock = createConnection(DAEMON_SOCKET_PATH); } catch (e) { settle(err(cdpError("transport_closed", e instanceof Error ? e.message : String(e)))); return; } + socket = sock; const connectTimer = setTimeout(() => { cleanup("Connection timeout"); settle(err(cdpError("timeout", `Daemon connection timed out after ${timeoutMs}ms`))); }, timeoutMs); - socket.on("connect", () => { + sock.on("connect", () => { clearTimeout(connectTimer); - rl = createInterface({ input: socket!, crlfDelay: Infinity }); + rl = createInterface({ input: sock, crlfDelay: Infinity }); rl.on("line", (line: string) => { const msg = deserialize(line); @@ -130,11 +132,11 @@ export const createDaemonTransport = (clientId: string): CdpTransport => { } }); - socket!.on("error", () => { + sock.on("error", () => { // Errors are surfaced via 'close' }); - socket!.on("close", () => { + sock.on("close", () => { clearTimeout(connectTimer); cleanup("Daemon socket closed"); // If connect hasn't settled yet, settle with error @@ -143,10 +145,10 @@ export const createDaemonTransport = (clientId: string): CdpTransport => { // Send registration const regMsg: WireControl = { type: "control", action: "register", clientId }; - socket!.write(serialize(regMsg) + "\n"); + sock.write(serialize(regMsg) + "\n"); }); - socket.on("error", (e: NodeJS.ErrnoException) => { + sock.on("error", (e: NodeJS.ErrnoException) => { clearTimeout(connectTimer); const msg = e.code === "ENOENT" ? "Daemon not running — socket not found" @@ -176,7 +178,8 @@ export const createDaemonTransport = (clientId: string): CdpTransport => { const timeoutMs = opts?.timeoutMs ?? 15_000; const sessionId = opts?.sessionId ?? null; - if (!socket || socket.destroyed || !registered) { + const sock = socket; + if (sock === null || sock.destroyed || !registered) { return Promise.resolve(err(cdpError("transport_closed", "Daemon not connected", method))); } @@ -189,7 +192,7 @@ export const createDaemonTransport = (clientId: string): CdpTransport => { ...(sessionId !== null && sessionId !== undefined ? { sessionId } : {}), }; - return sendWithTimeout(pending, id, method, timeoutMs, "Daemon", () => socket!.write(serialize(req) + "\n")); + return sendWithTimeout(pending, id, method, timeoutMs, "Daemon", () => sock.write(serialize(req) + "\n")); }; // ── Events ───────────────────────────────────────────────────────────── -- cgit v1.2.3