Skip to content

Commit 6d86408

Browse files
fix(preview): restore page contexts when the desktop relay reconnects
Reset the native Runtime domain once per CDP relay so Playwright receives execution context notifications after reconnecting to an existing Electron guest. Co-Authored-By: Codex <[email protected]>
1 parent a96cf08 commit 6d86408

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎apps/desktop/src/preview/CdpRelay.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { describe, expect, it, vi } from "vite-plus/test";
22

3-
import { createCdpRelayConnection, type CdpRelayTarget } from "./CdpRelay.ts";
3+
import {
4+
createCdpRelayConnection,
5+
type CdpRelayConnection,
6+
type CdpRelayTarget,
7+
} from "./CdpRelay.ts";
48

59
const makeTarget = (
610
send: CdpRelayTarget["send"],
@@ -19,6 +23,38 @@ const makeTarget = (
1923
const settle = () => new Promise<void>((resolve) => setImmediate(resolve));
2024

2125
describe("CDP relay", () => {
26+
it("announces the main execution context when reconnecting to an existing guest", async () => {
27+
let runtimeEnabled = false;
28+
let active: CdpRelayConnection;
29+
const context = { id: 1, name: "", auxData: { isDefault: true, frameId: "GUEST-TARGET" } };
30+
const target = makeTarget(async (method) => {
31+
if (method === "Runtime.disable") runtimeEnabled = false;
32+
if (method === "Runtime.enable" && !runtimeEnabled) {
33+
runtimeEnabled = true;
34+
active.event("Runtime.executionContextCreated", { context }, undefined);
35+
}
36+
return {};
37+
});
38+
const connect = async () => {
39+
const written: Array<Record<string, unknown>> = [];
40+
active = createCdpRelayConnection(target, (raw) => written.push(JSON.parse(raw)));
41+
active.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
42+
await settle();
43+
active.receive(
44+
JSON.stringify({ id: 2, method: "Runtime.enable", sessionId: "t3-preview-page" }),
45+
);
46+
await settle();
47+
return written;
48+
};
49+
const announced = {
50+
method: "Runtime.executionContextCreated",
51+
params: { context },
52+
sessionId: "t3-preview-page",
53+
};
54+
expect(await connect()).toContainEqual(announced);
55+
expect(await connect()).toContainEqual(announced);
56+
});
57+
2258
it("announces only the tab's page, under its real target id", async () => {
2359
const written: Array<Record<string, unknown>> = [];
2460
const relay = createCdpRelayConnection(makeTarget(vi.fn()), (raw) =>

‎apps/desktop/src/preview/CdpRelay.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function createCdpRelayConnection(
5050
): CdpRelayConnection {
5151
const send = (message: Record<string, unknown>) => write(JSON.stringify(message));
5252
let attached = false;
53+
let runtimeReset: Promise<unknown> | undefined;
5354
/** Relay-created page sessions share one real debugger; child frame sessions retain their own ids. */
5455
const sessions = new Map<string, "browser" | "page">([[PAGE_SESSION_ID, "page"]]);
5556
let sessionSequence = 0;
@@ -133,6 +134,11 @@ export function createCdpRelayConnection(
133134

134135
const pageCommand = (command: CdpCommand): Promise<unknown> => {
135136
const child = sessions.get(command.sessionId!) === "page" ? undefined : command.sessionId;
137+
if (command.method === "Runtime.enable" && child === undefined) {
138+
// Electron retains enabled domains across relays; re-enable once so a new client receives existing contexts.
139+
runtimeReset ??= target.send("Runtime.disable", {}, undefined);
140+
return runtimeReset.then(() => target.send(command.method, command.params ?? {}, child));
141+
}
136142
// Child frames/workers pause on auto-attach until resumed; only the main page can skip this command.
137143
if (command.method === "Runtime.runIfWaitingForDebugger" && child === undefined) {
138144
return Promise.resolve({});

0 commit comments

Comments
 (0)