feat(ipcclient): Add clientOnly mode to prevent clients from auto-starting servers and improve registration/reconnect behavior
This commit is contained in:
@@ -18,6 +18,12 @@ export interface IIpcMessageEnvelope<T = any> {
|
||||
export interface IIpcTransportOptions {
|
||||
/** Unique identifier for this transport */
|
||||
id: string;
|
||||
/**
|
||||
* When true, a client transport will NOT auto-start a server when connect()
|
||||
* encounters ECONNREFUSED/ENOENT. Useful for strict client/daemon setups.
|
||||
* Default: false. Can also be overridden by env SMARTIPC_CLIENT_ONLY=1.
|
||||
*/
|
||||
clientOnly?: boolean;
|
||||
/** Socket path for Unix domain sockets or pipe name for Windows */
|
||||
socketPath?: string;
|
||||
/** TCP host for network transport */
|
||||
@@ -195,7 +201,21 @@ export class UnixSocketTransport extends IpcTransport {
|
||||
|
||||
this.socket.on('error', (error: any) => {
|
||||
if (error.code === 'ECONNREFUSED' || error.code === 'ENOENT') {
|
||||
// No server exists, we should become the server
|
||||
// Determine if we must NOT auto-start server
|
||||
const envVal = process.env.SMARTIPC_CLIENT_ONLY;
|
||||
const envClientOnly = !!envVal && (envVal === '1' || envVal === 'true' || envVal === 'TRUE');
|
||||
const clientOnly = this.options.clientOnly === true || envClientOnly;
|
||||
|
||||
if (clientOnly) {
|
||||
// Reject instead of starting a server to avoid races
|
||||
const reason = error.code || 'UNKNOWN';
|
||||
const err = new Error(`Server not available (${reason}); clientOnly prevents auto-start`);
|
||||
(err as any).code = reason;
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
|
||||
// No server exists and clientOnly is false: become the server (back-compat)
|
||||
this.socket = null;
|
||||
this.startServer(socketPath).then(resolve).catch(reject);
|
||||
} else {
|
||||
@@ -718,4 +738,4 @@ export function createTransport(options: IIpcTransportOptions): IpcTransport {
|
||||
} else {
|
||||
return new UnixSocketTransport(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user