Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CdpRelay.ts

219 lines8.0 KB

1/**
2 * Presents one preview tab's debugger as a minimal CDP browser endpoint, so
3 * the environment server can drive a desktop `<webview>` with the same
4 * Playwright engine it uses for headless tabs.
5 *
6 * Only the tab's own page is reachable. Browser-level commands Playwright sends
7 * while connecting are answered here, and every page command goes to the tab's
8 * `webContents.debugger`. Electron allows one debugger client per webContents,
9 * so the relay shares the attach the preview manager already holds.
10 */
11​
12export interface CdpRelayTarget {
13 /** Sends a command to the tab's page, or to a child session such as an iframe. */
14 readonly send: (
15 method: string,
16 params: Record<string, unknown>,
17 sessionId: string | undefined,
18 ) => Promise<unknown>;
19 /** The page's own CDP target id; Playwright keys the main frame by it. */
20 readonly targetId: () => Promise<string>;
21 readonly url: () => string;
22 readonly title: () => string;
23 readonly userAgent: () => string;
24 /**
25 * Where the server wants this tab's downloads, named by their CDP guid. The
26 * desktop app and the server it launched share a disk, so the server's
27 * Playwright reads the file where it asked for it.
28 */
29 readonly setDownloadDirectory: (directory: string | null) => void;
31​
32export interface CdpRelayConnection {
33 /** Feed one message from the server. */
34 readonly receive: (raw: string) => void;
35 /** Feed one debugger event from the tab. */
36 readonly event: (method: string, params: unknown, sessionId: string | undefined) => void;
38​
39interface CdpCommand {
40 readonly id: number;
41 readonly method: string;
42 readonly params?: Record<string, unknown>;
43 readonly sessionId?: string;
45​
46const isCommand = (value: unknown): value is CdpCommand =>
47 typeof value === "object" &&
48 value !== null &&
49 typeof (value as { id?: unknown }).id === "number" &&
50 typeof (value as { method?: unknown }).method === "string";
51​
52const BROWSER_CONTEXT_ID = "t3-preview";
53/** Marks an event to send right after a command's reply. */
54const ANNOUNCE = Symbol("announce");
55/** The page's root session id, as Playwright sees it. */
56const PAGE_SESSION_ID = "t3-preview-page";
57​
58/** Browser-level commands that only need acknowledging for a page the desktop owns. */
59const ACKNOWLEDGED = new Set([
60 "Target.setDiscoverTargets",
61 "Browser.grantPermissions",
62 "Browser.resetPermissions",
63 "Browser.cancelDownload",
64]);
65​
66export function createCdpRelayConnection(
67 target: CdpRelayTarget,
68 write: (message: string) => void,
69): CdpRelayConnection {
70 const send = (message: Record<string, unknown>) => write(JSON.stringify(message));
71 let attached = false;
72 /**
73 * Sessions this relay invented. Extra page sessions (Playwright's
74 * `newCDPSession`) all reach the one real debugger session, so each gets the
75 * page's events; other ids are the page's own child sessions, such as iframes.
76 */
77 const sessions = new Map<string, "browser" | "page">([[PAGE_SESSION_ID, "page"]]);
78 let sessionSequence = 0;
79 const newSession = (kind: "browser" | "page") => {
80 const sessionId = `t3-preview-${kind}-${++sessionSequence}`;
81 sessions.set(sessionId, kind);
82 return sessionId;
83 };
84 const targetInfo = async () => ({
85 targetId: await target.targetId(),
86 type: "page",
87 title: target.title(),
88 url: target.url(),
89 attached: true,
90 canAccessOpener: false,
91 browserContextId: BROWSER_CONTEXT_ID,
92 });
93​
94 const browserCommand = async (command: CdpCommand): Promise<unknown> => {
95 if (ACKNOWLEDGED.has(command.method)) return {};
96 switch (command.method) {
97 case "Browser.setDownloadBehavior": {
98 // The tab's debugger reports downloads as Chromium does, so the server
99 // sees them as its own. Only the directory needs the desktop's help.
100 const directory = command.params?.["downloadPath"];
101 const allowed = command.params?.["behavior"] !== "deny" && typeof directory === "string";
102 target.setDownloadDirectory(allowed ? directory : null);
103 const { browserContextId: _ignored, ...params } = command.params ?? {};
104 return target.send("Browser.setDownloadBehavior", params, undefined);
105 }
106 case "Browser.getVersion":
107 return {
108 protocolVersion: "1.3",
109 product: "Electron",
110 revision: "",
111 userAgent: target.userAgent(),
112 jsVersion: "",
113 };
114 case "Target.getTargetInfo":
115 return {
116 targetInfo: {
117 targetId: "browser",
118 type: "browser",
119 title: "",
120 url: "",
121 attached: true,
122 canAccessOpener: false,
123 },
124 };
125 case "Target.getBrowserContexts":
126 return { browserContextIds: [] };
127 case "Target.getTargets":
128 return { targetInfos: [await targetInfo()] };
129 case "Target.attachToBrowserTarget":
130 return { sessionId: newSession("browser") };
131 case "Target.attachToTarget": {
132 const targetId = command.params?.["targetId"];
133 if (targetId !== (await target.targetId()))
134 throw new Error("Only this preview tab's page can be attached.");
135 return { sessionId: newSession("page") };
136 }
137 case "Target.detachFromTarget": {
138 const sessionId = command.params?.["sessionId"];
139 if (typeof sessionId === "string" && sessionId !== PAGE_SESSION_ID)
140 sessions.delete(sessionId);
141 return {};
142 }
143 case "Target.setAutoAttach": {
144 if (attached) return {};
145 attached = true;
146 // The page is announced once, right after this reply, as Chromium does.
147 const info = await targetInfo();
148 return {
149 [ANNOUNCE]: {
150 method: "Target.attachedToTarget",
151 params: { sessionId: PAGE_SESSION_ID, targetInfo: info, waitingForDebugger: false },
152 },
153 };
154 }
155 default:
156 throw new Error(`Not supported for a desktop preview tab: ${command.method}`);
157 }
158 };
159​
160 const pageCommand = (command: CdpCommand): Promise<unknown> => {
161 // The page is already running; nothing waits for a debugger.
162 if (command.method === "Runtime.runIfWaitingForDebugger") return Promise.resolve({});
163 const child = sessions.get(command.sessionId!) === "page" ? undefined : command.sessionId;
164 return target.send(command.method, command.params ?? {}, child);
165 };
166​
167 return {
168 receive: (raw) => {
169 let command: unknown;
170 try {
171 command = JSON.parse(raw);
172 } catch {
173 return;
174 }
175 if (!isCommand(command)) return;
176 const result =
177 command.sessionId === undefined || sessions.get(command.sessionId) === "browser"
178 ? browserCommand(command)
179 : pageCommand(command);
180 const route = command.sessionId === undefined ? {} : { sessionId: command.sessionId };
181 // Replies leave as commands finish, as Chromium's do, so a slow command
182 // such as a screenshot never holds up the ones behind it.
183 void result.then(
184 (value) => {
185 const announce =
186 typeof value === "object" && value !== null && ANNOUNCE in value
187 ? (value as { [ANNOUNCE]: Record<string, unknown> })[ANNOUNCE]
188 : undefined;
189 send({ id: command.id, result: announce ? {} : (value ?? {}), ...route });
190 if (announce) send(announce);
191 },
192 (cause: unknown) =>
193 send({
194 id: command.id,
195 error: {
196 code: -32000,
197 message: cause instanceof Error ? cause.message : String(cause),
198 },
199 ...route,
200 }),
201 );
202 },
203 event: (method, params, sessionId) => {
204 if (!attached) return;
205 // Download events are the browser's; Playwright listens on its root session.
206 if (method.startsWith("Browser.download")) {
207 send({ method, params });
208 return;
209 }
210 // Electron reports the page's own events with an empty session id.
211 if (sessionId) {
212 send({ method, params, sessionId });
213 return;
214 }
215 for (const [id, kind] of sessions)
216 if (kind === "page") send({ method, params, sessionId: id });
217 },
218 };