Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
3b1ee6460f | |||
1f86bb0eb4 | |||
463b4db091 | |||
66f463549d | |||
ea47e1afc0 | |||
68132b996b | |||
909a4e11ef | |||
fecda5e668 | |||
cea7ea469c | |||
712270ba62 |
9
package-lock.json
generated
9
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartipc",
|
"name": "@pushrocks/smartipc",
|
||||||
"version": "1.0.3",
|
"version": "1.0.8",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -185,13 +185,6 @@
|
|||||||
"integrity": "sha512-TXKDDqsc7sBTLl+oiYNaF6IdNk1n70i8ur8QfwcUU6tegTnrEkvMWy9h5Zdty/fq1ioCNpKLvuXoA+fgYVwKGQ==",
|
"integrity": "sha512-TXKDDqsc7sBTLl+oiYNaF6IdNk1n70i8ur8QfwcUU6tegTnrEkvMWy9h5Zdty/fq1ioCNpKLvuXoA+fgYVwKGQ==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@pushrocks/smartpromise": "^3.0.2"
|
"@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": {
|
"@pushrocks/smartevent": {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartipc",
|
"name": "@pushrocks/smartipc",
|
||||||
"version": "1.0.3",
|
"version": "1.0.8",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "node inter process communication",
|
"description": "node inter process communication",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -22,6 +22,7 @@
|
|||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pushrocks/smartdelay": "^2.0.3",
|
||||||
"@pushrocks/smartpromise": "^3.0.2",
|
"@pushrocks/smartpromise": "^3.0.2",
|
||||||
"@pushrocks/smartrx": "^2.0.3",
|
"@pushrocks/smartrx": "^2.0.3",
|
||||||
"@types/node-ipc": "^9.1.1",
|
"@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,38 @@ import * as smartipc from '../ts/index';
|
|||||||
import * as smartspawn from '@pushrocks/smartspawn';
|
import * as smartspawn from '@pushrocks/smartspawn';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
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 () => {
|
tap.test('should instantiate a valid instance', async () => {
|
||||||
testIpc = new smartipc.SmartIpc({
|
serverIpc = new smartipc.SmartIpc({
|
||||||
ipcSpace: 'testSmartIpc',
|
ipcSpace: 'testSmartIpc',
|
||||||
type: 'server'
|
type: 'server'
|
||||||
});
|
});
|
||||||
})
|
serverIpc.registerHandler({
|
||||||
|
keyword: 'hi',
|
||||||
tap.test('should create a client', async (tools) => {
|
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 clientIpc.stop();
|
||||||
|
await serverIpc.stop();
|
||||||
|
tools.delayFor(2000).then(() => {
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
61
ts/index.ts
61
ts/index.ts
@ -1,4 +1,5 @@
|
|||||||
import * as plugins from './smartipc.plugins';
|
import * as plugins from './smartipc.plugins';
|
||||||
|
import { EventEmitter } from 'events';
|
||||||
|
|
||||||
export interface ISmartIpcConstructorOptions {
|
export interface ISmartIpcConstructorOptions {
|
||||||
type: 'server' | 'client';
|
type: 'server' | 'client';
|
||||||
@ -9,13 +10,13 @@ export interface ISmartIpcConstructorOptions {
|
|||||||
ipcSpace: string;
|
ipcSpace: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface ISmartIpcHandlerPackage {
|
export interface ISmartIpcHandlerPackage {
|
||||||
keyword: string;
|
keyword: string;
|
||||||
handlerFunc: () => void;
|
handlerFunc: (dataArg: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SmartIpc {
|
export class SmartIpc {
|
||||||
|
public ipc = new plugins.nodeIpc.IPC();
|
||||||
public handlers: ISmartIpcHandlerPackage[] = [];
|
public handlers: ISmartIpcHandlerPackage[] = [];
|
||||||
|
|
||||||
public options: ISmartIpcConstructorOptions;
|
public options: ISmartIpcConstructorOptions;
|
||||||
@ -23,38 +24,78 @@ export class SmartIpc {
|
|||||||
this.options = optionsArg;
|
this.options = optionsArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* connect to the channel
|
* connect to the channel
|
||||||
*/
|
*/
|
||||||
public async start() {
|
public async start() {
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
|
let ipcEventEmitter;
|
||||||
switch (this.options.type) {
|
switch (this.options.type) {
|
||||||
case 'server':
|
case 'server':
|
||||||
plugins.nodeIpc.config.id = this.options.ipcSpace;
|
this.ipc.config.id = this.options.ipcSpace;
|
||||||
const done = plugins.smartpromise.defer();
|
this.ipc.serve(() => {
|
||||||
plugins.nodeIpc.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();
|
done.resolve();
|
||||||
});
|
});
|
||||||
await done.promise;
|
await done.promise;
|
||||||
break;
|
break;
|
||||||
case 'client':
|
|
||||||
plugins.nodeIpc.connectTo(this.options.ipcSpace);
|
|
||||||
default:
|
default:
|
||||||
throw new Error('type of ipc is not valid. Must be "server" or "client"');
|
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
|
* should stop the server
|
||||||
*/
|
*/
|
||||||
public async stop() {
|
public async stop() {
|
||||||
plugins.nodeIpc.server.stop();
|
switch (this.options.type) {
|
||||||
|
case 'server':
|
||||||
|
this.ipc.server.stop();
|
||||||
|
break;
|
||||||
|
case 'client':
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* regsiters a handler
|
* regsiters a handler
|
||||||
*/
|
*/
|
||||||
registerHandler (handlerPackage: ISmartIpcHandlerPackage) {
|
public registerHandler(handlerPackage: ISmartIpcHandlerPackage) {
|
||||||
this.handlers.push(handlerPackage);
|
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
|
// pushrocks scope
|
||||||
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartpromise from '@pushrocks/smartpromise';
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as smartrx from '@pushrocks/smartrx';
|
import * as smartrx from '@pushrocks/smartrx';
|
||||||
|
|
||||||
export {
|
export { smartdelay, smartpromise, smartrx };
|
||||||
smartpromise,
|
|
||||||
smartrx
|
|
||||||
};
|
|
||||||
|
|
||||||
// third party scope
|
// third party scope
|
||||||
import * as nodeIpc from 'node-ipc';
|
import * as nodeIpc from 'node-ipc';
|
||||||
|
|
||||||
export {
|
export { nodeIpc };
|
||||||
nodeIpc
|
|
||||||
};
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user