sandbox.pty. The module
has three callables you’ll use directly: pty.create() to start a new
session, pty.connect() to reattach to an existing one, and the
low-level pty.sendStdin / resize / kill trio when you already hold
a pid.
For conceptual background see the
PTY feature overview.
sandbox.pty.create(opts?) → Promise<PtyHandle>
Create a new PTY session. The sandbox spawns an interactive bash -l
login shell with TERM=xterm-256color pre-set.
PtyCreateOpts
| Field | Type | Default | Description |
|---|---|---|---|
size | PtySize | { cols: 80, rows: 24 } | Initial terminal size. |
user | string | "user" | User the shell runs as. |
cwd | string | undefined | Starting working directory. |
envs | Record<string, string> | undefined | Environment variables merged into the shell env. TERM defaults to xterm-256color. |
timeout | number | 3600 | PTY session TTL in seconds. 0 keeps the session alive until the sandbox itself expires. |
onData | (data: Uint8Array) => void | undefined | If provided, the handle auto-opens the output stream and invokes this for each chunk. |
requestTimeout | number | undefined | Per-request timeout in milliseconds for the create POST. |
sandbox.pty.connect(pid, opts?) → PtyHandle
Reattach to a PTY session that’s already running. Returns a fresh
PtyHandle that streams live output from the moment it connects.
Multiple clients can be attached to the same pid concurrently.
PtyConnectOpts
| Field | Type | Description |
|---|---|---|
onData | (data: Uint8Array) => void | Optional callback invoked with every output chunk. Omit to drive the stream manually via handle.stream(). |
PtyHandle
Returned by both create() and connect().
Properties
handle.pid: number— remote process id.
Methods
handle.sendInput(data: Uint8Array | string, requestTimeout?: number): Promise<void>
Forward input to the shell. Strings are UTF-8 encoded.
handle.resize(size: PtySize, requestTimeout?: number): Promise<void>
Change the remote terminal dimensions. Fires SIGWINCH inside the
sandbox so ncurses apps redraw.
handle.disconnect(): void
Stop consuming output without killing the remote process. A later
sandbox.pty.connect(pid) reattaches cleanly.
handle.kill(requestTimeout?: number): Promise<boolean>
Terminate the remote shell. Returns true if the session existed at
the time of the call.
handle.wait(): Promise<PtyResult>
Resolves when the remote shell exits.
handle.stream(): AsyncGenerator<Uint8Array>
Async iterator over raw output chunks — for when you want to drive the
stream manually instead of via onData:
onData and stream() on the same handle; they consume the
same underlying connection.
PtyResult
Low-level API (by pid)
When you don’t hold aPtyHandle, use the module-level methods:
Example: wire a sandbox PTY into xterm.js
sendInput, every output byte
comes back via onData, every pane resize fires TIOCSWINSZ inside
the microVM.