@rootintf/protocol-2phw 0.6.11 → 0.7.5-api-0.7

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