UNPKG

@grasplabs/grasp

Version:

TypeScript SDK for browser automation and secure command execution in highly available and scalable cloud browser environments

184 lines 6.86 kB
import { CommandHandle, CommandResult, CommandStartOpts, Sandbox } from 'e2b'; import { EventEmitter } from 'events'; import { ISandboxConfig, SandboxStatus, IScriptOptions } from '../types'; /** * Extended EventEmitter for background command execution * Emits 'stdout', 'stderr', and 'exit' events */ export declare class CommandEventEmitter extends EventEmitter { private handle; private isKilled; constructor(handle?: CommandHandle | null); /** * Set the command handle (used internally) * @param handle - Command handle to set */ setHandle(handle: CommandHandle): void; /** * Wait for command to complete * @returns Promise with command result */ wait(): Promise<CommandResult>; /** * Kill the running command */ kill(): Promise<void>; /** * Get the original command handle */ getHandle(): CommandHandle | null; } /** * E2B Sandbox service for managing sandbox lifecycle and operations */ export declare class SandboxService { private config; sandbox: Sandbox | null; private status; private logger; /** * Gets or creates a default logger instance * @returns Logger instance */ private getDefaultLogger; constructor(config: ISandboxConfig); get id(): string | undefined; get workspace(): string; get isDebug(): boolean; get timeout(): number; connectSandbox(sandboxId: string): Promise<void>; /** * Creates and starts a new sandbox * @returns Promise that resolves when sandbox is ready * @throws {Error} If sandbox creation fails */ createSandbox(templateId: string, envs?: Record<string, string>): Promise<void>; /** * Runs a command in the sandbox (background execution) * @param command - Command to execute * @param options - Execution options with background: true * @returns Promise with CommandEventEmitter for background execution * @throws {Error} If sandbox is not running or command fails */ runCommand(command: string, options: CommandStartOpts & { nohup?: boolean; background: true; }, quiet?: boolean): Promise<CommandEventEmitter>; /** * Runs a command in the sandbox (synchronous execution) * @param command - Command to execute * @param options - Execution options with background: false or undefined * @returns Promise with CommandResult for synchronous execution * @throws {Error} If sandbox is not running or command fails */ runCommand(command: string, options?: CommandStartOpts & { nohup?: boolean; background?: false; }, quiet?: boolean): Promise<CommandResult>; /** * Runs JavaScript code in the sandbox (background execution) * @param code - JavaScript code to execute * @param options - Script execution options with background: true * @returns Promise with CommandEventEmitter for background execution * @throws {Error} If sandbox is not running or script execution fails */ runScript(code: string, options: IScriptOptions & { background: true; }): Promise<CommandEventEmitter>; /** * Runs JavaScript code in the sandbox (synchronous execution) * @param code - JavaScript code to execute * @param options - Script execution options with background: false or undefined * @returns Promise with CommandResult for synchronous execution * @throws {Error} If sandbox is not running or script execution fails */ runScript(code: string, options?: IScriptOptions & { background?: false; }): Promise<CommandResult>; /** * Check if a file is binary based on its extension * @param filePath - File path to check * @returns True if file is binary, false otherwise */ private isBinaryFile; private encodeContent; readFileFromSandbox(remotePath: string, options?: { encoding?: 'utf8' | 'base64' | 'binary'; }): Promise<string | Buffer<ArrayBufferLike>>; writeFileToSandbox(remotePath: string, content: string | ArrayBuffer): Promise<import("e2b").EntryInfo>; /** * Copy file from sandbox to local filesystem * @param remotePath - File path in sandbox * @param localPath - Local destination path * @returns Promise that resolves when file is copied * @throws {Error} If sandbox is not running or copy fails */ copyFileFromSandbox(remotePath: string, localPath: string): Promise<void>; /** * Uploads a file to the sandbox * @param localPath - Local file path * @param remotePath - Destination path in sandbox * @returns Promise that resolves when file is uploaded * @throws {Error} If sandbox is not running or upload fails */ uploadFileToSandbox(localPath: string, remotePath: string): Promise<void>; /** * Lists files in a sandbox directory * @param path - Directory path in sandbox * @returns Promise with list of files * @throws {Error} If sandbox is not running or listing fails */ listFiles(path: string): Promise<string[]>; /** * 验证路径是否安全,只允许同步 /home/user 目录下的子目录 * @param path - 要验证的路径 * @returns 是否为安全路径 * @private */ private validateSafePath; /** * 检查远程路径是否为目录 * @param remotePath - 远程路径 * @returns 是否为目录 * @private */ private isDirectory; /** * 递归同步目录 * @param remotePath - 远程目录路径 * @param localPath - 本地目录路径 * @returns 同步的文件数量 * @private */ private syncDirectoryRecursive; /** * 同步 sandbox 中的 downloads 目录到本地(递归同步所有子目录) * @param dist - 本地目标目录路径,默认为 '/tmp/grasp/downloads' * @param src - 远程源目录路径,默认为 './downloads' * @returns 本地同步目录路径 * @throws {Error} 当路径不安全时抛出错误 */ syncDownloadsDirectory(dist?: string, src?: string): Promise<string>; /** * Gets the current sandbox status * @returns Current sandbox status */ getStatus(): SandboxStatus; /** * Gets sandbox ID if available * @returns Sandbox ID or null */ getSandboxId(): string | null; /** * Gets the external host address for a specific port * @param port - Port number to get host for * @returns External host address or null if sandbox not available */ getSandboxHost(port: number): string | null; /** * Destroys the sandbox and cleans up resources * @returns Promise that resolves when sandbox is destroyed */ destroy(): Promise<void>; } //# sourceMappingURL=sandbox.service.d.ts.map