aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/bridge.ts
blob: dc10f2b2be063b7bf4c2376da811d1d47dac4853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import WebSocket from "ws";
import { discoverWsUrl } from "../cdp/discovery";
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";
import type { CdpError } from "../cdp/errors";
import type { Result } from "../util/result";

export type SendToClient = (clientId: string, msg: WireResponse | WireEvent) => void;

export type EventHandler = (event: WireEvent, targetClientIds: string[]) => void;

export type CloseHandler = () => void;

export type CdpBridge = {
  start(): Promise<void>;
  stop(): Promise<void>;
  handleRequest(req: WireRequest, clientId: string, send: SendToClient): Promise<void>;
  isAlive(): boolean;
  getSessionOwner(sessionId: string): string | undefined;
  removeClient(clientId: string): void;
  onEvent(handler: EventHandler): void;
  onClose(handler: CloseHandler): void;
};

export type InFlight = {
  readonly clientId: string;
  readonly localId: number;
  readonly send: SendToClient;
  readonly timer: ReturnType<typeof setTimeout>;
  readonly isAttach: boolean;
};

// One map, keyed by the daemon-side id: the client that asked, the id it used, and everything needed to answer it.
export type IdMultiplexer = {
  allocate(entry: Omit<InFlight, "timer">, arm: (daemonId: number) => InFlight["timer"]): number;
  take(daemonId: number): InFlight | undefined;
  takeAll(): ReadonlyArray<InFlight>;
  clearClient(clientId: string): void;
};

export const createIdMultiplexer = (): IdMultiplexer => {
  let nextId = 1;
  const inFlight = new Map<number, InFlight>();

  return {
    allocate(entry, arm) {
      const daemonId = nextId++;
      inFlight.set(daemonId, { ...entry, timer: arm(daemonId) });
      return daemonId;
    },
    take(daemonId) {
      const e = inFlight.get(daemonId);
      if (!e) return undefined;
      inFlight.delete(daemonId);
      clearTimeout(e.timer);
      return e;
    },
    takeAll() {
      const all = [...inFlight.values()];
      inFlight.clear();
      for (const e of all) clearTimeout(e.timer);
      return all;
    },
    // A disconnected client must not be answered later: dropping its entries also cancels their timeouts.
    clearClient(clientId) {
      for (const [daemonId, e] of inFlight) {
        if (e.clientId !== clientId) continue;
        clearTimeout(e.timer);
        inFlight.delete(daemonId);
      }
    },
  };
};

export type EventRouter = {
  record(clientId: string, sessionId: string): void;
  release(sessionId: string): void;
  getOwner(sessionId: string): string | undefined;
  removeClient(clientId: string): void;
  route(event: WireEvent): string[];
};

export const createEventRouter = (): EventRouter => {
  const owners = new Map<string, string>();
  const clientSessions = new Map<string, Set<string>>();

  return {
    record(clientId, sessionId) {
      const prev = owners.get(sessionId);
      if (prev && prev !== clientId) clientSessions.get(prev)?.delete(sessionId);
      owners.set(sessionId, clientId);
      let s = clientSessions.get(clientId);
      if (!s) { s = new Set(); clientSessions.set(clientId, s); }
      s.add(sessionId);
    },
    release(sessionId) {
      const owner = owners.get(sessionId);
      if (owner) clientSessions.get(owner)?.delete(sessionId);
      owners.delete(sessionId);
    },
    getOwner(sessionId) {
      return owners.get(sessionId);
    },
    removeClient(clientId) {
      const sessions = clientSessions.get(clientId);
      if (sessions) { for (const sid of sessions) owners.delete(sid); }
      clientSessions.delete(clientId);
    },
    route(event) {
      if (event.sessionId) {
        const owner = owners.get(event.sessionId);
        return owner ? [owner] : [];
      }
      return [...clientSessions.keys()];
    },
  };
};

type CdpBridgeDependencies = {
  discoverWsUrl?: () => Promise<Result<string, CdpError>>;
  createWebSocket?: (url: string) => WebSocket;
  connectTimeoutMs?: number;
  onAttemptSettled?: () => void;
};

export const createCdpBridge = (options: CdpBridgeDependencies = {}): CdpBridge => {
  const {
    discoverWsUrl: discoverWsUrlImpl = discoverWsUrl,
    createWebSocket: createWebSocketImpl = (url: string): WebSocket =>
      new WebSocket(url, { perMessageDeflate: false }),
    connectTimeoutMs = CDP_CONNECT_TIMEOUT_MS,
    onAttemptSettled,
  } = options;

  let ws: WebSocket | null = null;
  let wsUrl: string | null = null;
  const mux = createIdMultiplexer();
  const router = createEventRouter();
  let eventHandler: EventHandler | null = null;
  let closeHandler: CloseHandler | null = null;

  // daemonId -> { generation, clientId }: lets a failed attempt reject only its own
  // in-flight requests, and a gone client's entries be dropped without late answers.
  const generations = new Map<number, { generation: number; clientId: string }>();

  let stopped = false;
  let connecting = false;
  let attemptGeneration = 0;
  let activeAttempt = 0;
  let activeSocket: WebSocket | null = null;

  const onChromeMessage = (raw: string): void => {
    let parsed: unknown;
    try { parsed = JSON.parse(raw); } catch { return; }
    if (!isCdpRawMessage(parsed)) return;
    const msg: CdpRawMessage = parsed;

    if (msg.id !== undefined) {
      generations.delete(msg.id);
      const cb = mux.take(msg.id);
      if (!cb) return;
      const localId = cb.localId;

      if (msg.error) {
        cb.send(cb.clientId, {
          type: "response",
          id: localId,
          error: { code: msg.error.code ?? -1, message: msg.error.message },
        });
      } else {
        if (cb.isAttach && msg.result) {
          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 });
      }
      return;
    }

    if (!msg.method) return;

    if (msg.method === "Inspector.detached" && msg.sessionId) {
      router.release(msg.sessionId);
    }

    const wireEvent: WireEvent = {
      type: "event",
      method: msg.method,
      ...(msg.params !== undefined ? { params: msg.params } : {}),
      ...(msg.sessionId !== undefined ? { sessionId: msg.sessionId } : {}),
    };

    if (eventHandler) {
      const targets = router.route(wireEvent);
      if (targets.length > 0) {
        eventHandler(wireEvent, targets);
      }
    }
  };

  const rejectCallbacksWhere = (
    pred: (info: { generation: number; clientId: string }) => boolean,
  ): void => {
    for (const [daemonId, info] of generations) {
      if (!pred(info)) continue;
      generations.delete(daemonId);
      const cb = mux.take(daemonId);
      if (!cb) continue;
      cb.send(cb.clientId, {
        type: "response",
        id: cb.localId,
        error: { code: -32000, message: "Chrome disconnected" },
      });
    }
  };

  const rejectAttemptCallbacks = (generation: number): void => {
    rejectCallbacksWhere((info) => info.generation === generation);
  };

  const rejectAllPendingCallbacks = (): void => {
    rejectCallbacksWhere(() => true);
  };

  const closeSocket = (socket: WebSocket): void => {
    try {
      socket.close();
    } catch {}
  };

  const isCurrentAttempt = (generation: number): boolean =>
    generation === activeAttempt && !stopped;

  const isCurrentAttemptSocket = (generation: number, socket: WebSocket): boolean =>
    isCurrentAttempt(generation) && activeSocket === socket;

  const clearActiveAttempt = (generation: number): void => {
    if (activeAttempt === generation) {
      activeAttempt = 0;
      activeSocket = null;
    }
  };

  // On-demand Chrome connection. Attempts happen only from start() and from
  // handleRequest() while disconnected; a failed or disconnected bridge stays
  // disconnected until the next explicit browser demand. No background retry loop.
  const tryConnect = (): void => {
    if (stopped || connecting || (ws && ws.readyState === WebSocket.OPEN)) return;

    const generation = ++attemptGeneration;
    activeAttempt = generation;
    connecting = true;

    const attempt = async (): Promise<void> => {
      let settled = false;
      let sock: WebSocket | null = null;
      let timeout: ReturnType<typeof setTimeout> | null = null;
      let settleAttempt: () => void = () => {};

      const settledPromise = new Promise<void>((resolve) => {
        settleAttempt = (): void => {
          if (settled) return;
          settled = true;
          if (timeout) {
            clearTimeout(timeout);
            timeout = null;
          }
          if (generation === activeAttempt && !stopped) {
            wsUrl = null;
            connecting = false;
          }
          rejectAttemptCallbacks(generation);
          onAttemptSettled?.();
          resolve();
        };
      });

      const clearAttemptTimeout = (): void => {
        if (timeout) {
          clearTimeout(timeout);
          timeout = null;
        }
      };

      const failCurrentAttempt = (notifyClose: boolean): void => {
        clearAttemptTimeout();
        const isCurrent = isCurrentAttempt(generation);
        if (!isCurrent) {
          rejectAttemptCallbacks(generation);
          return;
        }

        clearActiveAttempt(generation);
        if (ws === sock) ws = null;
        wsUrl = null;
        connecting = false;
        // Not via settleAttempt: a connection that opened already settled its attempt, so
        // its requests would otherwise hang to the command timeout instead of failing now.
        rejectAttemptCallbacks(generation);
        if (notifyClose) closeHandler?.();
        settleAttempt();
      };

      timeout = setTimeout(() => {
        if (!isCurrentAttempt(generation) || stopped) {
          failCurrentAttempt(false);
          return;
        }

        if (sock && sock.readyState !== WebSocket.CLOSED) {
          closeSocket(sock);
        }
        failCurrentAttempt(true);
      }, connectTimeoutMs);

      let url = wsUrl;
      if (!url) {
        const d = await discoverWsUrlImpl();
        if (!isCurrentAttempt(generation) || stopped) {
          failCurrentAttempt(false);
          return;
        }
        if (!d.success) {
          failCurrentAttempt(false);
          return;
        }
        url = d.data;
      }

      if (!isCurrentAttempt(generation) || stopped) {
        failCurrentAttempt(false);
        return;
      }

      try {
        sock = createWebSocketImpl(url);
      } catch {
        failCurrentAttempt(false);
        return;
      }

      if (!isCurrentAttempt(generation) || stopped) {
        closeSocket(sock);
        failCurrentAttempt(false);
        return;
      }

      wsUrl = url;
      activeSocket = sock;

      const onOpen = (): void => {
        if (!sock || !isCurrentAttemptSocket(generation, sock)) {
          clearAttemptTimeout();
          if (sock && sock.readyState !== WebSocket.CLOSED) {
            closeSocket(sock);
          }
          failCurrentAttempt(false);
          return;
        }

        clearAttemptTimeout();
        ws = sock;
        try {
          ws.send(JSON.stringify({ id: 0, method: "Target.setDiscoverTargets", params: { discover: true } }));
        } catch {
          failCurrentAttempt(true);
          if (sock.readyState !== WebSocket.CLOSED) {
            closeSocket(sock);
          }
          return;
        }
        console.log("[pi-browser-daemon] Connected to Chrome ✓");
        settleAttempt();
      };

      const onMessage = (data: WebSocket.Data): void => {
        if (!sock || !isCurrentAttemptSocket(generation, sock)) return;
        onChromeMessage(typeof data === "string" ? data : data.toString());
      };

      const onError = (): void => {
        clearAttemptTimeout();
        const notifyClose = !!sock && isCurrentAttemptSocket(generation, sock);
        failCurrentAttempt(notifyClose);
        if (sock && sock.readyState !== WebSocket.CLOSED) {
          closeSocket(sock);
        }
      };

      const onClose = (): void => {
        clearAttemptTimeout();
        const notifyClose = !!sock && isCurrentAttemptSocket(generation, sock);
        failCurrentAttempt(notifyClose);
      };

      sock.on("open", onOpen);
      sock.on("message", onMessage);
      sock.on("error", onError);
      sock.on("close", onClose);

      // The promise must always settle even on constructor errors.
      await settledPromise;
    };

    attempt().catch(() => {
      if (isCurrentAttempt(generation)) {
        connecting = false;
      }
    });
  };

  const start = async (): Promise<void> => {
    stopped = false;
    tryConnect();
  };

  const stop = async (): Promise<void> => {
    stopped = true;
    attemptGeneration += 1;
    activeAttempt = 0;
    activeSocket = null;
    connecting = false;
    rejectAllPendingCallbacks();
    if (ws) {
      try {
        ws.close(1000, "Shutdown");
      } catch {}
      ws = null;
    }
    wsUrl = null;
  };

  const waitForConnection = async (): Promise<boolean> => {
    const deadline = Date.now() + connectTimeoutMs;
    while (connecting && Date.now() < deadline) {
      if (ws && ws.readyState === WebSocket.OPEN) return true;
      await new Promise((r) => setTimeout(r, 5));
    }
    return ws !== null && ws.readyState === WebSocket.OPEN;
  };

  const handleRequest = async (
    req: WireRequest,
    clientId: string,
    send: SendToClient,
  ): Promise<void> => {
    let activeSocket = ws;
    if (!activeSocket || activeSocket.readyState !== WebSocket.OPEN) {
      // Connection attempts happen only in response to explicit browser work.
      tryConnect();
      const ready = await waitForConnection();
      if (!ready) {
        send(clientId, {
          type: "response",
          id: req.id,
          error: { code: -32000, message: "Chrome not connected" },
        });
        return;
      }
    }

    activeSocket = ws;
    if (!activeSocket || activeSocket.readyState !== WebSocket.OPEN) {
      send(clientId, {
        type: "response",
        id: req.id,
        error: { code: -32000, message: "Chrome not connected" },
      });
      return;
    }

    const isDetach = req.method === "Target.detachFromTarget";
    if (isDetach && req.sessionId) router.release(req.sessionId);

    const isAttach = req.method === "Target.attachToTarget";

    const daemonId = mux.allocate(
      { clientId, localId: req.id, send, isAttach },
      (id) =>
        setTimeout(() => {
          mux.take(id);
          generations.delete(id);
          send(clientId, {
            type: "response",
            id: req.id,
            error: { code: -32000, message: `Timeout after ${CDP_COMMAND_TIMEOUT_MS}ms: ${req.method}` },
          });
        }, CDP_COMMAND_TIMEOUT_MS),
    );
    generations.set(daemonId, { generation: attemptGeneration, clientId });

    const payload: Record<string, unknown> = {
      id: daemonId,
      method: req.method,
      params: req.params ?? {},
    };
    if (req.sessionId) payload["sessionId"] = req.sessionId;

    try {
      activeSocket.send(JSON.stringify(payload));
    } catch (e) {
      mux.take(daemonId);
      generations.delete(daemonId);
      send(clientId, {
        type: "response",
        id: req.id,
        error: { code: -32000, message: e instanceof Error ? e.message : String(e) },
      });
    }
  };

  return {
    start,
    stop,
    handleRequest,
    isAlive: () => ws !== null && ws.readyState === WebSocket.OPEN,
    getSessionOwner: (sid) => router.getOwner(sid),
    removeClient: (cid) => {
      rejectCallbacksWhere((info) => info.clientId === cid);
      mux.clearClient(cid);
      router.removeClient(cid);
    },
    onEvent: (h) => { eventHandler = h; },
    onClose: (h) => { closeHandler = h; },
  };
};