2021-07-03 07:35:02 +10:00
|
|
|
import net from 'net';
|
|
|
|
import tls from 'tls';
|
|
|
|
import EventParser from '../entities/EventParser.js';
|
|
|
|
import Message from 'js-message';
|
|
|
|
import fs from 'fs';
|
|
|
|
import Queue from 'js-queue';
|
|
|
|
import Events from 'event-pubsub';
|
2016-10-04 07:34:27 +11:00
|
|
|
|
2017-04-16 15:17:25 +10:00
|
|
|
let eventParser = new EventParser();
|
|
|
|
|
2016-09-30 23:00:28 +10:00
|
|
|
class Client extends Events{
|
|
|
|
constructor(config,log){
|
2017-04-16 15:17:25 +10:00
|
|
|
super();
|
2021-07-03 07:35:02 +10:00
|
|
|
this.config=config;
|
|
|
|
this.log=log;
|
|
|
|
this.publish=super.emit;
|
|
|
|
|
|
|
|
(config.maxRetries)? this.retriesRemaining=config.maxRetries:0;
|
2017-04-16 15:17:25 +10:00
|
|
|
|
|
|
|
eventParser=new EventParser(this.config);
|
2016-09-30 23:00:28 +10:00
|
|
|
}
|
2021-07-03 07:35:02 +10:00
|
|
|
|
|
|
|
Client=Client;
|
|
|
|
queue =new Queue;
|
|
|
|
socket=false;
|
|
|
|
connect=connect;
|
|
|
|
emit=emit;
|
|
|
|
retriesRemaining=0;
|
|
|
|
explicitlyDisconnected=false;
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
function emit(type,data){
|
2016-03-23 18:07:37 +11:00
|
|
|
this.log('dispatching event to ', this.id, this.path, ' : ', type, ',', data);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2016-01-10 23:18:14 +11:00
|
|
|
let message=new Message;
|
2015-09-27 20:32:14 +10:00
|
|
|
message.type=type;
|
|
|
|
message.data=data;
|
|
|
|
|
2015-08-23 15:46:55 +10:00
|
|
|
if(this.config.rawBuffer){
|
2020-11-12 07:21:02 +11:00
|
|
|
message=Buffer.from(type,this.config.encoding);
|
2015-08-23 15:46:55 +10:00
|
|
|
}else{
|
2017-04-16 15:17:25 +10:00
|
|
|
message=eventParser.format(message);
|
2015-08-23 15:46:55 +10:00
|
|
|
}
|
|
|
|
|
2021-07-03 07:35:02 +10:00
|
|
|
//volitile emit
|
2015-12-10 19:11:14 +11:00
|
|
|
if(!this.config.sync){
|
|
|
|
this.socket.write(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-03 07:35:02 +10:00
|
|
|
//sync, non-volitile, ack emit
|
2015-12-10 19:11:14 +11:00
|
|
|
this.queue.add(
|
|
|
|
syncEmit.bind(this,message)
|
|
|
|
);
|
2016-01-10 23:18:14 +11:00
|
|
|
}
|
2014-02-22 20:13:31 +11:00
|
|
|
|
2015-12-10 19:11:14 +11:00
|
|
|
function syncEmit(message){
|
2016-03-23 18:07:37 +11:00
|
|
|
this.log('dispatching event to ', this.id, this.path, ' : ', message);
|
2015-12-10 19:11:14 +11:00
|
|
|
this.socket.write(message);
|
|
|
|
}
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
function connect(){
|
|
|
|
//init client object for scope persistance especially inside of socket events.
|
2016-01-10 23:18:14 +11:00
|
|
|
let client=this;
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2016-03-23 18:07:37 +11:00
|
|
|
client.log('requested connection to ', client.id, client.path);
|
2014-02-22 20:13:31 +11:00
|
|
|
if(!this.path){
|
2016-03-23 18:07:37 +11:00
|
|
|
client.log('\n\n######\nerror: ', client.id ,' client has not specified socket path it wishes to connect to.');
|
2014-02-22 20:13:31 +11:00
|
|
|
return;
|
|
|
|
}
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2016-12-22 12:03:12 +11:00
|
|
|
const options={};
|
|
|
|
|
2014-02-26 12:15:43 +11:00
|
|
|
if(!client.port){
|
2016-03-23 18:07:37 +11:00
|
|
|
client.log('Connecting client on Unix Socket :', client.path);
|
2015-12-03 05:45:12 +11:00
|
|
|
|
2016-12-22 12:03:12 +11:00
|
|
|
options.path=client.path;
|
2015-12-03 05:45:12 +11:00
|
|
|
|
2015-11-12 22:57:17 +11:00
|
|
|
if (process.platform ==='win32' && !client.path.startsWith('\\\\.\\pipe\\')){
|
2016-12-22 12:03:12 +11:00
|
|
|
options.path = options.path.replace(/^\//, '');
|
|
|
|
options.path = options.path.replace(/\//g, '-');
|
2016-12-22 14:34:53 +11:00
|
|
|
options.path= `\\\\.\\pipe\\${options.path}`;
|
2015-09-27 21:45:57 +10:00
|
|
|
}
|
2016-12-22 12:03:12 +11:00
|
|
|
|
|
|
|
client.socket = net.connect(options);
|
2014-02-26 12:15:43 +11:00
|
|
|
}else{
|
2016-12-22 12:03:12 +11:00
|
|
|
options.host=client.path;
|
|
|
|
options.port=client.port;
|
|
|
|
|
|
|
|
if(client.config.interface.localAddress){
|
|
|
|
options.localAddress=client.config.interface.localAddress;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(client.config.interface.localPort){
|
|
|
|
options.localPort=client.config.interface.localPort;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(client.config.interface.family){
|
|
|
|
options.family=client.config.interface.family;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(client.config.interface.hints){
|
|
|
|
options.hints=client.config.interface.hints;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(client.config.interface.lookup){
|
|
|
|
options.lookup=client.config.interface.lookup;
|
|
|
|
}
|
|
|
|
|
2015-09-28 14:52:16 +10:00
|
|
|
if(!client.config.tls){
|
2016-12-22 12:03:12 +11:00
|
|
|
client.log('Connecting client via TCP to', options);
|
|
|
|
client.socket = net.connect(options);
|
2015-09-28 14:52:16 +10:00
|
|
|
}else{
|
2016-03-23 18:07:37 +11:00
|
|
|
client.log('Connecting client via TLS to', client.path ,client.port,client.config.tls);
|
2015-09-28 14:52:16 +10:00
|
|
|
if(client.config.tls.private){
|
|
|
|
client.config.tls.key=fs.readFileSync(client.config.tls.private);
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
if(client.config.tls.public){
|
|
|
|
client.config.tls.cert=fs.readFileSync(client.config.tls.public);
|
|
|
|
}
|
|
|
|
if(client.config.tls.trustedConnections){
|
|
|
|
if(typeof client.config.tls.trustedConnections === 'string'){
|
|
|
|
client.config.tls.trustedConnections=[client.config.tls.trustedConnections];
|
|
|
|
}
|
|
|
|
client.config.tls.ca=[];
|
2016-01-10 23:18:14 +11:00
|
|
|
for(let i=0; i<client.config.tls.trustedConnections.length; i++){
|
2015-09-28 14:52:16 +10:00
|
|
|
client.config.tls.ca.push(
|
|
|
|
fs.readFileSync(client.config.tls.trustedConnections[i])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-22 12:03:12 +11:00
|
|
|
Object.assign(client.config.tls,options);
|
2015-09-28 14:52:16 +10:00
|
|
|
|
|
|
|
client.socket = tls.connect(
|
|
|
|
client.config.tls
|
|
|
|
);
|
|
|
|
}
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.setEncoding(this.config.encoding);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.on(
|
|
|
|
'error',
|
|
|
|
function(err){
|
2016-03-23 18:07:37 +11:00
|
|
|
client.log('\n\n######\nerror: ', err);
|
2016-09-30 23:00:28 +10:00
|
|
|
client.publish('error', err);
|
2015-12-01 12:43:43 +11:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.on(
|
|
|
|
'connect',
|
2016-01-10 23:18:14 +11:00
|
|
|
function connectionMade(){
|
2016-09-30 23:00:28 +10:00
|
|
|
client.publish('connect');
|
2015-08-23 16:16:20 +10:00
|
|
|
client.retriesRemaining=client.config.maxRetries;
|
2015-12-01 12:43:43 +11:00
|
|
|
client.log('retrying reset');
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.on(
|
|
|
|
'close',
|
2016-01-10 23:18:14 +11:00
|
|
|
function connectionClosed(){
|
2016-03-23 18:07:37 +11:00
|
|
|
client.log('connection closed' ,client.id , client.path,
|
|
|
|
client.retriesRemaining, 'tries remaining of', client.config.maxRetries
|
2016-01-11 16:10:58 +11:00
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-09-07 09:28:49 +10:00
|
|
|
if(
|
2016-04-04 08:30:44 +10:00
|
|
|
client.config.stopRetrying ||
|
|
|
|
client.retriesRemaining<1 ||
|
|
|
|
client.explicitlyDisconnected
|
2015-08-23 15:46:55 +10:00
|
|
|
|
2014-09-07 09:28:49 +10:00
|
|
|
){
|
2016-09-30 23:00:28 +10:00
|
|
|
client.publish('disconnect');
|
2014-09-05 15:06:25 +10:00
|
|
|
client.log(
|
2016-03-23 18:07:37 +11:00
|
|
|
(client.config.id),
|
|
|
|
'exceeded connection rety amount of',
|
2016-01-10 23:18:14 +11:00
|
|
|
' or stopRetrying flag set.'
|
2014-09-05 15:06:25 +10:00
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-09-05 15:06:25 +10:00
|
|
|
client.socket.destroy();
|
2016-09-30 23:00:28 +10:00
|
|
|
client.publish('destroy');
|
2014-09-05 15:06:25 +10:00
|
|
|
client=undefined;
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-09-05 15:06:25 +10:00
|
|
|
return;
|
2014-03-02 11:31:05 +11:00
|
|
|
}
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
setTimeout(
|
2016-01-10 23:18:14 +11:00
|
|
|
function retryTimeout(){
|
2021-02-26 21:07:03 +11:00
|
|
|
if (client.explicitlyDisconnected) {
|
|
|
|
return;
|
|
|
|
}
|
2016-01-10 23:18:14 +11:00
|
|
|
client.retriesRemaining--;
|
|
|
|
client.connect();
|
|
|
|
}.bind(null,client),
|
2014-02-22 20:13:31 +11:00
|
|
|
client.config.retry
|
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2016-09-30 23:00:28 +10:00
|
|
|
client.publish('disconnect');
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.on(
|
|
|
|
'data',
|
|
|
|
function(data) {
|
2016-03-29 11:45:36 +11:00
|
|
|
client.log('## received events ##');
|
2015-08-23 15:46:55 +10:00
|
|
|
if(client.config.rawBuffer){
|
2016-09-30 23:00:28 +10:00
|
|
|
client.publish(
|
2015-08-23 15:46:55 +10:00
|
|
|
'data',
|
2020-11-12 07:21:02 +11:00
|
|
|
Buffer.from(data,client.config.encoding)
|
2015-08-23 15:46:55 +10:00
|
|
|
);
|
2015-12-10 19:11:14 +11:00
|
|
|
if(!client.config.sync){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.queue.next();
|
2015-08-23 15:46:55 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-10 23:18:14 +11:00
|
|
|
if(!this.ipcBuffer){
|
2014-03-04 07:02:56 +11:00
|
|
|
this.ipcBuffer='';
|
2016-01-10 23:18:14 +11:00
|
|
|
}
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-03-04 07:02:56 +11:00
|
|
|
data=(this.ipcBuffer+=data);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2017-04-16 15:17:25 +10:00
|
|
|
if(data.slice(-1)!=eventParser.delimiter || data.indexOf(eventParser.delimiter) == -1){
|
2016-03-23 18:07:37 +11:00
|
|
|
client.log('Messages are large, You may want to consider smaller messages.');
|
2014-03-04 07:02:56 +11:00
|
|
|
return;
|
|
|
|
}
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-03-04 07:02:56 +11:00
|
|
|
this.ipcBuffer='';
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2017-04-16 15:17:25 +10:00
|
|
|
const events = eventParser.parse(data);
|
2016-01-10 23:18:14 +11:00
|
|
|
const eCount = events.length;
|
|
|
|
for(let i=0; i<eCount; i++){
|
|
|
|
let message=new Message;
|
2015-09-27 20:32:14 +10:00
|
|
|
message.load(events[i]);
|
|
|
|
|
2016-03-23 18:07:37 +11:00
|
|
|
client.log('detected event', message.type, message.data);
|
2016-09-30 23:00:28 +10:00
|
|
|
client.publish(
|
2015-09-27 20:32:14 +10:00
|
|
|
message.type,
|
|
|
|
message.data
|
2014-02-22 20:13:31 +11:00
|
|
|
);
|
|
|
|
}
|
2015-12-10 19:11:14 +11:00
|
|
|
|
|
|
|
if(!client.config.sync){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.queue.next();
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-03 07:35:02 +10:00
|
|
|
export {
|
|
|
|
Client as default,
|
|
|
|
Client
|
|
|
|
};
|