BREAKING CHANGE(core): Refactor core IPC: replace node-ipc with native transports and add IpcChannel / IpcServer / IpcClient with heartbeat, reconnection, request/response and pub/sub. Update tests and documentation.
This commit is contained in:
117
ts/index.ts
117
ts/index.ts
@@ -1,103 +1,40 @@
|
||||
import * as plugins from './smartipc.plugins.js';
|
||||
import { EventEmitter } from 'events';
|
||||
export * from './classes.transports.js';
|
||||
export * from './classes.ipcchannel.js';
|
||||
export * from './classes.ipcserver.js';
|
||||
export * from './classes.ipcclient.js';
|
||||
|
||||
export interface ISmartIpcConstructorOptions {
|
||||
type: 'server' | 'client';
|
||||
|
||||
/**
|
||||
* the name of the message string
|
||||
*/
|
||||
ipcSpace: string;
|
||||
}
|
||||
|
||||
export interface ISmartIpcHandlerPackage {
|
||||
keyword: string;
|
||||
handlerFunc: (dataArg: string) => void;
|
||||
}
|
||||
import { IpcServer } from './classes.ipcserver.js';
|
||||
import { IpcClient } from './classes.ipcclient.js';
|
||||
import { IpcChannel } from './classes.ipcchannel.js';
|
||||
import type { IIpcServerOptions } from './classes.ipcserver.js';
|
||||
import type { IIpcClientOptions } from './classes.ipcclient.js';
|
||||
import type { IIpcChannelOptions } from './classes.ipcchannel.js';
|
||||
|
||||
/**
|
||||
* Main SmartIpc class - Factory for creating IPC servers, clients, and channels
|
||||
*/
|
||||
export class SmartIpc {
|
||||
public ipc = new plugins.nodeIpc.IPC();
|
||||
public handlers: ISmartIpcHandlerPackage[] = [];
|
||||
|
||||
public options: ISmartIpcConstructorOptions;
|
||||
constructor(optionsArg: ISmartIpcConstructorOptions) {
|
||||
this.options = optionsArg;
|
||||
/**
|
||||
* Create an IPC server
|
||||
*/
|
||||
public static createServer(options: IIpcServerOptions): IpcServer {
|
||||
return new IpcServer(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* connect to the channel
|
||||
* Create an IPC client
|
||||
*/
|
||||
public async start() {
|
||||
const done = plugins.smartpromise.defer();
|
||||
let ipcEventEmitter;
|
||||
switch (this.options.type) {
|
||||
case 'server':
|
||||
this.ipc.config.id = this.options.ipcSpace;
|
||||
this.ipc.serve(() => {
|
||||
ipcEventEmitter = this.ipc.server;
|
||||
done.resolve();
|
||||
});
|
||||
this.ipc.server.start();
|
||||
await plugins.smartdelay.delayFor(1000);
|
||||
await done.promise;
|
||||
break;
|
||||
case 'client':
|
||||
this.ipc.connectTo(this.options.ipcSpace, () => {
|
||||
ipcEventEmitter = this.ipc.of[this.options.ipcSpace];
|
||||
done.resolve();
|
||||
});
|
||||
await done.promise;
|
||||
break;
|
||||
default:
|
||||
throw new Error(
|
||||
'type of ipc is not valid. Must be "server" or "client"',
|
||||
);
|
||||
}
|
||||
|
||||
for (const handler of this.handlers) {
|
||||
ipcEventEmitter.on(handler.keyword, (dataArg) => {
|
||||
handler.handlerFunc(dataArg);
|
||||
});
|
||||
}
|
||||
public static createClient(options: IIpcClientOptions): IpcClient {
|
||||
return new IpcClient(options);
|
||||
}
|
||||
|
||||
/**
|
||||
* should stop the server
|
||||
* Create a raw IPC channel (for advanced use cases)
|
||||
*/
|
||||
public async stop() {
|
||||
switch (this.options.type) {
|
||||
case 'server':
|
||||
this.ipc.server.stop();
|
||||
break;
|
||||
case 'client':
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* regsiters a handler
|
||||
*/
|
||||
public registerHandler(handlerPackage: ISmartIpcHandlerPackage) {
|
||||
this.handlers.push(handlerPackage);
|
||||
}
|
||||
|
||||
/**
|
||||
* sends a message
|
||||
* @param payloadArg
|
||||
*/
|
||||
public sendMessage(messageIdentifierArg: string, payloadArg: string | any) {
|
||||
let payload: string = null;
|
||||
if (typeof payloadArg === 'string') {
|
||||
payload = payloadArg;
|
||||
} else {
|
||||
payload = JSON.stringify(payloadArg);
|
||||
}
|
||||
switch (this.options.type) {
|
||||
case 'server':
|
||||
this.ipc.server.emit(messageIdentifierArg, payload);
|
||||
break;
|
||||
case 'client':
|
||||
this.ipc.of[this.options.ipcSpace].emit(messageIdentifierArg, payload);
|
||||
}
|
||||
public static createChannel(options: IIpcChannelOptions): IpcChannel {
|
||||
return new IpcChannel(options);
|
||||
}
|
||||
}
|
||||
|
||||
// Export the main class as default
|
||||
export default SmartIpc;
|
Reference in New Issue
Block a user