このコンテンツはまだ日本語訳がありません。
PtyHandle
PTY session handle for managing a single PTY session.
Properties:
sessionIdstring
Provides methods for sending input, resizing the terminal, waiting for completion, and managing the WebSocket connection to a PTY session.
Example:
// Create a PTY sessionconst ptyHandle = await process.createPty({ id: 'my-session', cols: 120, rows: 30, onData: (data) => { const text = new TextDecoder().decode(data); process.stdout.write(text); },});
// Send commandsawait ptyHandle.sendInput('ls -la\n');await ptyHandle.sendInput('exit\n');
// Wait for completionconst result = await ptyHandle.wait();console.log(`PTY exited with code: ${result.exitCode}`);
// Clean upawait ptyHandle.disconnect();Accessors
error
Get Signature
get error(): stringError message if the PTY failed
Returns:
string
exitCode
Get Signature
get exitCode(): numberExit code of the PTY process (if terminated)
Returns:
number
Constructors
new PtyHandle()
new PtyHandle( ws: WebSocket, handleResize: (cols: number, rows: number) => Promise<PtySessionInfo>, handleKill: () => Promise<void>, onPty: (data: Uint8Array) => void | Promise<void>, sessionId: string): PtyHandleParameters:
wsWebSockethandleResize(cols: number, rows: number) => Promise<PtySessionInfo>handleKill() => Promise<void>onPty(data: Uint8Array) => void | Promise<void>sessionIdstring
Returns:
PtyHandle
Methods
disconnect()
disconnect(): Promise<void>Disconnect from the PTY session and clean up resources.
Closes the WebSocket connection and releases any associated resources. Should be called when done with the PTY session.
Returns:
Promise<void>
Example:
// Always clean up when donetry { // ... use PTY session} finally { await ptyHandle.disconnect();}isConnected()
isConnected(): booleanCheck if connected to the PTY session
Returns:
boolean
kill()
kill(): Promise<void>Kill the PTY process and terminate the session.
Forcefully terminates the PTY session and its associated process. This operation is irreversible and will cause the PTY to exit immediately.
Returns:
Promise<void>
Throws:
If the kill operation fails
Example:
// Kill a long-running processawait ptyHandle.kill();
// Wait to confirm terminationconst result = await ptyHandle.wait();console.log(`Process terminated with exit code: ${result.exitCode}`);resize()
resize(cols: number, rows: number): Promise<PtySessionInfo>Resize the PTY terminal dimensions.
Changes the terminal size which will notify terminal applications about the new dimensions via SIGWINCH signal.
Parameters:
colsnumber - New number of terminal columnsrowsnumber - New number of terminal rows
Returns:
Promise<PtySessionInfo>
Example:
// Resize to 120x30await ptyHandle.resize(120, 30);sendInput()
sendInput(data: string | Uint8Array<ArrayBufferLike>): Promise<void>Send input data to the PTY session.
Sends keyboard input or commands to the terminal session. The data will be processed as if it was typed in the terminal.
Parameters:
dataInput data to send (commands, keystrokes, etc.) -string|Uint8Array<ArrayBufferLike>
Returns:
Promise<void>
Throws:
If PTY is not connected or sending fails
Example:
// Send a commandawait ptyHandle.sendInput('ls -la\n');
// Send raw bytesawait ptyHandle.sendInput(new Uint8Array([3])); // Ctrl+Cwait()
wait(): Promise<PtyResult>Wait for the PTY process to exit and return the result.
This method blocks until the PTY process terminates and returns information about how it exited.
Returns:
Promise<PtyResult>- Result containing exit code and error information
Example:
// Wait for process to completeconst result = await ptyHandle.wait();
if (result.exitCode === 0) { console.log('Process completed successfully');} else { console.log(`Process failed with code: ${result.exitCode}`); if (result.error) { console.log(`Error: ${result.error}`); }}waitForConnection()
waitForConnection(): Promise<void>Wait for the WebSocket connection to be established.
This method ensures the PTY session is ready to receive input and send output. It waits for the server to confirm the connection is established.
Returns:
Promise<void>
Throws:
If connection times out (10 seconds) or connection fails