client.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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('js-queue');
  8. let Events = require('event-pubsub/es5');
  9. if(process.version[1]>4){
  10. Events = require('event-pubsub');
  11. }
  12. let eventParser = new EventParser();
  13. class Client extends Events{
  14. constructor(config,log){
  15. super();
  16. Object.assign(
  17. this,
  18. {
  19. Client : Client,
  20. config : config,
  21. queue : new Queue,
  22. socket : false,
  23. connect : connect,
  24. emit : emit,
  25. log : log,
  26. retriesRemaining:config.maxRetries||0,
  27. explicitlyDisconnected: false
  28. }
  29. );
  30. eventParser=new EventParser(this.config);
  31. }
  32. }
  33. function emit(type,data){
  34. this.log('dispatching event to ', this.id, this.path, ' : ', type, ',', data);
  35. let message=new Message;
  36. message.type=type;
  37. message.data=data;
  38. if(this.config.rawBuffer){
  39. message=new Buffer(type,this.config.encoding);
  40. }else{
  41. message=eventParser.format(message);
  42. }
  43. if(!this.config.sync){
  44. this.socket.write(message);
  45. return;
  46. }
  47. this.queue.add(
  48. syncEmit.bind(this,message)
  49. );
  50. }
  51. function syncEmit(message){
  52. this.log('dispatching event to ', this.id, this.path, ' : ', message);
  53. this.socket.write(message);
  54. }
  55. function connect(){
  56. //init client object for scope persistance especially inside of socket events.
  57. let client=this;
  58. client.log('requested connection to ', client.id, client.path);
  59. if(!this.path){
  60. client.log('\n\n######\nerror: ', client.id ,' client has not specified socket path it wishes to connect to.');
  61. return;
  62. }
  63. const options={};
  64. if(!client.port){
  65. client.log('Connecting client on Unix Socket :', client.path);
  66. options.path=client.path;
  67. if (process.platform ==='win32' && !client.path.startsWith('\\\\.\\pipe\\')){
  68. options.path = options.path.replace(/^\//, '');
  69. options.path = options.path.replace(/\//g, '-');
  70. options.path= `\\\\.\\pipe\\${options.path}`;
  71. }
  72. client.socket = net.connect(options);
  73. }else{
  74. options.host=client.path;
  75. options.port=client.port;
  76. if(client.config.interface.localAddress){
  77. options.localAddress=client.config.interface.localAddress;
  78. }
  79. if(client.config.interface.localPort){
  80. options.localPort=client.config.interface.localPort;
  81. }
  82. if(client.config.interface.family){
  83. options.family=client.config.interface.family;
  84. }
  85. if(client.config.interface.hints){
  86. options.hints=client.config.interface.hints;
  87. }
  88. if(client.config.interface.lookup){
  89. options.lookup=client.config.interface.lookup;
  90. }
  91. if(!client.config.tls){
  92. client.log('Connecting client via TCP to', options);
  93. client.socket = net.connect(options);
  94. }else{
  95. client.log('Connecting client via TLS to', client.path ,client.port,client.config.tls);
  96. if(client.config.tls.private){
  97. client.config.tls.key=fs.readFileSync(client.config.tls.private);
  98. }
  99. if(client.config.tls.public){
  100. client.config.tls.cert=fs.readFileSync(client.config.tls.public);
  101. }
  102. if(client.config.tls.trustedConnections){
  103. if(typeof client.config.tls.trustedConnections === 'string'){
  104. client.config.tls.trustedConnections=[client.config.tls.trustedConnections];
  105. }
  106. client.config.tls.ca=[];
  107. for(let i=0; i<client.config.tls.trustedConnections.length; i++){
  108. client.config.tls.ca.push(
  109. fs.readFileSync(client.config.tls.trustedConnections[i])
  110. );
  111. }
  112. }
  113. Object.assign(client.config.tls,options);
  114. client.socket = tls.connect(
  115. client.config.tls
  116. );
  117. }
  118. }
  119. client.socket.setEncoding(this.config.encoding);
  120. client.socket.on(
  121. 'error',
  122. function(err){
  123. client.log('\n\n######\nerror: ', err);
  124. client.publish('error', err);
  125. }
  126. );
  127. client.socket.on(
  128. 'connect',
  129. function connectionMade(){
  130. client.publish('connect');
  131. client.retriesRemaining=client.config.maxRetries;
  132. client.log('retrying reset');
  133. }
  134. );
  135. client.socket.on(
  136. 'close',
  137. function connectionClosed(){
  138. client.log('connection closed' ,client.id , client.path,
  139. client.retriesRemaining, 'tries remaining of', client.config.maxRetries
  140. );
  141. if(
  142. client.config.stopRetrying ||
  143. client.retriesRemaining<1 ||
  144. client.explicitlyDisconnected
  145. ){
  146. client.publish('disconnect');
  147. client.log(
  148. (client.config.id),
  149. 'exceeded connection rety amount of',
  150. ' or stopRetrying flag set.'
  151. );
  152. client.socket.destroy();
  153. client.publish('destroy');
  154. client=undefined;
  155. return;
  156. }
  157. setTimeout(
  158. function retryTimeout(){
  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. new Buffer(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;