@rootintf/protocol-2phw 0.7.5-api-0.7 → 1.0.28-api-0.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rootintf/protocol-2phw",
3
- "version": "0.7.5-api-0.7",
3
+ "version": "1.0.28-api-0.8",
4
4
  "description": "",
5
5
  "main": "dist/twoPhW.cjs.js",
6
6
  "browser": "dist/twoPhW.es.mjs",
@@ -26,8 +26,9 @@
26
26
  },
27
27
  "homepage": "https://gitlab.com/interface-x/npm-protocol-2PhW.git#readme",
28
28
  "dependencies": {
29
- "@knev/bitlogr": "^0.2.4",
30
- "@rootintf/json-msg": "^0.2.1"
29
+ "@knev/bitlogr": "^2.0.11",
30
+ "@rootintf/json-msg": "^1.1.1",
31
+ "ajv": "^8.17.1"
31
32
  },
32
33
  "devDependencies": {
33
34
  "@rollup/plugin-commonjs": "^28.0.3",
@@ -36,7 +37,7 @@
36
37
  "ajv-cli": "^5.0.0",
37
38
  "del-cli": "^6.0.0",
38
39
  "jasmine": "^5.6.0",
39
- "rollup": "^4.35.0",
40
+ "rollup": "^4.39.0",
40
41
  "uuid": "^11.1.0"
41
42
  }
42
43
  }
@@ -0,0 +1,12 @@
1
+ {
2
+ "$id" : "https://root-interface.se/protocols/2PhW/definitions_2PhW",
3
+ "$schema" : "https://json-schema.org/draft/2019-09/schema",
4
+ "definitions" : {
5
+ "2PhW" : {
6
+ "const": "0.8"
7
+ },
8
+ "warp_id" : {
9
+ "type" : "string"
10
+ }
11
+ }
12
+ }
@@ -1,12 +1,21 @@
1
1
  {
2
- "$id": "validate_MsgEngage",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/2PhW/schema_MsgEngage",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["engage"],
6
6
  "properties" : {
7
7
  "engage" : {
8
8
  "type" : "object",
9
- "required" : ["user", "hyperport"]
10
- }
9
+ "required" : [ "ok", "rtt_msec" ],
10
+ "properties": {
11
+ "ok" : {
12
+ "type" : "boolean"
13
+ },
14
+ "rtt_msec" : {
15
+ "type" : "integer"
16
+ }
17
+ }
18
+ },
19
+ "2PhW" : { "$ref": "definitions_2PhW#/definitions/2PhW" }
11
20
  }
12
21
  }
@@ -1,11 +1,12 @@
1
1
  {
2
- "$id": "validate_MsgOptions",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/2PhW/schema_MsgOptions",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["options"],
6
6
  "properties" : {
7
7
  "options" : {
8
8
  "type" : "object"
9
- }
9
+ },
10
+ "2PhW" : { "$ref": "definitions_2PhW#/definitions/2PhW" }
10
11
  }
11
12
  }
@@ -1,13 +1,14 @@
1
1
  {
2
- "$id": "validate_MsgWarp",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/2PhW/schema_MsgWarp",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["warp"],
6
6
  "properties" : {
7
7
  "warp" : {
8
8
  "type" : "object",
9
- "required" : ["user", "hyperport"],
9
+ "required" : ["id", "user", "hyperport", "ttl_msec"],
10
10
  "properties": {
11
+ "id" : { "$ref": "definitions_2PhW#/definitions/warp_id" },
11
12
  "ttl_msec" : {
12
13
  "type" : "integer"
13
14
  },
@@ -15,6 +16,7 @@
15
16
  "type" : "boolean"
16
17
  }
17
18
  }
18
- }
19
+ },
20
+ "2PhW" : { "$ref": "definitions_2PhW#/definitions/2PhW" }
19
21
  }
20
22
  }
@@ -1,6 +1,6 @@
1
1
  {
2
- "$id": "validate_Options_delay",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/2PhW/schema_Options_delay",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["ttl_msec_incr"],
6
6
  "properties" : {
@@ -0,0 +1,136 @@
1
+
2
+ import { LOGR } from '@knev/bitlogr';
3
+
4
+ //-------------------------------------------------------------------------------------------------
5
+
6
+ let LOGR_= LOGR.get_instance();
7
+ const log_= LOGR_.log;
8
+
9
+ const l_= {
10
+ EVENTS : 0b1 << 0,
11
+ TIMEOUTS : 0b1 << 1,
12
+ }
13
+ LOGR_.labels= l_;
14
+ // LOGR_.toggled= {
15
+ // EVENTS : 1
16
+ // }
17
+
18
+
19
+ //-------------------------------------------------------------------------------------------------
20
+
21
+ class EventLog {
22
+ constructor() {
23
+ this.arr_events = []; // In-memory event store
24
+ this.map_index_id = new Map(); // Map of id -> array of indices into arr_events; arr_events[idx]
25
+ this.map_timeouts = new Map(); // Store timeout references: idx -> timeout_id
26
+ }
27
+
28
+ addEvent(type, id, data, timeout_ms = null, timeout_callback = null) {
29
+ const evt = {
30
+ type,
31
+ dt: Date.now(),
32
+ id,
33
+ data,
34
+ timeout_id: null // Will store timeout reference if set
35
+ };
36
+ const idx = this.arr_events.push(evt) - 1; // More efficient than .length before push
37
+
38
+ let arr_indices = this.map_index_id.get(id);
39
+ if (! arr_indices) {
40
+ arr_indices = [];
41
+ this.map_index_id.set(id, arr_indices);
42
+ }
43
+ arr_indices.push(idx);
44
+
45
+ // Set timeout if specified
46
+ if (timeout_ms && timeout_callback) {
47
+ evt.timeout_id = setTimeout(() => {
48
+ if (this.map_timeouts.delete(idx)) {
49
+ evt.timeout_id = null;
50
+ timeout_callback(evt);
51
+ log_(l_.EVENTS, `Event DELETED[${idx}]: ${type}`, evt);
52
+ }
53
+ }, timeout_ms);
54
+
55
+ this.map_timeouts.set(idx, evt.timeout_id);
56
+ }
57
+
58
+ log_(l_.EVENTS, `Event added[${idx}] @${Date(evt.dt).toISOString()}: ${type}`, evt);
59
+ return { idx, evt };
60
+ }
61
+
62
+ cancelTimeout(idx) {
63
+ const timeout_id = this.map_timeouts.get(idx);
64
+ if (timeout_id) {
65
+ clearTimeout(timeout_id);
66
+ this.map_timeouts.delete(idx);
67
+ if (this.arr_events[idx])
68
+ this.arr_events[idx].timeout_id = null;
69
+ return true;
70
+ }
71
+ return false;
72
+ }
73
+
74
+ cancelAllTimeouts() {
75
+ for (const [idx, timeout_id] of this.map_timeouts) {
76
+ clearTimeout(timeout_id);
77
+ if (this.arr_events[idx])
78
+ this.arr_events[idx].timeout_id = null;
79
+ }
80
+ this.map_timeouts.clear();
81
+ }
82
+
83
+ getEventById(id, lookback_ms = null) {
84
+ const arr_indices = this.map_index_id.get(id);
85
+ if (!arr_indices?.length) return [];
86
+
87
+ const result = [];
88
+ const cutoff = lookback_ms !== null ? Date.now() - lookback_ms : -Infinity;
89
+
90
+ // Single loop with early cutoff
91
+ for (let i = arr_indices.length - 1; i >= 0; i--) {
92
+ const idx = arr_indices[i];
93
+ const evt = this.arr_events[idx];
94
+ if (evt.dt < cutoff)
95
+ break;
96
+ result.push({ idx, evt });
97
+ }
98
+
99
+ return result; // Newest to oldest order
100
+ }
101
+
102
+ // getRecentEvents(id, lookbackMs) {
103
+ // const cutoff = Date.now() - lookbackMs;
104
+ // return this.events.filter(e => e.id === id && e.dt >= cutoff);
105
+ // }
106
+
107
+ getEvents(lookback_ms, predicate) {
108
+ const cutoff = Date.now() - lookback_ms;
109
+ const result = [];
110
+
111
+ // Iterate backwards from the end
112
+ for (let idx = this.arr_events.length - 1; idx >= 0; idx--) {
113
+ const evt = this.arr_events[idx];
114
+ if (evt.dt < cutoff) {
115
+ break; // Stop once we pass the cutoff
116
+ }
117
+ if (predicate(evt)) {
118
+ result.push({ idx, evt });
119
+ }
120
+ }
121
+
122
+ return result; // Newest to oldest order
123
+ }
124
+
125
+ // setLogr(obj_logr) {
126
+ // this.logr.toggled = obj_logr;
127
+ // }
128
+
129
+ clear() {
130
+ this.cancelAllTimeouts(); // Clear all timeouts before clearing events
131
+ this.arr_events.length = 0;
132
+ this.map_index_id.clear();
133
+ }
134
+ }
135
+
136
+ export { l_ as l, EventLog };
@@ -1,15 +1,16 @@
1
1
 
2
- var schema_json_msg2PhW= require('../schema/schema_Msg2PhW.json');
3
- var schema_json_msgOptions_= require('../schema/schema_MsgOptions.json');
4
- var schema_json_options_delay_= require('../schema/schema_Options_delay.json');
5
- var schema_json_msgWarp_= require('../schema/schema_MsgWarp.json');
6
- var schema_json_msgEngage_= require('../schema/schema_MsgEngage.json');
2
+
3
+ var json_definitions_2PhW_= require('../schema/definitions_2PhW.json');
4
+ var json_schema_msgWarp_= require('../schema/schema_MsgWarp.json');
5
+ var json_schema_msgEngage_= require('../schema/schema_MsgEngage.json');
6
+ var json_schema_msgOptions_= require('../schema/schema_MsgOptions.json');
7
+ var json_schema_options_delay_= require('../schema/schema_Options_delay.json');
7
8
 
8
9
  // https://ajv.js.org/standalone.html
9
10
 
10
11
  const fs = require("fs")
11
12
  const path = require("path")
12
- const Ajv = require("ajv")
13
+ const Ajv2019 = require("ajv/dist/2019");
13
14
  const standaloneCode = require("ajv/dist/standalone").default
14
15
 
15
16
  // For ESM, the export name needs to be a valid export name, it can not be `export const #/definitions/Foo = ...;` so we
@@ -17,8 +18,23 @@ const standaloneCode = require("ajv/dist/standalone").default
17
18
  // `export const Foo = ...;export const Bar = ...;`
18
19
  // This mapping would not have been needed if the `$ids` was just `Bar` and `Foo` instead of `#/definitions/Foo`
19
20
  // and `#/definitions/Bar` respectfully
20
- const ajv = new Ajv({schemas: [schema_json_msg2PhW, schema_json_msgOptions_, schema_json_options_delay_, schema_json_msgWarp_, schema_json_msgEngage_], code: {source: true, esm: true}})
21
- let moduleCode = standaloneCode(ajv)
21
+ const ajv = new Ajv2019({
22
+ schemas: [
23
+ json_definitions_2PhW_,
24
+ json_schema_msgWarp_,
25
+ json_schema_msgEngage_,
26
+ json_schema_msgOptions_,
27
+ json_schema_options_delay_
28
+ ],
29
+ code: {source: true, esm: true}
30
+ });
31
+ let moduleCode = standaloneCode(ajv, {
32
+ "definitions_2PhW": "https://root-interface.se/protocols/2PhW/definitions_2PhW",
33
+ "schema_MsgWarp": "https://root-interface.se/protocols/2PhW/schema_MsgWarp",
34
+ "schema_MsgEngage": "https://root-interface.se/protocols/2PhW/schema_MsgEngage",
35
+ "schema_MsgOptions": "https://root-interface.se/protocols/2PhW/schema_MsgOptions",
36
+ "schema_Options_delay": "https://root-interface.se/protocols/2PhW/schema_Options_delay",
37
+ })
22
38
 
23
39
  // Now you can write the module code to file
24
40
  fs.writeFileSync(path.join(__dirname, "./validate-esm.mjs"), moduleCode)