bun-pty-rust

A fork of bun-pty with enhanced Rust-based PTY implementation for improved performance and reliability.
A cross-platform pseudo-terminal (PTY) implementation for Bun, powered by Rust's portable-pty library and Bun's FFI capabilities.
🚀 Features
- Cross-platform - Works on macOS, Linux, and Windows
- Simple API - Clean Promise-based API similar to node-pty
- Type-safe - Complete TypeScript definitions included
- Efficient - Rust backend with proper error handling and multithreading
- Zero dependencies - No external JavaScript dependencies required
- Modern - Built specifically for Bun using its FFI capabilities
📦 Installation
bun add bun-pty-rust
⚙️ Requirements
- Bun 1.0.0 or higher
- Rust is only needed if you're building from source (the npm package includes pre-built binaries)
📋 Platform Support
| macOS | ✅ | Fully supported |
| Linux | ✅ | Fully supported |
| Windows | ✅ | Fully supported |
🚦 Usage
Basic Example
import { spawn } from "bun-pty";
const terminal = spawn("bash", [], {
name: "xterm-256color",
cols: 80,
rows: 24
});
terminal.onData((data) => {
console.log("Received:", data);
});
terminal.onExit(({ exitCode, signal }) => {
console.log(`Process exited with code ${exitCode} and signal ${signal}`);
});
terminal.write("echo Hello from Bun PTY\n");
terminal.resize(100, 40);
setTimeout(() => {
terminal.kill();
}, 5000);
TypeScript Usage
The library includes complete TypeScript definitions. Here's how to use it with full type safety:
import { spawn } from "bun-pty";
import type { IPty, IExitEvent, IPtyForkOptions } from "bun-pty";
const options: IPtyForkOptions = {
name: "xterm-256color",
cols: 100,
rows: 30,
cwd: process.cwd()
};
const terminal: IPty = spawn("bash", [], options);
const dataHandler = terminal.onData((data: string) => {
process.stdout.write(data);
});
const exitHandler = terminal.onExit((event: IExitEvent) => {
console.log(`Process exited with code: ${event.exitCode}`);
});
dataHandler.dispose();
exitHandler.dispose();
Interactive Shell Example
import { spawn } from "bun-pty";
import { createInterface } from "node:readline";
const pty = spawn("bash", [], {
name: "xterm-256color",
cwd: process.cwd()
});
pty.onData((data) => {
process.stdout.write(data);
});
process.stdin.on("data", (data) => {
pty.write(data.toString());
});
pty.onExit(() => {
console.log("Terminal session ended");
process.exit(0);
});
process.on("SIGINT", () => {
pty.kill();
});
📖 API Reference
spawn(file: string, args: string[], options: IPtyForkOptions): IPty
Creates and spawns a new pseudoterminal.
file: The executable to launch
args: Arguments to pass to the executable
options: Configuration options
name: Terminal name (e.g., "xterm-256color")
cols: Number of columns (default: 80)
rows: Number of rows (default: 24)
cwd: Working directory (default: process.cwd())
env: Environment variables
Returns an IPty instance.
IPty Interface
interface IPty {
readonly pid: number;
readonly cols: number;
readonly rows: number;
readonly process: string;
onData: (listener: (data: string) => void) => IDisposable;
onExit: (listener: (event: IExitEvent) => void) => IDisposable;
write(data: string): void;
resize(cols: number, rows: number): void;
kill(signal?: string): void;
}
Event Types
interface IExitEvent {
exitCode: number;
signal?: number | string;
}
interface IDisposable {
dispose(): void;
}
🔧 Building from Source
If you want to build the package from source:
git clone https://github.com/shunkakinoki/bun-pty-rust.git
cd bun-pty-rust
bun install
bun run build
bun test
❓ Troubleshooting
Prebuilt Binaries
The npm package includes prebuilt binaries for macOS, Linux, and Windows. If you encounter issues with the prebuilt binaries, you can build from source:
bun add bun-pty
cd node_modules/bun-pty
bun run build
Common Issues
- Error: Unable to load shared library: Make sure you have the necessary system libraries installed.
- Process spawn fails: Check if you have the required permissions and paths.
📄 License
This project is licensed under the MIT License.
🙏 Credits
- Built specifically for Bun
- Uses portable-pty from WezTerm for cross-platform PTY support
- Inspired by node-pty for the API design