@rootintf/protocol-2phw 0.7.5-api-0.7 → 1.0.34-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,42 +1,43 @@
1
- {
2
- "name": "@rootintf/protocol-2phw",
3
- "version": "0.7.5-api-0.7",
4
- "description": "",
5
- "main": "dist/twoPhW.cjs.js",
6
- "browser": "dist/twoPhW.es.mjs",
7
- "scripts": {
8
- "build": "node src/ajv-gen-validate.cjs && rollup --config",
9
- "script": "node --experimental-json-modules tests/twoPhW.test.mjs",
10
- "test": "jasmine --config=jasmine.json",
11
- "clean": "del-cli dist && del-cli src/validate-esm.mjs && echo OK"
12
- },
13
- "repository": {
14
- "type": "git",
15
- "url": "git+https://gitlab.com/interface-x/npm-protocol-2PhW.git"
16
- },
17
- "keywords": [
18
- "metaverse",
19
- "warp",
20
- "protocol"
21
- ],
22
- "author": "K.Nevelsteen, PhD",
23
- "license": "ISC",
24
- "bugs": {
25
- "url": "https://gitlab.com/interface-x/npm-protocol-2PhW.git/-/issues"
26
- },
27
- "homepage": "https://gitlab.com/interface-x/npm-protocol-2PhW.git#readme",
28
- "dependencies": {
29
- "@knev/bitlogr": "^0.2.4",
30
- "@rootintf/json-msg": "^0.2.1"
31
- },
32
- "devDependencies": {
33
- "@rollup/plugin-commonjs": "^28.0.3",
34
- "@rollup/plugin-json": "^6.1.0",
35
- "@rollup/plugin-node-resolve": "^16.0.1",
36
- "ajv-cli": "^5.0.0",
37
- "del-cli": "^6.0.0",
38
- "jasmine": "^5.6.0",
39
- "rollup": "^4.35.0",
40
- "uuid": "^11.1.0"
41
- }
42
- }
1
+ {
2
+ "name": "@rootintf/protocol-2phw",
3
+ "version": "1.0.34-api.0.8",
4
+ "description": "",
5
+ "main": "dist/twoPhW.cjs.js",
6
+ "browser": "dist/twoPhW.es.mjs",
7
+ "scripts": {
8
+ "build": "node src/ajv-gen-validate.cjs && rollup --config",
9
+ "script": "node --experimental-json-modules tests/twoPhW.test.mjs",
10
+ "test": "jasmine --config=jasmine.json",
11
+ "clean": "del-cli dist && del-cli src/validate-esm.mjs && echo OK"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://gitlab.com/interface-x/npm-protocol-2PhW.git"
16
+ },
17
+ "keywords": [
18
+ "metaverse",
19
+ "warp",
20
+ "protocol"
21
+ ],
22
+ "author": "K.Nevelsteen, PhD",
23
+ "license": "ISC",
24
+ "bugs": {
25
+ "url": "https://gitlab.com/interface-x/npm-protocol-2PhW.git/-/issues"
26
+ },
27
+ "homepage": "https://gitlab.com/interface-x/npm-protocol-2PhW.git#readme",
28
+ "dependencies": {
29
+ "@knev/bitlogr": "^2.0.11",
30
+ "@rootintf/json-msg": "^1.1.5",
31
+ "ajv": "^8.17.1"
32
+ },
33
+ "devDependencies": {
34
+ "@rollup/plugin-commonjs": "^28.0.3",
35
+ "@rollup/plugin-json": "^6.1.0",
36
+ "@rollup/plugin-node-resolve": "^16.0.1",
37
+ "ajv-cli": "^5.0.0",
38
+ "del-cli": "^6.0.0",
39
+ "jasmine": "^5.6.0",
40
+ "rollup": "^4.39.0",
41
+ "uuid": "^11.1.0"
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,139 @@
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({idx, 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
+ // the error "toISOString is not a function" occurs because Date(evt.dt) is not doing what you expect.
59
+ // When you pass a number to the Date constructor without using new, it doesn’t return a Date object—it returns a string. Specifically, it’s equivalent to new Date(evt.dt).toString().
60
+ // Date(evt.dt).toISOString() -> new Date(evt.dt).toISOString()
61
+ log_(l_.EVENTS, () => [`Event added[${idx}] @${new Date(evt.dt).toISOString()}: ${type}`, evt]);
62
+ return { idx, evt };
63
+ }
64
+
65
+ cancelTimeout(idx) {
66
+ const timeout_id = this.map_timeouts.get(idx);
67
+ if (timeout_id) {
68
+ clearTimeout(timeout_id);
69
+ this.map_timeouts.delete(idx);
70
+ if (this.arr_events[idx])
71
+ this.arr_events[idx].timeout_id = null;
72
+ return true;
73
+ }
74
+ return false;
75
+ }
76
+
77
+ cancelAllTimeouts() {
78
+ for (const [idx, timeout_id] of this.map_timeouts) {
79
+ clearTimeout(timeout_id);
80
+ if (this.arr_events[idx])
81
+ this.arr_events[idx].timeout_id = null;
82
+ }
83
+ this.map_timeouts.clear();
84
+ }
85
+
86
+ getEventById(id, lookback_ms = null) {
87
+ const arr_indices = this.map_index_id.get(id);
88
+ if (!arr_indices?.length) return [];
89
+
90
+ const result = [];
91
+ const cutoff = lookback_ms !== null ? Date.now() - lookback_ms : -Infinity;
92
+
93
+ // Single loop with early cutoff
94
+ for (let i = arr_indices.length - 1; i >= 0; i--) {
95
+ const idx = arr_indices[i];
96
+ const evt = this.arr_events[idx];
97
+ if (evt.dt < cutoff)
98
+ break;
99
+ result.push({ idx, evt });
100
+ }
101
+
102
+ return result; // Newest to oldest order
103
+ }
104
+
105
+ // getRecentEvents(id, lookbackMs) {
106
+ // const cutoff = Date.now() - lookbackMs;
107
+ // return this.events.filter(e => e.id === id && e.dt >= cutoff);
108
+ // }
109
+
110
+ getEvents(lookback_ms, predicate) {
111
+ const cutoff = Date.now() - lookback_ms;
112
+ const result = [];
113
+
114
+ // Iterate backwards from the end
115
+ for (let idx = this.arr_events.length - 1; idx >= 0; idx--) {
116
+ const evt = this.arr_events[idx];
117
+ if (evt.dt < cutoff) {
118
+ break; // Stop once we pass the cutoff
119
+ }
120
+ if (predicate(evt)) {
121
+ result.push({ idx, evt });
122
+ }
123
+ }
124
+
125
+ return result; // Newest to oldest order
126
+ }
127
+
128
+ // setLogr(obj_logr) {
129
+ // this.logr.toggled = obj_logr;
130
+ // }
131
+
132
+ clear() {
133
+ this.cancelAllTimeouts(); // Clear all timeouts before clearing events
134
+ this.arr_events.length = 0;
135
+ this.map_index_id.clear();
136
+ }
137
+ }
138
+
139
+ 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)