|
| 1 | +/** |
| 2 | + * The desktop end of the desktop browser channel (see `DesktopBrowserEvent` in |
| 3 | + * contracts). The primary backend gets two file descriptors at spawn: this |
| 4 | + * service writes events for the desktop's tabs to one and reads commands from |
| 5 | + * the other. Each attached tab is reachable only through its `CdpRelay`. |
| 6 | + * |
| 7 | + * A tab is attached once its `<webview>` registers with a key the web app |
| 8 | + * gave it. The preview manager owns the tab's single debugger session and hands |
| 9 | + * it here; the relay shares it. |
| 10 | + */ |
| 11 | +import { |
| 12 | + DesktopBrowserCommand, |
| 13 | + DesktopBrowserEvent, |
| 14 | + type DesktopBrowserEvent as DesktopBrowserEventType, |
| 15 | +} from "@t3tools/contracts"; |
| 16 | +import * as Context from "effect/Context"; |
| 17 | +import * as Effect from "effect/Effect"; |
| 18 | +import * as Layer from "effect/Layer"; |
| 19 | +import * as Option from "effect/Option"; |
| 20 | +import * as PubSub from "effect/PubSub"; |
| 21 | +import * as Schema from "effect/Schema"; |
| 22 | +import * as Stream from "effect/Stream"; |
| 23 | + |
| 24 | +import { createCdpRelayConnection, type CdpRelayConnection } from "./CdpRelay.ts"; |
| 25 | + |
| 26 | +const encodeEvent = Schema.encodeSync(Schema.fromJsonString(DesktopBrowserEvent)); |
| 27 | +const decodeCommand = Schema.decodeUnknownOption(Schema.fromJsonString(DesktopBrowserCommand)); |
| 28 | +const lineEncoder = new TextEncoder(); |
| 29 | + |
| 30 | +export interface DesktopBrowserTabKey { |
| 31 | + readonly threadId: string; |
| 32 | + readonly tabId: string; |
| 33 | +} |
| 34 | + |
| 35 | +/** A tab's debugger, as the preview manager lends it to the relay. */ |
| 36 | +export interface DesktopBrowserTabDebugger { |
| 37 | + readonly webContents: Electron.WebContents; |
| 38 | + readonly debugger: Electron.Debugger; |
| 39 | +} |
| 40 | + |
| 41 | +const keyOf = ({ threadId, tabId }: DesktopBrowserTabKey) => `${threadId}\u0000${tabId}`; |
| 42 | + |
| 43 | +interface AttachedTab { |
| 44 | + readonly key: DesktopBrowserTabKey; |
| 45 | + readonly debuggee: DesktopBrowserTabDebugger; |
| 46 | + relay: CdpRelayConnection | null; |
| 47 | + readonly onMessage: ( |
| 48 | + event: Electron.Event, |
| 49 | + method: string, |
| 50 | + params: unknown, |
| 51 | + sessionId: string, |
| 52 | + ) => void; |
| 53 | +} |
| 54 | + |
| 55 | +export class DesktopBrowserHost extends Context.Service< |
| 56 | + DesktopBrowserHost, |
| 57 | + { |
| 58 | + /** Newline-delimited events for the backend's browser fd. */ |
| 59 | + readonly events: Stream.Stream<Uint8Array>; |
| 60 | + /** One line from the backend's browser control fd. */ |
| 61 | + readonly handleCommandLine: (line: string) => Effect.Effect<void>; |
| 62 | + /** Offers a server tab's `<webview>` to the server. */ |
| 63 | + readonly attach: (key: DesktopBrowserTabKey, debuggee: DesktopBrowserTabDebugger) => void; |
| 64 | + /** Withdraws it: closed, swapped, crashed, or devtools needs the debugger. */ |
| 65 | + readonly detach: (key: DesktopBrowserTabKey) => void; |
| 66 | + /** Re-announces attached tabs to a backend that just started. */ |
| 67 | + readonly announceAll: Effect.Effect<void>; |
| 68 | + } |
| 69 | +>()("@t3tools/desktop/preview/DesktopBrowserHost") {} |
| 70 | + |
| 71 | +export const make = Effect.gen(function* () { |
| 72 | + const outbox = yield* PubSub.unbounded<DesktopBrowserEventType>(); |
| 73 | + const runFork = Effect.runForkWith(yield* Effect.context<never>()); |
| 74 | + const tabs = new Map<string, AttachedTab>(); |
| 75 | + const emit = (event: DesktopBrowserEventType) => runFork(PubSub.publish(outbox, event)); |
| 76 | + |
| 77 | + const relayFor = (tab: AttachedTab) => { |
| 78 | + if (tab.relay) return tab.relay; |
| 79 | + const { webContents, debugger: debuggee } = tab.debuggee; |
| 80 | + tab.relay = createCdpRelayConnection( |
| 81 | + { |
| 82 | + send: (method, params, sessionId) => |
| 83 | + sessionId === undefined |
| 84 | + ? debuggee.sendCommand(method, params) |
| 85 | + : debuggee.sendCommand(method, params, sessionId), |
| 86 | + targetId: () => |
| 87 | + debuggee |
| 88 | + .sendCommand("Target.getTargetInfo") |
| 89 | + .then((result: { targetInfo: { targetId: string } }) => result.targetInfo.targetId), |
| 90 | + url: () => webContents.getURL(), |
| 91 | + title: () => webContents.getTitle(), |
| 92 | + userAgent: () => webContents.getUserAgent(), |
| 93 | + }, |
| 94 | + (message) => emit({ type: "cdp", ...tab.key, message }), |
| 95 | + ); |
| 96 | + return tab.relay; |
| 97 | + }; |
| 98 | + |
| 99 | + const detach = (key: DesktopBrowserTabKey) => { |
| 100 | + const id = keyOf(key); |
| 101 | + const tab = tabs.get(id); |
| 102 | + if (!tab) return; |
| 103 | + tabs.delete(id); |
| 104 | + tab.debuggee.debugger.off("message", tab.onMessage); |
| 105 | + emit({ type: "detached", ...key }); |
| 106 | + }; |
| 107 | + |
| 108 | + const attach = (key: DesktopBrowserTabKey, debuggee: DesktopBrowserTabDebugger) => { |
| 109 | + const id = keyOf(key); |
| 110 | + if (tabs.get(id)?.debuggee.webContents === debuggee.webContents) return; |
| 111 | + detach(key); |
| 112 | + const tab: AttachedTab = { |
| 113 | + key, |
| 114 | + debuggee, |
| 115 | + relay: null, |
| 116 | + onMessage: (_event, method, params, sessionId) => tab.relay?.event(method, params, sessionId), |
| 117 | + }; |
| 118 | + tabs.set(id, tab); |
| 119 | + debuggee.debugger.on("message", tab.onMessage); |
| 120 | + emit({ type: "attached", ...key }); |
| 121 | + }; |
| 122 | + |
| 123 | + const handleCommandLine = (line: string) => |
| 124 | + Effect.sync(() => { |
| 125 | + const command = decodeCommand(line); |
| 126 | + if (Option.isNone(command)) return; |
| 127 | + const tab = tabs.get(keyOf(command.value)); |
| 128 | + if (!tab) return; |
| 129 | + if (command.value.type === "release") { |
| 130 | + // A new server connection starts with a fresh relay and fresh sessions. |
| 131 | + tab.relay = null; |
| 132 | + return; |
| 133 | + } |
| 134 | + relayFor(tab).receive(command.value.message); |
| 135 | + }); |
| 136 | + |
| 137 | + return DesktopBrowserHost.of({ |
| 138 | + events: Stream.fromPubSub(outbox).pipe( |
| 139 | + Stream.map((event) => lineEncoder.encode(`${encodeEvent(event)}\n`)), |
| 140 | + ), |
| 141 | + handleCommandLine, |
| 142 | + attach, |
| 143 | + detach, |
| 144 | + announceAll: Effect.forEach( |
| 145 | + [...tabs.values()], |
| 146 | + (tab) => { |
| 147 | + tab.relay = null; |
| 148 | + return PubSub.publish(outbox, { type: "attached", ...tab.key }); |
| 149 | + }, |
| 150 | + { discard: true }, |
| 151 | + ), |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +export const layer = Layer.effect(DesktopBrowserHost, make); |
0 commit comments