unetjs 2.0.10 → 3.0.0
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.
- package/README.md +10 -6
- package/dist/cjs/unet.cjs +67 -51
- package/dist/esm/unet.js +65 -30
- package/dist/unetjs.js +65 -29
- package/dist/unetjs.js.map +1 -1
- package/dist/unetjs.min.js +1 -1
- package/dist/unetjs.min.js.map +1 -1
- package/package.json +11 -12
package/dist/unetjs.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.unet = {}));
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
-
/* fjage.js v1.
|
|
7
|
+
/* fjage.js v1.11.2 */
|
|
8
8
|
|
|
9
9
|
const isBrowser =
|
|
10
10
|
typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
@@ -29,10 +29,13 @@
|
|
|
29
29
|
(navigator.userAgent.includes("Node.js") ||
|
|
30
30
|
navigator.userAgent.includes("jsdom")));
|
|
31
31
|
|
|
32
|
-
typeof Deno !== "undefined" &&
|
|
32
|
+
typeof Deno !== "undefined" &&
|
|
33
|
+
typeof Deno.version !== "undefined" &&
|
|
34
|
+
typeof Deno.version.deno !== "undefined";
|
|
33
35
|
|
|
34
36
|
const SOCKET_OPEN = 'open';
|
|
35
37
|
const SOCKET_OPENING = 'opening';
|
|
38
|
+
const DEFAULT_RECONNECT_TIME$1 = 5000; // ms, delay between retries to connect to the server.
|
|
36
39
|
|
|
37
40
|
var createConnection;
|
|
38
41
|
|
|
@@ -44,17 +47,24 @@
|
|
|
44
47
|
|
|
45
48
|
/**
|
|
46
49
|
* Create an TCPConnector to connect to a fjage master over TCP
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
+
* @param {Object} opts
|
|
51
|
+
* @param {string} [opts.hostname='localhost'] - hostname/ip address of the master container to connect to
|
|
52
|
+
* @param {number} opts.port - port number of the master container to connect to
|
|
53
|
+
* @param {string} opts.pathname - path of the master container to connect to
|
|
54
|
+
* @param {boolean} opts.keepAlive - try to reconnect if the connection is lost
|
|
55
|
+
* @param {number} [opts.reconnectTime=5000] - time before reconnection is attempted after an error
|
|
50
56
|
*/
|
|
51
57
|
constructor(opts = {}) {
|
|
52
58
|
this.url = new URL('tcp://localhost');
|
|
53
59
|
let host = opts.hostname || 'localhost';
|
|
54
60
|
let port = opts.port || -1;
|
|
55
|
-
this.url.hostname =
|
|
56
|
-
this.url.port =
|
|
61
|
+
this.url.hostname = host;
|
|
62
|
+
this.url.port = port;
|
|
57
63
|
this._buf = '';
|
|
64
|
+
this._reconnectTime = opts.reconnectTime || DEFAULT_RECONNECT_TIME$1;
|
|
65
|
+
this._keepAlive = opts.keepAlive || true;
|
|
66
|
+
this._firstConn = true; // if the Gateway has managed to connect to a server before
|
|
67
|
+
this._firstReConn = true; // if the Gateway has attempted to reconnect to a server before
|
|
58
68
|
this.pendingOnOpen = []; // list of callbacks make as soon as gateway is open
|
|
59
69
|
this.connListeners = []; // external listeners wanting to listen connection events
|
|
60
70
|
this._sockInit(host, port);
|
|
@@ -79,7 +89,7 @@
|
|
|
79
89
|
}
|
|
80
90
|
}else {
|
|
81
91
|
this._sockSetup(host, port);
|
|
82
|
-
}
|
|
92
|
+
}
|
|
83
93
|
}
|
|
84
94
|
|
|
85
95
|
_sockSetup(host, port){
|
|
@@ -98,18 +108,18 @@
|
|
|
98
108
|
}
|
|
99
109
|
|
|
100
110
|
_sockReconnect(){
|
|
101
|
-
if (this._firstConn || !this.
|
|
111
|
+
if (this._firstConn || !this._keepAlive || this.sock.readyState == SOCKET_OPENING || this.sock.readyState == SOCKET_OPEN) return;
|
|
102
112
|
if (this._firstReConn) this._sendConnEvent(false);
|
|
103
113
|
this._firstReConn = false;
|
|
104
|
-
if(this.debug) console.log('Reconnecting to ', this.sock.remoteAddress + ':' + this.sock.remotePort);
|
|
105
114
|
setTimeout(() => {
|
|
106
115
|
this.pendingOnOpen = [];
|
|
107
|
-
this._sockSetup(this.
|
|
116
|
+
this._sockSetup(this.url.hostname, this.url.port);
|
|
108
117
|
}, this._reconnectTime);
|
|
109
118
|
}
|
|
110
119
|
|
|
111
120
|
_onSockOpen() {
|
|
112
121
|
this._sendConnEvent(true);
|
|
122
|
+
this._firstConn = false;
|
|
113
123
|
this.sock.on('close', this._sockReconnect.bind(this));
|
|
114
124
|
this.sock.on('data', this._processSockData.bind(this));
|
|
115
125
|
this.pendingOnOpen.forEach(cb => cb());
|
|
@@ -166,7 +176,7 @@
|
|
|
166
176
|
* @ignore
|
|
167
177
|
* @param {string} s - incoming message string
|
|
168
178
|
*/
|
|
169
|
-
|
|
179
|
+
|
|
170
180
|
/**
|
|
171
181
|
* Add listener for connection events
|
|
172
182
|
* @param {function} listener - a listener callback that is called when the connection is opened/closed
|
|
@@ -197,12 +207,16 @@
|
|
|
197
207
|
if (this.sock.readyState == SOCKET_OPENING) {
|
|
198
208
|
this.pendingOnOpen.push(() => {
|
|
199
209
|
this.sock.send('{"alive": false}\n');
|
|
200
|
-
this.sock.
|
|
210
|
+
this.sock.removeAllListeners('connect');
|
|
211
|
+
this.sock.removeAllListeners('error');
|
|
212
|
+
this.sock.removeAllListeners('close');
|
|
201
213
|
this.sock.destroy();
|
|
202
214
|
});
|
|
203
215
|
} else if (this.sock.readyState == SOCKET_OPEN) {
|
|
204
216
|
this.sock.send('{"alive": false}\n');
|
|
205
|
-
this.sock.
|
|
217
|
+
this.sock.removeAllListeners('connect');
|
|
218
|
+
this.sock.removeAllListeners('error');
|
|
219
|
+
this.sock.removeAllListeners('close');
|
|
206
220
|
this.sock.destroy();
|
|
207
221
|
}
|
|
208
222
|
}
|
|
@@ -227,11 +241,11 @@
|
|
|
227
241
|
*/
|
|
228
242
|
constructor(opts = {}) {
|
|
229
243
|
this.url = new URL('ws://localhost');
|
|
230
|
-
this.url.hostname = opts.hostname;
|
|
244
|
+
this.url.hostname = opts.hostname;
|
|
231
245
|
this.url.port = opts.port;
|
|
232
246
|
this.url.pathname = opts.pathname;
|
|
233
247
|
this._reconnectTime = opts.reconnectTime || DEFAULT_RECONNECT_TIME;
|
|
234
|
-
this._keepAlive = opts.keepAlive;
|
|
248
|
+
this._keepAlive = opts.keepAlive || true;
|
|
235
249
|
this.debug = opts.debug || false; // debug info to be logged to console?
|
|
236
250
|
this._firstConn = true; // if the Gateway has managed to connect to a server before
|
|
237
251
|
this._firstReConn = true; // if the Gateway has attempted to reconnect to a server before
|
|
@@ -317,7 +331,7 @@
|
|
|
317
331
|
* @ignore
|
|
318
332
|
* @param {string} s - incoming message string
|
|
319
333
|
*/
|
|
320
|
-
|
|
334
|
+
|
|
321
335
|
/**
|
|
322
336
|
* Add listener for connection events
|
|
323
337
|
* @param {function} listener - a listener callback that is called when the connection is opened/closed
|
|
@@ -361,6 +375,7 @@
|
|
|
361
375
|
|
|
362
376
|
/* global global Buffer */
|
|
363
377
|
|
|
378
|
+
|
|
364
379
|
const DEFAULT_QUEUE_SIZE = 128; // max number of old unreceived messages to store
|
|
365
380
|
|
|
366
381
|
/**
|
|
@@ -688,7 +703,13 @@
|
|
|
688
703
|
_sendEvent(type, val) {
|
|
689
704
|
if (Array.isArray(this.eventListeners[type])) {
|
|
690
705
|
this.eventListeners[type].forEach(l => {
|
|
691
|
-
l && {}.toString.call(l) === '[object Function]'
|
|
706
|
+
if (l && {}.toString.call(l) === '[object Function]'){
|
|
707
|
+
try {
|
|
708
|
+
l(val);
|
|
709
|
+
} catch (error) {
|
|
710
|
+
console.warn('Error in event listener : ' + error);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
692
713
|
});
|
|
693
714
|
}
|
|
694
715
|
}
|
|
@@ -717,18 +738,26 @@
|
|
|
717
738
|
var consumed = false;
|
|
718
739
|
if (Array.isArray(this.eventListeners['message'])){
|
|
719
740
|
for (var i = 0; i < this.eventListeners['message'].length; i++) {
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
741
|
+
try {
|
|
742
|
+
if (this.eventListeners['message'][i](msg)) {
|
|
743
|
+
consumed = true;
|
|
744
|
+
break;
|
|
745
|
+
}
|
|
746
|
+
} catch (error) {
|
|
747
|
+
console.warn('Error in message listener : ' + error);
|
|
723
748
|
}
|
|
724
749
|
}
|
|
725
750
|
}
|
|
726
751
|
// iterate over internal callbacks, until one consumes the message
|
|
727
752
|
for (var key in this.listener){
|
|
728
753
|
// callback returns true if it has consumed the message
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
754
|
+
try {
|
|
755
|
+
if (this.listener[key](msg)) {
|
|
756
|
+
consumed = true;
|
|
757
|
+
break;
|
|
758
|
+
}
|
|
759
|
+
} catch (error) {
|
|
760
|
+
console.warn('Error in listener : ' + error);
|
|
732
761
|
}
|
|
733
762
|
}
|
|
734
763
|
if(!consumed) {
|
|
@@ -816,6 +845,7 @@
|
|
|
816
845
|
this.connector.write('{"alive": true}');
|
|
817
846
|
this._update_watch();
|
|
818
847
|
}
|
|
848
|
+
this._sendEvent('conn', state);
|
|
819
849
|
});
|
|
820
850
|
return conn;
|
|
821
851
|
}
|
|
@@ -829,7 +859,12 @@
|
|
|
829
859
|
} else if (filter.__proto__.name == 'Message' || filter.__proto__.__proto__.name == 'Message') {
|
|
830
860
|
return filter.__clazz__ == msg.__clazz__;
|
|
831
861
|
} else if (typeof filter == 'function') {
|
|
832
|
-
|
|
862
|
+
try {
|
|
863
|
+
return filter(msg);
|
|
864
|
+
}catch(e){
|
|
865
|
+
console.warn('Error in filter : ' + e);
|
|
866
|
+
return false;
|
|
867
|
+
}
|
|
833
868
|
} else {
|
|
834
869
|
return msg instanceof filter;
|
|
835
870
|
}
|
|
@@ -1099,7 +1134,7 @@
|
|
|
1099
1134
|
let timer;
|
|
1100
1135
|
if (timeout > 0){
|
|
1101
1136
|
timer = setTimeout(() => {
|
|
1102
|
-
delete this.listener[lid];
|
|
1137
|
+
this.listener[lid] && delete this.listener[lid];
|
|
1103
1138
|
if (this.debug) console.log('Receive Timeout : ' + filter);
|
|
1104
1139
|
resolve();
|
|
1105
1140
|
}, timeout);
|
|
@@ -1107,7 +1142,7 @@
|
|
|
1107
1142
|
this.listener[lid] = msg => {
|
|
1108
1143
|
if (!this._matchMessage(filter, msg)) return false;
|
|
1109
1144
|
if(timer) clearTimeout(timer);
|
|
1110
|
-
delete this.listener[lid];
|
|
1145
|
+
this.listener[lid] && delete this.listener[lid];
|
|
1111
1146
|
resolve(msg);
|
|
1112
1147
|
return true;
|
|
1113
1148
|
};
|
|
@@ -1706,7 +1741,7 @@
|
|
|
1706
1741
|
|
|
1707
1742
|
constructor(hostname, port, path='') {
|
|
1708
1743
|
return (async () => {
|
|
1709
|
-
this.gw = new
|
|
1744
|
+
this.gw = new Gateway({
|
|
1710
1745
|
hostname : hostname,
|
|
1711
1746
|
port : port,
|
|
1712
1747
|
path : path
|
|
@@ -1966,7 +2001,8 @@
|
|
|
1966
2001
|
|
|
1967
2002
|
exports.AgentID = AgentID;
|
|
1968
2003
|
exports.CachingAgentID = CachingAgentID;
|
|
1969
|
-
exports.
|
|
2004
|
+
exports.CachingGateway = CachingGateway;
|
|
2005
|
+
exports.Gateway = Gateway;
|
|
1970
2006
|
exports.Message = Message;
|
|
1971
2007
|
exports.MessageClass = MessageClass;
|
|
1972
2008
|
exports.Performative = Performative;
|