Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
463b4db091 | |||
66f463549d | |||
ea47e1afc0 | |||
68132b996b | |||
909a4e11ef | |||
fecda5e668 | |||
cea7ea469c | |||
712270ba62 |
@ -14,4 +14,4 @@
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
}
|
||||
}
|
||||
}
|
9
package-lock.json
generated
9
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartipc",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.7",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@ -185,13 +185,6 @@
|
||||
"integrity": "sha512-TXKDDqsc7sBTLl+oiYNaF6IdNk1n70i8ur8QfwcUU6tegTnrEkvMWy9h5Zdty/fq1ioCNpKLvuXoA+fgYVwKGQ==",
|
||||
"requires": {
|
||||
"@pushrocks/smartpromise": "^3.0.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartpromise": {
|
||||
"version": "3.0.2",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.2.tgz",
|
||||
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"@pushrocks/smartevent": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/smartipc",
|
||||
"version": "1.0.3",
|
||||
"version": "1.0.7",
|
||||
"private": false,
|
||||
"description": "node inter process communication",
|
||||
"main": "dist/index.js",
|
||||
@ -22,6 +22,7 @@
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.3",
|
||||
"@pushrocks/smartpromise": "^3.0.2",
|
||||
"@pushrocks/smartrx": "^2.0.3",
|
||||
"@types/node-ipc": "^9.1.1",
|
||||
|
26
readme.md
Normal file
26
readme.md
Normal file
@ -0,0 +1,26 @@
|
||||
# @pushrocks/smartipc
|
||||
node inter process communication
|
||||
|
||||
## Availabililty and Links
|
||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@pushrocks/smartipc)
|
||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/smartipc)
|
||||
* [github.com (source mirror)](https://github.com/pushrocks/smartipc)
|
||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartipc/)
|
||||
|
||||
## Status for master
|
||||
[](https://gitlab.com/pushrocks/smartipc/commits/master)
|
||||
[](https://gitlab.com/pushrocks/smartipc/commits/master)
|
||||
[](https://www.npmjs.com/package/@pushrocks/smartipc)
|
||||
[](https://snyk.io/test/npm/@pushrocks/smartipc)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||
[](https://prettier.io/)
|
||||
|
||||
## Usage
|
||||
|
||||
For further information read the linked docs at the top of this readme.
|
||||
|
||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
||||
|
||||
[](https://maintainedby.lossless.com)
|
31
test/test.ts
31
test/test.ts
@ -4,21 +4,36 @@ import * as smartipc from '../ts/index';
|
||||
import * as smartspawn from '@pushrocks/smartspawn';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
|
||||
let testIpc: smartipc.SmartIpc;
|
||||
let serverIpc: smartipc.SmartIpc;
|
||||
let clientIpc: smartipc.SmartIpc;
|
||||
|
||||
tap.test('should instantiate a valid instance', async () => {
|
||||
testIpc = new smartipc.SmartIpc({
|
||||
serverIpc = new smartipc.SmartIpc({
|
||||
ipcSpace: 'testSmartIpc',
|
||||
type: 'server'
|
||||
});
|
||||
})
|
||||
|
||||
tap.test('should create a client', async (tools) => {
|
||||
|
||||
serverIpc.registerHandler({
|
||||
keyword: 'hi',
|
||||
handlerFunc: data => {
|
||||
console.log(data);
|
||||
}
|
||||
});
|
||||
await serverIpc.start();
|
||||
});
|
||||
|
||||
tap.test('should terminate the smartipc process', async () => {
|
||||
|
||||
tap.test('should create a client', async tools => {
|
||||
clientIpc = new smartipc.SmartIpc({
|
||||
ipcSpace: 'testSmartIpc',
|
||||
type: 'client'
|
||||
});
|
||||
await clientIpc.start();
|
||||
clientIpc.sendMessage('hi', { awesome: 'yes' });
|
||||
});
|
||||
|
||||
tap.test('should terminate the smartipc process', async tools => {
|
||||
await tools.delayFor(1000);
|
||||
await clientIpc.stop();
|
||||
await serverIpc.stop();
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
64
ts/index.ts
64
ts/index.ts
@ -1,4 +1,5 @@
|
||||
import * as plugins from './smartipc.plugins';
|
||||
import { EventEmitter } from 'events';
|
||||
|
||||
export interface ISmartIpcConstructorOptions {
|
||||
type: 'server' | 'client';
|
||||
@ -9,13 +10,13 @@ export interface ISmartIpcConstructorOptions {
|
||||
ipcSpace: string;
|
||||
}
|
||||
|
||||
|
||||
export interface ISmartIpcHandlerPackage {
|
||||
keyword: string;
|
||||
handlerFunc: () => void;
|
||||
handlerFunc: (dataArg: string) => void;
|
||||
}
|
||||
|
||||
export class SmartIpc {
|
||||
public ipc = new plugins.nodeIpc.IPC();
|
||||
public handlers: ISmartIpcHandlerPackage[] = [];
|
||||
|
||||
public options: ISmartIpcConstructorOptions;
|
||||
@ -23,38 +24,81 @@ export class SmartIpc {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* connect to the channel
|
||||
*/
|
||||
public async start() {
|
||||
const done = plugins.smartpromise.defer();
|
||||
let ipcEventEmitter;
|
||||
switch (this.options.type) {
|
||||
case 'server':
|
||||
plugins.nodeIpc.config.id = this.options.ipcSpace;
|
||||
const done = plugins.smartpromise.defer();
|
||||
plugins.nodeIpc.serve(() => {
|
||||
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;
|
||||
case 'client':
|
||||
plugins.nodeIpc.connectTo(this.options.ipcSpace);
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* should stop the server
|
||||
*/
|
||||
public async stop() {
|
||||
plugins.nodeIpc.server.stop();
|
||||
switch (this.options.type) {
|
||||
case 'server':
|
||||
this.ipc.server.stop();
|
||||
break;
|
||||
case 'client':
|
||||
break;
|
||||
}
|
||||
plugins.smartdelay.delayFor(2000).then(() => {
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* regsiters a handler
|
||||
*/
|
||||
registerHandler (handlerPackage: ISmartIpcHandlerPackage) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,11 @@
|
||||
// pushrocks scope
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrx from '@pushrocks/smartrx';
|
||||
|
||||
export {
|
||||
smartpromise,
|
||||
smartrx
|
||||
};
|
||||
export { smartdelay, smartpromise, smartrx };
|
||||
|
||||
// third party scope
|
||||
import * as nodeIpc from 'node-ipc';
|
||||
|
||||
export {
|
||||
nodeIpc
|
||||
};
|
||||
|
||||
export { nodeIpc };
|
||||
|
Reference in New Issue
Block a user