4 Commits

Author SHA1 Message Date
909a4e11ef 1.0.5 2019-04-09 12:30:13 +02:00
fecda5e668 fix(core): update 2019-04-09 12:30:12 +02:00
cea7ea469c 1.0.4 2019-04-08 19:56:22 +02:00
712270ba62 fix(core): update 2019-04-08 19:56:21 +02:00
7 changed files with 110 additions and 36 deletions

9
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartipc", "name": "@pushrocks/smartipc",
"version": "1.0.3", "version": "1.0.5",
"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": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartipc", "name": "@pushrocks/smartipc",
"version": "1.0.3", "version": "1.0.5",
"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
View 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
[![build status](https://gitlab.com/pushrocks/smartipc/badges/master/build.svg)](https://gitlab.com/pushrocks/smartipc/commits/master)
[![coverage report](https://gitlab.com/pushrocks/smartipc/badges/master/coverage.svg)](https://gitlab.com/pushrocks/smartipc/commits/master)
[![npm downloads per month](https://img.shields.io/npm/dm/@pushrocks/smartipc.svg)](https://www.npmjs.com/package/@pushrocks/smartipc)
[![Known Vulnerabilities](https://snyk.io/test/npm/@pushrocks/smartipc/badge.svg)](https://snyk.io/test/npm/@pushrocks/smartipc)
[![TypeScript](https://img.shields.io/badge/TypeScript->=%203.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
[![JavaScript Style Guide](https://img.shields.io/badge/code%20style-prettier-ff69b4.svg)](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)
[![repo-footer](https://pushrocks.gitlab.io/assets/repo-footer.svg)](https://maintainedby.lossless.com)

View File

@ -4,21 +4,36 @@ 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 tools.delayFor(1000);
await clientIpc.stop();
await serverIpc.stop();
}); });
tap.start(); tap.start();

View File

@ -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,81 @@ 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;
}
plugins.smartdelay.delayFor(2000).then(() => {
process.exit(0);
});
} }
/** /**
* 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);
}
}
} }

View File

@ -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
};