total5 0.0.1-2 → 0.0.1-21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,7 @@ var isFLOWSTREAMWORKER = false;
16
16
  var Parent = W.parentPort;
17
17
  var CALLBACKS = {};
18
18
  var FLOWS = {};
19
+ var PROXIES = {};
19
20
  var TMS = {};
20
21
  var RPC = {};
21
22
  var CALLBACKID = 1;
@@ -72,13 +73,29 @@ var isrunning = false;
72
73
  */
73
74
 
74
75
  function Instance(instance, id) {
75
- this.httproutes = {};
76
- this.version = VERSION;
77
- this.id = id;
78
- this.flow = instance;
76
+ var self = this;
77
+ self.httproutes = {};
78
+ self.version = VERSION;
79
+ self.id = id;
80
+ self.flow = instance;
79
81
  // this.onoutput = null;
80
82
  }
81
83
 
84
+ Instance.prototype = {
85
+
86
+ get worker() {
87
+ return this.flow;
88
+ },
89
+
90
+ get stream() {
91
+ return this.flow;
92
+ }
93
+ };
94
+
95
+ Instance.prototype.postMessage = function(msg) {
96
+ this.flow.postMessage && this.flow.postMessage(msg);
97
+ };
98
+
82
99
  Instance.prototype.httprequest = function(opt, callback) {
83
100
 
84
101
  // opt.route {String} a URL address
@@ -189,7 +206,7 @@ Instance.prototype.httprouting = function() {
189
206
 
190
207
  if (meta.headers) {
191
208
  for (var key in meta.headers)
192
- self.header(key, meta.headers[key]);
209
+ self.response.headers[key] = meta.headers[key];
193
210
  }
194
211
 
195
212
  if (meta.cookies && meta.cookies instanceof Array) {
@@ -198,7 +215,7 @@ Instance.prototype.httprouting = function() {
198
215
  var value = item.value;
199
216
  var expiration = item.expiration || item.expires || item.expire;
200
217
  if (name && value && expiration)
201
- self.res.cookie(name, value, expiration, item.options || item.config);
218
+ self.cookie(name, value, expiration, item.options || item.config);
202
219
  }
203
220
  }
204
221
 
@@ -209,13 +226,13 @@ Instance.prototype.httprouting = function() {
209
226
  break;
210
227
  case 'text':
211
228
  case 'plain':
212
- self.plain(data);
229
+ self.text(data);
213
230
  break;
214
231
  case 'html':
215
- self.content(data, 'text/html');
232
+ self.html(data);
216
233
  break;
217
234
  case 'xml':
218
- self.content(data, 'text/xml');
235
+ self.binary(Buffer.from(data, 'utf8'), 'text/xml');
219
236
  break;
220
237
  case 'json':
221
238
  self.json(data);
@@ -369,6 +386,16 @@ Instance.prototype.eval = function(msg, callback) {
369
386
  return self;
370
387
  };
371
388
 
389
+ Instance.prototype.restart = function() {
390
+ var self = this;
391
+ self.flow.$socket && self.flow.$socket.destroy();
392
+ self.flow.$client && self.flow.$client.destroy();
393
+ if (self.flow.terminate)
394
+ self.flow.terminate();
395
+ else
396
+ self.flow.kill(9);
397
+ };
398
+
372
399
  // Destroys the Flow
373
400
  Instance.prototype.kill = Instance.prototype.destroy = function() {
374
401
 
@@ -378,22 +405,26 @@ Instance.prototype.kill = Instance.prototype.destroy = function() {
378
405
  self.flow.$destroyed = true;
379
406
 
380
407
  if (self.flow.isworkerthread) {
381
- self.flow.$socket && self.flow.$socket.destroy();
382
- self.flow.$client && self.flow.$client.destroy();
383
- if (self.flow.terminate)
384
- self.flow.terminate();
385
- else
386
- self.flow.kill(9);
408
+
409
+ self.postMessage({ TYPE: 'stream/destroy' });
410
+ setTimeout(self => self.flow.terminate ? self.flow.terminate() : self.flow.kill(9), 1000, self);
411
+
412
+ if (PROXIES[self.id]) {
413
+ PROXIES[self.id].remove();
414
+ delete PROXIES[self.id];
415
+ }
416
+
387
417
  } else {
388
418
  if (self.flow.sockets) {
389
419
  for (var key in self.flow.sockets)
390
420
  self.flow.sockets[key].destroy();
391
421
  }
392
- self.flow.$socket && self.flow.$socket.destroy();
393
- self.flow.$client && self.flow.$client.destroy();
394
422
  self.flow.destroy();
395
423
  }
396
424
 
425
+ self.flow.$socket && self.flow.$socket.destroy();
426
+ self.flow.$client && self.flow.$client.destroy();
427
+
397
428
  for (var key in CALLBACKS) {
398
429
  if (CALLBACKS[key].id === self.id)
399
430
  delete CALLBACKS[key];
@@ -563,14 +594,48 @@ Instance.prototype.reconfigure = function(id, config) {
563
594
  return self;
564
595
  };
565
596
 
566
- Instance.prototype.reload = function(data, restart) {
597
+ Instance.prototype.reload = function(data) {
567
598
  var self = this;
568
- return self.refresh(self.id, 'meta', data, restart);
599
+ var flow = self.flow;
600
+
601
+ if (PROXIES[data.id]) {
602
+ PROXIES[data.id].remove();
603
+ delete PROXIES[data.id];
604
+ }
605
+
606
+ if (flow.isworkerthread) {
607
+
608
+ if (data.proxypath) {
609
+
610
+ if (!data.unixsocket) {
611
+ data.unixsocket = flow.$schema.unixsocket || makeunixsocket(data.id);
612
+ flow.$schema.unixsocket = data.unixsocket;
613
+ }
614
+
615
+ PROXIES[data.id] = F.proxy(data.proxypath, data.unixsocket);
616
+ }
617
+
618
+ for (let key in data)
619
+ flow.$schema[key] = data[key];
620
+
621
+ self.proxypath = data.proxypath;
622
+ flow.postMessage2({ TYPE: 'stream/rewrite', data: data });
623
+
624
+ } else {
625
+ for (let key in data)
626
+ flow.$schema[key] = data[key];
627
+ flow.variables = data.variables;
628
+ if (data.variables2)
629
+ flow.variables2 = data.variables2;
630
+ flow.rewrite(data, () => flow.proxy.refreshmeta());
631
+ }
632
+ return self;
569
633
  };
570
634
 
571
635
  Instance.prototype.refresh = function(id, type, data, restart) {
572
636
  var self = this;
573
637
  var flow = self.flow;
638
+
574
639
  if (flow.isworkerthread) {
575
640
 
576
641
  for (var key in data)
@@ -581,9 +646,8 @@ Instance.prototype.refresh = function(id, type, data, restart) {
581
646
  flow.terminate();
582
647
  else
583
648
  flow.kill(9);
584
- }
585
-
586
- flow.postMessage2({ TYPE: 'stream/refresh', id: id, type: type, data: data });
649
+ } else
650
+ flow.postMessage2({ TYPE: 'stream/refresh', id: id, type: type, data: data });
587
651
 
588
652
  } else {
589
653
 
@@ -790,14 +854,14 @@ function exec(self, opt) {
790
854
 
791
855
  if (opt.id[0] === '@') {
792
856
  id = opt.id.substring(1);
793
- for (var key in instances) {
857
+ for (let key in instances) {
794
858
  if (instances[key].component === id)
795
859
  target.push(instances[key]);
796
860
  }
797
861
  } else if (opt.id[0] === '#') {
798
862
  id = opt.id.substring(1);
799
- for (var key in instances) {
800
- if (instances[key].module.id === id)
863
+ for (let key in instances) {
864
+ if (instances[key].module.name === id)
801
865
  target.push(instances[key]);
802
866
  }
803
867
  } else {
@@ -901,7 +965,7 @@ function httprequest(self, opt, callback) {
901
965
  }
902
966
 
903
967
  function killprocess() {
904
- console.error('Main process doesn\'t respond');
968
+ // console.error('Main process doesn\'t respond');
905
969
  process.exit(1);
906
970
  }
907
971
 
@@ -921,12 +985,24 @@ function init_current(meta, callback, nested) {
921
985
  var flow = MAKEFLOWSTREAM(meta);
922
986
  FLOWS[meta.id] = flow;
923
987
 
924
- if (isFLOWSTREAMWORKER && meta.unixsocket && meta.proxypath) {
925
- if (!F.isWindows)
926
- F.Fs.unlink(meta.unixsocket, NOOP);
927
- F.http({ load: 'none', unixsocket: meta.unixsocket, config: { $stats: false, $sourcemap: false }});
988
+ if (isFLOWSTREAMWORKER) {
989
+ if (meta.unixsocket && meta.proxypath) {
990
+ if (!F.isWindows)
991
+ F.Fs.unlink(meta.unixsocket, NOOP);
992
+ F.http({ load: 'none', unixsocket: meta.unixsocket, config: { $stats: false, $sourcemap: false }});
993
+ } else {
994
+ F.config.$sourcemap = false;
995
+ F.config.$stats = false;
996
+ F.load('none');
997
+ }
928
998
  }
929
999
 
1000
+ flow.env = meta.env;
1001
+ flow.origin = meta.origin;
1002
+ flow.proxypath = meta.proxypath || '';
1003
+ flow.proxy.online = false;
1004
+ flow.proxy.ping = 0;
1005
+
930
1006
  if (meta.import) {
931
1007
  var tmp = meta.import.split(/,|;/).trim();
932
1008
  for (var m of tmp) {
@@ -936,11 +1012,13 @@ function init_current(meta, callback, nested) {
936
1012
  }
937
1013
  }
938
1014
 
939
- flow.env = meta.env;
940
- flow.origin = meta.origin;
941
- flow.proxypath = meta.proxypath || '';
942
- flow.proxy.online = false;
943
- flow.proxy.ping = 0;
1015
+ if (meta.initscript) {
1016
+ try {
1017
+ new Function('instance', meta.initscript)(flow);
1018
+ } catch (e) {
1019
+ flow.error(e, 'initscript');
1020
+ }
1021
+ }
944
1022
 
945
1023
  flow.$instance = new Instance(flow, meta.id);
946
1024
 
@@ -949,7 +1027,6 @@ function init_current(meta, callback, nested) {
949
1027
  };
950
1028
 
951
1029
  if (!nested && Parent) {
952
-
953
1030
  Parent.on('message', function(msg) {
954
1031
 
955
1032
  var id;
@@ -961,6 +1038,10 @@ function init_current(meta, callback, nested) {
961
1038
  flow.proxy.ping = setTimeout(killprocess, 10000);
962
1039
  break;
963
1040
 
1041
+ case 'stream/destroy':
1042
+ flow.destroy();
1043
+ break;
1044
+
964
1045
  case 'stream/export':
965
1046
  msg.data = flow.export2();
966
1047
  Parent.postMessage(msg);
@@ -1030,6 +1111,25 @@ function init_current(meta, callback, nested) {
1030
1111
  flow.save();
1031
1112
  break;
1032
1113
 
1114
+ case 'stream/rewrite':
1115
+ for (var key in msg.data)
1116
+ flow.$schema[key] = msg.data[key];
1117
+
1118
+ flow.rewrite(msg.data, function() {
1119
+
1120
+ // @err {Error}
1121
+
1122
+ flow.proxy.refreshmeta();
1123
+
1124
+ if (flow.proxy.online) {
1125
+ flow.proxy.send({ TYPE: 'flow/components', data: flow.components(true) });
1126
+ flow.proxy.send({ TYPE: 'flow/design', data: flow.export() });
1127
+ flow.proxy.send({ TYPE: 'flow/variables', data: flow.variables });
1128
+ }
1129
+
1130
+ });
1131
+ break;
1132
+
1033
1133
  case 'stream/refresh':
1034
1134
 
1035
1135
  if (msg.type === 'meta' && msg.data) {
@@ -1199,12 +1299,20 @@ function init_current(meta, callback, nested) {
1199
1299
  var componentid = '';
1200
1300
 
1201
1301
  if (instance) {
1202
- if (source === 'instance_message') {
1302
+ if (typeof(instance) === 'string') {
1303
+ if (source === 'add') {
1304
+ componentid = instance;
1305
+ F.error(err, 'FlowStream | register component | ' + instance);
1306
+ } else if (meta.design[instance]) {
1307
+ instanceid = instance;
1308
+ componentid = meta.design[instance].component;
1309
+ }
1310
+ } else if (source === 'instance_message') {
1203
1311
  if (instance.instance) {
1204
1312
  instanceid = instance.instance.id;
1205
1313
  componentid = instance.instance.component;
1206
1314
  } else
1207
- console.log('ERROR', instance);
1315
+ F.error(err, 'FlowStream | message | ' + instance);
1208
1316
  } else if (source === 'instance_close') {
1209
1317
  instanceid = instance.id;
1210
1318
  componentid = instance.component;
@@ -1345,7 +1453,15 @@ function init_worker(meta, type, callback) {
1345
1453
  var worker = type === 'worker' ? (new W.Worker(__filename, { workerData: meta })) : Fork(__filename, forkargs, { serialization: 'json', detached: false });
1346
1454
  var ischild = false;
1347
1455
 
1348
- meta.unixsocket = F.isWindows ? ('\\\\?\\pipe\\flowstream' + F.directory.makeid() + meta.id + Date.now().toString(36)) : (F.Path.join(F.Os.tmpdir(), 'flowstream_' + F.directory.makeid() + '_' + meta.id + '_' + Date.now().toString(36) + '.socket'));
1456
+ meta.unixsocket = makeunixsocket(meta.id);
1457
+
1458
+ if (PROXIES[meta.id]) {
1459
+ PROXIES[meta.id].remove();
1460
+ delete PROXIES[meta.id];
1461
+ }
1462
+
1463
+ if (meta.proxypath)
1464
+ PROXIES[meta.id] = F.proxy(meta.proxypath, meta.unixsocket);
1349
1465
 
1350
1466
  if (!worker.postMessage) {
1351
1467
  worker.postMessage = worker.send;
@@ -1389,10 +1505,14 @@ function init_worker(meta, type, callback) {
1389
1505
 
1390
1506
  var tmp;
1391
1507
 
1508
+ Flow.$events.message && Flow.emit('message', worker.$instance.id, msg);
1509
+
1392
1510
  switch (msg.TYPE) {
1393
1511
 
1394
1512
  case 'stream/stats':
1395
1513
  worker.stats = msg.data;
1514
+ if (Flow.$events.stats)
1515
+ Flow.emit('stats', meta.id, msg.data);
1396
1516
  break;
1397
1517
 
1398
1518
  case 'stream/restart':
@@ -1452,7 +1572,7 @@ function init_worker(meta, type, callback) {
1452
1572
  break;
1453
1573
 
1454
1574
  case 'stream/error':
1455
- tmp = { TYPE: 'flow/error', error: msg.error, stack: msg.stack, source: msg.source, id: msg.id, component: msg.component };
1575
+ tmp = { TYPE: 'flow/error', error: msg.error, stack: msg.stack, source: msg.source, id: msg.id, component: msg.component, ts: new Date() };
1456
1576
  worker.$socket && worker.$socket.send(tmp);
1457
1577
  worker.$client && worker.$client.send(tmp);
1458
1578
  worker.$instance.onerror && worker.$instance.onerror(msg.error, msg.source, msg.id, msg.component, msg.stack);
@@ -1723,12 +1843,11 @@ exports.client = function(flow, socket) {
1723
1843
  function MAKEFLOWSTREAM(meta) {
1724
1844
 
1725
1845
  var flow = F.TFlowStream.create(meta.id, function(err, type, instance) {
1726
- flow.proxy.error(err, type, instance);
1846
+ this.proxy.error(err, type, instance);
1727
1847
  });
1728
1848
 
1729
- var saveid;
1849
+ var saveid = null;
1730
1850
 
1731
- flow.metadata = meta;
1732
1851
  flow.cloning = meta.cloning != false;
1733
1852
  flow.export_instance2 = function(id) {
1734
1853
 
@@ -1767,25 +1886,26 @@ function MAKEFLOWSTREAM(meta) {
1767
1886
  }
1768
1887
  };
1769
1888
 
1889
+ function stringifyskip(key, value) {
1890
+ return key === '$$ID' || key === '$$REQUIRED' ? undefined : value;
1891
+ }
1892
+
1770
1893
  flow.export2 = function() {
1771
1894
 
1772
1895
  var variables = flow.variables;
1773
1896
  var design = {};
1774
1897
  var components = {};
1775
- var sources = {};
1776
-
1777
- for (var key in flow.sources) {
1778
- var com = flow.sources[key];
1779
- sources[key] = com;
1780
- }
1898
+ var sources = JSON.parse(JSON.stringify(flow.sources, stringifyskip));
1781
1899
 
1782
1900
  for (var key in flow.meta.components) {
1783
1901
  var com = flow.meta.components[key];
1784
1902
  components[key] = com.ui.raw;
1785
1903
  }
1786
1904
 
1787
- for (var key in flow.meta.flow)
1905
+ for (var key in flow.meta.flow) {
1788
1906
  design[key] = flow.export_instance2(key);
1907
+ delete design[key].template;
1908
+ }
1789
1909
 
1790
1910
  var data = {};
1791
1911
  data.paused = flow.paused;
@@ -1811,7 +1931,7 @@ function MAKEFLOWSTREAM(meta) {
1811
1931
  return data;
1812
1932
  };
1813
1933
 
1814
- var save_force = function() {
1934
+ var saveforce = function() {
1815
1935
  saveid && clearTimeout(saveid);
1816
1936
  saveid = null;
1817
1937
  flow.proxy.save(flow.export2());
@@ -1827,7 +1947,7 @@ function MAKEFLOWSTREAM(meta) {
1827
1947
  return;
1828
1948
 
1829
1949
  clearTimeout(saveid);
1830
- saveid = setTimeout(save_force, 5000);
1950
+ saveid = setTimeout(saveforce, 5000);
1831
1951
  };
1832
1952
 
1833
1953
  flow.save = function() {
@@ -1886,6 +2006,9 @@ function MAKEFLOWSTREAM(meta) {
1886
2006
  };
1887
2007
 
1888
2008
  flow.proxy.message = function(msg, clientid, callback) {
2009
+
2010
+ var tmp;
2011
+
1889
2012
  switch (msg.TYPE) {
1890
2013
 
1891
2014
  case 'call':
@@ -2167,6 +2290,19 @@ function MAKEFLOWSTREAM(meta) {
2167
2290
  break;
2168
2291
 
2169
2292
  case 'component_read':
2293
+
2294
+ tmp = flow.meta.components[msg.id];
2295
+ if (tmp && tmp.meta) {
2296
+ if (tmp.meta.readonly) {
2297
+ msg.TYPE = 'flow/component_read';
2298
+ msg.data = null;
2299
+ msg.error = 'The component cannot be edited';
2300
+ flow.proxy.online && flow.proxy.send(msg, 1, clientid);
2301
+ callback && callback(msg);
2302
+ return;
2303
+ }
2304
+ }
2305
+
2170
2306
  msg.TYPE = 'flow/component_read';
2171
2307
  msg.data = flow.meta.components[msg.id] ? flow.meta.components[msg.id].ui.raw : null;
2172
2308
  msg.error = msg.data == null ? 'Not found' : null;
@@ -2175,6 +2311,19 @@ function MAKEFLOWSTREAM(meta) {
2175
2311
  break;
2176
2312
 
2177
2313
  case 'component_save':
2314
+
2315
+ // check prev functionality
2316
+ tmp = flow.meta.components[msg.id];
2317
+ if (tmp && tmp.meta) {
2318
+ if (tmp.meta.readonly) {
2319
+ msg.TYPE = 'flow/component_save';
2320
+ msg.error = 'The component cannot be edited';
2321
+ flow.proxy.online && flow.proxy.send(msg, 1, clientid);
2322
+ callback && callback(msg);
2323
+ return;
2324
+ }
2325
+ }
2326
+
2178
2327
  flow.add(msg.id, msg.data, function(err) {
2179
2328
  delete msg.data;
2180
2329
  msg.TYPE = 'flow/component_save';
@@ -2187,10 +2336,24 @@ function MAKEFLOWSTREAM(meta) {
2187
2336
  break;
2188
2337
 
2189
2338
  case 'component_remove':
2339
+
2340
+ // check prev functionality
2341
+ tmp = flow.meta.components[msg.id];
2342
+ if (tmp && tmp.meta) {
2343
+ if (tmp.meta.protected) {
2344
+ msg.TYPE = 'flow/component_remove';
2345
+ msg.error = 'The component cannot be removed';
2346
+ flow.proxy.online && flow.proxy.send(msg, 1, clientid);
2347
+ callback && callback(msg);
2348
+ return;
2349
+ }
2350
+ }
2351
+
2190
2352
  flow.unregister(msg.id, function() {
2191
2353
  refresh_components();
2192
2354
  save();
2193
2355
  });
2356
+
2194
2357
  break;
2195
2358
  }
2196
2359
  };
@@ -2206,7 +2369,7 @@ function MAKEFLOWSTREAM(meta) {
2206
2369
  if (meta.paused)
2207
2370
  flow.pause(true);
2208
2371
 
2209
- flow.load(meta.components, meta.design, function() {
2372
+ flow.load(meta, function() {
2210
2373
 
2211
2374
  if (flow.sources) {
2212
2375
  Object.keys(flow.sources).wait(function(key, next) {
@@ -2273,7 +2436,13 @@ function MAKEFLOWSTREAM(meta) {
2273
2436
  // Each 9 seconds
2274
2437
  if (notifier % 3 === 0) {
2275
2438
  notifier = 0;
2276
- Parent && Parent.postMessage({ TYPE: 'stream/stats', data: { paused: flow.paused, messages: flow.stats.messages, pending: flow.stats.pending, memory: flow.stats.memory, minutes: flow.stats.minutes, errors: flow.stats.errors, mm: flow.stats.mm }});
2439
+ if (Parent || Flow.$events.stats) {
2440
+ let pstats = { paused: flow.paused, messages: flow.stats.messages, pending: flow.stats.pending, memory: flow.stats.memory, minutes: flow.stats.minutes, errors: flow.stats.errors, mm: flow.stats.mm };
2441
+ if (Parent)
2442
+ Parent.postMessage({ TYPE: 'stream/stats', data: pstats });
2443
+ else if (Flow.$events.stats)
2444
+ Flow.emit(flow.$schema.id, pstats);
2445
+ }
2277
2446
  }
2278
2447
 
2279
2448
  notifier++;
@@ -2504,7 +2673,7 @@ function MAKEFLOWSTREAM(meta) {
2504
2673
  });
2505
2674
 
2506
2675
  var makemeta = function() {
2507
- return { TYPE: 'flow/flowstream', id: flow.$schema.id, version: VERSION, paused: flow.paused, node: F.version_node, total: F.version, name: flow.$schema.name, version2: flow.$schema.version, icon: flow.$schema.icon, reference: flow.$schema.reference, author: flow.$schema.author, color: flow.$schema.color, origin: flow.$schema.origin, notify: flow.$schema.origin + NOTIFYPATH + flow.$schema.id + '-/', readme: flow.$schema.readme, url: flow.$schema.url, proxypath: flow.$schema.proxypath, env: flow.$schema.env, worker: isFLOWSTREAMWORKER ? (W.workerData ? 'Worker Thread' : 'Child Process') : false, cloning: flow.cloning };
2676
+ return { TYPE: 'flow/flowstream', id: flow.$schema.id, version: VERSION, paused: flow.paused, node: F.version_node, total: F.version, name: flow.$schema.name, version2: flow.$schema.version, icon: flow.$schema.icon, reference: flow.$schema.reference, author: flow.$schema.author, color: flow.$schema.color, origin: flow.$schema.origin, notify: flow.$schema.origin + NOTIFYPATH + flow.$schema.id + '-/', readme: flow.$schema.readme, url: flow.$schema.url, proxypath: isFLOWSTREAMWORKER ? flow.$schema.proxypath : '/', env: flow.$schema.env, worker: isFLOWSTREAMWORKER ? (W.workerData ? 'Worker Thread' : 'Child Process') : false, cloning: flow.cloning };
2508
2677
  };
2509
2678
 
2510
2679
  flow.proxy.refreshmeta = function() {
@@ -2539,51 +2708,50 @@ function MAKEFLOWSTREAM(meta) {
2539
2708
  // TMS implementation:
2540
2709
  TMS.check = function(item, callback) {
2541
2710
 
2542
- WEBSOCKETCLIENT(function(client) {
2711
+ var client = F.websocketclient();
2543
2712
 
2544
- if (item.token)
2545
- client.headers['x-token'] = item.token;
2713
+ if (item.token)
2714
+ client.headers['x-token'] = item.token;
2546
2715
 
2547
- client.options.reconnect = 0;
2716
+ client.options.reconnect = 0;
2548
2717
 
2549
- client.on('open', function() {
2550
- client.tmsready = true;
2551
- });
2552
-
2553
- client.on('error', function(err) {
2554
- client.tmsready = false;
2555
- callback(err);
2556
- clearTimeout(client.timeout);
2557
- });
2718
+ client.on('open', function() {
2719
+ client.tmsready = true;
2720
+ });
2558
2721
 
2559
- client.on('close', function() {
2560
- client.tmsready = false;
2561
- callback('401: Unauthorized');
2562
- });
2722
+ client.on('error', function(err) {
2723
+ client.tmsready = false;
2724
+ callback(err);
2725
+ clearTimeout(client.timeout);
2726
+ });
2563
2727
 
2564
- client.on('message', function(msg) {
2565
- switch (msg.type) {
2566
- case 'ping':
2567
- msg.type = 'pong';
2568
- client.send(msg);
2569
- break;
2570
- case 'meta':
2571
- callback(null, msg);
2572
- clearTimeout(client.timeout);
2573
- client.close();
2574
- break;
2575
- }
2576
- });
2728
+ client.on('close', function() {
2729
+ client.tmsready = false;
2730
+ callback('401: Unauthorized');
2731
+ });
2577
2732
 
2578
- client.timeout = setTimeout(function() {
2579
- if (client.tmsready) {
2733
+ client.on('message', function(msg) {
2734
+ switch (msg.type) {
2735
+ case 'ping':
2736
+ msg.type = 'pong';
2737
+ client.send(msg);
2738
+ break;
2739
+ case 'meta':
2740
+ callback(null, msg);
2741
+ clearTimeout(client.timeout);
2580
2742
  client.close();
2581
- callback('408: Timeout');
2582
- }
2583
- }, 1500);
2584
-
2585
- client.connect(item.url.replace(/^http/g, 'ws'));
2743
+ break;
2744
+ }
2586
2745
  });
2746
+
2747
+ client.timeout = setTimeout(function() {
2748
+ if (client.tmsready) {
2749
+ client.close();
2750
+ callback('408: Timeout');
2751
+ }
2752
+ }, 2500);
2753
+
2754
+ client.connect(item.url.replace(/^http/g, 'ws'));
2587
2755
  };
2588
2756
 
2589
2757
  function makemodel(item) {
@@ -2597,172 +2765,163 @@ TMS.connect = function(fs, sourceid, callback) {
2597
2765
  delete fs.sockets[sourceid];
2598
2766
  }
2599
2767
 
2600
- WEBSOCKETCLIENT(function(client) {
2768
+ var client = F.websocketclient();
2601
2769
 
2602
- var item = fs.sources[sourceid];
2603
- var prev;
2770
+ var item = fs.sources[sourceid];
2771
+ var prev;
2604
2772
 
2605
- item.restart = false;
2606
- client.options.reconnectserver = true;
2607
- client.callbacks = {};
2608
- client.callbackindexer = 0;
2609
- client.callbacktimeout = function(callbackid) {
2610
- var cb = client.callbacks[callbackid];
2611
- if (cb) {
2612
- delete client.callbacks[callbackid];
2613
- cb(new ErrorBuilder().push(408).output());
2614
- }
2615
- };
2773
+ item.restart = false;
2774
+ client.options.reconnectserver = true;
2775
+ client.callbacks = {};
2776
+ client.callbackindexer = 0;
2777
+ client.callbacktimeout = function(callbackid) {
2778
+ var cb = client.callbacks[callbackid];
2779
+ if (cb) {
2780
+ delete client.callbacks[callbackid];
2781
+ cb(new ErrorBuilder().push(408).output());
2782
+ }
2783
+ };
2616
2784
 
2617
- if (item.token)
2618
- client.headers['x-token'] = item.token;
2785
+ if (item.token)
2786
+ client.headers['x-token'] = item.token;
2619
2787
 
2620
- var syncforce = function() {
2621
- client.synchronize();
2622
- };
2788
+ var syncforce = function() {
2789
+ client.synchronize();
2790
+ };
2623
2791
 
2624
- client.on('open', function() {
2625
- prev = null;
2626
- fs.sockets[item.id] = client;
2627
- item.error = 0;
2628
- item.init = true;
2629
- item.online = true;
2630
- client.subscribers = {};
2631
- client.tmsready = true;
2632
- client.model = makemodel(item);
2633
- setTimeout(syncforce, 10);
2634
- });
2792
+ client.on('open', function() {
2793
+ prev = null;
2794
+ fs.sockets[item.id] = client;
2795
+ item.error = 0;
2796
+ item.init = true;
2797
+ item.online = true;
2798
+ client.subscribers = {};
2799
+ client.tmsready = true;
2800
+ client.model = makemodel(item);
2801
+ setTimeout(syncforce, 10);
2802
+ });
2635
2803
 
2636
- client.synchronize = function() {
2804
+ client.synchronize = function() {
2637
2805
 
2638
- if (!client.tmsready)
2639
- return;
2806
+ if (!client.tmsready)
2807
+ return;
2640
2808
 
2641
- var publishers = {};
2809
+ var publishers = {};
2642
2810
 
2643
- for (var key in fs.meta.flow) {
2644
- var instance = fs.meta.flow[key];
2645
- var com = fs.meta.components[instance.component];
2646
- if (com && com.itemid === item.id && com.outputs && com.outputs.length) {
2647
- if (Object.keys(instance.connections).length)
2648
- publishers[com.schema.id] = 1;
2649
- }
2811
+ for (var key in fs.meta.flow) {
2812
+ var instance = fs.meta.flow[key];
2813
+ var com = fs.meta.components[instance.component];
2814
+ if (com && com.itemid === item.id && com.outputs && com.outputs.length) {
2815
+ if (Object.keys(instance.connections).length)
2816
+ publishers[com.schema.id] = 1;
2650
2817
  }
2818
+ }
2651
2819
 
2652
- var keys = Object.keys(publishers);
2653
- var cache = keys.join(',');
2820
+ var keys = Object.keys(publishers);
2821
+ var cache = keys.join(',');
2654
2822
 
2655
- if (!prev || prev !== cache) {
2656
- prev = cache;
2657
- client.send({ type: 'subscribers', subscribers: keys });
2658
- }
2823
+ if (!prev || prev !== cache) {
2824
+ prev = cache;
2825
+ client.send({ type: 'subscribers', subscribers: keys });
2826
+ }
2659
2827
 
2660
- };
2828
+ };
2661
2829
 
2662
- client.on('close', function(code) {
2830
+ client.on('close', function(code) {
2663
2831
 
2664
- if (code === 4001)
2665
- client.destroy();
2832
+ if (code === 4001)
2833
+ client.destroy();
2666
2834
 
2667
- item.error = code;
2668
- item.online = false;
2835
+ item.error = code;
2836
+ item.online = false;
2669
2837
 
2670
- client.model = makemodel(item);
2671
- // AUDIT(client, 'close');
2838
+ client.model = makemodel(item);
2839
+ // AUDIT(client, 'close');
2672
2840
 
2673
- delete fs.sockets[item.id];
2674
- client.tmsready = false;
2675
- });
2841
+ delete fs.sockets[item.id];
2842
+ client.tmsready = false;
2843
+ });
2676
2844
 
2677
- client.on('message', function(msg) {
2845
+ client.on('message', function(msg) {
2678
2846
 
2679
- var type = msg.type || msg.TYPE;
2847
+ var type = msg.type || msg.TYPE;
2848
+ var tmp;
2680
2849
 
2681
- switch (type) {
2682
- case 'meta':
2850
+ switch (type) {
2851
+ case 'meta':
2683
2852
 
2684
- item.meta = msg;
2853
+ item.meta = msg;
2685
2854
 
2686
- var checksum = HASH(JSON.stringify(msg)).toString(36);
2687
- client.subscribers = {};
2688
- client.publishers = {};
2689
- client.calls = {};
2855
+ tmp = HASH(JSON.stringify(msg)).toString(36);
2856
+ client.subscribers = {};
2857
+ client.publishers = {};
2858
+ client.calls = {};
2690
2859
 
2691
- for (var i = 0; i < msg.publish.length; i++) {
2692
- var pub = msg.publish[i];
2693
- client.publishers[pub.id] = pub.schema;
2694
- }
2860
+ for (let pub of msg.publish)
2861
+ client.publishers[pub.id] = pub.schema;
2695
2862
 
2696
- for (var i = 0; i < msg.subscribe.length; i++) {
2697
- var sub = msg.subscribe[i];
2698
- client.subscribers[sub.id] = 1;
2699
- }
2863
+ for (let sub of msg.subscribe)
2864
+ client.subscribers[sub.id] = 1;
2700
2865
 
2701
- if (msg.call) {
2702
- for (var i = 0; i < msg.call.length; i++) {
2703
- var call = msg.call[i];
2704
- client.calls[call.id] = 1;
2705
- }
2706
- }
2866
+ if (msg.call) {
2867
+ for (let call of msg.call)
2868
+ client.calls[call.id] = 1;
2869
+ }
2707
2870
 
2708
- if (item.checksum !== checksum) {
2709
- item.checksum = checksum;
2710
- item.init = false;
2711
- TMS.refresh2(fs);
2712
- }
2871
+ if (item.checksum !== tmp) {
2872
+ item.checksum = tmp;
2873
+ item.init = false;
2874
+ TMS.refresh2(fs);
2875
+ }
2713
2876
 
2714
- client.synchronize();
2715
- break;
2877
+ client.synchronize();
2878
+ break;
2716
2879
 
2717
- case 'call':
2880
+ case 'call':
2718
2881
 
2719
- var callback = client.callbacks[msg.callbackid];
2720
- if (callback) {
2721
- callback.id && clearTimeout(callback.id);
2722
- callback.callback(msg.error ? msg.data : null, msg.error ? null : msg.data);
2723
- delete client.callbacks[msg.callbackid];
2724
- }
2882
+ tmp = client.callbacks[msg.callbackid];
2883
+ if (tmp) {
2884
+ tmp.id && clearTimeout(tmp.id);
2885
+ tmp.callback(msg.error ? msg.data : null, msg.error ? null : msg.data);
2886
+ delete client.callbacks[msg.callbackid];
2887
+ }
2725
2888
 
2726
- break;
2889
+ break;
2727
2890
 
2728
- case 'subscribers':
2729
- client.subscribers = {};
2730
- if (msg.subscribers instanceof Array) {
2731
- for (var i = 0; i < msg.subscribers.length; i++) {
2732
- var key = msg.subscribers[i];
2733
- client.subscribers[key] = 1;
2734
- }
2735
- }
2736
- break;
2891
+ case 'subscribers':
2892
+ client.subscribers = {};
2893
+ if (msg.subscribers instanceof Array) {
2894
+ for (let key of msg.subscribers)
2895
+ client.subscribers[key] = 1;
2896
+ }
2897
+ break;
2737
2898
 
2738
- case 'publish':
2899
+ case 'publish':
2739
2900
 
2740
- if (fs.paused)
2741
- return;
2901
+ if (fs.paused)
2902
+ return;
2742
2903
 
2743
- var schema = client.publishers[msg.id];
2744
- if (schema) {
2745
- // HACK: very fast validation
2746
- var err = new F.TBuilders.ErrorBuilder();
2747
- var data = F.TJSONSchema.transform(schema, err, msg.data, true);
2748
- if (data) {
2749
- var id = 'pub' + item.id + 'X' + msg.id;
2750
- for (var key in fs.meta.flow) {
2751
- var flow = fs.meta.flow[key];
2752
- if (flow.component === id)
2753
- flow.process(data, client);
2754
- }
2904
+ tmp = client.publishers[msg.id];
2905
+ if (tmp) {
2906
+ // HACK: very fast validation
2907
+ var err = new F.TBuilders.ErrorBuilder();
2908
+ var data = F.TJSONSchema.transform(tmp, err, msg.data, true);
2909
+ if (data) {
2910
+ var id = 'pub' + item.id + 'X' + msg.id;
2911
+ for (let key in fs.meta.flow) {
2912
+ var flow = fs.meta.flow[key];
2913
+ if (flow.component === id)
2914
+ flow.process(data, client);
2755
2915
  }
2756
2916
  }
2917
+ }
2757
2918
 
2758
- break;
2759
- }
2760
-
2761
- });
2762
-
2763
- client.connect(item.url.replace(/^http/g, 'ws'));
2764
- callback && setImmediate(callback);
2919
+ break;
2920
+ }
2765
2921
  });
2922
+
2923
+ client.connect(item.url.replace(/^http/g, 'ws'));
2924
+ callback && setImmediate(callback);
2766
2925
  };
2767
2926
 
2768
2927
  const TEMPLATE_PUBLISH = `<script total>
@@ -2938,7 +3097,6 @@ TMS.refresh = function(fs, callback) {
2938
3097
 
2939
3098
  var item = fs.sources[key];
2940
3099
  if (item.init) {
2941
-
2942
3100
  if (item.restart || !fs.sources[key])
2943
3101
  TMS.connect(fs, item.id, next);
2944
3102
  else
@@ -3099,32 +3257,6 @@ TMS.refresh2 = function(fs) {
3099
3257
  setTimeout2('tms_refresh_' + fs.name, fs => TMS.refresh(fs), 500, null, fs);
3100
3258
  };
3101
3259
 
3102
- isFLOWSTREAMWORKER = W.workerData || process.argv.indexOf('--fork') !== -1;
3103
-
3104
- // Runs the worker
3105
- if (W.workerData) {
3106
- F.dir(F.path.join(__dirname, '../'));
3107
- exports.init(W.workerData);
3108
- }
3109
-
3110
- if (process.argv.includes('--fork')) {
3111
-
3112
- process.once('message', function(msg) {
3113
- if (msg.TYPE === 'init') {
3114
- Parent = process;
3115
- if (!Parent.postMessage)
3116
- Parent.postMessage = process.send;
3117
- F.dir(process.argv[2]);
3118
- exports.init(msg.data);
3119
- }
3120
- });
3121
-
3122
- F.on('error', function(obj) {
3123
- if (obj.error.indexOf('ERR_IPC_CHANNEL_CLOSED') !== -1)
3124
- process.exit(1);
3125
- });
3126
- }
3127
-
3128
3260
  function initrunning() {
3129
3261
 
3130
3262
  if (isrunning)
@@ -3149,4 +3281,38 @@ function initrunning() {
3149
3281
  }
3150
3282
  });
3151
3283
 
3284
+ }
3285
+
3286
+ function makeunixsocket(id) {
3287
+ return F.isWindows ? ('\\\\?\\pipe\\flowstream' + F.directory.makeid() + id + Date.now().toString(36)) : (F.Path.join(F.Os.tmpdir(), 'flowstream_' + F.directory.makeid() + '_' + id + '_' + Date.now().toString(36) + '.socket'));
3288
+ }
3289
+
3290
+ if (process.argv[1].endsWith('flow-flowstream.js')) {
3291
+
3292
+ isFLOWSTREAMWORKER = W.workerData || process.argv.indexOf('--fork') !== -1;
3293
+
3294
+ // Runs the worker
3295
+ if (W.workerData) {
3296
+ F.dir(F.path.join(__dirname, '../'));
3297
+ exports.init(W.workerData);
3298
+ }
3299
+
3300
+ if (process.argv.includes('--fork')) {
3301
+
3302
+ process.once('message', function(msg) {
3303
+ if (msg.TYPE === 'init') {
3304
+ Parent = process;
3305
+ if (!Parent.postMessage)
3306
+ Parent.postMessage = process.send;
3307
+ F.dir(process.argv[2]);
3308
+ exports.init(msg.data);
3309
+ }
3310
+ });
3311
+
3312
+ F.on('error', function(obj) {
3313
+ if (obj.error.indexOf('ERR_IPC_CHANNEL_CLOSED') !== -1)
3314
+ process.exit(1);
3315
+ });
3316
+ }
3317
+
3152
3318
  }