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