client.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. 'use strict';
  2. const net = require('net'),
  3. tls = require('tls'),
  4. EventParser = require('../entities/EventParser.js'),
  5. Message = require('js-message'),
  6. fs = require('fs'),
  7. Queue = require('@node-ipc/js-queue'),
  8. Events = require('event-pubsub');
  9. let eventParser = new EventParser();
  10. class Client extends Events{
  11. constructor(config,log){
  12. super();
  13. Object.assign(
  14. this,
  15. {
  16. Client : Client,
  17. config : config,
  18. queue : new Queue,
  19. socket : false,
  20. connect : connect,
  21. emit : emit,
  22. log : log,
  23. retriesRemaining:config.maxRetries||0,
  24. explicitlyDisconnected: false
  25. }
  26. );
  27. eventParser=new EventParser(this.config);
  28. }
  29. }
  30. function emit(type,data){
  31. this.log('dispatching event to ', this.id, this.path, ' : ', type, ',', data);
  32. let message=new Message;
  33. message.type=type;
  34. message.data=data;
  35. if(this.config.rawBuffer){
  36. message=Buffer.from(type,this.config.encoding);
  37. }else{
  38. message=eventParser.format(message);
  39. }
  40. if(!this.config.sync){
  41. this.socket.write(message);
  42. return;
  43. }
  44. this.queue.add(
  45. syncEmit.bind(this,message)
  46. );
  47. }
  48. function syncEmit(message){
  49. this.log('dispatching event to ', this.id, this.path, ' : ', message);
  50. this.socket.write(message);
  51. }
  52. function connect(){
  53. //init client object for scope persistance especially inside of socket events.
  54. let client=this;
  55. client.log('requested connection to ', client.id, client.path);
  56. if(!this.path){
  57. client.log('\n\n######\nerror: ', client.id ,' client has not specified socket path it wishes to connect to.');
  58. return;
  59. }
  60. const options={};
  61. if(!client.port){
  62. client.log('Connecting client on Unix Socket :', client.path);
  63. options.path=client.path;
  64. if (process.platform ==='win32' && !client.path.startsWith('\\\\.\\pipe\\')){
  65. options.path = options.path.replace(/^\//, '');
  66. options.path = options.path.replace(/\//g, '-');
  67. options.path= `\\\\.\\pipe\\${options.path}`;
  68. }
  69. client.socket = net.connect(options);
  70. }else{
  71. options.host=client.path;
  72. options.port=client.port;
  73. if(client.config.interface.localAddress){
  74. options.localAddress=client.config.interface.localAddress;
  75. }
  76. if(client.config.interface.localPort){
  77. options.localPort=client.config.interface.localPort;
  78. }
  79. if(client.config.interface.family){
  80. options.family=client.config.interface.family;
  81. }
  82. if(client.config.interface.hints){
  83. options.hints=client.config.interface.hints;
  84. }
  85. if(client.config.interface.lookup){
  86. options.lookup=client.config.interface.lookup;
  87. }
  88. if(!client.config.tls){
  89. client.log('Connecting client via TCP to', options);
  90. client.socket = net.connect(options);
  91. }else{
  92. client.log('Connecting client via TLS to', client.path ,client.port,client.config.tls);
  93. if(client.config.tls.private){
  94. client.config.tls.key=fs.readFileSync(client.config.tls.private);
  95. }
  96. if(client.config.tls.public){
  97. client.config.tls.cert=fs.readFileSync(client.config.tls.public);
  98. }
  99. if(client.config.tls.trustedConnections){
  100. if(typeof client.config.tls.trustedConnections === 'string'){
  101. client.config.tls.trustedConnections=[client.config.tls.trustedConnections];
  102. }
  103. client.config.tls.ca=[];
  104. for(let i=0; i<client.config.tls.trustedConnections.length; i++){
  105. client.config.tls.ca.push(
  106. fs.readFileSync(client.config.tls.trustedConnections[i])
  107. );
  108. }
  109. }
  110. Object.assign(client.config.tls,options);
  111. client.socket = tls.connect(
  112. client.config.tls
  113. );
  114. }
  115. }
  116. client.socket.setEncoding(this.config.encoding);
  117. client.socket.on(
  118. 'error',
  119. function(err){
  120. client.log('\n\n######\nerror: ', err);
  121. client.publish('error', err);
  122. }
  123. );
  124. client.socket.on(
  125. 'connect',
  126. function connectionMade(){
  127. client.publish('connect');
  128. client.retriesRemaining=client.config.maxRetries;
  129. client.log('retrying reset');
  130. }
  131. );
  132. client.socket.on(
  133. 'close',
  134. function connectionClosed(){
  135. client.log('connection closed' ,client.id , client.path,
  136. client.retriesRemaining, 'tries remaining of', client.config.maxRetries
  137. );
  138. if(
  139. client.config.stopRetrying ||
  140. client.retriesRemaining<1 ||
  141. client.explicitlyDisconnected
  142. ){
  143. client.publish('disconnect');
  144. client.log(
  145. (client.config.id),
  146. 'exceeded connection rety amount of',
  147. ' or stopRetrying flag set.'
  148. );
  149. client.socket.destroy();
  150. client.publish('destroy');
  151. client=undefined;
  152. return;
  153. }
  154. setTimeout(
  155. function retryTimeout(){
  156. if (client.explicitlyDisconnected) {
  157. return;
  158. }
  159. client.retriesRemaining--;
  160. client.connect();
  161. }.bind(null,client),
  162. client.config.retry
  163. );
  164. client.publish('disconnect');
  165. }
  166. );
  167. client.socket.on(
  168. 'data',
  169. function(data) {
  170. client.log('## received events ##');
  171. if(client.config.rawBuffer){
  172. client.publish(
  173. 'data',
  174. Buffer.from(data,client.config.encoding)
  175. );
  176. if(!client.config.sync){
  177. return;
  178. }
  179. client.queue.next();
  180. return;
  181. }
  182. if(!this.ipcBuffer){
  183. this.ipcBuffer='';
  184. }
  185. data=(this.ipcBuffer+=data);
  186. if(data.slice(-1)!=eventParser.delimiter || data.indexOf(eventParser.delimiter) == -1){
  187. client.log('Messages are large, You may want to consider smaller messages.');
  188. return;
  189. }
  190. this.ipcBuffer='';
  191. const events = eventParser.parse(data);
  192. const eCount = events.length;
  193. for(let i=0; i<eCount; i++){
  194. let message=new Message;
  195. message.load(events[i]);
  196. client.log('detected event', message.type, message.data);
  197. client.publish(
  198. message.type,
  199. message.data
  200. );
  201. }
  202. if(!client.config.sync){
  203. return;
  204. }
  205. client.queue.next();
  206. }
  207. );
  208. }
  209. module.exports=Client;