Push build

This commit is contained in:
ThatTonybo
2025-08-12 00:52:41 +10:00
parent a4eab3119e
commit c2a7e0c327
2 changed files with 85 additions and 1 deletions

1
.gitignore vendored
View File

@@ -3,7 +3,6 @@ node_modules
# output # output
out out
dist
*.tgz *.tgz
# code coverage # code coverage

85
dist/index.js vendored Normal file
View File

@@ -0,0 +1,85 @@
import EventEmitter from 'node:events';
import IPCModule from 'node-ipc';
/**
* Handles Inter-Process Communication (IPC) via. server/socket
*
* @example
* // Simple echo example
* const ipc = new IPC('echo');
*
* ipc.on('connect', () => {
* console.log('Connected');
* ipc.send(ipc.name, { hello: 'world!' });
* });
*
* ipc.on('disconnect', () => console.log('Disconnected'));
*
* ipc.on('message', (message: Message) => {
* console.log(`Message:`, message);
* });
*/
export default class IPC extends EventEmitter {
name;
ready;
ipc;
constructor(name, debug = false) {
super();
this.name = name;
this.ready = false;
this.ipc = IPCModule;
this.ipc.config.appspace = 'hop.';
this.ipc.config.id = name;
this.ipc.config.retry = 2500;
this.ipc.config.maxRetries = 5;
this.ipc.config.logger = debug ? console.log.bind(this) : () => null;
}
/**
* Connect to the IPC server/socket
*/
connect() {
this.ipc.connectTo('ipc', () => {
this.ipc.of.ipc?.on('connect', () => this.onConnect());
this.ipc.of.ipc?.on('disconnect', () => this.onDisconnect());
this.ipc.of.ipc?.on('ready', () => this.onReady());
this.ipc.of.ipc?.on('message', (raw) => {
const data = JSON.parse(raw);
this.onMessage(data);
});
});
}
/**
* Send a message to another process via. IPC
* @param to Who to send the message to
* @param data JSON data to send to the process
*/
send(to, data) {
if (!this.ready)
return;
const message = {
from: this.name,
to,
payload: JSON.stringify(data)
};
this.ipc.of.ipc?.emit('message', message);
}
onConnect() {
this.ipc.of.ipc?.emit('identify', {
from: this.name
});
}
onDisconnect() {
this.emit('disconnect');
}
onReady() {
this.emit('connect');
this.ready = true;
}
onMessage(raw) {
const message = {
from: raw.from,
to: raw.to,
payload: JSON.parse(raw.payload)
};
this.emit('message', message);
}
}