Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7d6a85d
Give frames origins, cross-origin windows and messages
brainkim Nov 9, 2026
ce8a6c2
Describe frames, their origins and postMessage in the guide
brainkim Nov 9, 2026
57f61aa
Build a frame's engine from settings, and move grids between heaps
brainkim Nov 10, 2026
9b96231
Draw an iframe's src document from another isolate
brainkim Nov 10, 2026
a55d50c
Show a frame's document from an isolate of its own
brainkim Nov 10, 2026
7470f3b
Run isolated frames in isolated-vm, scripts and modules included
brainkim Nov 10, 2026
c816174
Start isolated frames in isolated-vm with isolatedFrames: "auto"
brainkim Nov 10, 2026
1fbba98
Edit, focus and click in an isolated frame, and say when it crashes
brainkim Nov 10, 2026
37e78a7
Load an isolated frame's linked stylesheets before its first paint
brainkim Nov 10, 2026
da2b29c
Tell the page a frame's form posts and same-document URL changes
brainkim Nov 10, 2026
69db6f4
Hold each isolated frame to its own limits, and report what it uses
brainkim Nov 10, 2026
43e7c97
Paint an isolated frame's images as kitty placeholders, and post them
brainkim Nov 10, 2026
b1e4f82
Describe frames in isolates in the guide
brainkim Nov 10, 2026
47fc2c9
Draw the images an isolated frame posts, as the page draws a program's
brainkim Nov 10, 2026
977d97e
Keep the destination the engine gives an isolated frame's requests
brainkim Nov 10, 2026
78b9120
Post an isolated frame's images at any color depth
brainkim Nov 10, 2026
a16d395
Fire an isolated frame's title, navigation and history on its iframe
brainkim Nov 10, 2026
ee1e556
Reach isolated-vm, esbuild and Node's modules at run time
brainkim Nov 10, 2026
53d457a
Isolate cross-origin frames without an option or events of their own
brainkim Nov 11, 2026
b5df8c8
Drop an isolated frame's own limits and its stats
brainkim Nov 11, 2026
842216a
Skip the isolate tests under Node 24, where isolated-vm does not load
brainkim Nov 12, 2026
81ebc55
Name a frame test's script by its path, not by a URL's
brainkim Nov 12, 2026
536476e
Bundle the frame runtime from its file's path on Windows
brainkim Nov 12, 2026
3ca8c68
Wait for a frame's message in a test, not for thirty milliseconds
brainkim Nov 12, 2026
58ff254
Give a calm frame two seconds to start in the runaway frame's test
brainkim Nov 12, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ jobs:
with:
node-version: ${{ matrix.node }}

- run: bun install --frozen-lockfile
# isolated-vm asks for Node 24. Under the floor its binary does not
# load, and its install script fails trying to build one.
- run: bun install --frozen-lockfile ${{ matrix.node == '20.19' && '--ignore-scripts' || '' }}

# libuild's executable is a shell script, which npx can't start on
# Windows.
Expand Down
17 changes: 17 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 117 additions & 1 deletion docs/guides/06-images-and-canvas.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Images and canvas
description: How <img> and <canvas> draw pixels, and how <embed> runs a program.
description: How <img> and <canvas> draw pixels, how <iframe> shows a document, and how <embed> runs a program.
---

A cell shows two pixels: a lower half block, `▄`, in the foreground
Expand Down Expand Up @@ -375,6 +375,122 @@ the page's borders.
| `reset()` | Clears the grid and settings |
| `isContextLost()` | Always `false` |

## `<iframe>`

A frame shows its `srcdoc`, or an empty `about:blank` document. TermDOM
loads a frame's `src` only where it can isolate the frame, described
below. The frame draws its document in its
box, which is 300 by 150 CSS pixels unless CSS or the `width` and
`height` attributes say otherwise.

Each frame has a window of its own. Where the runtime has `node:vm`, as
Node, Bun and Deno do, the window is in a realm of its own, with its own
`Array`, `Object` and DOM interfaces. Elsewhere, as in a browser or
isolated-vm, the window shares the page's realm, but has the rest of a
window: `postMessage()`, `location`, `parent`, `top`, `frameElement`,
timers, and the rest.

Handler attributes in a frame don't run, even with `scripting:
"enabled"`.

### Origins

A frame showing `srcdoc` or `about:blank` has the page's origin. A
`sandbox` attribute without `allow-same-origin` gives the frame an
opaque origin of its own, which reads as `"null"`. A frame inside a
sandboxed frame is sandboxed too.

Windows of two origins can't read each other. `contentDocument` is
null, and `contentWindow`, `frames[0]`, a frame's name on the window,
`parent` and `top` give a stand-in for the window. The stand-in has only
`window`, `self`, `frames`, `length`, `top`, `parent`, `opener`,
`closed`, `location`, `postMessage()`, `close()`, `focus()`, `blur()`,
and the child frames by index. Its `location` lets you set `href` and
call `replace()`. Anything else throws a `SecurityError`. For example,
`frame.contentWindow.getSelection()` throws for a sandboxed frame. Add
`allow-same-origin` to the sandbox to read it.

### Messages

`postMessage(message, targetOrigin)` works between a page and its
frames, both ways:

```ts
frame.contentWindow.postMessage("hello", "https://example.com");
window.addEventListener("message", (event) => {
if (event.source === frame.contentWindow) {
console.log(event.origin, event.data);
}
});
```

The message arrives later, as a task, in a `message` event. Its
`origin` is the sender's, `"null"` for an opaque one, and its `source`
is the sender as the receiver holds it, so `event.source === parent`
holds in a frame. The message lands only if the receiver's origin is
`targetOrigin`. `"*"` matches any origin, and `"/"` the sender's.

Without `node:vm`, TermDOM can't tell whose code calls `postMessage()`
on a window of the caller's origin, so the message comes from the top
window. A message sent through the stand-in for a window of another
origin comes from the window that holds the stand-in.

### Navigation

A frame may set `parent.location.href`, also across origins. The parent
fires `navigate` on `window.navigation`, where a listener can intercept
or cancel it. A navigation to another document that nothing intercepts
throws `NotSupportedError`, as it does in the page. A sandboxed frame
may navigate its top window only with `allow-top-navigation`, or with
`allow-top-navigation-by-user-activation` during a user's action, and
otherwise throws `SecurityError`.

### Frames in isolates

An `<iframe src>` with an `http:` or `https:` URL of another origin
than the page's loads its page in a V8 isolate of its own, where the
[isolated-vm](https://github.com/laverdet/isolated-vm) package is
installed next to TermDOM. It runs on Node only, from Node 24, and
asks that Node start with `--no-node-snapshot`. Without it, the frame
shows an empty `about:blank`.

```sh
npm install isolated-vm
node --no-node-snapshot app.js
```

```ts
const dom = new TermDOM({scripting: "enabled"});
dom.document.body.innerHTML =
'<iframe src="https://example.com/" style="width: 100%; height: 20em"></iframe>';
```

A frame lays its page out in the page's cell, so a page that shows web
pages wants a cell the size web pages expect, such as `cellSize: {width:
8, height: 16}` or `"auto"`. Under the default cell of one CSS pixel, a
page meant for a browser lays out 8 times as wide.

TermDOM loads the frame's page the way it loads an image: under the
page's `csp`, where `frame-src` governs frames, and through its `fetch`
listeners. So do the frame's own stylesheets, images and scripts,
without the headers Fetch forbids a page to set, such as `Cookie`. The
frame lays out, runs its scripts and paints in its isolate, and the page
draws the cells it paints, checking each one, so a frame can't send the
terminal escape codes.

Each frame's isolate has 256 MB of heap, and each call into it 30
seconds. A frame that runs past either is stopped and shows nothing, and
the page goes on. A frame runs scripts where the page has `scripting:
"enabled"` and a `sandbox` attribute, if it has one, includes
`allow-scripts`.

Click a frame, or focus it, and keys, the mouse and pastes go to the
document in it. To the page, the frame's window is one of another
origin, whatever its URL: `contentDocument` is null, and the two talk
through `postMessage()`. A frame that sets `parent.location.href` asks
the page to navigate, as above, and a link or form in the frame loads
the new page in a new isolate.

## `<embed type="application/x-pty">`

Experimental. It may change in a minor release.
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
],
"scripts": {
"bench": "node bench/run.ts",
"build": "libuild build",
"build": "libuild build && node scripts/build-isolate-frame.ts",
"lint": "eslint .",
"prepublishOnly": "echo 'ERROR: Cannot publish from root directory. Use libuild to publish or stage the package.' && exit 1",
"smoke": "node scripts/smoke.js",
Expand All @@ -54,9 +54,13 @@
"parse5": "8.1.0"
},
"peerDependencies": {
"isolated-vm": "^7.0.2",
"node-pty": "^1.1.0"
},
"peerDependenciesMeta": {
"isolated-vm": {
"optional": true
},
"node-pty": {
"optional": true
}
Expand All @@ -83,13 +87,17 @@
"@webref/idl": "^3.84.0",
"@xterm/headless": "^6",
"codemirror": "^6.0.2",
"core-js-pure": "3.50.0",
"esbuild": "^0.28.1",
"eslint": "^10",
"eslint-plugin-acrocase": "^0.1.1",
"eslint-plugin-crank": "^0.2.2",
"eslint-plugin-esfold": "^0.1.14",
"fast-check": "^4.10.2",
"fflate": "0.8.3",
"ghostty-web": "0.4.0",
"htm": "^3.1.1",
"isolated-vm": "^7.0.2",
"katex": "^0.18.9",
"marked": "^18.0.14",
"marked-highlight": "^2.2.4",
Expand All @@ -102,6 +110,7 @@
"tsx": "^4.23.15",
"typescript": "~6.0.3",
"vue": "^3.5.43",
"web-streams-polyfill": "4.3.0",
"webidl2": "^24.5.0"
},
"module": "./dist/index.js",
Expand Down
17 changes: 17 additions & 0 deletions scripts/build-isolate-frame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Writes the isolated frame runtime next to the built modules, so a
* package loads it without bundling anything at run time.
*
* bun run build
*/
import {writeFile} from "node:fs/promises";

import {
buildFrameScript,
FRAME_SCRIPT,
} from "../src/internal/isolate/bundle.ts";

await writeFile(
new URL(`../dist/${FRAME_SCRIPT}`, import.meta.url),
await buildFrameScript(),
);
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import {
setDecodeWorkerURL,
} from "./internal/images.ts";
import {Input, resolvePendingHover} from "./internal/input.ts";
import {findFrameIsolator} from "./internal/isolatedframes.ts";
import {
compressKittyUploads,
forgetKittyImages,
Expand Down Expand Up @@ -680,6 +681,7 @@ export class TermDOM extends EventTarget {
const images = toGraphicsOption("images", options.images);
const canvas = toGraphicsOption("canvas", options.canvas);
const pty = toPTYSpawner(options.ptyEmbeds);
const isolateFrame = findFrameIsolator();
const slots = _(this);
slots.colorDepth = toColorDepth(options.colorDepth);
slots.graphicsProtocols = {
Expand Down Expand Up @@ -774,6 +776,7 @@ export class TermDOM extends EventTarget {
images: images.setting,
canvas: canvas.setting,
pty,
isolateFrame,
load: (request, context) =>
answerRequest(
slots.fetchTarget,
Expand Down
Loading