UNPKG

@comake/skl-js-engine

Version:

Standard Knowledge Language Javascript Engine

87 lines (86 loc) 2.62 kB
/// <reference types="node" /> import { EventEmitter } from 'node:events'; /** * Bidirectional StdioTransport for child processes * This class handles both server (receiving requests) and client (sending requests) functionality * over a single stdio channel with proper message routing. * * Usage in child process: * ```typescript * const transport = new ChildStdioTransport(); * * // Register methods that parent can call * transport.registerMethod('ping', async () => 'pong'); * * // Make requests to parent * const result = await transport.request('getTime'); * ``` */ export declare class ChildStdioTransport extends EventEmitter { private readonly server; private readonly client; private readonly logger; private readonly messageBuffer; private name?; private initialized; constructor(); /** * Set a name for this transport (used in logging) */ setName(name: string): void; /** * Initialize the bidirectional transport * This should be called once after setting up all methods */ initialize(): Promise<void>; /** * Register a method that the parent can call * @param method - Method name * @param handler - Method handler function */ registerMethod<TParams = any, TResult = any>(method: string, handler: (params: TParams) => Promise<TResult> | TResult): void; /** * Send a request to the parent process * @param method - Method name * @param params - Method parameters * @param options - Request options * @returns Promise resolving to the response */ request<TParams = any, TResult = any>(method: string, params?: TParams, options?: { timeout?: number; }): Promise<TResult>; /** * Send a notification to the parent process (no response expected) * @param method - Method name * @param params - Method parameters */ notify<TParams = any>(method: string, params?: TParams): Promise<void>; /** * Get transport statistics */ getStats(): { serverMethods: number; pendingRequests: number; initialized: boolean; }; /** * Close the transport and cleanup resources */ close(): Promise<void>; /** * Set up stdio communication handlers */ private setupStdioCommunication; /** * Handle incoming message and route to appropriate handler */ private handleIncomingMessage; /** * Send a message to the parent process */ private sendMessage; /** * Set up event handlers */ private setupEventHandlers; }