From b913f2d4af218ae61de7073778feb56da89a15de Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Tue, 28 Jul 2026 22:08:16 +0530 Subject: refactor: daemon internals, and fix a leak the merge exposed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IdMultiplexer and EventRouter were the only classes in src/; they are now createIdMultiplexer/createEventRouter closures like every other factory here. The multiplexer kept {clientId, localId} per daemon id while the bridge kept {send, clientId, localId, timer, isAttach} under the same key — two maps of overlapping data, and mux.resolve()'s return value was discarded at three of four call sites. One map now holds the union. That merge fixes a real leak: removeClient cleared the multiplexer but never the callbacks map, so a disconnected client's timers stayed armed and later fired a response at a socket that was gone. clearClient now cancels them, and a test pins it. handleRequest polled every 250ms for up to 15s per request when Chrome was down; N queued requests now await one connect signal. server.ts uses its own protocol's serialize/deserialize instead of hand-rolled JSON, and broadcast no longer routes through `this` — the only `this` in the codebase, and the one thing that breaks when the server object is destructured. cdp/event-queue.ts held two unrelated concerns; the request/timeout half moves to cdp/pending-requests.ts. cdp/daemon-transport.ts is the client half of the daemon protocol, so it moves to daemon/transport.ts. test/daemon/ needs adding to the test glob to run at all — there was no daemon test directory before because none of this was reachable without live Chrome. Co-Authored-By: Claude Opus 5 (1M context) --- src/cdp/event-queue.ts | 47 ----------------------------------------------- 1 file changed, 47 deletions(-) (limited to 'src/cdp/event-queue.ts') diff --git a/src/cdp/event-queue.ts b/src/cdp/event-queue.ts index 330bdb8..08024b0 100644 --- a/src/cdp/event-queue.ts +++ b/src/cdp/event-queue.ts @@ -1,9 +1,5 @@ -import { type Result, err } from "../util/result"; -import { type CdpError, cdpError } from "./errors"; import type { CdpEvent } from "./types"; -export type CdpResult = Result; - export type EventQueue = { readonly push: (e: CdpEvent) => void; readonly end: () => void; @@ -40,46 +36,3 @@ export const makeEventQueue = (): EventQueue => { }, }; }; - -export type Pending = { - readonly resolve: (v: Result) => void; - readonly timer: ReturnType; - readonly method: string; -}; - -export const rejectAllPending = (pending: Map, reason: string): void => { - for (const [, p] of pending) { - clearTimeout(p.timer); - p.resolve(err(cdpError("transport_closed", reason, p.method))); - } - pending.clear(); -}; - -export const sendWithTimeout = ( - pending: Map, - id: number, - method: string, - timeoutMs: number, - timeoutLabel: string, - send: () => void, -): Promise => - new Promise((resolve) => { - const timer = setTimeout(() => { - pending.delete(id); - resolve(err(cdpError("timeout", `${timeoutLabel} timeout after ${timeoutMs}ms: ${method}`, method))); - }, timeoutMs); - pending.set(id, { resolve, timer, method }); - try { - send(); - } catch (e) { - clearTimeout(timer); - pending.delete(id); - resolve(err(cdpError("transport_closed", e instanceof Error ? e.message : String(e), method))); - } - }); - -export const makeOnClose = (closeListeners: Set<() => void>) => - (cb: () => void): (() => void) => { - closeListeners.add(cb); - return () => closeListeners.delete(cb); - }; -- cgit v1.2.3