@rootintf/protocol-2phw 0.6.9

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.
@@ -0,0 +1,842 @@
1
+ const validate_Msg2PhW = validate10$1;const pattern0 = new RegExp("0\\.6", "u");function validate10$1(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data["2PhW"] === undefined) && (missing0 = "2PhW")){validate10$1.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data["2PhW"] !== undefined){let data0 = data["2PhW"];{if(typeof data0 === "string"){if(!pattern0.test(data0)){validate10$1.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"#/properties/2PhW/pattern",keyword:"pattern",params:{pattern: "0\\.6"},message:"must match pattern \""+"0\\.6"+"\""}];return false;}}else {validate10$1.errors = [{instancePath:instancePath+"/2PhW",schemaPath:"#/properties/2PhW/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}}}}}else {validate10$1.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10$1.errors = vErrors;return errors === 0;}const validate_MsgOptions = validate11$1;function validate11$1(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.options === undefined) && (missing0 = "options")){validate11$1.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.options !== undefined){let data0 = data.options;if(!(data0 && typeof data0 == "object" && !Array.isArray(data0))){validate11$1.errors = [{instancePath:instancePath+"/options",schemaPath:"#/properties/options/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}else {validate11$1.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11$1.errors = vErrors;return errors === 0;}const validate_Options_delay = validate12$1;function validate12$1(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ttl_msec_incr === undefined) && (missing0 = "ttl_msec_incr")){validate12$1.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ttl_msec !== undefined){if(!(Array.isArray(data.ttl_msec))){validate12$1.errors = [{instancePath:instancePath+"/ttl_msec",schemaPath:"#/properties/ttl_msec/type",keyword:"type",params:{type: "array"},message:"must be array"}];return false;}}}}else {validate12$1.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12$1.errors = vErrors;return errors === 0;}const validate_MsgWarp = validate13;function validate13(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.warp === undefined) && (missing0 = "warp")){validate13.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.warp !== undefined){let data0 = data.warp;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if(((data0.user === undefined) && (missing1 = "user")) || ((data0.hyperport === undefined) && (missing1 = "hyperport"))){validate13.errors = [{instancePath:instancePath+"/warp",schemaPath:"#/properties/warp/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ttl_msec !== undefined){let data1 = data0.ttl_msec;if(!(((typeof data1 == "number") && (!(data1 % 1) && !isNaN(data1))) && (isFinite(data1)))){validate13.errors = [{instancePath:instancePath+"/warp/ttl_msec",schemaPath:"#/properties/warp/properties/ttl_msec/type",keyword:"type",params:{type: "integer"},message:"must be integer"}];return false;}}}}else {validate13.errors = [{instancePath:instancePath+"/warp",schemaPath:"#/properties/warp/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate13.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate13.errors = vErrors;return errors === 0;}const validate_MsgEngage = validate14;function validate14(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.engage === undefined) && (missing0 = "engage")){validate14.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.engage !== undefined){let data0 = data.engage;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if(((data0.user === undefined) && (missing1 = "user")) || ((data0.hyperport === undefined) && (missing1 = "hyperport"))){validate14.errors = [{instancePath:instancePath+"/engage",schemaPath:"#/properties/engage/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}}else {validate14.errors = [{instancePath:instancePath+"/engage",schemaPath:"#/properties/engage/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate14.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate14.errors = vErrors;return errors === 0;}
2
+
3
+ // console.log('OUT', __name({variableName}) );
4
+
5
+ function l_toBigInt_$1(ref, obj, ignore= false) {
6
+ console.assert(ref !== BigInt(0), 'no labels initialized');
7
+ let bigInt = BigInt(0);
8
+ for (const [k,v] of Object.entries(obj)) {
9
+ if ( ( ignore || v ) && ref[k])
10
+ bigInt|= BigInt( ref[k] );
11
+ // console.log('0b'+ bigInt.toString(2) );
12
+ }
13
+ return bigInt;
14
+ }
15
+
16
+ function l_LL_(obj, x) {
17
+ let obj_new= {};
18
+ for (var [k,v] of Object.entries(obj))
19
+ obj_new[k]= v<<x;
20
+ return obj_new;
21
+ }
22
+
23
+ //-------------------------------------------------------------------------------------------------
24
+
25
+ function handler_default_$1( /* ... */ ) {
26
+ // https://stackoverflow.com/questions/18746440/passing-multiple-arguments-to-console-log
27
+ var args = Array.prototype.slice.call(arguments);
28
+ console.log.apply(console, args);
29
+ }
30
+
31
+ //-------------------------------------------------------------------------------------------------
32
+
33
+ class BitLogr$1 {
34
+ constructor() {
35
+ this._handler_log= handler_default_$1;
36
+ this._Bint_labels= BigInt(0);
37
+ this._Bint_toggled= BigInt(0);
38
+
39
+ BitLogr$1.prototype['log']= function (nr_logged, /* ... */ ) {
40
+ // console.log('NOP')
41
+ };
42
+ }
43
+
44
+ set handler(fx) {
45
+ this._handler_log= fx;
46
+ }
47
+
48
+ get labels() { return this._Bint_labels; }
49
+ set labels(obj) {
50
+ this._Bint_labels= obj;
51
+ this._Bint_toggled= BigInt(0);
52
+ }
53
+
54
+ // put= function(label, abbrv) {
55
+ // let name= __name(label);
56
+ // _labels[name]= label[name];
57
+ // console.log(_labels);
58
+ // }
59
+
60
+ get toggled() { return this._Bint_toggled; }
61
+ set toggled(obj) {
62
+ this._Bint_toggled= l_toBigInt_$1(this._Bint_labels, obj);
63
+
64
+ if (this._Bint_toggled === BigInt(0)) {
65
+ console.log('adlkjasdlfk');
66
+ return;
67
+ }
68
+
69
+ BitLogr$1.prototype['log']= function (nr_logged, /* ... */ ) {
70
+ if ( (BigInt(nr_logged) & this._Bint_toggled) === BigInt(0))
71
+ return false;
72
+
73
+ var args = Array.prototype.slice.call(arguments);
74
+ args.shift(); // remove first arg: nr_logged
75
+ this._handler_log.apply(this, args);
76
+
77
+ return true;
78
+ };
79
+ }
80
+
81
+ // log= function (nr_logged, /* ... */ ) {}
82
+ }
83
+
84
+ var jsonMsg_cjs = {};
85
+
86
+ Object.defineProperty(jsonMsg_cjs, '__esModule', { value: true });
87
+
88
+ const validate_Msg = validate10;function validate10(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate10.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate10.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.referer !== undefined){const _errs3 = errors;if(typeof data.referer !== "string"){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs5 = errors;if(typeof data.dt !== "string"){validate10.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs5 === errors;}else {var valid0 = true;}}}}}else {validate10.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10.errors = vErrors;return errors === 0;}const validate_Msg_msg = validate11;function validate11(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate11.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate11.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11.errors = vErrors;return errors === 0;}const validate_MsgAck = validate12;function validate12(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate12.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate12.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate12.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12.errors = vErrors;return errors === 0;}
89
+
90
+ // Unique ID creation requires a high quality random # generator. In the browser we therefore
91
+ // require the crypto API and do not support built-in fallback to lower quality random number
92
+ // generators (like Math.random()).
93
+ let getRandomValues;
94
+ const rnds8 = new Uint8Array(16);
95
+ function rng() {
96
+ // lazy load so that environments that need to polyfill have a chance to do so
97
+ if (!getRandomValues) {
98
+ // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
99
+ getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
100
+
101
+ if (!getRandomValues) {
102
+ throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
103
+ }
104
+ }
105
+
106
+ return getRandomValues(rnds8);
107
+ }
108
+
109
+ /**
110
+ * Convert array of 16 byte values to UUID string format of the form:
111
+ * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
112
+ */
113
+
114
+ const byteToHex = [];
115
+
116
+ for (let i = 0; i < 256; ++i) {
117
+ byteToHex.push((i + 0x100).toString(16).slice(1));
118
+ }
119
+
120
+ function unsafeStringify(arr, offset = 0) {
121
+ // Note: Be careful editing this code! It's been tuned for performance
122
+ // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
123
+ return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
124
+ }
125
+
126
+ const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
127
+ var native = {
128
+ randomUUID
129
+ };
130
+
131
+ function v4(options, buf, offset) {
132
+ if (native.randomUUID && !buf && !options) {
133
+ return native.randomUUID();
134
+ }
135
+
136
+ options = options || {};
137
+ const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
138
+
139
+ rnds[6] = rnds[6] & 0x0f | 0x40;
140
+ rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
141
+
142
+ if (buf) {
143
+ offset = offset || 0;
144
+
145
+ for (let i = 0; i < 16; ++i) {
146
+ buf[offset + i] = rnds[i];
147
+ }
148
+
149
+ return buf;
150
+ }
151
+
152
+ return unsafeStringify(rnds);
153
+ }
154
+
155
+ // console.log('OUT', __name({variableName}) );
156
+
157
+ function l_toBigInt_(ref, obj, ignore= false) {
158
+ console.assert(ref !== BigInt(0), 'no labels initialized');
159
+ let bigInt = BigInt(0);
160
+ for (const [k,v] of Object.entries(obj)) {
161
+ if ( ( ignore || v ) && ref[k])
162
+ bigInt|= BigInt( ref[k] );
163
+ // console.log('0b'+ bigInt.toString(2) );
164
+ }
165
+ return bigInt;
166
+ }
167
+
168
+ //-------------------------------------------------------------------------------------------------
169
+
170
+ function handler_default_( /* ... */ ) {
171
+ // https://stackoverflow.com/questions/18746440/passing-multiple-arguments-to-console-log
172
+ var args = Array.prototype.slice.call(arguments);
173
+ console.log.apply(console, args);
174
+ }
175
+
176
+ //-------------------------------------------------------------------------------------------------
177
+
178
+ class BitLogr {
179
+ constructor() {
180
+ this._handler_log= handler_default_;
181
+ this._Bint_labels= BigInt(0);
182
+ this._Bint_toggled= BigInt(0);
183
+
184
+ BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
185
+ // console.log('NOP')
186
+ };
187
+ }
188
+
189
+ set handler(fx) {
190
+ this._handler_log= fx;
191
+ }
192
+
193
+ get labels() { return this._Bint_labels; }
194
+ set labels(obj) {
195
+ this._Bint_labels= obj;
196
+ this._Bint_toggled= BigInt(0);
197
+ }
198
+
199
+ // put= function(label, abbrv) {
200
+ // let name= __name(label);
201
+ // _labels[name]= label[name];
202
+ // console.log(_labels);
203
+ // }
204
+
205
+ get toggled() { return this._Bint_toggled; }
206
+ set toggled(obj) {
207
+ this._Bint_toggled= l_toBigInt_(this._Bint_labels, obj);
208
+
209
+ if (this._Bint_toggled === BigInt(0)) {
210
+ console.log('adlkjasdlfk');
211
+ return;
212
+ }
213
+
214
+ BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
215
+ if ( (BigInt(nr_logged) & this._Bint_toggled) === BigInt(0))
216
+ return false;
217
+
218
+ var args = Array.prototype.slice.call(arguments);
219
+ args.shift(); // remove first arg: nr_logged
220
+ this._handler_log.apply(this, args);
221
+
222
+ return true;
223
+ };
224
+ }
225
+
226
+ // log= function (nr_logged, /* ... */ ) {}
227
+ }
228
+
229
+ let LOGR_$1= new BitLogr();
230
+
231
+ const l_$1= {
232
+ VALIDATION : 0b1 << 0,
233
+ };
234
+
235
+ // TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
236
+
237
+ // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
238
+ var JSON_Msg = (function() {
239
+ return {
240
+ merge : function(json) {
241
+ const dt_Msg= new Date();
242
+ let json_msg= {
243
+ id : v4(),
244
+ referer : null,
245
+ dt : dt_Msg.toISOString(),
246
+ };
247
+ // https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object
248
+ for (var key of Object.keys(json)) {
249
+ json_msg[key]= json[key];
250
+ }
251
+ return json_msg;
252
+ },
253
+ validate : function(json) {
254
+ const b_valid= validate_Msg(json);
255
+ if (!b_valid)
256
+ LOGR_$1.log(l_$1.VALIDATION, 'JSON_Msg: validate: ', validate_Msg.errors);
257
+ return b_valid;
258
+ }
259
+ // TODO: requires a separate ajv module require, and JTD specific schema
260
+ // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
261
+ // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
262
+ // static parse(json) {
263
+ // return this.#ajv_parse_(json);
264
+ // }
265
+ }
266
+ })();
267
+
268
+ var JSON_Msg_msg = (function() {
269
+ return {
270
+ create_with_msg_merge : function(_msg, json) {
271
+ let json_msg= JSON_Msg.merge(json);
272
+ json_msg._msg= _msg;
273
+ return json_msg;
274
+ },
275
+ validate : function(json) {
276
+ if (! JSON_Msg.validate(json))
277
+ return false;
278
+ const b_valid= validate_Msg_msg(json);
279
+ if (!b_valid)
280
+ LOGR_$1.log(l_$1.VALIDATION, 'JSON_Msg_msg: validate: ', validate_Msg_msg.errors);
281
+ return b_valid;
282
+ }
283
+ }
284
+ })();
285
+
286
+ var JSON_MsgAck = (function() {
287
+ return {
288
+ create_with_msgOk : function(_msg, b_ok= true) {
289
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
290
+ ack : {
291
+ "ok" : b_ok,
292
+ },
293
+ });
294
+ },
295
+ create_with_msgExpired : function(_msg) {
296
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
297
+ ack : {
298
+ "ok" : false,
299
+ "expired" : true
300
+ },
301
+ });
302
+ },
303
+ validate : function(json) {
304
+ if (! JSON_Msg_msg.validate(json))
305
+ return false;
306
+ const b_valid= validate_MsgAck(json);
307
+ if (!b_valid)
308
+ LOGR_$1.log(l_$1.VALIDATION, 'JSON_MsgAck: validate: ', validate_MsgAck.errors);
309
+ return b_valid;
310
+ }
311
+ }
312
+ })();
313
+
314
+ var JSON_Msg_1 = jsonMsg_cjs.JSON_Msg = JSON_Msg;
315
+ var JSON_MsgAck_1 = jsonMsg_cjs.JSON_MsgAck = JSON_MsgAck;
316
+ var JSON_Msg_msg_1 = jsonMsg_cjs.JSON_Msg_msg = JSON_Msg_msg;
317
+
318
+ const k_TTL= 20000;
319
+
320
+ //-------------------------------------------------------------------------------------------------
321
+
322
+ let LOGR_= new BitLogr$1();
323
+
324
+ const l_msgs_ = {
325
+ MSG : 0b1 << 0,
326
+ WARP : 0b1 << 1,
327
+ ENGAGE : 0b1 << 2,
328
+ ACK : 0b1 << 3,
329
+ };
330
+
331
+ const l_= {
332
+ HANDLERS : 0b1 << 0,
333
+ DROPS : 0b1 << 1,
334
+ VALIDATION : 0b1 << 2,
335
+ ... l_LL_(l_msgs_, 8)
336
+ };
337
+ LOGR_.labels= l_;
338
+
339
+ //-------------------------------------------------------------------------------------------------
340
+
341
+ let map_Context_= new Map();
342
+
343
+ function set_ctx(key, ctx) {
344
+ map_Context_.set(key, ctx);
345
+ // console.log(map_Context_);
346
+ return ctx;
347
+ }
348
+
349
+ function get_ctx(key) {
350
+ return map_Context_.get(key);
351
+ }
352
+
353
+ //-------------------------------------------------------------------------------------------------
354
+
355
+ class Config {
356
+ receiver(callback_msgWarp,callback_msgEngage) {
357
+ this._callback_msgWarp= callback_msgWarp;
358
+ this._callback_msgEngage= callback_msgEngage;
359
+ }
360
+ sender(callback_msgAckWarp,callback_msgAckEngage ) {
361
+ this._callback_msgAckWarp= callback_msgAckWarp;
362
+ this._callback_msgAckEngage= callback_msgAckEngage;
363
+ }
364
+ set callback_msgOptions(callback) {
365
+ this._callback_msgOptions= callback;
366
+ }
367
+ set logr(obj_logr) {
368
+ LOGR_.toggled= obj_logr;
369
+ }
370
+ }
371
+
372
+ // var config = (function() {
373
+ // let _options;
374
+
375
+ // return {
376
+ // get callback_Abort() { return _options.callback_Abort; },
377
+
378
+ // set options(obj) {
379
+ // _options= obj;
380
+ // if (_options.logr) // && options_.logr[ __name(l_) ] )
381
+ // LOGR_.toggled= _options.logr
382
+ // }
383
+ // }
384
+ // })();
385
+
386
+ //-------------------------------------------------------------------------------------------------
387
+
388
+ let k_VERSION= "0.6";
389
+
390
+ var JSON_MsgOptions = (function() {
391
+ return {
392
+ create_with_msg_options : function(_msg, options) {
393
+ return JSON_Msg_msg_1.create_with_msg_merge(_msg, {
394
+ options: options,
395
+ "2PhW": k_VERSION
396
+ });
397
+ },
398
+ validate : function(json) {
399
+ if (! JSON_Msg_msg_1.validate(json))
400
+ return false;
401
+ if (! validate_Msg2PhW(json)) {
402
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_Msg2PhW_: ', validate_Msg2PhW.errors);
403
+ return false;
404
+ }
405
+ if (! validate_MsgOptions(json)) {
406
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgOptions: validate_MsgOptions_: ', validate_MsgOptions.errors);
407
+ return false;
408
+ }
409
+ return true;
410
+ }
411
+ }
412
+ })();
413
+
414
+ var JSON_MsgWarp = (function() {
415
+ return {
416
+ create_with_user_hyperport : function(json_user, json_hyperport, nr_ttl_msec) {
417
+ return JSON_Msg_1.merge({
418
+ warp : {
419
+ user : json_user,
420
+ hyperport : json_hyperport,
421
+ ttl_msec : nr_ttl_msec
422
+ },
423
+ "2PhW": k_VERSION
424
+ });
425
+ },
426
+ validate : function(json) {
427
+ if (! JSON_Msg_1.validate(json))
428
+ return false;
429
+ if (! validate_Msg2PhW(json)) {
430
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_Msg2PhW_: ', validate_Msg2PhW.errors);
431
+ return false;
432
+ }
433
+ if (! validate_MsgWarp(json)) {
434
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_MsgWarp_: ', validate_MsgWarp.errors);
435
+ return false;
436
+ }
437
+ return true;
438
+ }
439
+ }
440
+ })();
441
+
442
+ var JSON_MsgEngage = (function() {
443
+ return {
444
+ create_with_MsgWarp : function(json_msgWarp) {
445
+ let json_msgEngage= JSON_Msg_1.merge({
446
+ engage : {
447
+ user : json_msgWarp.warp.user,
448
+ hyperport : json_msgWarp.warp.hyperport,
449
+ },
450
+ "2PhW": k_VERSION
451
+ });
452
+ json_msgEngage["id"]= json_msgWarp.id;
453
+ return json_msgEngage;
454
+ },
455
+ validate : function(json) {
456
+ if (! JSON_Msg_1.validate(json))
457
+ return false;
458
+ if (! validate_Msg2PhW(json)) {
459
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgWarp: validate_Msg2PhW_: ', validate_Msg2PhW.errors);
460
+ return false;
461
+ }
462
+ if (! validate_MsgEngage(json)) {
463
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgEngage: validate_MsgEngage_: ', validate_MsgEngage.errors);
464
+ return false;
465
+ }
466
+ return true;
467
+ }
468
+ }
469
+ })();
470
+
471
+ var JSON_MsgAckWarp = (function() {
472
+ return {
473
+ // createWith_msgAck_msg : function(json_msgAck, _msg) {
474
+ // const json_msgAckWarp= structuredClone(json_msgAck);
475
+ // json_msgAckWarp._msg= _msg;
476
+ // return json_msgAckWarp;
477
+ // }
478
+ create_with_msgExpired : function( json_msgWarp ) {
479
+ const json_msgAck= JSON_MsgAck_1.create_with_msgExpired( json_msgWarp );
480
+ json_msgAck["2PhW"]= k_VERSION;
481
+ return json_msgAck;
482
+ },
483
+ validate : function(json) {
484
+ if (! validate_Msg2PhW(json)) {
485
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAckWarp: validate_Msg2PhW_: ', validate_Msg2PhW.errors);
486
+ return false;
487
+ }
488
+ if (! JSON_MsgAck_1.validate(json))
489
+ return false;
490
+ if (! JSON_MsgWarp.validate(json._msg))
491
+ return false;
492
+ return true;
493
+ }
494
+ }
495
+ })();
496
+
497
+ var JSON_MsgAckEngage = (function() {
498
+ return {
499
+ // createWith_msgAck_msg : function(json_msgAck, _msg) {
500
+ // const json_msgAckEngage= structuredClone(json_msgAck);
501
+ // json_msgAckEngage._msg= _msg;
502
+ // return json_msgAckEngage;
503
+ // }
504
+ validate : function(json) {
505
+ if (! validate_Msg2PhW(json)) {
506
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAckEngage: validate_Msg2PhW_: ', validate_Msg2PhW.errors);
507
+ return false;
508
+ }
509
+ if (! JSON_MsgAck_1.validate(json))
510
+ return false;
511
+ if (! JSON_MsgEngage.validate(json._msg))
512
+ return false;
513
+ return true;
514
+ }
515
+ }
516
+ })();
517
+
518
+
519
+ //-------------------------------------------------------------------------------------------------
520
+ // State
521
+
522
+ class StateState {
523
+ constructor(initState) {
524
+ this.state_current_ = initState;
525
+ this.dt_start_= null;
526
+ this.nr_timeout_id_= -1;
527
+ this.nr_remaining_msec_= 0;
528
+ this.f_callback_= undefined;
529
+ }
530
+ start() {
531
+ this.dt_start_= Date.now();
532
+ console.log(`setTimeout: start ${this.dt_start_}, remaining ${this.nr_remaining_msec_}`);
533
+ this.nr_timeout_id_= setTimeout(this.f_callback_, this.nr_remaining_msec_);
534
+ }
535
+ changeState(newState, nr_ttl_msec, f_callback) {
536
+ if (! this.state_current_.changeState(newState))
537
+ return false;
538
+ this.state_current_ = newState;
539
+
540
+ clearTimeout(this.nr_timeout_id_);
541
+ if (nr_ttl_msec != undefined && f_callback != undefined) {
542
+ this.f_callback_= f_callback;
543
+ this.nr_remaining_msec_= nr_ttl_msec;
544
+ this.start();
545
+ }
546
+ return true;
547
+ }
548
+ delayTimeout(nr_delay_msec) {
549
+ clearTimeout(this.nr_timeout_id_);
550
+ const nr_elapsed_ms= Date.now() - this.dt_start_;
551
+ this.nr_remaining_msec_= Math.max(this.nr_remaining_msec_ - nr_elapsed_ms, 0) + nr_delay_msec;
552
+ this.start();
553
+ }
554
+ inState(stateClass) {
555
+ return this.state_current_ instanceof stateClass;
556
+ }
557
+ }
558
+
559
+ class StateInitialState {
560
+ changeState(newState) {
561
+ return true;
562
+ }
563
+ }
564
+
565
+ // class State<Name> {
566
+ // changeState= function(newState) {
567
+ // return false;
568
+ // }
569
+ // }
570
+
571
+ // if (newState instanceof State<Name>)
572
+ // return true;
573
+
574
+ // if (! <Class>.inState( State<Name> ))
575
+ // if (! <Class>.changeState( new State<Name>() ))
576
+
577
+ //-------------------------------------------------------------------------------------------------
578
+
579
+ class ProtocolStateState extends StateState {
580
+ constructor(initState= new StateInitialState() ) {
581
+ super(initState);
582
+ }
583
+ }
584
+
585
+ class StateWarp {
586
+ changeState(newState) {
587
+ if (newState instanceof StateAckWarp)
588
+ return true;
589
+ return false;
590
+ }
591
+ }
592
+
593
+ class StateAckWarp {
594
+ changeState(newState) {
595
+ if (newState instanceof StateEngage)
596
+ return true;
597
+ return false;
598
+ }
599
+ }
600
+
601
+ class StateEngage {
602
+ changeState(newState) {
603
+ if (newState instanceof StateAckEngage)
604
+ return true;
605
+ return false;
606
+ }
607
+ }
608
+
609
+ class StateAckEngage {
610
+ changeState(newState) {
611
+ return false;
612
+ }
613
+ }
614
+
615
+ //-------------------------------------------------------------------------------------------------
616
+
617
+ function handler_msgOptions_(cfg, msg, json_msgOptions)
618
+ {
619
+ LOGR_.log(l_.HANDLERS, 'handler_msgOptions_: ...');
620
+
621
+ if (! validate_Options_delay(json_msgOptions.options))
622
+ return false;
623
+
624
+ if (JSON_MsgWarp.validate(json_msgOptions._msg)) {
625
+ const json_msgWarp= json_msgOptions._msg;
626
+
627
+ const ctx= get_ctx(json_msgWarp.id);
628
+ if (!ctx )
629
+ return false;
630
+
631
+ json_msgOptions.options.ttl_msec_incr[0];
632
+ const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
633
+
634
+ //TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
635
+
636
+ LOGR_.log(l_.HANDLERS, 'handler_msgOptions_: ', json_msgOptions);
637
+
638
+ ctx.protocol.delayTimeout(ttl_incr);
639
+
640
+ if (cfg._callback_msgOptions)
641
+ return cfg._callback_msgOptions(msg, json_msgOptions);
642
+
643
+ return true;
644
+ }
645
+
646
+ return false;
647
+ }
648
+
649
+ //-------------------------------------------------------------------------------------------------
650
+ // Sender
651
+
652
+ function handler_msgAckWarp_(cfg, msg, json_msgAckWarp)
653
+ {
654
+ // NOTE: json_msgAck here has a string as _msg, instead of JSON.
655
+
656
+ LOGR_.log(l_.HANDLERS, 'handler_msgAckWarp: ...');
657
+
658
+ const json_msgWarp= json_msgAckWarp._msg;
659
+
660
+ const ctx= get_ctx(json_msgWarp.id);
661
+ if (!ctx || !ctx.protocol.changeState( new StateAckWarp() ))
662
+ return false;
663
+
664
+ //TODO: check if multiple warps have been sent out with the same destination
665
+
666
+ LOGR_.log(l_.HANDLERS, 'handler_msgAckWarp: ', json_msgAckWarp);
667
+
668
+ // const json_msgAckWarp= JSON_MsgAckWarp.createWith_msgAck_msg(json_msgAck, json_msgWarp);
669
+
670
+ if (! cfg._callback_msgAckWarp(msg, json_msgAckWarp))
671
+ return false;
672
+
673
+ const b_res= ctx.protocol.changeState( new StateEngage() );
674
+ console.assert(b_res);
675
+
676
+ return true;
677
+ }
678
+
679
+ function handler_msgAckEngage_(cfg, msg, json_msgAckEngage)
680
+ {
681
+ // NOTE: json_msgAck here has a string as _msg, instead of JSON.
682
+
683
+ LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ...');
684
+
685
+ const json_msgEngage= json_msgAckEngage._msg;
686
+
687
+ const ctx= get_ctx(json_msgEngage.id);
688
+ if (!ctx || !ctx.protocol.changeState( new StateAckEngage() ))
689
+ return false;
690
+
691
+ LOGR_.log(l_.HANDLERS, 'handler_msgAckEngage: ', json_msgAckEngage);
692
+
693
+ // const json_msgAckEngage= JSON_MsgAckEngage.createWith_msgAck_msg(json_msgAck, json_msgEngage);
694
+
695
+ if (! cfg._callback_msgAckEngage(msg, json_msgAckEngage))
696
+ return false;
697
+
698
+ return true;
699
+ }
700
+
701
+ //-------------------------------------------------------------------------------------------------
702
+ // Receiver
703
+
704
+ function handler_msgWarp_(cfg, msg, json_msgWarp)
705
+ {
706
+ LOGR_.log(l_.HANDLERS, 'handler_msgWarp: ...');
707
+
708
+ let ctx= get_ctx(json_msgWarp.id);
709
+ if (ctx === undefined)
710
+ ctx= set_ctx(json_msgWarp.id, { protocol: new ProtocolStateState() });
711
+
712
+ if (!ctx || !ctx.protocol.changeState( new StateWarp() ))
713
+ return false;
714
+
715
+ LOGR_.log(l_.HANDLERS, 'handler_msgWarp: ', json_msgWarp);
716
+
717
+ if (! cfg._callback_msgWarp(msg, json_msgWarp))
718
+ return false;
719
+
720
+ const b_res= ctx.protocol.changeState( new StateAckWarp() );
721
+ console.assert(b_res);
722
+
723
+ return true;
724
+ }
725
+
726
+ function handler_msgEngage_(cfg, msg, json_msgEngage)
727
+ {
728
+ LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ...');
729
+
730
+ const ctx= get_ctx(json_msgEngage.id);
731
+ if (!ctx || !ctx.protocol.changeState( new StateEngage() ))
732
+ return false;
733
+
734
+ LOGR_.log(l_.HANDLERS, 'handler_msgEngage: ', json_msgEngage);
735
+
736
+ if (! cfg._callback_msgEngage(msg, json_msgEngage))
737
+ return false;
738
+
739
+ const b_res= ctx.protocol.changeState( new StateAckEngage() );
740
+ console.assert(b_res);
741
+
742
+ return true;
743
+ }
744
+
745
+ //-------------------------------------------------------------------------------------------------
746
+
747
+ class TwoPhW {
748
+ constructor() {
749
+ this._config= new Config();
750
+ }
751
+
752
+ get config() {
753
+ return this._config;
754
+ }
755
+
756
+ // this function need to be removed when a microservice db is used instead of a state machine
757
+ create_MsgWarp_out(str_id, json_user, json_hyperport, nr_ttl_msec)
758
+ {
759
+ if (nr_ttl_msec === undefined)
760
+ nr_ttl_msec= k_TTL;
761
+
762
+ const json_msgWarp= JSON_MsgWarp.create_with_user_hyperport(json_user, json_hyperport, nr_ttl_msec);
763
+ json_msgWarp.id= str_id;
764
+
765
+ const ctx= set_ctx(json_msgWarp.id, { protocol: new ProtocolStateState() });
766
+ const b_res= ctx.protocol.changeState( new StateWarp(), nr_ttl_msec, function() {
767
+ const json_Nack= JSON_MsgAckWarp.create_with_msgExpired( json_msgWarp );
768
+ this._config._callback_msgAckWarp(json_Nack, json_Nack);
769
+ }.bind(this) );
770
+
771
+ console.assert(b_res);
772
+ return json_msgWarp;
773
+ }
774
+
775
+ handler_msg_json(msg, json_msg)
776
+ {
777
+ LOGR_.log(l_.HANDLERS, 'handler_msg_json: ', json_msg);
778
+
779
+ // ADR: I could parse json_Msg_msg first and then MsgAck or MsgOptions, but
780
+ // doing so means super classes get parsed multiple times for no reason
781
+
782
+ if (JSON_MsgOptions.validate(json_msg) && handler_msgOptions_(this._config, msg, json_msg))
783
+ return true;
784
+
785
+ if (JSON_MsgWarp.validate(json_msg) && handler_msgWarp_(this._config, msg, json_msg))
786
+ return true;
787
+
788
+ if (JSON_MsgEngage.validate(json_msg) && handler_msgEngage_(this._config, msg, json_msg))
789
+ return true;
790
+
791
+ if (JSON_MsgAckWarp.validate(json_msg) && handler_msgAckWarp_(this._config, msg, json_msg))
792
+ return true;
793
+
794
+ if (JSON_MsgAckEngage.validate(json_msg) && handler_msgAckEngage_(this._config, msg, json_msg))
795
+ return true;
796
+
797
+ // https://attacomsian.com/blog/javascript-iterate-objects
798
+ // Object.keys(json_msg).forEach((key, index) => {
799
+ // if (key == 'dt')
800
+ // return;
801
+ // if (key == 'referer')
802
+ // return;
803
+ // console.log(`${key}: ${json_msg[key]}`);
804
+ // });
805
+
806
+ return false;
807
+ }
808
+
809
+ handler_msg_str(msg, str_msg)
810
+ {
811
+ LOGR_.log(l_.HANDLERS, 'handler_msg_str: ['+ str_msg +']');
812
+
813
+ // ... test strings here
814
+
815
+ let json_msg;
816
+ try {
817
+ json_msg= JSON.parse(str_msg);
818
+ }
819
+ catch (err) {
820
+ return false;
821
+ }
822
+
823
+ return this.handler_msg_json(msg, json_msg);
824
+ }
825
+
826
+ handler_msg(msg)
827
+ {
828
+ LOGR_.log(l_.HANDLERS, 'handler_msg: ', msg);
829
+
830
+ if (typeof (msg) === 'string' && this.handler_msg_str(msg, msg))
831
+ return true;
832
+
833
+ if (typeof (msg) === 'object' && this.handler_msg_json(msg, msg))
834
+ return true;
835
+
836
+ LOGR_.log(l_.DROPS, "handler_msg: DROP! ", msg);
837
+ return false
838
+ }
839
+
840
+ }
841
+
842
+ export { JSON_MsgAckEngage, JSON_MsgAckWarp, JSON_MsgEngage, JSON_MsgOptions, JSON_MsgWarp, TwoPhW, l_ as l };