): string[] {
+ const asked: string[] = [];
+ dom.addEventListener("fetch", (event: any) => {
+ asked.push(event.request.url);
+ const page = pages[event.request.url];
+ if (page !== undefined) {
+ event.respondWith(
+ new Response(page, {headers: {"content-type": "text/html"}}),
+ );
+ }
+ });
+ return asked;
+}
+
+const FRAME_STYLE =
+ "display: block; border: 1px solid; width: 14ch; height: 5px";
+
+test("a frame's document loads through the page and starts in the isolator", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const terminal = new MockProcess({cols: 30, rows: 8});
+ const dom = new TermDOM({
+ transport: terminal.transport,
+ url: "https://page.example/",
+ });
+ useIsolator(dom, isolate);
+ answerPages(dom, {"https://frame.example/a": "framed
"});
+ dom.document.body.innerHTML =
+ ``;
+ const frame = dom.document.querySelector("iframe")!;
+ let loads = 0;
+ frame.addEventListener("load", () => loads++);
+ await settle(dom);
+ await until(() => frames.length === 1);
+ expect(frames[0].init).toMatchObject({
+ url: "https://frame.example/a",
+ html: "framed
",
+ cols: 12,
+ rows: 3,
+ scripting: false,
+ });
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ expect(loads).toBe(0);
+ frames[0].post({type: "load"});
+ expect(loads).toBe(1);
+ dom.dispose();
+});
+
+test("the cells a frame paints are drawn in its box, without control characters", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const terminal = new MockProcess({cols: 30, rows: 6});
+ const dom = new TermDOM({transport: terminal.transport});
+ useIsolator(dom, isolate);
+ answerPages(dom, {"https://frame.example/": ""});
+ dom.document.body.innerHTML =
+ ``;
+ await settle(dom);
+ await until(() => frames.length === 1);
+ frames[0].post(paintText(["inside", "a\x1b[2Jb"], 12));
+ await settle(dom);
+ expect(rows(terminal, 5)).toEqual([
+ "┌────────────┐",
+ "│inside │",
+ "│a [2Jb │",
+ "│ │",
+ "└────────────┘",
+ ]);
+ dom.dispose();
+});
+
+test("a frame's cells claim no columns their text does not take", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const terminal = new MockProcess({cols: 30, rows: 6});
+ const dom = new TermDOM({transport: terminal.transport});
+ useIsolator(dom, isolate);
+ answerPages(dom, {"https://frame.example/": ""});
+ dom.document.body.innerHTML =
+ ``;
+ await settle(dom);
+ await until(() => frames.length === 1);
+ const message = paintText(["abc"], 12) as Extract<
+ FrameMessage,
+ {type: "paint"}
+ >;
+ message.cells.attrs[0] |= 0x1f << 9;
+ message.cells.cluster[1] = 0x110000;
+ message.cells.graphemes.push([0x8000_0001, "\x07"]);
+ message.cells.cluster[2] = 0x8000_0001;
+ frames[0].post(message);
+ await settle(dom);
+ expect(rows(terminal, 2)[1]).toBe("│a │");
+ dom.dispose();
+});
+
+test("a click focuses the frame, and its keys and mouse go there in its own cells", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const terminal = new MockProcess({cols: 30, rows: 8});
+ const dom = new TermDOM({transport: terminal.transport});
+ useIsolator(dom, isolate);
+ answerPages(dom, {"https://frame.example/": ""});
+ dom.document.body.innerHTML =
+ ``;
+ await dom.attach();
+ await settle(dom);
+ await until(() => frames.length === 1);
+ const frame = dom.document.querySelector("iframe")!;
+ let parentKeys = 0;
+ dom.document.addEventListener("keypress", () => parentKeys++);
+ // The frame's third column on its second row, inside the border.
+ terminal.stdin.simulateResponse("\x1b[<0;4;3M\x1b[<0;4;3m");
+ await inputDispatched();
+ await settle(dom);
+ expect(dom.document.activeElement).toBe(frame);
+ terminal.stdin.simulateResponse("x");
+ await inputDispatched();
+ const items = frames[0].sent
+ .filter((message) => message.type === "input")
+ .map((message) => message.item);
+ expect(items).toContainEqual({
+ kind: "mouse",
+ button: 0,
+ col: 2,
+ row: 1,
+ release: false,
+ ticks: 1,
+ });
+ expect(items).toContainEqual({kind: "focus", focused: true});
+ expect(items).toContainEqual([
+ expect.objectContaining({kind: "key", key: "x"}),
+ ]);
+ expect(parentKeys).toBe(0);
+ dom.dispose();
+});
+
+test("the page and the frame reach each other only through messages", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const dom = new TermDOM({
+ transport: new MockProcess({cols: 30, rows: 8}).transport,
+ url: "https://page.example/",
+ });
+ useIsolator(dom, isolate);
+ answerPages(dom, {"https://frame.example/": ""});
+ dom.document.body.innerHTML =
+ ``;
+ const frame = dom.document.querySelector("iframe")!;
+ await settle(dom);
+ await until(() => frames.length === 1);
+ expect(frame.contentDocument).toBe(null);
+ expect(() => (frame.contentWindow as any).document).toThrow();
+ frame.contentWindow!.postMessage({hello: 1}, "https://frame.example");
+ expect(frames[0].sent).toContainEqual({
+ type: "message",
+ data: {hello: 1},
+ origin: "https://page.example",
+ targetOrigin: "https://frame.example",
+ });
+ const received: any[] = [];
+ dom.window.addEventListener("message", (event: MessageEvent) => {
+ received.push([
+ event.data,
+ event.origin,
+ event.source === frame.contentWindow,
+ ]);
+ });
+ frames[0].post({
+ type: "message",
+ data: "elsewhere",
+ targetOrigin: "https://other.example",
+ });
+ frames[0].post({type: "message", data: "back", targetOrigin: "*"});
+ await until(() => received.length > 0);
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ expect(received).toEqual([["back", "https://frame.example", true]]);
+ dom.dispose();
+});
+
+test("a frame's loads go through the page's fetch listeners, without forbidden headers", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const dom = new TermDOM({
+ transport: new MockProcess({cols: 30, rows: 8}).transport,
+ });
+ useIsolator(dom, isolate);
+ const headers: Array<[string, string]> = [];
+ dom.addEventListener("fetch", (event: any) => {
+ if (event.request.url === "https://frame.example/") {
+ event.respondWith(new Response(""));
+ } else if (event.request.url === "https://frame.example/data.json") {
+ event.request.headers.forEach((value: string, name: string) => {
+ headers.push([name, value]);
+ });
+ event.respondWith(new Response("{}", {status: 201}));
+ }
+ });
+ dom.document.body.innerHTML =
+ ``;
+ await settle(dom);
+ await until(() => frames.length === 1);
+ frames[0].post({
+ type: "fetch",
+ id: 1,
+ request: {
+ url: "https://frame.example/data.json",
+ method: "GET",
+ headers: [["x-asked", "yes"], ["cookie", "stolen=1"]],
+ body: null,
+ redirect: "follow",
+ destination: "",
+ referrer: "",
+ },
+ });
+ frames[0].post({
+ type: "fetch",
+ id: 2,
+ request: {
+ url: "file:///etc/passwd",
+ method: "GET",
+ headers: [],
+ body: null,
+ redirect: "follow",
+ destination: "",
+ referrer: "",
+ },
+ });
+ await until(() => frames[0].sent.length >= 2);
+ expect(headers).toEqual([["x-asked", "yes"]]);
+ const response = frames[0].sent.find(
+ (message) => message.type === "response",
+ );
+ expect(response).toMatchObject({type: "response", id: 1});
+ expect(new TextDecoder().decode((response as any).response.body)).toBe("{}");
+ expect((response as any).response.status).toBe(201);
+ expect(frames[0].sent).toContainEqual({
+ type: "fetchError",
+ id: 2,
+ message: "Failed to fetch",
+ });
+ dom.dispose();
+});
+
+test("a frame-src policy keeps a frame's document from loading", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const dom = new TermDOM({
+ transport: new MockProcess({cols: 30, rows: 8}).transport,
+ csp: "frame-src https://allowed.example",
+ });
+ useIsolator(dom, isolate);
+ const asked = answerPages(dom, {
+ "https://allowed.example/": "allowed
",
+ "https://blocked.example/": "blocked
",
+ });
+ dom.document.body.innerHTML =
+ ``;
+ await settle(dom);
+ await until(() => frames.length === 2);
+ expect(asked).toEqual(["https://allowed.example/"]);
+ expect(
+ frames
+ .map((frame) => [frame.init.url, frame.init.html.includes("")])
+ .sort(),
+ ).toEqual([
+ ["https://allowed.example/", true],
+ ["https://blocked.example/", false],
+ ]);
+ dom.dispose();
+});
+
+test("a srcdoc frame stays in the page's heap, and a removed frame's isolate goes", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const dom = new TermDOM({
+ transport: new MockProcess({cols: 30, rows: 8}).transport,
+ });
+ useIsolator(dom, isolate);
+ answerPages(dom, {"https://frame.example/": ""});
+ dom.document.body.innerHTML =
+ ``;
+ await settle(dom);
+ await until(() => frames.length === 1);
+ const [local, isolated] = dom.document.querySelectorAll("iframe");
+ expect(local.contentDocument!.getElementById("here")).not.toBe(null);
+ isolated.remove();
+ expect(frames[0].disposed).toBe(true);
+ dom.dispose();
+});
+
+// A frame's red image, 2 by 1 cells, of 16 by 16 pixels, and the cells
+// that show it under id 7 at the frame's top left.
+function postRedImage(frame: FakeFrame, cols: number): void {
+ const pixels = new Uint8ClampedArray(16 * 16 * 4);
+ for (let index = 0; index < pixels.length; index += 4) {
+ pixels.set([255, 0, 0, 255], index);
+ }
+ frame.post({
+ type: "images",
+ added: [{id: 7, width: 16, height: 16, cols: 2, rows: 1, pixels}],
+ dropped: [],
+ });
+ const grid = new CellGrid(1, cols);
+ grid.setPlaceholder(0, 0, 0, {fg: toCellColor(7)});
+ grid.setPlaceholder(1, 0, 1, {fg: toCellColor(7)});
+ grid.setPlaceholder(2, 0, 0, {fg: toCellColor(8)});
+ frame.post({type: "paint", cells: exportGrid(grid), caret: null});
+}
+
+test("a frame's images are drawn in half blocks where the terminal has no graphics", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const terminal = new MockProcess({cols: 30, rows: 6});
+ const dom = new TermDOM({
+ transport: terminal.transport,
+ colorDepth: "rgb",
+ images: "cells",
+ });
+ useIsolator(dom, isolate);
+ answerPages(dom, {"https://frame.example/": ""});
+ dom.document.body.innerHTML =
+ ``;
+ await settle(dom);
+ await until(() => frames.length === 1);
+ expect(frames[0].init.images).toBe("kitty");
+ postRedImage(frames[0], 12);
+ await settle(dom);
+ const buffer = (terminal as any).terminal.buffer.active;
+ const line = buffer.getLine(1);
+ const backgrounds = [1, 2, 3].map((col) => {
+ const cell = line.getCell(col);
+ return cell.isBgRGB() ? cell.getBgColor() : null;
+ });
+ expect(backgrounds).toEqual([0xff0000, 0xff0000, null]);
+ dom.dispose();
+});
+
+test("a frame's images are sent in the kitty protocol where the terminal draws it", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const terminal = new MockProcess({cols: 30, rows: 6});
+ scriptReplies(terminal, [{ask: "\x1b[16t", reply: "\x1b[6;16;8t"}]);
+ const output = captureRawOutput(terminal);
+ const dom = new TermDOM({
+ transport: terminal.transport,
+ colorDepth: "rgb",
+ images: "kitty",
+ });
+ useIsolator(dom, isolate);
+ answerPages(dom, {"https://frame.example/": ""});
+ dom.document.body.innerHTML =
+ ``;
+ await settle(dom);
+ await until(() => frames.length === 1);
+ postRedImage(frames[0], 12);
+ await settle(dom);
+ const uploads = output().match(/\x1b_G[^;\x1b]*/g) ?? [];
+ expect(uploads.length).toBe(1);
+ expect(uploads[0]).toContain("c=2,r=1");
+ dom.dispose();
+});
+
+test("a frame goes to the document its own navigation names", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const terminal = new MockProcess({cols: 30, rows: 8});
+ const dom = new TermDOM({transport: terminal.transport});
+ useIsolator(dom, isolate);
+ const asked = answerPages(dom, {
+ "https://frame.example/": "",
+ "https://frame.example/b/next": "
next
",
+ });
+ dom.document.body.innerHTML =
+ ``;
+ await settle(dom);
+ await until(() => frames.length === 1);
+ frames[0].post({type: "navigate", target: "self", url: "file:///etc/passwd"});
+ frames[0].post({
+ type: "history",
+ url: "https://bank.example/",
+ replace: false,
+ });
+ frames[0].post({type: "history", url: "/b/here", replace: false});
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ expect(frames[0].disposed).toBe(false);
+ frames[0].post({type: "navigate", target: "self", url: "next"});
+ await until(() => frames.length === 2);
+ expect(frames[0].disposed).toBe(true);
+ expect(frames[1].init.html).toBe("next
");
+ expect(asked).toEqual([
+ "https://frame.example/",
+ "https://frame.example/b/next",
+ ]);
+ dom.dispose();
+});
+
+test("a form a frame posts loads its next document with a POST", async () => {
+ const {frames, isolate} = fakeIsolator();
+ const terminal = new MockProcess({cols: 30, rows: 8});
+ const dom = new TermDOM({transport: terminal.transport});
+ useIsolator(dom, isolate);
+ const posted: Array<{method: string; type: string | null; body: string}> = [];
+ dom.addEventListener("fetch", (event: any) => {
+ const request: Request = event.request;
+ if (request.method === "POST") {
+ event.respondWith(
+ request.text().then((body) => {
+ posted.push({
+ method: request.method,
+ type: request.headers.get("content-type"),
+ body,
+ });
+ return new Response("thanks
");
+ }),
+ );
+ } else {
+ event.respondWith(new Response(""));
+ }
+ });
+ dom.document.body.innerHTML =
+ ``;
+ await settle(dom);
+ await until(() => frames.length === 1);
+ frames[0].post({
+ type: "navigate",
+ target: "self",
+ url: "https://frame.example/send",
+ method: "post",
+ body: new TextEncoder().encode("q=tea"),
+ contentType: "application/x-www-form-urlencoded\r\nx-evil: 1",
+ });
+ await until(() => frames.length === 2);
+ expect(posted).toEqual([
+ {
+ method: "POST",
+ type: "application/x-www-form-urlencodedx-evil: 1",
+ body: "q=tea",
+ },
+ ]);
+ expect(frames[1].init.html).toBe("thanks
");
+ dom.dispose();
+});
diff --git a/tests/isolated-vm-frames.test.ts b/tests/isolated-vm-frames.test.ts
new file mode 100644
index 00000000..3a0253b6
--- /dev/null
+++ b/tests/isolated-vm-frames.test.ts
@@ -0,0 +1,243 @@
+/**
+ * Frames in isolated-vm's isolates, end to end: the page loads the
+ * frame's document, the frame lays it out, runs its scripts and paints it
+ * in an isolate of its own, and the page draws it, hands it keys and
+ * trades messages with it. A frame that runs forever is stopped alone.
+ * isolated-vm loads on Node only, from Node 24.
+ */
+import {expect, test} from "@b9g/libuild/test";
+
+import {TermDOM} from "../src/index.ts";
+import {encodePNG} from "../src/internal/imagecodecs.ts";
+import {createIsolatedVMFrames} from "../src/internal/isolatedvm.ts";
+import type {FrameIsolator} from "../src/internal/isolateprotocol.ts";
+import {_} from "../src/internal/slots.ts";
+import {
+ captureRawOutput,
+ inputDispatched,
+ MockProcess,
+ nextFrame,
+ scriptReplies,
+ until,
+} from "./test-utils.ts";
+
+const onNode =
+ (globalThis as {Bun?: unknown}).Bun === undefined &&
+ (globalThis as {Deno?: unknown}).Deno === undefined;
+// isolated-vm 7 asks for Node 24, and neither loads nor builds under 20.
+const loads = onNode && Number(process.versions.node.split(".")[0]) >= 24;
+const nodeTest = loads ? test : test.skip;
+
+function rows(terminal: MockProcess, count: number): string[] {
+ const buffer = (terminal as any).terminal.buffer.active;
+ return Array.from(
+ {length: count},
+ (_, row) => buffer.getLine(row).translateToString(true).trimEnd(),
+ );
+}
+
+function servePages(dom: TermDOM, pages: Record): void {
+ dom.addEventListener("fetch", (event: any) => {
+ const page = pages[event.request.url];
+ if (page !== undefined) {
+ event.respondWith(
+ new Response(page, {headers: {"content-type": "text/html"}}),
+ );
+ }
+ });
+}
+
+async function drawn(
+ dom: TermDOM,
+ terminal: MockProcess,
+ text: string,
+): Promise {
+ const deadline = Date.now() + 20000;
+ while (Date.now() < deadline) {
+ await nextFrame(dom);
+ if (rows(terminal, 12).some((row) => row.includes(text))) {
+ break;
+ }
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ }
+ return rows(terminal, 12);
+}
+
+const FRAME = "display: block; border: 1px solid; width: 30ch; height: 5px";
+
+nodeTest(
+ "a frame's document is laid out and painted in its own isolate",
+ async () => {
+ const terminal = new MockProcess({cols: 40, rows: 12});
+ const dom = new TermDOM({transport: terminal.transport, colorDepth: "rgb"});
+ servePages(dom, {
+ "https://frame.example/": "from the isolate
",
+ });
+ dom.document.body.innerHTML =
+ `page
`;
+ const screen = await drawn(dom, terminal, "from the isolate");
+ expect(screen.slice(0, 3)).toEqual([
+ "page",
+ "┌────────────────────────────┐",
+ "│from the isolate │",
+ ]);
+ await dom.dispose();
+ },
+ 60000,
+);
+
+nodeTest(
+ "a frame's scripts run there, and it trades messages with the page",
+ async () => {
+ const terminal = new MockProcess({cols: 40, rows: 12});
+ const dom = new TermDOM({
+ transport: terminal.transport,
+ url: "https://page.example/",
+ scripting: "enabled",
+ });
+ servePages(dom, {
+ "https://frame.example/":
+ "waiting
",
+ });
+ const received: string[] = [];
+ dom.window.addEventListener("message", (event: MessageEvent) => {
+ received.push(event.data);
+ });
+ dom.document.body.innerHTML =
+ ``;
+ const frame = dom.document.querySelector("iframe")!;
+ await dom.attach();
+ await until(() => received.includes("ready"));
+ expect(received).toEqual(["ready"]);
+ frame.contentWindow!.postMessage("ping", "https://frame.example");
+ await until(() => received.includes("got ping"));
+ const screen = await drawn(dom, terminal, "ping from");
+ expect(screen.slice(1, 3)).toEqual([
+ "│ping from https:// │",
+ "│page.example │",
+ ]);
+ await dom.dispose();
+ },
+ 60000,
+);
+
+nodeTest(
+ "keys typed into a focused frame reach its field",
+ async () => {
+ const terminal = new MockProcess({cols: 40, rows: 12});
+ const dom = new TermDOM({transport: terminal.transport});
+ servePages(dom, {
+ "https://frame.example/":
+ "",
+ });
+ dom.document.body.innerHTML =
+ ``;
+ await dom.attach();
+ await drawn(dom, terminal, "┌");
+ dom.document.querySelector("iframe")!.focus();
+ await nextFrame(dom);
+ terminal.stdin.simulateResponse("typed");
+ await inputDispatched();
+ const screen = await drawn(dom, terminal, "typed");
+ expect(screen[1]).toMatch(/^│typed/);
+ await dom.dispose();
+ },
+ 60000,
+);
+
+nodeTest(
+ "a frame that runs forever is stopped, and the page goes on",
+ async () => {
+ const terminal = new MockProcess({cols: 40, rows: 12});
+ const dom = new TermDOM({
+ transport: terminal.transport,
+ scripting: "enabled",
+ });
+ // Long enough for the calm frame to start on a busy machine: the
+ // limit is on the clock, and holds a frame's first paint too.
+ (_(dom.document as never) as {extra: {isolateFrame: FrameIsolator}})
+ .extra.isolateFrame = await createIsolatedVMFrames({taskTimeout: 2000});
+ servePages(dom, {
+ "https://spin.example/": "spin
",
+ "https://calm.example/": "calm
",
+ });
+ dom.document.body.innerHTML =
+ `` +
+ ``;
+ const screen = await drawn(dom, terminal, "calm");
+ expect(screen).toContain("│calm │");
+ dom.document.body.append("after");
+ const later = await drawn(dom, terminal, "after");
+ expect(later.some((row) => row.includes("after"))).toBe(true);
+ await dom.dispose();
+ },
+ 60000,
+);
+
+nodeTest(
+ "an image in a frame goes to the terminal in the kitty protocol",
+ async () => {
+ const terminal = new MockProcess({cols: 40, rows: 12});
+ scriptReplies(terminal, [{ask: "\x1b[16t", reply: "\x1b[6;16;8t"}]);
+ const output = captureRawOutput(terminal);
+ const dom = new TermDOM({
+ transport: terminal.transport,
+ colorDepth: "rgb",
+ images: "kitty",
+ cellSize: {width: 8, height: 16},
+ });
+ const data = new Uint8ClampedArray(16 * 32 * 4).fill(255);
+ let binary = "";
+ for (const byte of encodePNG({width: 16, height: 32, data})) {
+ binary += String.fromCharCode(byte);
+ }
+ servePages(dom, {
+ "https://frame.example/":
+ "" +
+ `
`,
+ });
+ dom.document.body.innerHTML =
+ "";
+ await dom.attach();
+ const deadline = Date.now() + 20000;
+ while (!/\x1b_G[^;]*c=2,r=2/.test(output()) && Date.now() < deadline) {
+ await nextFrame(dom);
+ await new Promise((resolve) => setTimeout(resolve, 20));
+ }
+ expect(output()).toMatch(/\x1b_G[^;]*c=2,r=2/);
+ await dom.dispose();
+ },
+ 60000,
+);
+
+nodeTest(
+ "a link clicked in a frame loads its next page there",
+ async () => {
+ const terminal = new MockProcess({cols: 40, rows: 12});
+ const dom = new TermDOM({
+ transport: terminal.transport,
+ scripting: "enabled",
+ });
+ servePages(dom, {
+ "https://frame.example/":
+ "go",
+ "https://frame.example/next": "next page
",
+ });
+ dom.document.body.innerHTML =
+ ``;
+ await dom.attach();
+ const screen = await drawn(dom, terminal, "next page");
+ expect(screen[1]).toMatch(/^│next page/);
+ await dom.dispose();
+ },
+ 60000,
+);