ipc-link v0.0.1
IPC-Link
IPC-Link is a mini-framework for node-ipc that is fully compatible with TypeScript (and in a near future, ECMAScript Modules). It is designed to have "connection channels" where two processes send data back and forth.
This framework has a queue system that holds Promises temporarily until the message has been replied back, and depending on the variable success, it may or may not reject said Promise.
Usage
You can check examples here.
Process One:
const { Server } = require('../../src/index');
console.log('Spawned test-one!');
new Server('test-one', { retry: 1500, silent: true })
.on('message', console.log)
.on('error', console.error)
.once('start', (reason) => { console.log('Successfully started!', reason); })
.start('Login!');
.send('test-two', { content: 'Hello' })
.then(console.log)
.catch(console.error);Proccess Two:
const { Server } = require('../../src/index');
console.log('Spawned test-two!');
new Server('test-two', { retry: 1500, silent: true })
.on('message', message => { message.reply({ response: `${message.data.content} world!` }); })
.on('error', console.error)
.once('start', (reason) => { console.log('Successfully started!', reason); })
.start('Login!');Process One will send Process Two an object with content Hello, Process Two receives back and replies it with Hello (content sent by the first process) and sends another object with response: 'Hello world!' to Process One. Evaluating .send('test-two', { content: 'Hello' }) to Promise<{ id, success: true, response: 'Hello World' }>, which is logged to console.
Specification
It is important that you have a single IPCLink.Server instance because node-ipc is basically a singleton, and creating multiple instances of this may duplicate messages or corrupt the configuration. In a near future, node-ipc may get rewritten in a fork or in a backends/ folder in this repository for further support with latest versions of Node.js (new Buffer() is being used, which is deprecated starting from Node.js 10).
Proccess One
- Let
serverbe the result of evaluatingnew Server(name, options);. - Consider there is a
messageevent being listened inserver. - Consider
server.start();has already been called. - Let
socketNamebe a string, e.g.'world'. - Let
databe an object literal, e.g.{ test: 'Hello ' }. - Perform
server.send(socketName, data);. - Let
senderSocketbe the Socket instance from this process. - Let
hasSocketbe the result of evaluatingserver.hasSocket(name);. - If
hasSocketistrue, skip to the next point. Otherwise,- connect to the socket via
server.connectToand await its evaluation.
- connect to the socket via
- Let
socketbe the result of evaluatingserver.getSocket(name);.- Let
IPCSocketCollectionbe an object ofNodeIPC.Servers. - Let
IPCSocketServerbe the result of accessing to the propertynameofIPCSocketCollection. - If
IPCSocketServerisundefined, letsocketbenull. Otherwise- Let
socketbeIPCSocketServer.socket, being this a Socket instance.
- Let
- Let
- If
datahas a property ofid, letidbedata.id. Otherwise letidbe a random base36 number generated automatically. - Let
preparedDatabe an object where:idrefers toid.sentByrefers toserver.name.datarefers todata.
- Let
stringifiedDatabe the result of evaluatingJSON.stringify(preparedData);. - Perform
socket.write, sendingstringifiedDatato the Socket. - Let
temporalPromisebe a Promise evaluated withnew Promise();. - Let
resolveandrejectbe the first and second parameters fromtemporalPromise's callbacks. - Let
queuePromisebe an object where:resolverefers toresolve.rejectrefers toreject.
- Let
promiseCollectionbe the internal Promise collection from IPC-Link of typeMap<string, { resolve: Function, reject: Function }>;. - Perform
promiseCollection.set(id, queuePromise);. - Return
queuePromise.
Proccess Two
- Let
receiverServerbe the result of evaluatingnew Server(name, options);in the target process. - Let
messagePayloadbe the result of evaluatingJSON.parse(stringifiedData);. - Let
messagebe the result of evaluatingnew Message(receiverServer, senderSocket, messagePayload);. - Send
messagetoreceiverServer's EventEmitter for its handling. - Let
responseDatabe an object. - If
responseDatahas a property ofsuccess, letsuccessberesponseData.success. Otherwise- Let
successArgumentbe the result of evaluating the third argument fromServer#send. - If
successArgumentisundefined, letsuccessbetrue. Otherwise letsuccessbesuccessArgument.
- Let
- Let
finalizedResponseDatabe an object where:idrefers toid.successrefers tosuccess.- All properties of
responseDataare applied over the properties ofidandsuccess.
- Let
stringifiedResponseDatabe the result of evaluatingJSON.stringify(finalizedResponseData);. - Perform
senderSocket.write, sendingstringifiedResponseDatato the Socket.
Proccess One
- Let
parsedResponseDatabe the result of evaluatingJSON.parse(stringifiedResponseData);. - Let
responseIDbe the result of evaluatingparsedResponseData.id. - If
responseIDdoes not equals toidvia Strict Equality Comparison,- Ignore the request. Otherwise,
- Let
promisebequeuePromise.- Let
successResponsebe the result of evaluatingparsedResponseData.success. - If
successResponseistrue, evaluatepromise.resolve(parsedResponseData);, resolvingtemporalPromisewith the value ofparsedResponseData. Otherwise evaluatepromise.reject(parsedResponseData);, rejectingtemporalPromisewith the value ofparsedResponseData.
- Let
8 years ago