feat(ipcclient): Add clientOnly mode to prevent clients from auto-starting servers and improve registration/reconnect behavior

This commit is contained in:
2025-08-29 08:48:38 +00:00
parent fd3fc7518b
commit fa53dcfc4f
8 changed files with 178 additions and 40 deletions

View File

@@ -45,6 +45,7 @@ export class IpcClient extends plugins.EventEmitter {
private messageHandlers = new Map<string, (payload: any) => any | Promise<any>>();
private isConnected = false;
private clientId: string;
private didRegisterOnce = false;
constructor(options: IIpcClientOptions) {
super();
@@ -66,30 +67,7 @@ export class IpcClient extends plugins.EventEmitter {
// Helper function to attempt registration
const attemptRegistration = async (): Promise<void> => {
const registerTimeoutMs = this.options.registerTimeoutMs || 5000;
try {
const response = await this.channel.request<any, any>(
'__register__',
{
clientId: this.clientId,
metadata: this.options.metadata
},
{
timeout: registerTimeoutMs,
headers: { clientId: this.clientId } // Include clientId in headers for proper routing
}
);
if (!response.success) {
throw new Error(response.error || 'Registration failed');
}
this.isConnected = true;
this.emit('connect');
} catch (error) {
throw new Error(`Failed to register with server: ${error.message}`);
}
await this.attemptRegistrationInternal();
};
// Helper function to attempt connection with retry
@@ -170,6 +148,38 @@ export class IpcClient extends plugins.EventEmitter {
}
}
/**
* Attempt to register this client over the current channel connection.
* Sets connection flags and emits 'connect' on success.
*/
private async attemptRegistrationInternal(): Promise<void> {
const registerTimeoutMs = this.options.registerTimeoutMs || 5000;
try {
const response = await this.channel.request<any, any>(
'__register__',
{
clientId: this.clientId,
metadata: this.options.metadata
},
{
timeout: registerTimeoutMs,
headers: { clientId: this.clientId }
}
);
if (!response.success) {
throw new Error(response.error || 'Registration failed');
}
this.isConnected = true;
this.didRegisterOnce = true;
this.emit('connect');
} catch (error: any) {
throw new Error(`Failed to register with server: ${error.message}`);
}
}
/**
* Disconnect from the server
*/
@@ -188,8 +198,16 @@ export class IpcClient extends plugins.EventEmitter {
*/
private setupChannelHandlers(): void {
// Forward channel events
this.channel.on('connect', () => {
// Don't emit connect here, wait for successful registration
this.channel.on('connect', async () => {
// On reconnects, re-register automatically when we had connected before
if (this.didRegisterOnce && !this.isConnected) {
try {
await this.attemptRegistrationInternal();
} catch (error) {
this.emit('error', error);
}
}
// For initial connect(), registration is handled explicitly there
});
this.channel.on('disconnect', (reason) => {
@@ -343,4 +361,4 @@ export class IpcClient extends plugins.EventEmitter {
public getStats(): any {
return this.channel.getStats();
}
}
}