import * as plugins from './smartipc.plugins.js'; import { IpcChannel } from './classes.ipcchannel.js'; import type { IIpcChannelOptions } from './classes.ipcchannel.js'; /** * Options for IPC Client */ export interface IIpcClientOptions extends IIpcChannelOptions { /** Client identifier */ clientId?: string; /** Client metadata */ metadata?: Record; } /** * IPC Client for connecting to an IPC server */ export class IpcClient extends plugins.EventEmitter { private options: IIpcClientOptions; private channel: IpcChannel; private messageHandlers = new Map any | Promise>(); private isConnected = false; private clientId: string; constructor(options: IIpcClientOptions) { super(); this.options = options; this.clientId = options.clientId || plugins.crypto.randomUUID(); // Create the channel this.channel = new IpcChannel(this.options); this.setupChannelHandlers(); } /** * Connect to the server */ public async connect(): Promise { if (this.isConnected) { return; } // Connect the channel await this.channel.connect(); // Register with the server try { const response = await this.channel.request( '__register__', { clientId: this.clientId, metadata: this.options.metadata }, { timeout: 5000 } ); if (!response.success) { throw new Error(response.error || 'Registration failed'); } this.isConnected = true; this.emit('connect'); } catch (error) { await this.channel.disconnect(); throw new Error(`Failed to register with server: ${error.message}`); } } /** * Disconnect from the server */ public async disconnect(): Promise { if (!this.isConnected) { return; } this.isConnected = false; await this.channel.disconnect(); this.emit('disconnect'); } /** * Setup channel event handlers */ private setupChannelHandlers(): void { // Forward channel events this.channel.on('connect', () => { // Don't emit connect here, wait for successful registration }); this.channel.on('disconnect', (reason) => { this.isConnected = false; this.emit('disconnect', reason); }); this.channel.on('error', (error) => { this.emit('error', error); }); this.channel.on('reconnecting', (info) => { this.emit('reconnecting', info); }); // Handle messages this.channel.on('message', (message) => { // Check if we have a handler for this message type if (this.messageHandlers.has(message.type)) { const handler = this.messageHandlers.get(message.type)!; // If message expects a response if (message.headers?.requiresResponse && message.id) { Promise.resolve() .then(() => handler(message.payload)) .then((result) => { return this.channel.sendMessage( `${message.type}_response`, result, { correlationId: message.id } ); }) .catch((error) => { return this.channel.sendMessage( `${message.type}_response`, null, { correlationId: message.id, error: error.message } ); }); } else { // Fire and forget handler(message.payload); } } else { // Emit unhandled message this.emit('message', message); } }); } /** * Register a message handler */ public onMessage(type: string, handler: (payload: any) => any | Promise): void { this.messageHandlers.set(type, handler); } /** * Send a message to the server */ public async sendMessage(type: string, payload: any, headers?: Record): Promise { if (!this.isConnected) { throw new Error('Client is not connected'); } // Always include clientId in headers await this.channel.sendMessage(type, payload, { ...headers, clientId: this.clientId }); } /** * Send a request to the server and wait for response */ public async request( type: string, payload: TReq, options?: { timeout?: number; headers?: Record } ): Promise { if (!this.isConnected) { throw new Error('Client is not connected'); } // Always include clientId in headers return this.channel.request(type, payload, { ...options, headers: { ...options?.headers, clientId: this.clientId } }); } /** * Subscribe to a topic (pub/sub pattern) */ public async subscribe(topic: string, handler: (payload: any) => void): Promise { // Register local handler this.messageHandlers.set(`topic:${topic}`, handler); // Notify server about subscription await this.sendMessage('__subscribe__', { topic }); } /** * Unsubscribe from a topic */ public async unsubscribe(topic: string): Promise { // Remove local handler this.messageHandlers.delete(`topic:${topic}`); // Notify server about unsubscription await this.sendMessage('__unsubscribe__', { topic }); } /** * Publish to a topic */ public async publish(topic: string, payload: any): Promise { await this.sendMessage('__publish__', { topic, payload }); } /** * Get client ID */ public getClientId(): string { return this.clientId; } /** * Check if client is connected */ public getIsConnected(): boolean { return this.isConnected && this.channel.isConnected(); } /** * Get client statistics */ public getStats(): any { return this.channel.getStats(); } }