The Pty module provides interactive pseudo-terminal (PTY) sessions over WebSocket. Access it via sandbox.pty.
Creating a PTY Session
const session = await sandbox . pty . create ({
cols: 120 ,
rows: 40 ,
onOutput : ( data ) => {
process . stdout . write ( data );
},
});
sandbox.pty.create(opts?)
Opens a new interactive terminal session.
Terminal width in columns.
onOutput
(data: Uint8Array) => void
Callback invoked when the terminal produces output.
Returns: Promise<PtySession>
PtySession
Property / Method Type Description sessionIdstringUnique session identifier send(data)(data: string | Uint8Array) => voidSend input to the terminal close()() => voidClose the PTY session
Examples
Run an Interactive Command
const session = await sandbox . pty . create ({
onOutput : ( data ) => {
const text = new TextDecoder (). decode ( data );
process . stdout . write ( text );
},
});
// Type a command
session . send ( 'ls -la \n ' );
// Wait a moment for output, then close
setTimeout (() => session . close (), 2000 );
const session = await sandbox . pty . create ({
cols: 80 ,
rows: 24 ,
onOutput : ( data ) => {
process . stdout . write ( new TextDecoder (). decode ( data ));
},
});
// Forward stdin to the PTY
process . stdin . setRawMode ( true );
process . stdin . on ( 'data' , ( chunk ) => {
session . send ( chunk );
});
PTY sessions use WebSocket connections under the hood. Each session maintains a persistent connection to the sandbox worker.