|
| 1 | +# zigpty |
| 2 | + |
| 3 | +Tiny, cross-platform PTY library for Node.js, built in Zig, also usable as a standalone Zig package. Supports Linux, macOS, and Windows (via ConPTY). |
| 4 | + |
| 5 | +Drop-in replacement for [`node-pty`](https://github.com/microsoft/node-pty). **350x smaller** (43 KB vs 15.5 MB packed, 176 KB vs 64.4 MB installed), no `node-gyp` or C++ compiler needed, and ships musl prebuilds for Alpine. |
| 6 | + |
| 7 | +## Use cases |
| 8 | + |
| 9 | +Regular `child_process.spawn()` runs programs without a terminal attached. That means no colors, no cursor control, no prompts — programs like `vim`, `top`, `htop`, or interactive shells simply don't work. A **PTY** (pseudo-terminal) makes the subprocess think it's connected to a real terminal. Colors, line editing, full-screen TUIs, and terminal resizing all work as expected. |
| 10 | + |
| 11 | +- **Terminal emulators** — embed a terminal in Electron, Tauri, or a web app |
| 12 | +- **Remote shells** — stream a PTY over WebSocket from a Node.js server |
| 13 | +- **CI / automation** — run programs that require a TTY (interactive installers, REPLs) |
| 14 | +- **Testing** — test CLI tools that use colors, prompts, or cursor movement |
| 15 | +- **AI agents** — give LLM agents a real shell to run commands, observe output, and interact with CLIs |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +```ts |
| 20 | +import { spawn } from "zigpty"; |
| 21 | + |
| 22 | +// auto-detects default shell ($SHELL on Unix, %COMSPEC% on Windows) |
| 23 | +const pty = spawn(undefined, [], { |
| 24 | + cols: 80, |
| 25 | + rows: 24, |
| 26 | + terminal: { |
| 27 | + data(terminal, data: Uint8Array) { |
| 28 | + process.stdout.write(data); |
| 29 | + }, |
| 30 | + }, |
| 31 | + onExit(exitCode, signal) { |
| 32 | + console.log("exited:", exitCode); |
| 33 | + }, |
| 34 | +}); |
| 35 | + |
| 36 | +pty.write("echo hello\n"); |
| 37 | +pty.resize(120, 40); |
| 38 | +await pty.exited; // Promise<number> |
| 39 | +``` |
| 40 | + |
| 41 | +Terminal callbacks bypass Node.js streams and deliver raw `Uint8Array` directly from native code. You can also use the `onData`/`onExit` event listeners instead: |
| 42 | + |
| 43 | +```ts |
| 44 | +pty.onData((data) => process.stdout.write(data)); |
| 45 | +pty.onExit(({ exitCode }) => console.log("exited:", exitCode)); |
| 46 | +``` |
| 47 | + |
| 48 | +The `Terminal` class can be reused across multiple spawns and supports `AsyncDisposable`: |
| 49 | + |
| 50 | +```ts |
| 51 | +import { spawn, Terminal } from "zigpty"; |
| 52 | + |
| 53 | +await using terminal = new Terminal({ |
| 54 | + data(term, data) { process.stdout.write(data); }, |
| 55 | +}); |
| 56 | + |
| 57 | +const pty = spawn("/bin/sh", ["-c", "echo hello"], { terminal }); |
| 58 | +await pty.exited; |
| 59 | +// terminal.close() called automatically by `await using` |
| 60 | +``` |
| 61 | + |
| 62 | +## API |
| 63 | + |
| 64 | +### `spawn(file, args?, options?)` |
| 65 | + |
| 66 | +Spawn a process inside a new PTY. |
| 67 | + |
| 68 | +**Options:** |
| 69 | + |
| 70 | +```ts |
| 71 | +interface IPtyOptions { |
| 72 | + cols?: number; // Default: 80 |
| 73 | + rows?: number; // Default: 24 |
| 74 | + cwd?: string; // Default: process.cwd() |
| 75 | + env?: Record<string, string>; // Default: process.env |
| 76 | + name?: string; // Sets TERM (e.g. "xterm-256color") |
| 77 | + encoding?: BufferEncoding | null; // Default: "utf8", null for raw Buffer |
| 78 | + uid?: number; // Unix user ID |
| 79 | + gid?: number; // Unix group ID |
| 80 | + handleFlowControl?: boolean; // Intercept XON/XOFF (default: false) |
| 81 | + terminal?: TerminalOptions | Terminal; // Bun-compatible terminal callbacks |
| 82 | + onExit?: (exitCode: number, signal: number) => void; |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +**Returns:** |
| 87 | + |
| 88 | +```ts |
| 89 | +interface IPty { |
| 90 | + pid: number; |
| 91 | + cols: number; |
| 92 | + rows: number; |
| 93 | + readonly process: string; // Foreground process name |
| 94 | + readonly exited: Promise<number>; // Resolves with exit code |
| 95 | + readonly exitCode: number | null; // Exit code or null if running |
| 96 | + |
| 97 | + onData: (cb: (data: string | Buffer) => void) => IDisposable; |
| 98 | + onExit: (cb: (e: { exitCode: number; signal: number }) => void) => IDisposable; |
| 99 | + |
| 100 | + write(data: string): void; |
| 101 | + resize(cols: number, rows: number): void; |
| 102 | + kill(signal?: string): void; // Default: SIGHUP |
| 103 | + pause(): void; |
| 104 | + resume(): void; |
| 105 | + close(): void; |
| 106 | +} |
| 107 | + |
| 108 | +### `open(options?)` |
| 109 | + |
| 110 | +Create a PTY pair without spawning a process — useful when you need to control the child process yourself. |
| 111 | + |
| 112 | +```ts |
| 113 | +import { open } from "zigpty"; |
| 114 | +
|
| 115 | +const { master, slave, pty } = open({ cols: 80, rows: 24 }); |
| 116 | +``` |
| 117 | + |
| 118 | +## Platform support |
| 119 | + |
| 120 | +| Platform | Status | |
| 121 | +| -------------------- | ------- | |
| 122 | +| Linux x64 (glibc) | ✅ | |
| 123 | +| Linux x64 (musl) | ✅ | |
| 124 | +| Linux arm64 (glibc) | ✅ | |
| 125 | +| Linux arm64 (musl) | ✅ | |
| 126 | +| macOS x64 | ✅ | |
| 127 | +| macOS arm64 | ✅ | |
| 128 | +| Windows x64 | ✅ | |
| 129 | +| Windows arm64 | ✅ | |
| 130 | + |
| 131 | +All 8 platform binaries are prebuilt — no compiler needed at install time. On Linux, the native loader tries glibc first and falls back to musl automatically. |
| 132 | + |
| 133 | +## Zig package |
| 134 | + |
| 135 | +The PTY core is a standalone Zig package with no Node.js or NAPI dependency. |
| 136 | + |
| 137 | +```sh |
| 138 | +zig fetch --save git+https://github.com/pithings/zigpty.git |
| 139 | +``` |
| 140 | + |
| 141 | +Wire it up in `build.zig`: |
| 142 | + |
| 143 | +```zig |
| 144 | +const zigpty = b.dependency("zigpty", .{ .target = target, .optimize = optimize }); |
| 145 | +exe.root_module.addImport("zigpty", zigpty.module("zigpty")); |
| 146 | +``` |
| 147 | + |
| 148 | +API: |
| 149 | + |
| 150 | +```zig |
| 151 | +const pty = @import("zigpty"); |
| 152 | +
|
| 153 | +// Fork a process with a PTY |
| 154 | +const result = try pty.forkPty(.{ |
| 155 | + .file = "/bin/bash", |
| 156 | + .argv = &.{ "/bin/bash", null }, |
| 157 | + .envp = &.{ "TERM=xterm-256color", null }, |
| 158 | + .cwd = "/home/user", |
| 159 | + .cols = 120, |
| 160 | + .rows = 40, |
| 161 | +}); |
| 162 | +// result.fd — PTY file descriptor (read/write) |
| 163 | +// result.pid — child process ID |
| 164 | +
|
| 165 | +// Open a bare PTY pair (no process spawned) |
| 166 | +const pair = try pty.openPty(80, 24); |
| 167 | +// pair.master, pair.slave |
| 168 | +
|
| 169 | +// Resize |
| 170 | +try pty.resize(result.fd, 80, 24, 0, 0); |
| 171 | +
|
| 172 | +// Foreground process name |
| 173 | +var buf: [4096]u8 = undefined; |
| 174 | +const name: ?[]const u8 = pty.getProcessName(result.fd, &buf); |
| 175 | +
|
| 176 | +// Block until child exits |
| 177 | +const exit_info = pty.waitForExit(result.pid); |
| 178 | +// exit_info.exit_code, exit_info.signal_code |
| 179 | +``` |
| 180 | + |
| 181 | +## Building from source |
| 182 | + |
| 183 | +Requires [Zig](https://ziglang.org/) 0.15.1+. |
| 184 | + |
| 185 | +```sh |
| 186 | +zig build # Build prebuilds (all targets) |
| 187 | +zig build --release # Release build |
| 188 | +bun run build # Build + bundle TypeScript |
| 189 | +bun vitest run # Run tests |
| 190 | +``` |
| 191 | + |
| 192 | +## Credits |
| 193 | + |
| 194 | +API-compatible with [node-pty](https://github.com/microsoft/node-pty). Terminal API inspired by [Bun](https://bun.sh/docs/runtime/child-process#terminal-pty-support). |
| 195 | + |
| 196 | +## License |
| 197 | + |
| 198 | +MIT |
0 commit comments