Fork Horse repository, listed by its owner. Publisher snapshot.
packages/forkhorse-gatekeeper/test/configuration-fixture-main.ts
Snapshot at 34e3175
1import { newMessagePortRpcSession, RpcTarget } from "capnweb";
2import { checkConnectionDraft, type ConnectionDraft } from "../src/configuration.ts";
3
4class FixtureDraftApi extends RpcTarget {
5 async checkDraft(draft: ConnectionDraft) { return checkConnectionDraft(draft); }
6}
7const iframe = document.createElement("iframe");
8iframe.title = "Local fixture configurator";
9iframe.setAttribute("sandbox", "allow-scripts");
10iframe.style.cssText = "width:100%;height:600px;border:0";
11class FixtureHost extends RpcTarget {
12 get gatekeeper() { return new FixtureDraftApi(); }
13 async setSelectionReady(ready: boolean) {
14 if (ready) throw new Error("Fixture must not enable connection creation.");
15 document.getElementById("fixture-output")!.textContent = "Host confirms: connection creation disabled.";
16 }
17 async resize(height: number) { iframe.style.height = `${Math.min(800, Math.max(200, height))}px`; }
18}
19let session: ReturnType<typeof newMessagePortRpcSession> | undefined;
20const handshake = (event: MessageEvent) => {
21 if (event.source !== iframe.contentWindow || event.origin !== "null" ||
22 event.data?.type !== "handshake" || !event.ports[0]) return;
23 if (session) { event.ports[0].close(); return; }
24 session = newMessagePortRpcSession(event.ports[0], new FixtureHost());
25};
26const response = await fetch("/dist/configurator.html");
27if (!response.ok) throw new Error("Build the configurator before starting this fixture.");
28iframe.srcdoc = await response.text();
29window.addEventListener("message", handshake);
30document.getElementById("configurator")!.replaceChildren(iframe);
31window.addEventListener("pagehide", () => {
32 window.removeEventListener("message", handshake);
33 session?.[Symbol.dispose]();
34}, { once: true });
35