@rootintf/protocol-2phw 2.3.7-api.2 → 3.1.2-api.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +427 -1
- package/dist/twoPhW.cjs.js +379 -1218
- package/dist/twoPhW.d.ts +83 -49
- package/dist/twoPhW.es.mjs +365 -1210
- package/package.json +27 -19
- package/src/twoPhW.ts +865 -833
- package/dist/ajv-gen-validate.d.cts +0 -1
- package/dist/validate-esm.d.mts +0 -48
- package/schema/schema_2PhW.json +0 -61
- package/schema/schema_MsgOptions.json +0 -12
- package/schema/schema_Options_delay.json +0 -17
- package/src/ajv-gen-validate.cjs +0 -40
- package/src/validate-esm.mjs +0 -1
package/dist/twoPhW.cjs.js
CHANGED
|
@@ -1,937 +1,63 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var bitlogr = require('@knev/bitlogr');
|
|
4
|
+
var jsonMsg = require('@rootintf/json-msg');
|
|
3
5
|
var eventlog = require('@rootintf/eventlog');
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} {
|
|
11
|
-
if (data && typeof data == "object" && !Array.isArray(data)) {
|
|
12
|
-
let missing0;
|
|
13
|
-
if ((data.warp === undefined) && (missing0 = "warp")) {
|
|
14
|
-
validate19.errors = [{ instancePath, schemaPath: "#/required", keyword: "required", params: { missingProperty: missing0 }, message: "must have required property '" + missing0 + "'" }];
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
if (data.warp !== undefined) {
|
|
19
|
-
let data0 = data.warp;
|
|
20
|
-
const _errs1 = errors;
|
|
21
|
-
{
|
|
22
|
-
if (data0 && typeof data0 == "object" && !Array.isArray(data0)) {
|
|
23
|
-
let missing1;
|
|
24
|
-
if (((((data0.id === undefined) && (missing1 = "id")) || ((data0.user === undefined) && (missing1 = "user"))) || ((data0.hyperport === undefined) && (missing1 = "hyperport"))) || ((data0.ttl_msec === undefined) && (missing1 = "ttl_msec"))) {
|
|
25
|
-
validate19.errors = [{ instancePath: instancePath + "/warp", schemaPath: "#/properties/warp/required", keyword: "required", params: { missingProperty: missing1 }, message: "must have required property '" + missing1 + "'" }];
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
if (data0.id !== undefined) {
|
|
30
|
-
const _errs3 = errors;
|
|
31
|
-
if (typeof data0.id !== "string") {
|
|
32
|
-
validate19.errors = [{ instancePath: instancePath + "/warp/id", schemaPath: "definitions#/definitions/warp_id/type", keyword: "type", params: { type: "string" }, message: "must be string" }];
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
var valid1 = _errs3 === errors;
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
var valid1 = true;
|
|
39
|
-
}
|
|
40
|
-
if (valid1) {
|
|
41
|
-
if (data0.ttl_msec !== undefined) {
|
|
42
|
-
let data2 = data0.ttl_msec;
|
|
43
|
-
const _errs6 = errors;
|
|
44
|
-
if (!(((typeof data2 == "number") && (!(data2 % 1) && !isNaN(data2))) && (isFinite(data2)))) {
|
|
45
|
-
validate19.errors = [{ instancePath: instancePath + "/warp/ttl_msec", schemaPath: "#/properties/warp/properties/ttl_msec/type", keyword: "type", params: { type: "integer" }, message: "must be integer" }];
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
var valid1 = _errs6 === errors;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
var valid1 = true;
|
|
52
|
-
}
|
|
53
|
-
if (valid1) {
|
|
54
|
-
if (data0.instantaneous !== undefined) {
|
|
55
|
-
const _errs8 = errors;
|
|
56
|
-
if (typeof data0.instantaneous !== "boolean") {
|
|
57
|
-
validate19.errors = [{ instancePath: instancePath + "/warp/instantaneous", schemaPath: "#/properties/warp/properties/instantaneous/type", keyword: "type", params: { type: "boolean" }, message: "must be boolean" }];
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
var valid1 = _errs8 === errors;
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
var valid1 = true;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
validate19.errors = [{ instancePath: instancePath + "/warp", schemaPath: "#/properties/warp/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
var valid0 = _errs1 === errors;
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
var valid0 = true;
|
|
78
|
-
}
|
|
79
|
-
if (valid0) {
|
|
80
|
-
if (data["2PhW"] !== undefined) {
|
|
81
|
-
const _errs10 = errors;
|
|
82
|
-
if ("2" !== data["2PhW"]) {
|
|
83
|
-
validate19.errors = [{ instancePath: instancePath + "/2PhW", schemaPath: "definitions#/definitions/2PhW/const", keyword: "const", params: { allowedValue: "2" }, message: "must be equal to constant" }];
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
var valid0 = _errs10 === errors;
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
var valid0 = true;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
validate19.errors = [{ instancePath, schemaPath: "#/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
} validate19.errors = vErrors; return errors === 0; }
|
|
99
|
-
validate19.evaluated = { "props": { "warp": true, "2PhW": true }, "dynamicProps": false, "dynamicItems": false };
|
|
100
|
-
const schema_MsgEngage = validate20;
|
|
101
|
-
function validate20(data, { instancePath = "", parentData, parentDataProperty, rootData = data, dynamicAnchors = {} } = {}) { let vErrors = null; let errors = 0; const evaluated0 = validate20.evaluated; if (evaluated0.dynamicProps) {
|
|
102
|
-
evaluated0.props = undefined;
|
|
103
|
-
} if (evaluated0.dynamicItems) {
|
|
104
|
-
evaluated0.items = undefined;
|
|
105
|
-
} {
|
|
106
|
-
if (data && typeof data == "object" && !Array.isArray(data)) {
|
|
107
|
-
let missing0;
|
|
108
|
-
if ((data.engage === undefined) && (missing0 = "engage")) {
|
|
109
|
-
validate20.errors = [{ instancePath, schemaPath: "#/required", keyword: "required", params: { missingProperty: missing0 }, message: "must have required property '" + missing0 + "'" }];
|
|
110
|
-
return false;
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
if (data.engage !== undefined) {
|
|
114
|
-
let data0 = data.engage;
|
|
115
|
-
const _errs1 = errors;
|
|
116
|
-
{
|
|
117
|
-
if (data0 && typeof data0 == "object" && !Array.isArray(data0)) {
|
|
118
|
-
let missing1;
|
|
119
|
-
if (((data0.ok === undefined) && (missing1 = "ok")) || ((data0.rtt_msec === undefined) && (missing1 = "rtt_msec"))) {
|
|
120
|
-
validate20.errors = [{ instancePath: instancePath + "/engage", schemaPath: "#/properties/engage/required", keyword: "required", params: { missingProperty: missing1 }, message: "must have required property '" + missing1 + "'" }];
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
if (data0.ok !== undefined) {
|
|
125
|
-
const _errs3 = errors;
|
|
126
|
-
if (typeof data0.ok !== "boolean") {
|
|
127
|
-
validate20.errors = [{ instancePath: instancePath + "/engage/ok", schemaPath: "#/properties/engage/properties/ok/type", keyword: "type", params: { type: "boolean" }, message: "must be boolean" }];
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
var valid1 = _errs3 === errors;
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
var valid1 = true;
|
|
134
|
-
}
|
|
135
|
-
if (valid1) {
|
|
136
|
-
if (data0.rtt_msec !== undefined) {
|
|
137
|
-
let data2 = data0.rtt_msec;
|
|
138
|
-
const _errs5 = errors;
|
|
139
|
-
if (!(((typeof data2 == "number") && (!(data2 % 1) && !isNaN(data2))) && (isFinite(data2)))) {
|
|
140
|
-
validate20.errors = [{ instancePath: instancePath + "/engage/rtt_msec", schemaPath: "#/properties/engage/properties/rtt_msec/type", keyword: "type", params: { type: "integer" }, message: "must be integer" }];
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
var valid1 = _errs5 === errors;
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
var valid1 = true;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
validate20.errors = [{ instancePath: instancePath + "/engage", schemaPath: "#/properties/engage/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
var valid0 = _errs1 === errors;
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
var valid0 = true;
|
|
160
|
-
}
|
|
161
|
-
if (valid0) {
|
|
162
|
-
if (data["2PhW"] !== undefined) {
|
|
163
|
-
const _errs7 = errors;
|
|
164
|
-
if ("2" !== data["2PhW"]) {
|
|
165
|
-
validate20.errors = [{ instancePath: instancePath + "/2PhW", schemaPath: "definitions#/definitions/2PhW/const", keyword: "const", params: { allowedValue: "2" }, message: "must be equal to constant" }];
|
|
166
|
-
return false;
|
|
167
|
-
}
|
|
168
|
-
var valid0 = _errs7 === errors;
|
|
169
|
-
}
|
|
170
|
-
else {
|
|
171
|
-
var valid0 = true;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
validate20.errors = [{ instancePath, schemaPath: "#/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
|
|
178
|
-
return false;
|
|
179
|
-
}
|
|
180
|
-
} validate20.errors = vErrors; return errors === 0; }
|
|
181
|
-
validate20.evaluated = { "props": { "engage": true, "2PhW": true }, "dynamicProps": false, "dynamicItems": false };
|
|
182
|
-
|
|
183
|
-
// https://stackoverflow.com/questions/4602141/variable-name-as-a-string-in-javascript
|
|
184
|
-
// console.log('OUT', __name({variableName}) );
|
|
185
|
-
//-------------------------------------------------------------------------------------------------
|
|
186
|
-
function l_length_(obj_labels) {
|
|
187
|
-
if (!obj_labels || typeof obj_labels !== 'object')
|
|
188
|
-
throw new Error('obj_labels must be an object');
|
|
189
|
-
const obj_1label = Object.values(obj_labels);
|
|
190
|
-
if (!obj_1label.every(v => typeof v === 'number' && Number.isFinite(v)))
|
|
191
|
-
throw new Error('All values must be finite numbers');
|
|
192
|
-
const labels = Object.values(obj_labels);
|
|
193
|
-
if (labels.length === 0)
|
|
194
|
-
return 1; // Empty object case, start at 1
|
|
195
|
-
const value_max = Math.max(...labels);
|
|
196
|
-
if (value_max <= 0)
|
|
197
|
-
return 1;
|
|
198
|
-
const bit_highest = Math.floor(Math.log2(value_max));
|
|
199
|
-
return 1 << (bit_highest + 1);
|
|
200
|
-
}
|
|
201
|
-
function l_array_(arr_labels, start = 1) {
|
|
202
|
-
if (!Array.isArray(arr_labels))
|
|
203
|
-
throw new Error('arr_labels must be an array');
|
|
204
|
-
if (!Number.isSafeInteger(start) || start < 0)
|
|
205
|
-
throw new Error('start must be a safe, non-negative integer');
|
|
206
|
-
return Object.freeze(arr_labels.reduce((acc, key, index) => {
|
|
207
|
-
acc[key] = start << index;
|
|
208
|
-
return acc;
|
|
209
|
-
}, {}));
|
|
210
|
-
}
|
|
211
|
-
function l_concat_(obj_labels, arg) {
|
|
212
|
-
if (!obj_labels || typeof obj_labels !== 'object')
|
|
213
|
-
throw new Error('obj_labels must be an object');
|
|
214
|
-
const obj_1label = Object.values(obj_labels);
|
|
215
|
-
if (!obj_1label.every(v => typeof v === 'number' && Number.isFinite(v)))
|
|
216
|
-
throw new Error('All values must be finite numbers');
|
|
217
|
-
if (!arg || (typeof arg !== 'object' && !Array.isArray(arg)))
|
|
218
|
-
throw new Error('arg must be an object or array');
|
|
219
|
-
if (Array.isArray(arg)) {
|
|
220
|
-
const len = l_length_(obj_labels);
|
|
221
|
-
const arr_labels_new = l_array_(arg, len);
|
|
222
|
-
return l_concat_(obj_labels, arr_labels_new);
|
|
223
|
-
}
|
|
224
|
-
const next_pos = l_length_(obj_labels);
|
|
225
|
-
const arg_entries = Object.entries(arg);
|
|
226
|
-
const result = Object.create(null);
|
|
227
|
-
Object.entries(obj_labels).forEach(([k, v]) => {
|
|
228
|
-
if (k !== '__proto__' && k !== 'constructor')
|
|
229
|
-
result[k] = v;
|
|
230
|
-
});
|
|
231
|
-
let min_arg = Infinity;
|
|
232
|
-
for (const [, value] of arg_entries) {
|
|
233
|
-
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
234
|
-
continue; // Skip non-numeric
|
|
235
|
-
if (value > 0 && value < min_arg)
|
|
236
|
-
min_arg = value;
|
|
237
|
-
}
|
|
238
|
-
// Shift only if min_arg is less than next_pos
|
|
239
|
-
const shift = min_arg === Infinity || min_arg >= next_pos
|
|
240
|
-
? 0
|
|
241
|
-
: Math.floor(Math.log2(next_pos / min_arg));
|
|
242
|
-
for (const [key, value] of arg_entries) {
|
|
243
|
-
if (!(key in result)) {
|
|
244
|
-
result[key] = value === 0 ? 0 : value << shift;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
return Object.freeze(result);
|
|
248
|
-
}
|
|
249
|
-
function l_merge_(obj_labels1, obj_labels2) {
|
|
250
|
-
if (!obj_labels1 || typeof obj_labels1 !== 'object')
|
|
251
|
-
throw new Error('obj_labels must be an object');
|
|
252
|
-
const obj_1label1 = Object.values(obj_labels1);
|
|
253
|
-
if (!obj_1label1.every(v => typeof v === 'number' && Number.isFinite(v)))
|
|
254
|
-
throw new Error('All values must be finite numbers');
|
|
255
|
-
if (!obj_labels2 || typeof obj_labels2 !== 'object')
|
|
256
|
-
throw new Error('obj_labels must be an object');
|
|
257
|
-
const obj_1label2 = Object.values(obj_labels2);
|
|
258
|
-
if (!obj_1label2.every(v => typeof v === 'number' && Number.isFinite(v)))
|
|
259
|
-
throw new Error('All values must be finite numbers');
|
|
260
|
-
const result = Object.create(null);
|
|
261
|
-
Object.entries(obj_labels1).forEach(([k, v]) => {
|
|
262
|
-
if (k !== '__proto__' && k !== 'constructor')
|
|
263
|
-
result[k] = v;
|
|
264
|
-
});
|
|
265
|
-
const set_values = new Set(Object.values(obj_labels1)); // Track all used bit values
|
|
266
|
-
// Find the highest bit position to start shifting from if needed
|
|
267
|
-
const value_highest = Math.max(0, ...Array.from(set_values));
|
|
268
|
-
let next_shift = value_highest ? Math.floor(Math.log2(value_highest)) + 1 : 0;
|
|
269
|
-
// Process second set
|
|
270
|
-
for (const [key, value] of Object.entries(obj_labels2)) {
|
|
271
|
-
if (key in result) {
|
|
272
|
-
// Same key: Values must match
|
|
273
|
-
if (result[key] !== value) {
|
|
274
|
-
throw new Error(`Key '${key}' has conflicting values: ${result[key]} (obj_labels1) vs ${value} (obj_labels2)`);
|
|
275
|
-
}
|
|
276
|
-
// No action needed if values match, already in result
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
let maxIterations = 1000;
|
|
280
|
-
// New key: Add if value is unique, otherwise shift
|
|
281
|
-
let value_new = value;
|
|
282
|
-
while (set_values.has(value_new) && maxIterations--) {
|
|
283
|
-
value_new = 1 << next_shift++;
|
|
284
|
-
}
|
|
285
|
-
if (maxIterations <= 0)
|
|
286
|
-
throw new Error('Too many collisions in l_merge_');
|
|
287
|
-
result[key] = value_new;
|
|
288
|
-
set_values.add(value_new);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
return Object.freeze(result);
|
|
292
|
-
}
|
|
293
|
-
function l_LL_(obj, x) {
|
|
294
|
-
if (!obj || typeof obj !== 'object')
|
|
295
|
-
throw new Error('obj must be an object');
|
|
296
|
-
if (!Number.isSafeInteger(x) || x < 0)
|
|
297
|
-
throw new Error('Shift value must be a safe, non-negative integer');
|
|
298
|
-
const obj_new = {};
|
|
299
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
300
|
-
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
301
|
-
continue; // Skip non-numeric
|
|
302
|
-
obj_new[k] = v << x;
|
|
303
|
-
}
|
|
304
|
-
return Object.freeze(obj_new);
|
|
305
|
-
}
|
|
306
|
-
function l_RR_(obj, x) {
|
|
307
|
-
if (!obj || typeof obj !== 'object')
|
|
308
|
-
throw new Error('obj must be an object');
|
|
309
|
-
if (!Number.isSafeInteger(x) || x < 0)
|
|
310
|
-
throw new Error('Shift value must be a safe, non-negative integer');
|
|
311
|
-
const obj_new = {};
|
|
312
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
313
|
-
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
314
|
-
continue; // Skip non-numeric
|
|
315
|
-
obj_new[k] = v >> x;
|
|
316
|
-
}
|
|
317
|
-
return Object.freeze(obj_new);
|
|
318
|
-
}
|
|
319
|
-
function l_assert_(actual, required) {
|
|
320
|
-
if (!actual || typeof actual !== 'object')
|
|
321
|
-
return false;
|
|
322
|
-
if (!required || typeof required !== 'object')
|
|
323
|
-
return false;
|
|
324
|
-
const actualEntries = Object.entries(actual);
|
|
325
|
-
const requiredEntries = Object.entries(required);
|
|
326
|
-
// if (actualEntries.length === 0 && expectedEntries.length > 0)
|
|
327
|
-
// return false;
|
|
328
|
-
const actualValues = new Set();
|
|
329
|
-
for (const [, v] of actualEntries) {
|
|
330
|
-
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
331
|
-
return false;
|
|
332
|
-
actualValues.add(v);
|
|
333
|
-
}
|
|
334
|
-
const usedRequiredValues = new Set();
|
|
335
|
-
for (const [k, v] of requiredEntries) {
|
|
336
|
-
if (typeof v !== 'number' || !Number.isFinite(v))
|
|
337
|
-
return false;
|
|
338
|
-
if (k in actual) {
|
|
339
|
-
if (actual[k] !== v)
|
|
340
|
-
return false;
|
|
341
|
-
}
|
|
342
|
-
else {
|
|
343
|
-
if (!actualValues.has(v))
|
|
344
|
-
return false;
|
|
345
|
-
}
|
|
346
|
-
if (usedRequiredValues.has(v))
|
|
347
|
-
return false;
|
|
348
|
-
usedRequiredValues.add(v);
|
|
349
|
-
}
|
|
350
|
-
return true;
|
|
351
|
-
}
|
|
352
|
-
//-------------------------------------------------------------------------------------------------
|
|
353
|
-
function handler_default_( /* ... */) {
|
|
354
|
-
// https://stackoverflow.com/questions/18746440/passing-multiple-arguments-to-console-log
|
|
355
|
-
var args = Array.prototype.slice.call(arguments);
|
|
356
|
-
console.log.apply(console, args);
|
|
357
|
-
}
|
|
358
|
-
function l_toBigInt_(obj_labels, obj, ignore = false) {
|
|
359
|
-
if (!obj_labels || typeof obj_labels !== 'object')
|
|
360
|
-
throw new Error('obj_labels must be an object');
|
|
361
|
-
if (!obj || typeof obj !== 'object')
|
|
362
|
-
throw new Error('obj must be an object');
|
|
363
|
-
let bigint_l = BigInt(0);
|
|
364
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
365
|
-
if ((ignore || v) && obj_labels[k] !== undefined && typeof obj_labels[k] === 'number')
|
|
366
|
-
bigint_l |= BigInt(obj_labels[k]);
|
|
367
|
-
// console.log('0b'+ bigInt.toString(2) );
|
|
368
|
-
}
|
|
369
|
-
return bigint_l;
|
|
370
|
-
}
|
|
371
|
-
function lRef(initial) {
|
|
372
|
-
if (arguments.length === 0 || initial === undefined) {
|
|
373
|
-
return undefined;
|
|
374
|
-
}
|
|
375
|
-
let value = initial;
|
|
376
|
-
return {
|
|
377
|
-
get: () => value,
|
|
378
|
-
set: (newVal) => { value = newVal; }
|
|
379
|
-
};
|
|
380
|
-
}
|
|
381
|
-
/*
|
|
382
|
-
const l_ = {
|
|
383
|
-
get VALIDATION() { return logr_.lref.get().VALIDATION; }
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
function createBitFlags(ref) {
|
|
387
|
-
// Create a proxy so that any property access computes the current bit
|
|
388
|
-
return new Proxy({}, {
|
|
389
|
-
get(target, prop, receiver) {
|
|
390
|
-
const positions = ref.get(); // get current { VALIDATION: n, ... }
|
|
391
|
-
const position = positions[prop]; // e.g., positions['VALIDATION']
|
|
392
|
-
|
|
393
|
-
if (position === undefined) {
|
|
394
|
-
// Optional: warn or return 0 for unknown keys
|
|
395
|
-
console.warn(`Unknown bitflag key: ${String(prop)}`);
|
|
396
|
-
return 0;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
return 0b1 << position; // or 1 << position
|
|
400
|
-
},
|
|
401
|
-
|
|
402
|
-
// Optional: make Object.keys(l_) show the actual keys
|
|
403
|
-
ownKeys(target) {
|
|
404
|
-
return Object.keys(ref.get());
|
|
405
|
-
},
|
|
406
|
-
|
|
407
|
-
getOwnPropertyDescriptor(target, prop) {
|
|
408
|
-
return {
|
|
409
|
-
enumerable: true,
|
|
410
|
-
configurable: true,
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
type BitPositions = Record<string, number>;
|
|
417
|
-
|
|
418
|
-
function createBitFlags<T extends BitPositions>(ref: { get: () => T }) {
|
|
419
|
-
return new Proxy({} as { [K in keyof T]: number }, {
|
|
420
|
-
get(target, prop: string | symbol) {
|
|
421
|
-
if (typeof prop !== 'string') return undefined;
|
|
422
|
-
const positions = ref.get();
|
|
423
|
-
const position = positions[prop as keyof T];
|
|
424
|
-
if (position === undefined) return 0;
|
|
425
|
-
return 1 << position;
|
|
426
|
-
},
|
|
427
|
-
ownKeys() {
|
|
428
|
-
return Object.keys(ref.get());
|
|
429
|
-
},
|
|
430
|
-
getOwnPropertyDescriptor() {
|
|
431
|
-
return { enumerable: true, configurable: true };
|
|
432
|
-
}
|
|
433
|
-
});
|
|
434
|
-
}
|
|
435
|
-
*/
|
|
436
|
-
function create_Referenced_l_(ref) {
|
|
437
|
-
return new Proxy({}, {
|
|
438
|
-
get(target, prop) {
|
|
439
|
-
if (typeof prop !== 'string')
|
|
440
|
-
return undefined;
|
|
441
|
-
// if (prop === 'get') {
|
|
442
|
-
// return () => {
|
|
443
|
-
// const positions = ref.get();
|
|
444
|
-
// const result: Partial<Record<keyof T, number>> = {};
|
|
445
|
-
// for (const key in positions) {
|
|
446
|
-
// result[key as keyof T] = positions[key];
|
|
447
|
-
// }
|
|
448
|
-
// return result as Record<keyof T, number>;
|
|
449
|
-
// };
|
|
450
|
-
// }
|
|
451
|
-
if (prop === 'get')
|
|
452
|
-
return () => ref.get();
|
|
453
|
-
const positions = ref.get();
|
|
454
|
-
const value = positions[prop];
|
|
455
|
-
if (value === undefined)
|
|
456
|
-
return 0;
|
|
457
|
-
return value;
|
|
458
|
-
},
|
|
459
|
-
ownKeys() {
|
|
460
|
-
return Object.keys(ref.get());
|
|
461
|
-
},
|
|
462
|
-
getOwnPropertyDescriptor() {
|
|
463
|
-
return { enumerable: true, configurable: true };
|
|
464
|
-
}
|
|
465
|
-
});
|
|
466
|
-
}
|
|
467
|
-
const LOGR = (function () {
|
|
468
|
-
let _instance; // Private variable to hold the single instance
|
|
469
|
-
// Module-level state would work - but only when the module is loaded once.
|
|
470
|
-
// Your bundler is currently bundling @knev/bitlogr into your distribution file,
|
|
471
|
-
// creating a second copy. The Global Symbol approach would work around this,
|
|
472
|
-
// but it's treating the symptom, not the cause.
|
|
473
|
-
const GLOBAL_KEY = Symbol.for('@knev/bitlogr/LOGR');
|
|
474
|
-
// The real issue is your build configuration bundling dependencies that should remain external.
|
|
475
|
-
// rollup.config.mjs: external: ['@knev/bitlogr', 'uuid'], // Don't bundle these
|
|
476
|
-
function _create_instance() {
|
|
477
|
-
const _id = Math.random();
|
|
478
|
-
if (globalThis.LOGR_ENABLED ?? true)
|
|
479
|
-
console.log('creating LOGR instance:', _id);
|
|
480
|
-
// Private state (replacing constructor properties)
|
|
481
|
-
let _Bint_toggled = BigInt(0);
|
|
482
|
-
let _handler_log = handler_default_;
|
|
483
|
-
function _log_fxn(nr_logged, argsFn /* args */) {
|
|
484
|
-
// console.log('_log_fxn: ', BigInt(nr_logged), _Bint_toggled, (BigInt(nr_logged) & _Bint_toggled));
|
|
485
|
-
if ((BigInt(nr_logged) & _Bint_toggled) === BigInt(0))
|
|
486
|
-
return;
|
|
487
|
-
const args = argsFn();
|
|
488
|
-
_handler_log.apply(this, args);
|
|
489
|
-
}
|
|
490
|
-
return {
|
|
491
|
-
_id, // for testing
|
|
492
|
-
get handler() { return _handler_log; },
|
|
493
|
-
set handler(fx) {
|
|
494
|
-
_handler_log = fx;
|
|
495
|
-
},
|
|
496
|
-
get toggled() { return _Bint_toggled; },
|
|
497
|
-
// toggle(obj_labels, obj_toggled) {
|
|
498
|
-
// _Bint_toggled= l_toBigInt_(obj_labels, obj_toggled);
|
|
499
|
-
// },
|
|
500
|
-
toggle(labels, obj_toggled) {
|
|
501
|
-
const obj_labels = typeof labels?.get === 'function'
|
|
502
|
-
? labels.get()
|
|
503
|
-
: labels;
|
|
504
|
-
// console.log('obj_labels', obj_labels)
|
|
505
|
-
_Bint_toggled = l_toBigInt_(obj_labels, obj_toggled);
|
|
506
|
-
},
|
|
507
|
-
// Core internal log function (exposed only to created loggers)
|
|
508
|
-
_log_fxn,
|
|
509
|
-
create(options = {}) {
|
|
510
|
-
// This constant will be replaced at build time
|
|
511
|
-
if (!(globalThis.LOGR_ENABLED ?? true)) {
|
|
512
|
-
return {
|
|
513
|
-
_obj_labels: undefined, // optional: keep shape compatible if needed
|
|
514
|
-
log: () => { }, // does nothing
|
|
515
|
-
raw: () => { }, // does nothing
|
|
516
|
-
};
|
|
517
|
-
}
|
|
518
|
-
const _logger = {
|
|
519
|
-
// _lref_labels: (options.arr_labels === undefined) ? undefined : lRef( l_array_(options.arr_labels) ),
|
|
520
|
-
_lref_labels: (options.labels === undefined)
|
|
521
|
-
? undefined
|
|
522
|
-
: lRef(options.labels),
|
|
523
|
-
get l() {
|
|
524
|
-
// Always create a fresh proxy pointing to the current labels
|
|
525
|
-
return create_Referenced_l_({
|
|
526
|
-
get: () => this._lref_labels?.get() || {}
|
|
527
|
-
});
|
|
528
|
-
},
|
|
529
|
-
get lref() { return this._lref_labels; },
|
|
530
|
-
set lref(lref_labels_new) {
|
|
531
|
-
this._lref_labels = lref_labels_new;
|
|
532
|
-
},
|
|
533
|
-
log(nr_logged, argsFn) {
|
|
534
|
-
// This constant will be replaced at build time
|
|
535
|
-
if (!(globalThis.LOGR_ENABLED ?? true))
|
|
536
|
-
return;
|
|
537
|
-
_log_fxn.call(this, nr_logged, argsFn);
|
|
538
|
-
},
|
|
539
|
-
// Optional shorthand for common cases
|
|
540
|
-
raw(...args) {
|
|
541
|
-
_handler_log.apply(this, args);
|
|
542
|
-
}
|
|
543
|
-
};
|
|
544
|
-
return _logger;
|
|
545
|
-
},
|
|
546
|
-
};
|
|
547
|
-
}
|
|
548
|
-
// Public interface
|
|
549
|
-
return {
|
|
550
|
-
get_instance() {
|
|
551
|
-
if (!(globalThis.LOGR_USE_GLOBAL_KEY ?? true)) {
|
|
552
|
-
if (!_instance)
|
|
553
|
-
_instance = _create_instance(); // Lazy initialization
|
|
554
|
-
return _instance;
|
|
555
|
-
}
|
|
556
|
-
if (!globalThis[GLOBAL_KEY])
|
|
557
|
-
globalThis[GLOBAL_KEY] = _create_instance();
|
|
558
|
-
return globalThis[GLOBAL_KEY];
|
|
559
|
-
},
|
|
560
|
-
// For testing only - reset the singleton
|
|
561
|
-
_reset_for_testing() {
|
|
562
|
-
delete globalThis[GLOBAL_KEY];
|
|
563
|
-
}
|
|
564
|
-
};
|
|
565
|
-
})();
|
|
566
|
-
|
|
567
|
-
var logr_es = /*#__PURE__*/Object.freeze({
|
|
568
|
-
__proto__: null,
|
|
569
|
-
LOGR: LOGR,
|
|
570
|
-
_create_Referenced_l: create_Referenced_l_,
|
|
571
|
-
lRef: lRef,
|
|
572
|
-
l_LL: l_LL_,
|
|
573
|
-
l_RR: l_RR_,
|
|
574
|
-
l_array: l_array_,
|
|
575
|
-
l_assert: l_assert_,
|
|
576
|
-
l_concat: l_concat_,
|
|
577
|
-
l_length: l_length_,
|
|
578
|
-
l_merge: l_merge_
|
|
579
|
-
});
|
|
580
|
-
|
|
581
|
-
function getAugmentedNamespace(n) {
|
|
582
|
-
if (Object.prototype.hasOwnProperty.call(n, '__esModule')) return n;
|
|
583
|
-
var f = n.default;
|
|
584
|
-
if (typeof f == "function") {
|
|
585
|
-
var a = function a () {
|
|
586
|
-
var isInstance = false;
|
|
587
|
-
try {
|
|
588
|
-
isInstance = this instanceof a;
|
|
589
|
-
} catch {}
|
|
590
|
-
if (isInstance) {
|
|
591
|
-
return Reflect.construct(f, arguments, this.constructor);
|
|
592
|
-
}
|
|
593
|
-
return f.apply(this, arguments);
|
|
594
|
-
};
|
|
595
|
-
a.prototype = f.prototype;
|
|
596
|
-
} else a = {};
|
|
597
|
-
Object.defineProperty(a, '__esModule', {value: true});
|
|
598
|
-
Object.keys(n).forEach(function (k) {
|
|
599
|
-
var d = Object.getOwnPropertyDescriptor(n, k);
|
|
600
|
-
Object.defineProperty(a, k, d.get ? d : {
|
|
601
|
-
enumerable: true,
|
|
602
|
-
get: function () {
|
|
603
|
-
return n[k];
|
|
604
|
-
}
|
|
605
|
-
});
|
|
606
|
-
});
|
|
607
|
-
return a;
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
var jsonMsg_cjs = {};
|
|
611
|
-
|
|
612
|
-
var require$$0 = /*@__PURE__*/getAugmentedNamespace(logr_es);
|
|
613
|
-
|
|
614
|
-
var hasRequiredJsonMsg_cjs;
|
|
615
|
-
|
|
616
|
-
function requireJsonMsg_cjs () {
|
|
617
|
-
if (hasRequiredJsonMsg_cjs) return jsonMsg_cjs;
|
|
618
|
-
hasRequiredJsonMsg_cjs = 1;
|
|
619
|
-
|
|
620
|
-
var bitlogr = require$$0;
|
|
621
|
-
|
|
622
|
-
const schema_Msg = validate18;
|
|
623
|
-
function validate18(data, { instancePath = "", parentData, parentDataProperty, rootData = data, dynamicAnchors = {} } = {}) { let vErrors = null; let errors = 0; const evaluated0 = validate18.evaluated; if (evaluated0.dynamicProps) {
|
|
624
|
-
evaluated0.props = undefined;
|
|
625
|
-
} if (evaluated0.dynamicItems) {
|
|
626
|
-
evaluated0.items = undefined;
|
|
627
|
-
} {
|
|
628
|
-
if (data && typeof data == "object" && !Array.isArray(data)) {
|
|
629
|
-
let missing0;
|
|
630
|
-
if ((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))) {
|
|
631
|
-
validate18.errors = [{ instancePath, schemaPath: "#/required", keyword: "required", params: { missingProperty: missing0 }, message: "must have required property '" + missing0 + "'" }];
|
|
632
|
-
return false;
|
|
633
|
-
}
|
|
634
|
-
else {
|
|
635
|
-
if (data.id !== undefined) {
|
|
636
|
-
const _errs1 = errors;
|
|
637
|
-
if (typeof data.id !== "string") {
|
|
638
|
-
validate18.errors = [{ instancePath: instancePath + "/id", schemaPath: "#/properties/id/type", keyword: "type", params: { type: "string" }, message: "must be string" }];
|
|
639
|
-
return false;
|
|
640
|
-
}
|
|
641
|
-
var valid0 = _errs1 === errors;
|
|
642
|
-
}
|
|
643
|
-
else {
|
|
644
|
-
var valid0 = true;
|
|
645
|
-
}
|
|
646
|
-
if (valid0) {
|
|
647
|
-
if (data.dt !== undefined) {
|
|
648
|
-
const _errs3 = errors;
|
|
649
|
-
if (typeof data.dt !== "string") {
|
|
650
|
-
validate18.errors = [{ instancePath: instancePath + "/dt", schemaPath: "#/properties/dt/type", keyword: "type", params: { type: "string" }, message: "must be string" }];
|
|
651
|
-
return false;
|
|
652
|
-
}
|
|
653
|
-
var valid0 = _errs3 === errors;
|
|
654
|
-
}
|
|
655
|
-
else {
|
|
656
|
-
var valid0 = true;
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
else {
|
|
662
|
-
validate18.errors = [{ instancePath, schemaPath: "#/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
|
|
663
|
-
return false;
|
|
664
|
-
}
|
|
665
|
-
} validate18.errors = vErrors; return errors === 0; }
|
|
666
|
-
validate18.evaluated = { "props": { "id": true, "dt": true }, "dynamicProps": false, "dynamicItems": false };
|
|
667
|
-
const schema_MsgEffect = validate19;
|
|
668
|
-
function validate19(data, { instancePath = "", parentData, parentDataProperty, rootData = data, dynamicAnchors = {} } = {}) { let vErrors = null; let errors = 0; const evaluated0 = validate19.evaluated; if (evaluated0.dynamicProps) {
|
|
669
|
-
evaluated0.props = undefined;
|
|
670
|
-
} if (evaluated0.dynamicItems) {
|
|
671
|
-
evaluated0.items = undefined;
|
|
672
|
-
} {
|
|
673
|
-
if (data && typeof data == "object" && !Array.isArray(data)) {
|
|
674
|
-
let missing0;
|
|
675
|
-
if ((data._cause === undefined) && (missing0 = "_cause")) {
|
|
676
|
-
validate19.errors = [{ instancePath, schemaPath: "#/required", keyword: "required", params: { missingProperty: missing0 }, message: "must have required property '" + missing0 + "'" }];
|
|
677
|
-
return false;
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
else {
|
|
681
|
-
validate19.errors = [{ instancePath, schemaPath: "#/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
|
|
682
|
-
return false;
|
|
683
|
-
}
|
|
684
|
-
} validate19.errors = vErrors; return errors === 0; }
|
|
685
|
-
validate19.evaluated = { "dynamicProps": false, "dynamicItems": false };
|
|
686
|
-
const schema_MsgAckEffect = validate20;
|
|
687
|
-
function validate20(data, { instancePath = "", parentData, parentDataProperty, rootData = data, dynamicAnchors = {} } = {}) { let vErrors = null; let errors = 0; const evaluated0 = validate20.evaluated; if (evaluated0.dynamicProps) {
|
|
688
|
-
evaluated0.props = undefined;
|
|
689
|
-
} if (evaluated0.dynamicItems) {
|
|
690
|
-
evaluated0.items = undefined;
|
|
691
|
-
} {
|
|
692
|
-
if (data && typeof data == "object" && !Array.isArray(data)) {
|
|
693
|
-
let missing0;
|
|
694
|
-
if ((data.ack === undefined) && (missing0 = "ack")) {
|
|
695
|
-
validate20.errors = [{ instancePath, schemaPath: "#/required", keyword: "required", params: { missingProperty: missing0 }, message: "must have required property '" + missing0 + "'" }];
|
|
696
|
-
return false;
|
|
697
|
-
}
|
|
698
|
-
else {
|
|
699
|
-
if (data.ack !== undefined) {
|
|
700
|
-
let data0 = data.ack;
|
|
701
|
-
{
|
|
702
|
-
if (data0 && typeof data0 == "object" && !Array.isArray(data0)) {
|
|
703
|
-
let missing1;
|
|
704
|
-
if ((data0.ok === undefined) && (missing1 = "ok")) {
|
|
705
|
-
validate20.errors = [{ instancePath: instancePath + "/ack", schemaPath: "#/properties/ack/required", keyword: "required", params: { missingProperty: missing1 }, message: "must have required property '" + missing1 + "'" }];
|
|
706
|
-
return false;
|
|
707
|
-
}
|
|
708
|
-
else {
|
|
709
|
-
if (data0.ok !== undefined) {
|
|
710
|
-
if (typeof data0.ok !== "boolean") {
|
|
711
|
-
validate20.errors = [{ instancePath: instancePath + "/ack/ok", schemaPath: "#/properties/ack/properties/ok/type", keyword: "type", params: { type: "boolean" }, message: "must be boolean" }];
|
|
712
|
-
return false;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
else {
|
|
718
|
-
validate20.errors = [{ instancePath: instancePath + "/ack", schemaPath: "#/properties/ack/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
|
|
719
|
-
return false;
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
}
|
|
725
|
-
else {
|
|
726
|
-
validate20.errors = [{ instancePath, schemaPath: "#/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
|
|
727
|
-
return false;
|
|
728
|
-
}
|
|
729
|
-
} validate20.errors = vErrors; return errors === 0; }
|
|
730
|
-
validate20.evaluated = { "props": { "ack": true }, "dynamicProps": false, "dynamicItems": false };
|
|
731
|
-
|
|
732
|
-
const byteToHex = [];
|
|
733
|
-
for (let i = 0; i < 256; ++i) {
|
|
734
|
-
byteToHex.push((i + 0x100).toString(16).slice(1));
|
|
735
|
-
}
|
|
736
|
-
function unsafeStringify(arr, offset = 0) {
|
|
737
|
-
return (byteToHex[arr[offset + 0]] +
|
|
738
|
-
byteToHex[arr[offset + 1]] +
|
|
739
|
-
byteToHex[arr[offset + 2]] +
|
|
740
|
-
byteToHex[arr[offset + 3]] +
|
|
741
|
-
'-' +
|
|
742
|
-
byteToHex[arr[offset + 4]] +
|
|
743
|
-
byteToHex[arr[offset + 5]] +
|
|
744
|
-
'-' +
|
|
745
|
-
byteToHex[arr[offset + 6]] +
|
|
746
|
-
byteToHex[arr[offset + 7]] +
|
|
747
|
-
'-' +
|
|
748
|
-
byteToHex[arr[offset + 8]] +
|
|
749
|
-
byteToHex[arr[offset + 9]] +
|
|
750
|
-
'-' +
|
|
751
|
-
byteToHex[arr[offset + 10]] +
|
|
752
|
-
byteToHex[arr[offset + 11]] +
|
|
753
|
-
byteToHex[arr[offset + 12]] +
|
|
754
|
-
byteToHex[arr[offset + 13]] +
|
|
755
|
-
byteToHex[arr[offset + 14]] +
|
|
756
|
-
byteToHex[arr[offset + 15]]).toLowerCase();
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
let getRandomValues;
|
|
760
|
-
const rnds8 = new Uint8Array(16);
|
|
761
|
-
function rng() {
|
|
762
|
-
if (!getRandomValues) {
|
|
763
|
-
if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
|
|
764
|
-
throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
|
|
765
|
-
}
|
|
766
|
-
getRandomValues = crypto.getRandomValues.bind(crypto);
|
|
767
|
-
}
|
|
768
|
-
return getRandomValues(rnds8);
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
772
|
-
var native = { randomUUID };
|
|
773
|
-
|
|
774
|
-
function _v4(options, buf, offset) {
|
|
775
|
-
options = options || {};
|
|
776
|
-
const rnds = options.random ?? options.rng?.() ?? rng();
|
|
777
|
-
if (rnds.length < 16) {
|
|
778
|
-
throw new Error('Random bytes length must be >= 16');
|
|
779
|
-
}
|
|
780
|
-
rnds[6] = (rnds[6] & 0x0f) | 0x40;
|
|
781
|
-
rnds[8] = (rnds[8] & 0x3f) | 0x80;
|
|
782
|
-
return unsafeStringify(rnds);
|
|
783
|
-
}
|
|
784
|
-
function v4(options, buf, offset) {
|
|
785
|
-
if (native.randomUUID && true && !options) {
|
|
786
|
-
return native.randomUUID();
|
|
787
|
-
}
|
|
788
|
-
return _v4(options);
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
//-------------------------------------------------------------------------------------------------
|
|
792
|
-
const LOGR_ = bitlogr.LOGR.get_instance();
|
|
793
|
-
const logr_ = LOGR_.create({ labels: bitlogr.l_array(['VALIDATION']) });
|
|
794
|
-
const l_ = logr_.l;
|
|
795
|
-
// console.log('File location:', import.meta.url, '[Implementation]');
|
|
796
|
-
//-------------------------------------------------------------------------------------------------
|
|
797
|
-
// TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
|
|
798
|
-
// "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
|
|
799
|
-
var JSON_Msg = (function () {
|
|
800
|
-
return {
|
|
801
|
-
merge: function (json) {
|
|
802
|
-
return Object.assign({
|
|
803
|
-
id: v4(),
|
|
804
|
-
referer: null,
|
|
805
|
-
dt: new Date().toISOString()
|
|
806
|
-
}, json || {});
|
|
807
|
-
},
|
|
808
|
-
validate: function (json) {
|
|
809
|
-
const b_valid = schema_Msg(json);
|
|
810
|
-
if (!b_valid)
|
|
811
|
-
logr_.log(l_.VALIDATION, () => ['JSON_Msg: validate: ', schema_Msg.errors]);
|
|
812
|
-
return b_valid;
|
|
813
|
-
},
|
|
814
|
-
// TODO: requires a separate ajv module require, and JTD specific schema
|
|
815
|
-
// https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
|
|
816
|
-
// static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
|
|
817
|
-
// static parse(json) {
|
|
818
|
-
// return this.#ajv_parse_(json);
|
|
819
|
-
// }
|
|
820
|
-
};
|
|
821
|
-
})();
|
|
822
|
-
var JSON_MsgEffect = (function () {
|
|
823
|
-
return {
|
|
824
|
-
create_with_cause_merge: function (msg_cause, json) {
|
|
825
|
-
const _cause = msg_cause;
|
|
826
|
-
return Object.assign(JSON_Msg.merge(json), { _cause });
|
|
827
|
-
},
|
|
828
|
-
validate: function (json) {
|
|
829
|
-
if (!JSON_Msg.validate(json))
|
|
830
|
-
return false;
|
|
831
|
-
const b_valid = schema_MsgEffect(json);
|
|
832
|
-
if (!b_valid)
|
|
833
|
-
logr_.log(l_.VALIDATION, () => ['JSON_MsgEffect: validate: ', schema_MsgEffect.errors]);
|
|
834
|
-
return b_valid;
|
|
835
|
-
}
|
|
836
|
-
};
|
|
837
|
-
})();
|
|
838
|
-
var JSON_MsgAck = (function () {
|
|
839
|
-
return {
|
|
840
|
-
create_with_cause_Ok: function (msg_cause, b_ok = true) {
|
|
841
|
-
return JSON_MsgEffect.create_with_cause_merge(msg_cause, {
|
|
842
|
-
ack: {
|
|
843
|
-
"ok": b_ok,
|
|
844
|
-
},
|
|
845
|
-
});
|
|
846
|
-
},
|
|
847
|
-
create_with_cause_Expired: function (msg_cause) {
|
|
848
|
-
return JSON_MsgEffect.create_with_cause_merge(msg_cause, {
|
|
849
|
-
ack: {
|
|
850
|
-
"ok": false,
|
|
851
|
-
"expired": true
|
|
852
|
-
},
|
|
853
|
-
});
|
|
854
|
-
},
|
|
855
|
-
validate: function (json) {
|
|
856
|
-
if (!JSON_MsgEffect.validate(json))
|
|
857
|
-
return false;
|
|
858
|
-
const b_valid = schema_MsgAckEffect(json);
|
|
859
|
-
if (!b_valid)
|
|
860
|
-
logr_.log(l_.VALIDATION, () => ['JSON_MsgAckEffect: validate: ', schema_MsgAckEffect.errors]);
|
|
861
|
-
return b_valid;
|
|
862
|
-
}
|
|
863
|
-
};
|
|
864
|
-
})();
|
|
865
|
-
|
|
866
|
-
jsonMsg_cjs.JSON_Msg = JSON_Msg;
|
|
867
|
-
jsonMsg_cjs.JSON_MsgAck = JSON_MsgAck;
|
|
868
|
-
jsonMsg_cjs.JSON_MsgEffect = JSON_MsgEffect;
|
|
869
|
-
jsonMsg_cjs.logr = logr_;
|
|
870
|
-
return jsonMsg_cjs;
|
|
871
|
-
}
|
|
872
|
-
|
|
873
|
-
var jsonMsg_cjsExports = requireJsonMsg_cjs();
|
|
874
|
-
|
|
7
|
+
// The wire interface (validators, constructors, VERSION) is GENERATED by iface-gen from schema_TwoPhW.json
|
|
8
|
+
// -- in the HOST's project, never here. This package has no interface and no schema of its own: the host
|
|
9
|
+
// hands its generated interface to protocol_TwoPhW() (at the bottom of this file), which is what binds
|
|
10
|
+
// the two. So the package and the host cannot disagree about the wire, the version included.
|
|
11
|
+
// import { v4 as uuidv4 } from 'uuid'; // uuid is already a dep of JSON_Msg, so why not
|
|
875
12
|
// interface Event extends Required<EventMinimal> {
|
|
876
13
|
// type : string;
|
|
877
14
|
// id : string | number;
|
|
878
15
|
// timeout_id : NodeJS.Timeout | null;
|
|
879
16
|
// };
|
|
880
|
-
const k_TTL = 20000;
|
|
881
17
|
//-------------------------------------------------------------------------------------------------
|
|
882
|
-
const obj_labels_merge_ =
|
|
883
|
-
const obj_labels_concat_ =
|
|
884
|
-
const lref_ = lRef(obj_labels_concat_);
|
|
885
|
-
|
|
18
|
+
const obj_labels_merge_ = bitlogr.l_merge(eventlog.logr.lref.get(), jsonMsg.logr.lref.get()); // VALIDATION : 4
|
|
19
|
+
const obj_labels_concat_ = bitlogr.l_concat(obj_labels_merge_, bitlogr.l_array(['HANDLERS', 'DROPS', 'PROTOCOL_STATE', 'RECVER', 'SENDER']));
|
|
20
|
+
const lref_ = bitlogr.lRef(obj_labels_concat_);
|
|
21
|
+
jsonMsg.logr.lref = lref_;
|
|
886
22
|
eventlog.logr.lref = lref_;
|
|
887
|
-
const LOGR_ = LOGR.get_instance();
|
|
23
|
+
const LOGR_ = bitlogr.LOGR.get_instance();
|
|
888
24
|
const logr_ = LOGR_.create(); // don't let create() make a new ref, assign lref_ from here
|
|
889
25
|
logr_.lref = lref_;
|
|
890
26
|
const l_ = logr_.l;
|
|
891
27
|
class Config {
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
28
|
+
// A step's callback is what makes the step this participant's to handle: with no callback set,
|
|
29
|
+
// the step's handler refuses it (returns false) before anything is logged. Every participant on
|
|
30
|
+
// the ME hears every exchange, so "no callback" is the normal case for a role it does not play.
|
|
31
|
+
_callback_msgWarp = null;
|
|
32
|
+
_callback_effEngage = null;
|
|
33
|
+
_callback_ackWarp = null;
|
|
34
|
+
_callback_ackEngage = null;
|
|
35
|
+
// Optional, and not what makes a hold ours (that is the sender role, _callback_ackWarp): a hold
|
|
36
|
+
// moves the Warp's expiry whether or not anybody wants to be told about it.
|
|
37
|
+
_callback_holdWarp = null;
|
|
38
|
+
// "a step I sent was never answered inside its window", one per role: the sender's fires for its
|
|
39
|
+
// Warp / Engage, the receiver's for its AckWarp. A participant playing both roles keeps both.
|
|
40
|
+
_callback_sender_sent_expired;
|
|
41
|
+
_callback_receiver_sent_expired;
|
|
898
42
|
constructor() {
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
console.assert(false, 'callback_msgWarp uninitialized');
|
|
902
|
-
return false;
|
|
903
|
-
};
|
|
904
|
-
this._callback_msgEngage = (_msg, _json_msg) => {
|
|
905
|
-
console.assert(false, 'callback_msgEngage uninitialized');
|
|
906
|
-
return false;
|
|
907
|
-
};
|
|
908
|
-
this._callback_msgAckWarp = (_msg, _json_msg) => {
|
|
909
|
-
console.assert(false, 'callback_msgAckWarp uninitialized');
|
|
910
|
-
return false;
|
|
911
|
-
};
|
|
912
|
-
this._callback_msgAckEngage = (_msg, _json_msg) => {
|
|
913
|
-
console.assert(false, 'callback_msgAckEngage uninitialized');
|
|
914
|
-
return false;
|
|
915
|
-
};
|
|
916
|
-
this._callback_msg_sent_expired = (_msg, _json_msg) => {
|
|
917
|
-
console.assert(false, '_callback_msg_sent_expired uninitialized');
|
|
43
|
+
this._callback_sender_sent_expired = (_msg, _json_msg) => {
|
|
44
|
+
console.assert(false, '_callback_sender_sent_expired uninitialized');
|
|
918
45
|
};
|
|
919
|
-
this.
|
|
920
|
-
console.assert(false, '
|
|
46
|
+
this._callback_receiver_sent_expired = (_msg, _json_msg) => {
|
|
47
|
+
console.assert(false, '_callback_receiver_sent_expired uninitialized');
|
|
921
48
|
};
|
|
922
49
|
}
|
|
923
|
-
receiver(callback_msgWarp,
|
|
50
|
+
receiver(callback_msgWarp, callback_effEngage, callback_msg_sent_expired) {
|
|
924
51
|
this._callback_msgWarp = callback_msgWarp;
|
|
925
|
-
this.
|
|
926
|
-
this.
|
|
52
|
+
this._callback_effEngage = callback_effEngage;
|
|
53
|
+
this._callback_receiver_sent_expired = callback_msg_sent_expired;
|
|
927
54
|
}
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
this.
|
|
931
|
-
this.
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
this._callback_msgOptions = callback;
|
|
55
|
+
// callback_holdWarp: the receiver says it is working on our Warp (what it waits for, how far it is)
|
|
56
|
+
sender(callback_ackWarp, callback_ackEngage, callback_msg_sent_expired, callback_holdWarp = null) {
|
|
57
|
+
this._callback_ackWarp = callback_ackWarp;
|
|
58
|
+
this._callback_ackEngage = callback_ackEngage;
|
|
59
|
+
this._callback_sender_sent_expired = callback_msg_sent_expired;
|
|
60
|
+
this._callback_holdWarp = callback_holdWarp;
|
|
935
61
|
}
|
|
936
62
|
}
|
|
937
63
|
// var config = (function() {
|
|
@@ -946,148 +72,31 @@ class Config {
|
|
|
946
72
|
// }
|
|
947
73
|
// })();
|
|
948
74
|
//-------------------------------------------------------------------------------------------------
|
|
949
|
-
|
|
950
|
-
//
|
|
951
|
-
//
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
75
|
+
// Timing. Every step is timed from the message it answers, by that message's own dt; no step looks further
|
|
76
|
+
// back than the message before it. Two durations:
|
|
77
|
+
// - TTL (warp.ttl_msec, set by the sender): the longest a message may take to travel ONE way. Network only.
|
|
78
|
+
// - the lease (hold.ttl_msec of a JSON_HoldWarp, set by the receiver): "my next word comes within this long".
|
|
79
|
+
// Work only.
|
|
80
|
+
// A message that hands the turn over (Warp, AckWarp, Engage) is answered within its dt + TTL*2, there and
|
|
81
|
+
// back. After a hold the next word is due within the hold's dt + its lease + TTL. Of several holds the one
|
|
82
|
+
// that ends latest counts; nothing is added up. The exchange as a whole has no limit: it stays open for as
|
|
83
|
+
// long as leases are renewed.
|
|
84
|
+
// the window in which a message that hands the turn over is answered: TTL there, TTL back
|
|
85
|
+
function window_msec_(nr_ttl_msec) {
|
|
86
|
+
return nr_ttl_msec * 2;
|
|
87
|
+
}
|
|
88
|
+
// Are these the same Warp? The identity of an exchange: the Warp's own dt, its warp.id and its sender.
|
|
89
|
+
function compare_msgWarp_(json_L, json_R) {
|
|
90
|
+
if (json_L.dt !== json_R.dt)
|
|
91
|
+
return false;
|
|
92
|
+
if (json_L.warp.id !== json_R.warp.id)
|
|
93
|
+
return false;
|
|
94
|
+
if (json_L.referer[0] !== json_R.referer[0])
|
|
95
|
+
return false;
|
|
96
|
+
if (json_L.referer[1] !== json_R.referer[1])
|
|
97
|
+
return false;
|
|
98
|
+
return true;
|
|
959
99
|
}
|
|
960
|
-
var JSON_MsgOptions = (function () {
|
|
961
|
-
return {
|
|
962
|
-
create_with_cause_options: function (msg_cause, options) {
|
|
963
|
-
return jsonMsg_cjsExports.JSON_MsgEffect.create_with_msg_merge(msg_cause, {
|
|
964
|
-
options: options,
|
|
965
|
-
"2PhW": k_VERSION
|
|
966
|
-
});
|
|
967
|
-
},
|
|
968
|
-
validate: function (json) {
|
|
969
|
-
// if (! JSON_Msg_msg.validate(json))
|
|
970
|
-
// return false;
|
|
971
|
-
// if (! schema_MsgOptions_(json)) {
|
|
972
|
-
// logr_.log(l_.VALIDATION, () => ['JSON_MsgOptions: schema_MsgOptions_: ', (schema_MsgOptions_ as any).errors])
|
|
973
|
-
// return false;
|
|
974
|
-
// }
|
|
975
|
-
return true;
|
|
976
|
-
}
|
|
977
|
-
};
|
|
978
|
-
})();
|
|
979
|
-
var JSON_MsgWarp = (function () {
|
|
980
|
-
return {
|
|
981
|
-
create_with_id_user_hyperport: function (json_warp_id, json_user, json_hyperport, nr_ttl_msec) {
|
|
982
|
-
return jsonMsg_cjsExports.JSON_Msg.merge({
|
|
983
|
-
warp: {
|
|
984
|
-
id: json_warp_id,
|
|
985
|
-
user: json_user,
|
|
986
|
-
hyperport: json_hyperport,
|
|
987
|
-
ttl_msec: nr_ttl_msec
|
|
988
|
-
},
|
|
989
|
-
"2PhW": k_VERSION
|
|
990
|
-
});
|
|
991
|
-
},
|
|
992
|
-
compare: function (json_L, json_R) {
|
|
993
|
-
if (json_L.dt !== json_R.dt)
|
|
994
|
-
return false;
|
|
995
|
-
if (json_L.warp.id !== json_R.warp.id)
|
|
996
|
-
return false;
|
|
997
|
-
if (json_L.referer[0] !== json_R.referer[0])
|
|
998
|
-
return false;
|
|
999
|
-
if (json_L.referer[1] !== json_R.referer[1])
|
|
1000
|
-
return false;
|
|
1001
|
-
return true;
|
|
1002
|
-
},
|
|
1003
|
-
validate: function (json_msg) {
|
|
1004
|
-
// if (! JSON_Msg.validate(json))
|
|
1005
|
-
// return false;
|
|
1006
|
-
if (!schema_MsgWarp(json_msg)) {
|
|
1007
|
-
logr_.log(l_.VALIDATION, () => ['JSON_MsgWarp: schema_MsgWarp_: ', schema_MsgWarp.errors]);
|
|
1008
|
-
return false;
|
|
1009
|
-
}
|
|
1010
|
-
return true;
|
|
1011
|
-
}
|
|
1012
|
-
};
|
|
1013
|
-
})();
|
|
1014
|
-
var JSON_MsgEngage = (function () {
|
|
1015
|
-
return {
|
|
1016
|
-
create_with_Warp: function (json_msgWarp) {
|
|
1017
|
-
console.assert(json_msgWarp.ack === undefined, "Engage with a Warp, not an Ack");
|
|
1018
|
-
return jsonMsg_cjsExports.JSON_MsgEffect.create_with_cause_merge(json_msgWarp, {
|
|
1019
|
-
engage: {
|
|
1020
|
-
"ok": true,
|
|
1021
|
-
"rtt_msec": Date.now() - Date.parse(json_msgWarp.dt)
|
|
1022
|
-
},
|
|
1023
|
-
"2PhW": k_VERSION
|
|
1024
|
-
});
|
|
1025
|
-
},
|
|
1026
|
-
validate: function (json_msgEffect) {
|
|
1027
|
-
// if (! JSON_Msg_msg.validate(json))
|
|
1028
|
-
// return false;
|
|
1029
|
-
if (!JSON_MsgWarp.validate(json_msgEffect._cause))
|
|
1030
|
-
return false;
|
|
1031
|
-
if (!schema_MsgEngage(json_msgEffect)) {
|
|
1032
|
-
logr_.log(l_.VALIDATION, () => ['JSON_MsgEngage: schema_MsgEngage_: ', schema_MsgEngage.errors]);
|
|
1033
|
-
return false;
|
|
1034
|
-
}
|
|
1035
|
-
return true;
|
|
1036
|
-
}
|
|
1037
|
-
};
|
|
1038
|
-
})();
|
|
1039
|
-
var JSON_MsgAckWarp = (function () {
|
|
1040
|
-
return {
|
|
1041
|
-
// createWith_msgAck_msg : function(json_msgAck, _msg) {
|
|
1042
|
-
// const json_msgAckWarp= structuredClone(json_msgAck);
|
|
1043
|
-
// json_msgAckWarp._cause= _cause;
|
|
1044
|
-
// return json_msgAckWarp;
|
|
1045
|
-
// }
|
|
1046
|
-
create_with_Warp_Ok: function (json_msgWarp, b_ok = true) {
|
|
1047
|
-
const json_msgAck = jsonMsg_cjsExports.JSON_MsgAck.create_with_cause_Ok(json_msgWarp, b_ok);
|
|
1048
|
-
json_msgAck["2PhW"] = k_VERSION;
|
|
1049
|
-
return json_msgAck;
|
|
1050
|
-
},
|
|
1051
|
-
// create_with_msgExpired : function( json_msgWarp ) {
|
|
1052
|
-
// const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgWarp )
|
|
1053
|
-
// json_msgAck_msg["2PhW"]= k_VERSION;
|
|
1054
|
-
// return json_msgAck_msg;
|
|
1055
|
-
// },
|
|
1056
|
-
validate: function (json_msgAck) {
|
|
1057
|
-
// if (! JSON_MsgAck_msg.validate(json))
|
|
1058
|
-
// return false;
|
|
1059
|
-
if (!JSON_MsgWarp.validate(json_msgAck._cause))
|
|
1060
|
-
return false;
|
|
1061
|
-
return true;
|
|
1062
|
-
}
|
|
1063
|
-
};
|
|
1064
|
-
})();
|
|
1065
|
-
var JSON_MsgAckEngage = (function () {
|
|
1066
|
-
return {
|
|
1067
|
-
// createWith_msgAck_msg : function(json_msgAck, _msg) {
|
|
1068
|
-
// const json_msgAckEngage= structuredClone(json_msgAck);
|
|
1069
|
-
// json_msgAckEngage._cause= _cause;
|
|
1070
|
-
// return json_msgAckEngage;
|
|
1071
|
-
// }
|
|
1072
|
-
create_with_Engage_Ok: function (json_msgEngage, b_ok = true) {
|
|
1073
|
-
const json_msgAck = jsonMsg_cjsExports.JSON_MsgAck.create_with_cause_Ok(json_msgEngage, b_ok);
|
|
1074
|
-
json_msgAck["2PhW"] = k_VERSION;
|
|
1075
|
-
return json_msgAck;
|
|
1076
|
-
},
|
|
1077
|
-
// create_with_msgExpired : function( json_msgEngage ) {
|
|
1078
|
-
// const json_msgAck_msg= JSON_MsgAck_msg.create_with_msgExpired( json_msgEngage )
|
|
1079
|
-
// json_msgAck_msg["2PhW"]= k_VERSION;
|
|
1080
|
-
// return json_msgAck_msg;
|
|
1081
|
-
// },
|
|
1082
|
-
validate: function (json_msgAck) {
|
|
1083
|
-
// if (! JSON_MsgAck_msg.validate(json_msgAck_msg))
|
|
1084
|
-
// return false;
|
|
1085
|
-
if (!JSON_MsgEngage.validate(json_msgAck._cause))
|
|
1086
|
-
return false;
|
|
1087
|
-
return true;
|
|
1088
|
-
}
|
|
1089
|
-
};
|
|
1090
|
-
})();
|
|
1091
100
|
//-------------------------------------------------------------------------------------------------
|
|
1092
101
|
// States
|
|
1093
102
|
class StateState {
|
|
@@ -1127,7 +136,7 @@ class StateState {
|
|
|
1127
136
|
class StateAckEngage extends StateState {
|
|
1128
137
|
constructor(idx_event, event) { super(idx_event, event); }
|
|
1129
138
|
get evt_AckEngage() { return this.event; }
|
|
1130
|
-
get
|
|
139
|
+
get json_ackEngage() { return this.json_msg; }
|
|
1131
140
|
canChangeState(_state_new) {
|
|
1132
141
|
return false;
|
|
1133
142
|
}
|
|
@@ -1135,53 +144,80 @@ class StateAckEngage extends StateState {
|
|
|
1135
144
|
class StateEngage extends StateState {
|
|
1136
145
|
constructor(idx_event, event) { super(idx_event, event); }
|
|
1137
146
|
get evt_Engage() { return this.event; }
|
|
1138
|
-
get
|
|
147
|
+
get json_effEngage() { return this.json_msg; }
|
|
1139
148
|
canChangeState(state_new) {
|
|
1140
149
|
if (false === state_new instanceof StateAckEngage)
|
|
1141
150
|
return false;
|
|
1142
|
-
// The
|
|
1143
|
-
const
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
if (state_new.evt_AckEngage.dt >= Date.parse(json_msgWarp.dt) + timeout_ms)
|
|
151
|
+
// The AckEngage is accepted until THE ENGAGE'S creation time + TTL*2
|
|
152
|
+
const timeout_ms = window_msec_(this.json_effEngage._cause.warp.ttl_msec);
|
|
153
|
+
logr_.log(l_.PROTOCOL_STATE, () => [`StateEngage: evt_AckEngage.dt@${state_new.evt_AckEngage.dt}, effEngage.dt@${Date.parse(this.json_effEngage.dt)} + ${timeout_ms}`]);
|
|
154
|
+
if (state_new.evt_AckEngage.dt >= Date.parse(this.json_effEngage.dt) + timeout_ms)
|
|
1147
155
|
return false;
|
|
1148
156
|
return true;
|
|
1149
157
|
}
|
|
1150
158
|
}
|
|
159
|
+
// Can a JSON_EffEngage follow this JSON_AckWarp? Not after a nack (it ends the exchange), and not
|
|
160
|
+
// after the ack of an instantaneous warp (single phase: the ack concludes it).
|
|
161
|
+
function isEngageAwaited_(json_ackWarp) {
|
|
162
|
+
return !!json_ackWarp.ack.ok && !json_ackWarp._cause.warp.instantaneous;
|
|
163
|
+
}
|
|
1151
164
|
class StateAckWarp extends StateState {
|
|
1152
165
|
constructor(idx_event, event) { super(idx_event, event); }
|
|
1153
166
|
get evt_AckWarp() { return this.event; }
|
|
1154
|
-
get
|
|
167
|
+
get json_ackWarp() { return this.json_msg; }
|
|
1155
168
|
canChangeState(state_new) {
|
|
1156
169
|
if (false === state_new instanceof StateEngage)
|
|
1157
170
|
return false;
|
|
1158
|
-
if (!this.
|
|
171
|
+
if (!isEngageAwaited_(this.json_ackWarp))
|
|
1159
172
|
return false;
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
// - AckWarp: TAckWarp +2TLL +STT; TAckWarp= T0 +STT;
|
|
1166
|
-
// - STT: Date of the Ack minus date of the Warp.
|
|
1167
|
-
const timeout_ms = time_ttl_rtt_(this.json_msgAckWarp._cause.warp.ttl_msec) + Date.parse(this.json_msgAckWarp.dt) - Date.parse(this.json_msgAckWarp._cause.dt);
|
|
1168
|
-
logr_.log(l_.PROTOCOL_STATE, () => [`StateAckWarp: evt_Engage.dt@${state_new.evt_Engage.dt}, msgAckWarp.dt@${Date.parse(this.json_msgAckWarp.dt)} + ${timeout_ms}`]);
|
|
1169
|
-
if (state_new.evt_Engage.dt >= Date.parse(this.json_msgAckWarp.dt) + timeout_ms)
|
|
173
|
+
// The Engage is accepted until THE ACK'S creation time + TTL*2. Nothing of phase 1 enters it: how
|
|
174
|
+
// long the receiver took to acknowledge the Warp is not this phase's business.
|
|
175
|
+
const timeout_ms = window_msec_(this.json_ackWarp._cause.warp.ttl_msec);
|
|
176
|
+
logr_.log(l_.PROTOCOL_STATE, () => [`StateAckWarp: evt_Engage.dt@${state_new.evt_Engage.dt}, msgAckWarp.dt@${Date.parse(this.json_ackWarp.dt)} + ${timeout_ms}`]);
|
|
177
|
+
if (state_new.evt_Engage.dt >= Date.parse(this.json_ackWarp.dt) + timeout_ms)
|
|
1170
178
|
return false;
|
|
1171
179
|
return true;
|
|
1172
180
|
}
|
|
1173
181
|
}
|
|
1174
182
|
class StateWarp extends StateState {
|
|
183
|
+
// where the latest lease of a JSON_HoldWarp moved the end of the window to (epoch msec), and the event
|
|
184
|
+
// whose timer now guards it. null: no hold moved it.
|
|
185
|
+
_nr_deadline_lease = null;
|
|
186
|
+
_idx_event_timer = null;
|
|
1175
187
|
constructor(idx_event, event) { super(idx_event, event); }
|
|
1176
188
|
get evt_Warp() { return this.event; }
|
|
1177
189
|
get json_msgWarp() { return this.json_msg; }
|
|
190
|
+
// Where a JSON_HoldWarp puts the end of the window: its OWN dt + its lease (hold.ttl_msec) + the
|
|
191
|
+
// warp's TTL (one for the next message to travel). NaN when its dt does not read: a lease with no anchor.
|
|
192
|
+
static deadline_holdWarp(json_holdWarp) {
|
|
193
|
+
const nr_lease = jsonMsg.JSON_Hold.lease_epoch_msec(json_holdWarp);
|
|
194
|
+
return (nr_lease === null) ? NaN : nr_lease + json_holdWarp._cause.warp.ttl_msec;
|
|
195
|
+
}
|
|
196
|
+
// The last instant the JSON_AckWarp is accepted: THE WARP'S creation time + TTL*2, or where a hold
|
|
197
|
+
// moved it, whichever is later. The latest counts; nothing is added up.
|
|
198
|
+
get deadline_msec() {
|
|
199
|
+
const nr_deadline = Date.parse(this.json_msgWarp.dt) + window_msec_(this.json_msgWarp.warp.ttl_msec);
|
|
200
|
+
return (this._nr_deadline_lease !== null && this._nr_deadline_lease > nr_deadline) ? this._nr_deadline_lease : nr_deadline;
|
|
201
|
+
}
|
|
202
|
+
// the event that holds this Warp's expiry timer: the Warp's own, or the hold that last moved the window
|
|
203
|
+
get idx_event_timer() {
|
|
204
|
+
return (this._idx_event_timer !== null) ? this._idx_event_timer : this.idx_event;
|
|
205
|
+
}
|
|
206
|
+
// A hold on this Warp, logged as event idx_event. Returns whether it moved the end of the window:
|
|
207
|
+
// not when its lease ends no later than the window in force, nor when its dt does not read.
|
|
208
|
+
lease(idx_event, json_holdWarp) {
|
|
209
|
+
const nr_deadline = StateWarp.deadline_holdWarp(json_holdWarp);
|
|
210
|
+
if (!(nr_deadline > this.deadline_msec))
|
|
211
|
+
return false;
|
|
212
|
+
this._nr_deadline_lease = nr_deadline;
|
|
213
|
+
this._idx_event_timer = idx_event;
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
1178
216
|
canChangeState(state_new) {
|
|
1179
217
|
if (false === state_new instanceof StateAckWarp)
|
|
1180
218
|
return false;
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
logr_.log(l_.PROTOCOL_STATE, () => [`StateWarp: evt_AckWarp.dt@${state_new.evt_AckWarp.dt}, msgWarp.dt@${Date.parse(this.json_msgWarp.dt)} + ${timeout_ms}`]);
|
|
1184
|
-
if (state_new.evt_AckWarp.dt >= Date.parse(this.json_msgWarp.dt) + timeout_ms)
|
|
219
|
+
logr_.log(l_.PROTOCOL_STATE, () => [`StateWarp: evt_AckWarp.dt@${state_new.evt_AckWarp.dt}, deadline@${this.deadline_msec}`]);
|
|
220
|
+
if (state_new.evt_AckWarp.dt >= this.deadline_msec)
|
|
1185
221
|
return false;
|
|
1186
222
|
return true;
|
|
1187
223
|
}
|
|
@@ -1225,10 +261,23 @@ class ProtocolStateState {
|
|
|
1225
261
|
return this._state;
|
|
1226
262
|
}
|
|
1227
263
|
canChangeState(state_new) {
|
|
264
|
+
// Every window is measured from a message's own dt, and the schema only asks dt to be a string.
|
|
265
|
+
// A step whose dt does not read as a time has no window to be inside of: never legal. (Unchecked,
|
|
266
|
+
// it parses to NaN, every "is it past the deadline?" answers no, and it escapes all the windows.)
|
|
267
|
+
// Every logged step passed here, so the states can rely on the dt of the messages they wrap.
|
|
268
|
+
if (!Number.isFinite(Date.parse(state_new.json_msg.dt)))
|
|
269
|
+
return false;
|
|
270
|
+
// An exchange is a dialogue: a step we RECEIVE is only legal after a step we SENT, and vice
|
|
271
|
+
// versa. Every participant on the ME hears every exchange, so without this a warp we merely
|
|
272
|
+
// heard would be "answered" by the ack its real receiver sent, and we could answer our own step.
|
|
273
|
+
if (!(this._state instanceof StateInitialState) && this._state.event.data.b_sent === state_new.event.data.b_sent)
|
|
274
|
+
return false;
|
|
1228
275
|
return this._state.canChangeState(state_new);
|
|
1229
276
|
}
|
|
1230
277
|
}
|
|
1231
278
|
//-------------------------------------------------------------------------------------------------
|
|
279
|
+
const kstr_EVT_DROPPED_ = '2PhW[DROPPED!]';
|
|
280
|
+
const kstr_EVT_HOLDWARP_ = '2PhW[JSON_HoldWarp]';
|
|
1232
281
|
class TwoPhW_EventLog {
|
|
1233
282
|
_eventLog;
|
|
1234
283
|
constructor(eventLog) {
|
|
@@ -1236,32 +285,35 @@ class TwoPhW_EventLog {
|
|
|
1236
285
|
}
|
|
1237
286
|
_warp(json_msg) {
|
|
1238
287
|
const b_Ack = json_msg.ack !== undefined;
|
|
1239
|
-
const
|
|
288
|
+
const b_Hold = !b_Ack && json_msg.hold !== undefined; // a message that carries both is an ack
|
|
289
|
+
const json = (b_Ack || b_Hold) ? json_msg["_cause"] : json_msg;
|
|
1240
290
|
if (json.warp) {
|
|
1241
291
|
return {
|
|
1242
|
-
str_type:
|
|
292
|
+
str_type: b_Ack ? '2PhW[JSON_AckWarp]' : (b_Hold ? kstr_EVT_HOLDWARP_ : '2PhW[JSON_MsgWarp]'),
|
|
1243
293
|
json_msgWarp: json
|
|
1244
294
|
};
|
|
1245
295
|
}
|
|
1246
296
|
if (json.engage) {
|
|
1247
297
|
return {
|
|
1248
|
-
str_type:
|
|
298
|
+
str_type: b_Ack ? '2PhW[JSON_AckEngage]' : '2PhW[JSON_EffEngage]',
|
|
1249
299
|
json_msgWarp: json["_cause"]
|
|
1250
300
|
};
|
|
1251
301
|
}
|
|
1252
302
|
return { str_type: null, json_msgWarp: null }; // Explicit default for invalid cases
|
|
1253
303
|
}
|
|
1254
304
|
// _addEvent(msg: t_msg, json_msg : JSON_Msg, timeout_ms? : number, timeout_callback? : ( args? : {idx_event : number, event : EventMinimal} ) => {}) {
|
|
1255
|
-
_addEvent(msg, json_msg, timeout_ms, timeout_callback) {
|
|
305
|
+
_addEvent(msg, json_msg, b_sent, timeout_ms, timeout_callback) {
|
|
1256
306
|
const { str_type, json_msgWarp } = this._warp(json_msg);
|
|
1257
|
-
const { idx } = this._eventLog.add_Event(str_type, json_msgWarp.warp.id, { msg, json_msg }, timeout_ms, timeout_callback);
|
|
307
|
+
const { idx } = this._eventLog.add_Event(str_type, json_msgWarp.warp.id, { msg, json_msg, b_sent }, timeout_ms, timeout_callback);
|
|
1258
308
|
logr_.log(l_.PROTOCOL_STATE, () => [`_addEvent: [${idx}] timeout:${Date.now()}+${timeout_ms} `, json_msg]);
|
|
1259
309
|
}
|
|
1260
|
-
|
|
310
|
+
// Record a message we refused, for forensics: under its exchange and in the shape of every other
|
|
311
|
+
// 2PhW event, but never a step -- traverse_State skips it.
|
|
312
|
+
_dropEvent(msg, json_msg) {
|
|
1261
313
|
const { json_msgWarp } = this._warp(json_msg);
|
|
1262
314
|
if (!json_msgWarp)
|
|
1263
315
|
return;
|
|
1264
|
-
this._eventLog.add_Event(
|
|
316
|
+
this._eventLog.add_Event(kstr_EVT_DROPPED_, json_msgWarp.warp.id, { msg, json_msg, b_sent: false });
|
|
1265
317
|
}
|
|
1266
318
|
_cancelTimeout(idx_event) {
|
|
1267
319
|
logr_.log(l_.PROTOCOL_STATE, () => [`_cancelTimeout: [${idx_event}] timeout cancelled`]);
|
|
@@ -1269,25 +321,39 @@ class TwoPhW_EventLog {
|
|
|
1269
321
|
}
|
|
1270
322
|
traverse_State(json_msgWarp) {
|
|
1271
323
|
const protocolState = new ProtocolStateState();
|
|
1272
|
-
|
|
324
|
+
// Look back over the WHOLE exchange: to the Warp's own dt (+ TTL*2: a participant logs by its own
|
|
325
|
+
// clock). An exchange has no limit as a whole (holds keep it open for as long as they are renewed), so
|
|
326
|
+
// its age says nothing; whether it is still open is decided by its windows. A Warp whose dt does not
|
|
327
|
+
// read has no exchange to look back over, and is refused by every caller.
|
|
328
|
+
const nr_age_msec = Date.now() - Date.parse(json_msgWarp.dt);
|
|
329
|
+
const lookback_ms = window_msec_(json_msgWarp.warp.ttl_msec) + (Number.isFinite(nr_age_msec) ? Math.max(nr_age_msec, 0) : 0);
|
|
330
|
+
const arr_evts = this._eventLog.get_Events_by_id(json_msgWarp.warp.id, lookback_ms);
|
|
1273
331
|
logr_.log(l_.EVENTS, () => ['traverse_State: arr_evts: ', arr_evts]);
|
|
1274
|
-
//TODO: what is the right TTL? should it be a constant or calculated
|
|
1275
332
|
for (let i = arr_evts.length - 1; i >= 0; i--) {
|
|
1276
333
|
const { idx, evt } = arr_evts[i];
|
|
334
|
+
// a dropped message is not a step: not of this exchange, and it must not poison it either
|
|
335
|
+
// (it may well belong to ANOTHER exchange that reused the warp id -- that is why it was dropped)
|
|
336
|
+
if (evt.type === kstr_EVT_DROPPED_)
|
|
337
|
+
continue;
|
|
1277
338
|
const json_msg = evt.data.json_msg;
|
|
1278
|
-
if (!
|
|
339
|
+
if (!compare_msgWarp_(json_msgWarp, this._warp(json_msg).json_msgWarp))
|
|
1279
340
|
protocolState.state.changeState(new StateINVALID(idx, evt));
|
|
1280
341
|
switch (evt.type) {
|
|
1281
342
|
case '2PhW[JSON_MsgWarp]':
|
|
1282
343
|
protocolState.state.changeState(new StateWarp(idx, evt));
|
|
1283
344
|
break;
|
|
1284
|
-
case
|
|
345
|
+
case kstr_EVT_HOLDWARP_:
|
|
346
|
+
// not a step: it changes no state, it moves the window of the Warp it is about
|
|
347
|
+
if (protocolState.state instanceof StateWarp)
|
|
348
|
+
protocolState.state.lease(idx, json_msg);
|
|
349
|
+
break;
|
|
350
|
+
case '2PhW[JSON_AckWarp]':
|
|
1285
351
|
protocolState.state.changeState(new StateAckWarp(idx, evt));
|
|
1286
352
|
break;
|
|
1287
|
-
case '2PhW[
|
|
353
|
+
case '2PhW[JSON_EffEngage]':
|
|
1288
354
|
protocolState.state.changeState(new StateEngage(idx, evt));
|
|
1289
355
|
break;
|
|
1290
|
-
case '2PhW[
|
|
356
|
+
case '2PhW[JSON_AckEngage]':
|
|
1291
357
|
protocolState.state.changeState(new StateAckEngage(idx, evt));
|
|
1292
358
|
break;
|
|
1293
359
|
}
|
|
@@ -1314,36 +380,9 @@ class TwoPhW {
|
|
|
1314
380
|
return this._2PhW_eventLog;
|
|
1315
381
|
}
|
|
1316
382
|
//-------------------------------------------------------------------------------------------------
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
if (! validate_Options_delay_(json_msgOptions.options))
|
|
1321
|
-
return false;
|
|
1322
|
-
|
|
1323
|
-
if (JSON_MsgWarp.validate(json_msgOptions._cause)) {
|
|
1324
|
-
const json_msgWarp= json_msgOptions._cause;
|
|
1325
|
-
|
|
1326
|
-
const ctx= get_ctx(json_msgWarp.id);
|
|
1327
|
-
if (!ctx )
|
|
1328
|
-
return false;
|
|
1329
|
-
|
|
1330
|
-
const ttl_msec= json_msgOptions.options.ttl_msec_incr[0];
|
|
1331
|
-
const ttl_incr= json_msgOptions.options.ttl_msec_incr[1];
|
|
1332
|
-
|
|
1333
|
-
//TODO: double check ttl_msec is the same as current accummulated ttl for idempotence.
|
|
1334
|
-
|
|
1335
|
-
logr_.log(l_.HANDLERS, () => ['handler_msgOptions_: ', json_msgOptions]);
|
|
1336
|
-
|
|
1337
|
-
ctx.protocol.delayTimeout(ttl_incr);
|
|
1338
|
-
|
|
1339
|
-
if (cfg._callback_msgOptions)
|
|
1340
|
-
return cfg._callback_msgOptions(msg, json_msgOptions);
|
|
1341
|
-
|
|
1342
|
-
return true;
|
|
1343
|
-
}
|
|
1344
|
-
*/
|
|
1345
|
-
return false;
|
|
1346
|
-
}
|
|
383
|
+
// The typed handlers (handler_json_<message>) are what the responder's base dispatch calls AFTER it
|
|
384
|
+
// validated the message as that type (generated: `JSON_AckWarp.validate(json_ack) &&
|
|
385
|
+
// this.handler_json_ackWarp(msg, json_ack)`). They do not validate again.
|
|
1347
386
|
//-------------------------------------------------------------------------------------------------
|
|
1348
387
|
// sender Source (src)
|
|
1349
388
|
register_msgWarp(msg, json_msgWarp) {
|
|
@@ -1351,173 +390,295 @@ class TwoPhW {
|
|
|
1351
390
|
const protocolState = this.eventLog.traverse_State(json_msgWarp);
|
|
1352
391
|
const b_changeState = protocolState.canChangeState(new StateWarp(null, {
|
|
1353
392
|
dt: Date.now(),
|
|
1354
|
-
data: { msg, json_msg: json_msgWarp }
|
|
393
|
+
data: { msg, json_msg: json_msgWarp, b_sent: true }
|
|
1355
394
|
}));
|
|
1356
395
|
logr_.log(l_.HANDLERS | l_.SENDER, () => ['register_msgWarp: protocolState.canChangeState ', b_changeState]);
|
|
1357
396
|
if (!b_changeState)
|
|
1358
397
|
return false;
|
|
1359
|
-
|
|
398
|
+
// the first answer, a hold or the ack, is due within the Warp's dt + TTL*2
|
|
399
|
+
const timeout_ms = window_msec_(json_msgWarp.warp.ttl_msec);
|
|
1360
400
|
const elapsed_ms = Date.now() - Date.parse(json_msgWarp.dt);
|
|
1361
|
-
this.eventLog._addEvent(msg, json_msgWarp, timeout_ms - elapsed_ms, () => {
|
|
1362
|
-
this.config.
|
|
401
|
+
this.eventLog._addEvent(msg, json_msgWarp, true, timeout_ms - elapsed_ms, () => {
|
|
402
|
+
this.config._callback_sender_sent_expired(msg, json_msgWarp);
|
|
1363
403
|
});
|
|
404
|
+
return true;
|
|
1364
405
|
}
|
|
1365
|
-
|
|
1366
|
-
logr_.log(l_.HANDLERS | l_.SENDER, () => ['
|
|
1367
|
-
|
|
406
|
+
handler_json_ackWarp(msg, json_ackWarp) {
|
|
407
|
+
logr_.log(l_.HANDLERS | l_.SENDER, () => ['handler_json_ackWarp: ', json_ackWarp]);
|
|
408
|
+
// no callback for this step: not ours -- before anything is logged
|
|
409
|
+
if (!this.config._callback_ackWarp)
|
|
410
|
+
return false;
|
|
411
|
+
const json_msgWarp = json_ackWarp._cause;
|
|
1368
412
|
const protocolState = this.eventLog.traverse_State(json_msgWarp);
|
|
1369
413
|
const b_changeState = protocolState.canChangeState(new StateAckWarp(null, {
|
|
1370
414
|
dt: Date.now(),
|
|
1371
|
-
data: { msg, json_msg:
|
|
415
|
+
data: { msg, json_msg: json_ackWarp, b_sent: false }
|
|
1372
416
|
}));
|
|
1373
|
-
logr_.log(l_.HANDLERS | l_.SENDER, () => ['
|
|
417
|
+
logr_.log(l_.HANDLERS | l_.SENDER, () => ['handler_json_ackWarp: protocolState.canChangeState ', b_changeState]);
|
|
1374
418
|
if (!b_changeState)
|
|
1375
419
|
return false;
|
|
1376
|
-
|
|
1377
|
-
this.eventLog.
|
|
1378
|
-
|
|
420
|
+
// the Warp's own timer, or the one of the hold that last moved its window
|
|
421
|
+
this.eventLog._cancelTimeout(protocolState.state.idx_event_timer);
|
|
422
|
+
this.eventLog._addEvent(msg, json_ackWarp, false);
|
|
423
|
+
return this.config._callback_ackWarp(msg, json_ackWarp);
|
|
424
|
+
}
|
|
425
|
+
// The receiver says it is working on a Warp we sent and has not decided yet (JSON_HoldWarp). Not a step:
|
|
426
|
+
// the exchange stays where it is, waiting for the JSON_AckWarp. What changes is how long we wait for it:
|
|
427
|
+
// the expiry of the Warp moves to the end of the hold's lease.
|
|
428
|
+
handler_json_holdWarp(msg, json_holdWarp) {
|
|
429
|
+
logr_.log(l_.HANDLERS | l_.SENDER, () => ['handler_json_holdWarp: ', json_holdWarp]);
|
|
430
|
+
// the sender role is not played here: not ours -- before anything is logged
|
|
431
|
+
if (!this.config._callback_ackWarp)
|
|
432
|
+
return false;
|
|
433
|
+
const json_msgWarp = json_holdWarp._cause;
|
|
434
|
+
// ours iff it is about a Warp WE sent that is still unanswered and still inside its window. Anything
|
|
435
|
+
// else (a Warp we only heard, one already acknowledged or expired) is somebody else's, or too late.
|
|
436
|
+
const state = this.eventLog.traverse_State(json_msgWarp).state;
|
|
437
|
+
if (!(state instanceof StateWarp) || !state.event.data.b_sent)
|
|
438
|
+
return false;
|
|
439
|
+
const nr_now = Date.now();
|
|
440
|
+
if (nr_now >= state.deadline_msec)
|
|
441
|
+
return false;
|
|
442
|
+
const nr_deadline = StateWarp.deadline_holdWarp(json_holdWarp);
|
|
443
|
+
if (!Number.isFinite(nr_deadline))
|
|
444
|
+
return false; // a dt that does not read: a lease with no anchor
|
|
445
|
+
if (nr_deadline > state.deadline_msec) {
|
|
446
|
+
// the timer follows the window: the one in force is cancelled, this hold carries the new one.
|
|
447
|
+
// What expires is still the Warp: "the step I sent was never answered".
|
|
448
|
+
const { msg: msg_Warp, json_msg: json_msgWarp_sent } = state.event.data;
|
|
449
|
+
this.eventLog._cancelTimeout(state.idx_event_timer);
|
|
450
|
+
this.eventLog._addEvent(msg, json_holdWarp, false, nr_deadline - nr_now, () => {
|
|
451
|
+
this.config._callback_sender_sent_expired(msg_Warp, json_msgWarp_sent);
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
else
|
|
455
|
+
this.eventLog._addEvent(msg, json_holdWarp, false); // ends inside the window in force: logged, nothing moves
|
|
456
|
+
return this.config._callback_holdWarp ? this.config._callback_holdWarp(msg, json_holdWarp) : true;
|
|
1379
457
|
}
|
|
1380
|
-
|
|
1381
|
-
logr_.log(l_.SENDER, () => ['
|
|
1382
|
-
const json_msgWarp =
|
|
458
|
+
register_effEngage(msg, json_effEngage) {
|
|
459
|
+
logr_.log(l_.SENDER, () => ['register_effEngage: ', json_effEngage]);
|
|
460
|
+
const json_msgWarp = json_effEngage._cause;
|
|
1383
461
|
const protocolState = this.eventLog.traverse_State(json_msgWarp);
|
|
1384
462
|
const b_changeState = protocolState.canChangeState(new StateEngage(null, {
|
|
1385
463
|
dt: Date.now(),
|
|
1386
|
-
data: { msg, json_msg:
|
|
464
|
+
data: { msg, json_msg: json_effEngage, b_sent: true }
|
|
1387
465
|
}));
|
|
1388
|
-
logr_.log(l_.HANDLERS | l_.SENDER, () => ['
|
|
466
|
+
logr_.log(l_.HANDLERS | l_.SENDER, () => ['register_effEngage: protocolState.canChangeState ', b_changeState]);
|
|
1389
467
|
if (!b_changeState)
|
|
1390
468
|
return false;
|
|
1391
|
-
|
|
1392
|
-
const
|
|
1393
|
-
|
|
1394
|
-
|
|
469
|
+
// the AckEngage is due within the Engage's own dt + TTL*2
|
|
470
|
+
const timeout_ms = window_msec_(json_msgWarp.warp.ttl_msec);
|
|
471
|
+
const elapsed_ms = Date.now() - Date.parse(json_effEngage.dt);
|
|
472
|
+
this.eventLog._addEvent(msg, json_effEngage, true, timeout_ms - elapsed_ms, () => {
|
|
473
|
+
this.config._callback_sender_sent_expired(msg, json_effEngage);
|
|
1395
474
|
});
|
|
475
|
+
return true;
|
|
1396
476
|
}
|
|
1397
|
-
|
|
1398
|
-
logr_.log(l_.HANDLERS | l_.SENDER, () => ['
|
|
1399
|
-
|
|
477
|
+
handler_json_ackEngage(msg, json_ackEngage) {
|
|
478
|
+
logr_.log(l_.HANDLERS | l_.SENDER, () => ['handler_json_ackEngage: ', json_ackEngage]);
|
|
479
|
+
// no callback for this step: not ours -- before anything is logged
|
|
480
|
+
if (!this.config._callback_ackEngage)
|
|
481
|
+
return false;
|
|
482
|
+
const json_msgWarp = json_ackEngage._cause._cause;
|
|
1400
483
|
const protocolState = this.eventLog.traverse_State(json_msgWarp);
|
|
1401
484
|
const b_changeState = protocolState.canChangeState(new StateAckEngage(null, {
|
|
1402
485
|
dt: Date.now(),
|
|
1403
|
-
data: { msg, json_msg:
|
|
486
|
+
data: { msg, json_msg: json_ackEngage, b_sent: false }
|
|
1404
487
|
}));
|
|
1405
|
-
logr_.log(l_.HANDLERS | l_.SENDER, () => ['
|
|
488
|
+
logr_.log(l_.HANDLERS | l_.SENDER, () => ['handler_json_ackEngage: protocolState.canChangeState ', b_changeState]);
|
|
1406
489
|
if (!b_changeState)
|
|
1407
490
|
return false;
|
|
1408
491
|
this.eventLog._cancelTimeout(protocolState.state.idx_event);
|
|
1409
|
-
this.eventLog._addEvent(msg,
|
|
1410
|
-
return this.config.
|
|
492
|
+
this.eventLog._addEvent(msg, json_ackEngage, false);
|
|
493
|
+
return this.config._callback_ackEngage(msg, json_ackEngage);
|
|
1411
494
|
}
|
|
1412
495
|
//-------------------------------------------------------------------------------------------------
|
|
1413
496
|
// receiver Destination (dst)
|
|
1414
|
-
|
|
1415
|
-
logr_.log(l_.HANDLERS | l_.RECVER, () => ['
|
|
497
|
+
handler_json_msgWarp(msg, json_msgWarp) {
|
|
498
|
+
logr_.log(l_.HANDLERS | l_.RECVER, () => ['handler_json_msgWarp: ', json_msgWarp]);
|
|
499
|
+
// no callback for this step: not ours -- before anything is logged
|
|
500
|
+
if (!this.config._callback_msgWarp)
|
|
501
|
+
return false;
|
|
1416
502
|
const protocolState = this.eventLog.traverse_State(json_msgWarp);
|
|
1417
503
|
const b_changeState = protocolState.canChangeState(new StateWarp(null, {
|
|
1418
504
|
dt: Date.now(),
|
|
1419
|
-
data: { msg, json_msg: json_msgWarp }
|
|
505
|
+
data: { msg, json_msg: json_msgWarp, b_sent: false }
|
|
1420
506
|
}));
|
|
1421
|
-
logr_.log(l_.HANDLERS | l_.RECVER, () => ['
|
|
507
|
+
logr_.log(l_.HANDLERS | l_.RECVER, () => ['handler_json_msgWarp: protocolState.canChangeState: ', b_changeState]);
|
|
1422
508
|
if (!b_changeState)
|
|
1423
509
|
return false;
|
|
1424
|
-
this.eventLog._addEvent(msg, json_msgWarp);
|
|
510
|
+
this.eventLog._addEvent(msg, json_msgWarp, false);
|
|
1425
511
|
return this.config._callback_msgWarp(msg, json_msgWarp);
|
|
1426
512
|
}
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
513
|
+
// Register a JSON_HoldWarp we are about to publish: "this Warp is being worked on, wait for the ack until
|
|
514
|
+
// my lease ends". Legal only for a Warp we RECEIVED, have not answered, and whose window is still open
|
|
515
|
+
// (a window that has closed cannot be reopened). It is what lets register_ackWarp succeed later than the
|
|
516
|
+
// Warp's own dt + TTL*2. Returns true, or false when it is not legal now: then do not publish it.
|
|
517
|
+
register_holdWarp(msg, json_holdWarp) {
|
|
518
|
+
logr_.log(l_.RECVER, () => ['register_holdWarp: ', json_holdWarp]);
|
|
519
|
+
const json_msgWarp = json_holdWarp._cause;
|
|
520
|
+
const state = this.eventLog.traverse_State(json_msgWarp).state;
|
|
521
|
+
if (!(state instanceof StateWarp) || state.event.data.b_sent)
|
|
522
|
+
return false;
|
|
523
|
+
if (Date.now() >= state.deadline_msec)
|
|
524
|
+
return false;
|
|
525
|
+
if (!Number.isFinite(StateWarp.deadline_holdWarp(json_holdWarp)))
|
|
526
|
+
return false;
|
|
527
|
+
this.eventLog._addEvent(msg, json_holdWarp, true);
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
register_ackWarp(msg, json_ackWarp) {
|
|
531
|
+
logr_.log(l_.RECVER, () => ['register_ackWarp: ', json_ackWarp]);
|
|
532
|
+
const json_msgWarp = json_ackWarp._cause;
|
|
1430
533
|
const protocolState = this.eventLog.traverse_State(json_msgWarp);
|
|
1431
534
|
const b_changeState = protocolState.canChangeState(new StateAckWarp(null, {
|
|
1432
535
|
dt: Date.now(),
|
|
1433
|
-
data: { msg, json_msg:
|
|
536
|
+
data: { msg, json_msg: json_ackWarp, b_sent: true }
|
|
1434
537
|
}));
|
|
1435
|
-
logr_.log(l_.HANDLERS | l_.RECVER, () => ['
|
|
538
|
+
logr_.log(l_.HANDLERS | l_.RECVER, () => ['register_ackWarp: protocolState.canChangeState: ', b_changeState]);
|
|
1436
539
|
if (!b_changeState)
|
|
1437
540
|
return false;
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
this.
|
|
541
|
+
// The expiry means "the Engage I was waiting for never came". An ack no Engage can follow
|
|
542
|
+
// concludes the exchange and awaits nothing: logged, with no timer.
|
|
543
|
+
if (!isEngageAwaited_(json_ackWarp)) {
|
|
544
|
+
this.eventLog._addEvent(msg, json_ackWarp, true);
|
|
545
|
+
return true;
|
|
546
|
+
}
|
|
547
|
+
// the Engage is due within the ack's own dt + TTL*2
|
|
548
|
+
const elapsed_ms = Date.now() - Date.parse(json_ackWarp.dt);
|
|
549
|
+
const timeout_ms = window_msec_(json_msgWarp.warp.ttl_msec);
|
|
550
|
+
this.eventLog._addEvent(msg, json_ackWarp, true, timeout_ms - elapsed_ms, () => {
|
|
551
|
+
this.config._callback_receiver_sent_expired(msg, json_ackWarp);
|
|
1442
552
|
});
|
|
553
|
+
return true;
|
|
1443
554
|
}
|
|
1444
|
-
|
|
1445
|
-
logr_.log(l_.HANDLERS | l_.RECVER, () => ['
|
|
1446
|
-
|
|
555
|
+
handler_json_effEngage(msg, json_effEngage) {
|
|
556
|
+
logr_.log(l_.HANDLERS | l_.RECVER, () => ['handler_json_effEngage: ', json_effEngage]);
|
|
557
|
+
// no callback for this step: not ours -- before anything is logged
|
|
558
|
+
if (!this.config._callback_effEngage)
|
|
559
|
+
return false;
|
|
560
|
+
const json_msgWarp = json_effEngage._cause;
|
|
1447
561
|
const protocolState = this.eventLog.traverse_State(json_msgWarp);
|
|
1448
562
|
const b_changeState = protocolState.canChangeState(new StateEngage(null, {
|
|
1449
563
|
dt: Date.now(),
|
|
1450
|
-
data: { msg, json_msg:
|
|
564
|
+
data: { msg, json_msg: json_effEngage, b_sent: false }
|
|
1451
565
|
}));
|
|
1452
|
-
logr_.log(l_.HANDLERS | l_.RECVER, () => ['
|
|
566
|
+
logr_.log(l_.HANDLERS | l_.RECVER, () => ['handler_json_effEngage: protocolState.canChangeState: ', b_changeState]);
|
|
1453
567
|
if (!b_changeState)
|
|
1454
568
|
return false;
|
|
1455
569
|
this.eventLog._cancelTimeout(protocolState.state.idx_event);
|
|
1456
|
-
this.eventLog._addEvent(msg,
|
|
1457
|
-
return this.config.
|
|
570
|
+
this.eventLog._addEvent(msg, json_effEngage, false);
|
|
571
|
+
return this.config._callback_effEngage(msg, json_effEngage);
|
|
1458
572
|
}
|
|
1459
|
-
|
|
1460
|
-
logr_.log(l_.RECVER, () => ['
|
|
1461
|
-
const json_msgWarp =
|
|
573
|
+
register_ackEngage(msg, json_ackEngage) {
|
|
574
|
+
logr_.log(l_.RECVER, () => ['register_ackEngage: ', json_ackEngage]);
|
|
575
|
+
const json_msgWarp = json_ackEngage._cause._cause;
|
|
1462
576
|
const protocolState = this.eventLog.traverse_State(json_msgWarp);
|
|
1463
577
|
const b_changeState = protocolState.canChangeState(new StateAckEngage(null, {
|
|
1464
578
|
dt: Date.now(),
|
|
1465
|
-
data: { msg, json_msg:
|
|
579
|
+
data: { msg, json_msg: json_ackEngage, b_sent: true }
|
|
1466
580
|
}));
|
|
1467
|
-
logr_.log(l_.HANDLERS | l_.RECVER, () => ['
|
|
581
|
+
logr_.log(l_.HANDLERS | l_.RECVER, () => ['register_ackEngage: protocolState.canChangeState: ', b_changeState]);
|
|
1468
582
|
if (!b_changeState)
|
|
1469
583
|
return false;
|
|
1470
|
-
this.eventLog._addEvent(msg,
|
|
1471
|
-
|
|
1472
|
-
//-------------------------------------------------------------------------------------------------
|
|
1473
|
-
handler_json_msgAck(msg, json_msgAck) {
|
|
1474
|
-
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msgAck: ', json_msgAck]);
|
|
1475
|
-
if (JSON_MsgAckWarp.validate(json_msgAck) && this._handler_msgAckWarp(msg, json_msgAck))
|
|
1476
|
-
return true;
|
|
1477
|
-
if (JSON_MsgAckEngage.validate(json_msgAck) && this._handler_msgAckEngage(msg, json_msgAck))
|
|
1478
|
-
return true;
|
|
1479
|
-
return false;
|
|
1480
|
-
}
|
|
1481
|
-
handler_json_msgEffect(msg, json_msgEffect) {
|
|
1482
|
-
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msgEffect: ', json_msgEffect]);
|
|
1483
|
-
// if (JSON_MsgAck.validate(json_msgEffect) && this.handler_json_msgAck(msg, json_msgEffect) )
|
|
1484
|
-
// return true;
|
|
1485
|
-
if (JSON_MsgEngage.validate(json_msgEffect) && this._handler_msgEngage(msg, json_msgEffect))
|
|
1486
|
-
return true;
|
|
1487
|
-
// if (JSON_MsgOptions.validate(json_msg) && this.handler_msgOptions_(this._config, msg, json_msg))
|
|
1488
|
-
// return true;
|
|
1489
|
-
return false;
|
|
1490
|
-
}
|
|
1491
|
-
handler_json_msg(msg, json_msg) {
|
|
1492
|
-
logr_.log(l_.HANDLERS, () => ['TwoPhW: handler_json_msg: ', json_msg]);
|
|
1493
|
-
if (JSON_MsgWarp.validate(json_msg) && this._handler_msgWarp(msg, json_msg))
|
|
1494
|
-
return true;
|
|
1495
|
-
// https://attacomsian.com/blog/javascript-iterate-objects
|
|
1496
|
-
// Object.keys(json_msg).forEach((key, index) => {
|
|
1497
|
-
// if (key == 'dt')
|
|
1498
|
-
// return;
|
|
1499
|
-
// if (key == 'referer')
|
|
1500
|
-
// return;
|
|
1501
|
-
// console.log(`${key}: ${json_msg[key]}`);
|
|
1502
|
-
// });
|
|
1503
|
-
return false;
|
|
584
|
+
this.eventLog._addEvent(msg, json_ackEngage, true);
|
|
585
|
+
return true;
|
|
1504
586
|
}
|
|
1505
587
|
}
|
|
588
|
+
// Bind the protocol to the HOST's generated interface. Called once, where the responder is set up:
|
|
589
|
+
//
|
|
590
|
+
// import * as iface_TwoPhW from '../generated/iface-TwoPhW';
|
|
591
|
+
// const { TwoPhW, JSON_MsgWarp, JSON_EffEngage, JSON_AckWarp, JSON_HoldWarp, JSON_AckEngage } = protocol_TwoPhW(iface_TwoPhW);
|
|
592
|
+
//
|
|
593
|
+
// Returns the state machine and, per message, the generated namespace (validate + merge /
|
|
594
|
+
// create_with_cause_merge; the validators are delta-only: the base is validated upstream, in the
|
|
595
|
+
// responder chain) extended with what the schema cannot express: the protocol's own constructors.
|
|
596
|
+
// Whatever is built through them carries the interface's own VERSION, so the package cannot speak
|
|
597
|
+
// another version of 2PhW than its host validates.
|
|
598
|
+
function protocol_TwoPhW(iface_TwoPhW) {
|
|
599
|
+
// What was handed in is the host's to get right: a wrong import (the module's default export, another
|
|
600
|
+
// interface) must fail here, where it is made, not as an undefined deep inside an exchange.
|
|
601
|
+
const arr_str_missing = [
|
|
602
|
+
['VERSION', typeof iface_TwoPhW?.VERSION === 'string'],
|
|
603
|
+
['JSON_MsgWarp.validate', typeof iface_TwoPhW?.JSON_MsgWarp?.validate === 'function'],
|
|
604
|
+
['JSON_MsgWarp.merge', typeof iface_TwoPhW?.JSON_MsgWarp?.merge === 'function'],
|
|
605
|
+
['JSON_AckWarp.validate', typeof iface_TwoPhW?.JSON_AckWarp?.validate === 'function'],
|
|
606
|
+
['JSON_HoldWarp.validate', typeof iface_TwoPhW?.JSON_HoldWarp?.validate === 'function'],
|
|
607
|
+
['JSON_HoldWarp.create_with_cause_ttl', typeof iface_TwoPhW?.JSON_HoldWarp?.create_with_cause_ttl === 'function'],
|
|
608
|
+
['JSON_EffEngage.validate', typeof iface_TwoPhW?.JSON_EffEngage?.validate === 'function'],
|
|
609
|
+
['JSON_EffEngage.create_with_cause_merge', typeof iface_TwoPhW?.JSON_EffEngage?.create_with_cause_merge === 'function'],
|
|
610
|
+
['JSON_AckEngage.validate', typeof iface_TwoPhW?.JSON_AckEngage?.validate === 'function']
|
|
611
|
+
].filter(([_str_member, b_present]) => !b_present).map(([str_member]) => str_member);
|
|
612
|
+
if (arr_str_missing.length)
|
|
613
|
+
throw new Error(`protocol_TwoPhW: not a generated 2PhW interface, it lacks: ${arr_str_missing.join(', ')}`);
|
|
614
|
+
const k_VERSION = iface_TwoPhW.VERSION;
|
|
615
|
+
const JSON_MsgWarp = {
|
|
616
|
+
...iface_TwoPhW.JSON_MsgWarp,
|
|
617
|
+
create_with_id_user_hyperport: function (json_warp_id, json_user, json_hyperport, nr_ttl_msec) {
|
|
618
|
+
return iface_TwoPhW.JSON_MsgWarp.merge({
|
|
619
|
+
id: json_warp_id,
|
|
620
|
+
user: json_user,
|
|
621
|
+
hyperport: json_hyperport,
|
|
622
|
+
ttl_msec: nr_ttl_msec
|
|
623
|
+
});
|
|
624
|
+
},
|
|
625
|
+
compare: compare_msgWarp_
|
|
626
|
+
};
|
|
627
|
+
const JSON_EffEngage = {
|
|
628
|
+
...iface_TwoPhW.JSON_EffEngage,
|
|
629
|
+
create_with_Warp: function (json_msgWarp) {
|
|
630
|
+
console.assert(json_msgWarp.ack === undefined, "Engage with a Warp, not an Ack");
|
|
631
|
+
return iface_TwoPhW.JSON_EffEngage.create_with_cause_merge(json_msgWarp, {
|
|
632
|
+
"ok": true
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
};
|
|
636
|
+
// iface-gen emits no constructor for a JSON_Ack: build the base ack, stamp the protocol marker
|
|
637
|
+
const JSON_AckWarp = {
|
|
638
|
+
...iface_TwoPhW.JSON_AckWarp,
|
|
639
|
+
create_with_Warp_Ok: function (json_msgWarp, b_ok = true) {
|
|
640
|
+
const json_ack = jsonMsg.JSON_Ack.create_with_cause_Ok(json_msgWarp, b_ok);
|
|
641
|
+
json_ack["2PhW"] = k_VERSION;
|
|
642
|
+
return json_ack;
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
const JSON_AckEngage = {
|
|
646
|
+
...iface_TwoPhW.JSON_AckEngage,
|
|
647
|
+
create_with_Engage_Ok: function (json_effEngage, b_ok = true) {
|
|
648
|
+
const json_ack = jsonMsg.JSON_Ack.create_with_cause_Ok(json_effEngage, b_ok);
|
|
649
|
+
json_ack["2PhW"] = k_VERSION;
|
|
650
|
+
return json_ack;
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
return {
|
|
654
|
+
TwoPhW,
|
|
655
|
+
VERSION: k_VERSION,
|
|
656
|
+
JSON_MsgWarp,
|
|
657
|
+
JSON_EffEngage,
|
|
658
|
+
JSON_AckWarp,
|
|
659
|
+
JSON_HoldWarp: iface_TwoPhW.JSON_HoldWarp, // as generated: its constructor is built from the lease already
|
|
660
|
+
JSON_AckEngage
|
|
661
|
+
};
|
|
662
|
+
}
|
|
1506
663
|
|
|
1507
|
-
exports
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
exports
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
664
|
+
Object.defineProperty(exports, "JSON_Ack", {
|
|
665
|
+
enumerable: true,
|
|
666
|
+
get: function () { return jsonMsg.JSON_Ack; }
|
|
667
|
+
});
|
|
668
|
+
Object.defineProperty(exports, "JSON_Eff", {
|
|
669
|
+
enumerable: true,
|
|
670
|
+
get: function () { return jsonMsg.JSON_Eff; }
|
|
671
|
+
});
|
|
672
|
+
Object.defineProperty(exports, "JSON_Msg", {
|
|
673
|
+
enumerable: true,
|
|
674
|
+
get: function () { return jsonMsg.JSON_Msg; }
|
|
675
|
+
});
|
|
1515
676
|
exports.StateAckEngage = StateAckEngage;
|
|
1516
677
|
exports.StateAckWarp = StateAckWarp;
|
|
1517
678
|
exports.StateEngage = StateEngage;
|
|
1518
679
|
exports.StateINVALID = StateINVALID;
|
|
1519
680
|
exports.StateInitialState = StateInitialState;
|
|
1520
681
|
exports.StateWarp = StateWarp;
|
|
1521
|
-
exports.TwoPhW = TwoPhW;
|
|
1522
682
|
exports.logr = logr_;
|
|
1523
|
-
exports.
|
|
683
|
+
exports.protocol_TwoPhW = protocol_TwoPhW;
|
|
684
|
+
exports.window_msec = window_msec_;
|