fix(ipc): Propagate per-client disconnects, add proper routing for targeted messages, and remove unused node-ipc deps

This commit is contained in:
2025-08-29 17:02:50 +00:00
parent 44770bf820
commit 1c08df8e6a
7 changed files with 53 additions and 223 deletions

View File

@@ -211,6 +211,17 @@ export class IpcServer extends plugins.EventEmitter {
this.isRunning = true;
this.startClientIdleCheck();
this.emit('start');
// Track individual client disconnects forwarded by the channel/transport
this.primaryChannel.on('clientDisconnected', (clientId?: string) => {
if (!clientId) return;
// Clean up any topic subscriptions and client map entry
this.cleanupClientSubscriptions(clientId);
if (this.clients.has(clientId)) {
this.clients.delete(clientId);
this.emit('clientDisconnect', clientId);
}
});
// Handle readiness based on options
if (options.readyWhen === 'accepting') {
@@ -375,7 +386,14 @@ export class IpcServer extends plugins.EventEmitter {
throw new Error(`Client ${clientId} not found`);
}
await client.channel.sendMessage(type, payload, headers);
// Ensure the target clientId is part of the headers so the transport
// can route the message to the correct socket instead of broadcasting.
const routedHeaders: Record<string, any> | undefined = {
...(headers || {}),
clientId,
};
await client.channel.sendMessage(type, payload, routedHeaders);
}
/**
@@ -400,13 +418,12 @@ export class IpcServer extends plugins.EventEmitter {
*/
public async broadcast(type: string, payload: any, headers?: Record<string, any>): Promise<void> {
const promises: Promise<void>[] = [];
for (const [clientId, client] of this.clients) {
for (const [clientId] of this.clients) {
promises.push(
client.channel.sendMessage(type, payload, headers)
.catch((error) => {
this.emit('error', error, clientId);
})
this.sendToClient(clientId, type, payload, headers).catch((error) => {
this.emit('error', error, clientId);
})
);
}
@@ -423,14 +440,13 @@ export class IpcServer extends plugins.EventEmitter {
headers?: Record<string, any>
): Promise<void> {
const promises: Promise<void>[] = [];
for (const [clientId, client] of this.clients) {
if (filter(clientId, client.metadata)) {
promises.push(
client.channel.sendMessage(type, payload, headers)
.catch((error) => {
this.emit('error', error, clientId);
})
this.sendToClient(clientId, type, payload, headers).catch((error) => {
this.emit('error', error, clientId);
})
);
}
}
@@ -552,4 +568,4 @@ export class IpcServer extends plugins.EventEmitter {
public getIsReady(): boolean {
return this.isReady;
}
}
}