/** * PTY session manager — mirrors `edge-client/src/session/shell.rs`. * * Owns PTY sessions keyed by `session_id` (UUID v4 string). Spawns the * shell via `node-pty`, pumps PTY stdout → framed data frames → an outbound * channel, writes browser keystrokes (data frames) into the PTY, handles * resize/close, and emits `shellExited` lifecycle events. * * Security: the PTY shell runs as the edge-client's Standard User (per * SPEC.md) — no privilege escalation. Shell is only active when * `shell.enabled = true` in config (default false); enforced by the * constructor gate. * * Frame codec: the 17-byte binary frame format (`[16-byte session_id | * 1-byte frame_type | payload]`) is provided by `shell-frame.ts`. We use * it directly via a thin `FrameCodec` adapter that converts UUID strings * to/from raw bytes at the codec boundary — keeping the wire bytes * EXACTLY byte-compatible with the Rust `portable-pty` + `portable-pty` * implementation. */ import type { IPty } from 'node-pty'; import { FrameType, type FrameTypeValue } from './shell-frame.js'; export type { IPty } from 'node-pty'; export { FrameType }; export type { FrameTypeValue }; /** * Frame codec adapter — internally delegates to `shell-frame.ts`'s * byte-exact codec, but exposes a string-keyed interface so this module * can pass UUID strings around as the rest of `session/` does. Wire * format is IDENTICAL to `shell-frame.ts` (no behavior drift possible). */ export interface FrameCodec { encode(sessionId: string, frameType: FrameTypeValue, payload: Buffer): Buffer; decode(data: Buffer): { sessionId: string; frameType: FrameTypeValue; payload: Buffer; } | undefined; } /** Default codec backed by `shell-frame.ts`'s byte-exact primitives. */ export declare const inlineFrameCodec: FrameCodec; /** Options passed to the PTY spawn function. */ export interface PtySpawnOptions { readonly name: string; readonly cols: number; readonly rows: number; readonly cwd: string; readonly env: Record; } /** * Function that spawns a PTY. Production uses `node-pty`, tests inject mocks. * Mirrors `pty.spawn(shell, args, opts)`. Returns IPty synchronously. */ export type PtyFactory = (program: string, args: string[], options: PtySpawnOptions) => IPty; export interface ShellManagerOptions { /** Shell gate — if false, all open() calls throw. Mirrors Rust shell.enabled. */ readonly enabled: boolean; } export type DataHandler = (sessionId: string, frameType: FrameTypeValue, payload: Buffer) => void; export type ExitHandler = (sessionId: string, code: number) => void; export declare class ShellManager { private readonly enabled; private readonly sessions; private readonly emitter; private codec; /** Injectable PTY factory — tests inject a mock, production uses defaultPtyFactory. */ private readonly ptyFactory; constructor(opts: ShellManagerOptions, codec?: FrameCodec, ptyFactory?: PtyFactory); /** * Open a new PTY shell session. The caller provides a UUID session_id * (assigned by the session manager). * * Mirrors Rust `ShellManager::open`. */ open(sessionId: string, cols: number, rows: number, shell?: string): Promise; /** * Write data to a session's PTY stdin (browser keystrokes). * Mirrors Rust `FrameType::Data` handling in `handle_frame`. */ write(sessionId: string, data: Buffer): void; /** * Resize a session's PTY. * Mirrors Rust `FrameType::Resize` handling in `handle_frame`. */ resize(sessionId: string, cols: number, rows: number): void; /** * Close a session's PTY (browser-initiated). * Mirrors Rust `close_session`. */ close(sessionId: string): void; /** * Handle an inbound binary frame from the browser (relayed by relay). * Mirrors Rust `ShellManager::handle_frame`. */ handleFrame(frameType: FrameTypeValue, sessionId: string, payload: Buffer): void; /** * Register a handler for PTY data frames (sessionId, frameType, payload). * The handler receives the structured payload, not raw wire bytes. */ onData(handler: DataHandler): void; /** * Register a handler for session exit events (sessionId, exitCode). */ onExit(handler: ExitHandler): void; /** * Register a handler for raw wire frames (Buffer). * The binary WS transport layer listens on this to send outbound frames. */ onFrame(handler: (frame: Buffer) => void): void; /** Number of active PTY sessions. For heartbeat metadata. */ activeCount(): number; /** * Close all sessions (edge shutdown). Mirrors Rust `close_all`. */ closeAll(): void; } /** Build a data frame (FrameType::Data). */ export declare function buildDataFrame(sessionId: string, payload: Buffer): Buffer; /** * Build a resize frame (FrameType::Resize). payload = cols (u16 BE) + rows (u16 BE). */ export declare function buildResizeFrame(sessionId: string, cols: number, rows: number): Buffer; /** Build a close frame (FrameType::Close). No payload. */ export declare function buildCloseFrame(sessionId: string): Buffer;