Pty

A terminal in the page, driven over the bridge. The section existing is what registers the plugin: a page-reachable pty is command execution, so an app that never declares one carries no pty namespace at all — naming pty in plugins is then the unknown-plugin error it should be.

// crust.config.ts
pty: {
	shell: "/bin/zsh",
},

shell is the program the terminal runs; absent means the user's login shell. It is also the only program: pty.open accepts a program argument only when it resolves to this same one, so a grant of pty:open opens this terminal, never an arbitrary command. Nothing but the host's config decides what runs.

The pty is Bun.Terminal on macOS and Linux, so those need no native half. Windows has no Bun.Terminal, so it is CreatePseudoConsole over the same bridge API: the Windows shim opens a ConPTY, starts pty.shell with PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, and a reader thread drains the console's output pipe. The commands, events and refusals are the same on all three, and pty.shell is the program on Windows too — cmd.exe when the section names none.

platformwhat pty.open does
macOSworks — Bun.Terminal is the pty, no native half
Linuxworks — the same Bun.Terminal path
Windowsworks — ConPTY over a pseudo-console, with pty.shell (cmd.exe by default) as the program

Commands

commandargs → answer
open{ cols?, rows? } → { ok, id, pid }, 80×24 by default and clamped to 500×200; the shell starts in $HOME (the process directory when unset) with TERM=xterm-256color
write{ id, data } → { ok, bytes }; data is UTF-8 text up to 64 KiB, so a keystroke burst is one call and a larger paste is the page's to split
resize{ id, cols, rows } → { ok, id, cols, rows }
close{ id } → { ok, closed, id }; closing the master hangs the shell up, and a child that traps SIGHUP is signalled rather than left
list{} → { ok, ids } — the handles still open

A refusal is a value with a code: invalid-args, program-not-allowed, spawn-failed, unknown-terminal (no such handle) and terminal-closed (the handle existed and is gone). None of them throws.

Events

Bytes come back as server-sent events, never as a command's answer — a terminal is a stream, not a request:

window.crust.listen("pty-data", ({ id, chunk }) => terminal.write(atob(chunk)));
window.crust.listen("pty-exit", ({ id, exitCode, signal }) => console.log(id, exitCode, signal));

chunk is base64, because a pty delivers bytes: an ANSI escape sequence or a multi-byte character is routinely split across two reads, and a decoded string would corrupt it. Decode to bytes and hand them to your emulator. A normal frame carries at most 8 KiB, and one read emits at most 8 frames — a larger read is coalesced into the final frame rather than dropped, so a program that floods the pty cannot enqueue an unbounded number of events while every byte still reaches the page in order.

pty-exit carries the child's own status when it ends on its own. It is not Bun.Terminal's exit callback — that reports the pty's lifecycle (0 for EOF, 1 for error) and would report every clean exit as the wrong number. A pty.close is a termination, so that event reports { exitCode: null, signal: "SIGTERM" } on every platform rather than a shell status the close itself never observed.

Capability

The page calls pty.open, so a label needs the action pty:open. It is deliberately not implied by a shell grant: shell:spawn runs an allowlisted program with no terminal, while a pty is an interactive shell a page can type into, so the two are granted separately. See Permissions.

Capabilities are per window label, so a pty can be held by a terminal window and refused to every other page in the app.

Lifetime

Every terminal is tracked by the same reap shell uses: app.quit(), a crust dev restart's SIGTERM and a normal exit all signal the children, so a shell cannot outlive the app that opened it. pty.close is the way a page ends one. A shell that types exit is closed and dropped from list() by its own exit event, so a dead terminal is never reported as open. Closed and dead handles keep answering terminal-closed until a 64-record cap prunes them, so a long-lived page that opens many terminals cannot grow that set without bound.

What stays the app's job

The screen. crust ships bytes and a size, never a terminal model: xterm.js (or any emulator), the scrollback and its budget, the theme and font, hyperlinks and search, keyboard chords and setRawMode are all the page's. So is choosing the program — that is pty.shell, the host's decision, not a default crust picks for you.

When the section is absent

Nothing is registered. A page that calls pty.open gets unknown-plugin, and plugins: ["pty"] fails startup with crust: unknown plugin "pty", because the name resolves to nothing without the section that declares it. That is the point of the gate: the surface exists because the app asked for it, never because a plugin list mentioned a name.

Note that plugins restricts registration to what it names, so a plugins list that omits pty also keeps the surface out even with the section present. The section is the declaration; the list can only narrow.