@rootintf/json-msg 4.0.2-api.4 → 4.0.6-api.5

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 CHANGED
@@ -1,43 +1,124 @@
1
+ # npm-json-msg (`@rootintf/json-msg`)
1
2
 
3
+ The npm implementation of the **`JSON_Msg` envelopes**, at api.5: the envelope every message on an
4
+ [IPSME](https://ipsme.dev) messaging environment (ME) travels in, and the three that build on it.
5
+ It is the npm twin of `cpp-json-msg`.
6
+
7
+ | Envelope | Extends | Adds | What it is |
8
+ |---|---|---|---|
9
+ | `JSON_Msg` | | `id`, `referer`, `dt` | a message: which one, who published it, when |
10
+ | `JSON_Eff` | `JSON_Msg` | `_cause` | an effect: a message published because of another, which it carries whole |
11
+ | `JSON_Ack` | `JSON_Eff` | `ack` | an acknowledgement: the final answer to its cause, with a verdict (`ack.ok`) |
12
+ | `JSON_Prog` | `JSON_Eff` | `progress` | a progress: an interim answer. The cause is being worked on and is not concluded |
13
+
14
+ What the fields mean is in the specification (`spec-json-msg` on <https://root-interface.se/protocols>).
15
+ How to use the package in full is in `antora-docs/modules/ROOT/pages/index.adoc`; this file is the short version.
16
+
17
+ ---
18
+
19
+ ## Install
20
+
21
+ ```sh
22
+ npm i @rootintf/json-msg
2
23
  ```
3
- {
4
- "$id": "validate_Msg",
5
- "$schema": "http://json-schema.org/draft-07/schema#",
6
- "type" : "object",
7
- "required" : ["id", "referer", "dt"],
8
- "properties" : {
9
- "id" : { "type" : "string"},
10
- "referer" : { "type" : "string"},
11
- "dt" : { "type" : "string" }
12
- }
13
- }
24
+
25
+ Dual ESM and CJS, with types.
26
+
27
+ ```js
28
+ import { JSON_Msg, JSON_Eff, JSON_Ack, JSON_Prog } from '@rootintf/json-msg';
14
29
  ```
15
30
 
31
+ A project rarely calls the package for its own messages. The interfaces `@rootintf/iface-gen` generates
32
+ build on it; this package is the base runtime they share.
33
+
34
+ ---
35
+
36
+ ## Use
37
+
38
+ ```js
39
+ // a message: a fresh id, dt = now, referer = null
40
+ const json_msg = JSON_Msg.merge({ discover: { ttl_msec: 3000 } });
41
+ json_msg.referer = [ 'app-subverse-map', str_instance ]; // [participant, instance]; stamp before publishing
42
+
43
+ // an effect of it
44
+ const json_eff = JSON_Eff.create_with_cause_merge(json_msg, { announce: {} });
45
+
46
+ // the final answer
47
+ const json_ack = JSON_Ack.create_with_cause_Ok(json_msg); // ack: { ok: true }
48
+ const json_nack = JSON_Ack.create_with_cause_Ok(json_msg, false); // ack: { ok: false }
49
+ const json_late = JSON_Ack.create_with_cause_Expired(json_msg); // ack: { ok: false, expired: true }
50
+
51
+ // an interim answer: "still on it; expect the next word within 15 s"
52
+ const json_prog = JSON_Prog.create_with_cause_ttl(json_msg, 15000, { eta_msec: 240000, done_pct: 40, stage: 'downloading' });
16
53
  ```
17
- {
18
- "$id": "validate_Msg_msg",
19
- "$schema": "http://json-schema.org/draft-07/schema#",
20
- "type" : "object",
21
- "required" : ["_msg"]
22
- }
54
+
55
+ ### A progress
56
+
57
+ `progress.ttl_msec` is a lease, measured from the progress's own `dt`: the next word about the cause,
58
+ another progress or the acknowledgement, is due before it runs out. It is the only required field.
59
+ `eta_msec` (an estimate of the time left), `done_pct` (0 to 100) and `stage` are optional.
60
+
61
+ ```js
62
+ const i_lease_msec = JSON_Prog.lease_epoch_msec(json_prog); // dt + ttl_msec, or null when dt does not read
23
63
  ```
24
64
 
65
+ The package defines the fields and this arithmetic, and no policy: how much of a lease is believed, and
66
+ for how long renewals are honoured, is decided by the protocol and the participant.
67
+
68
+ ### Validating
69
+
70
+ Each `validate` is cumulative (`JSON_Ack.validate` also checks `JSON_Eff` and `JSON_Msg`) and is a
71
+ TypeScript type predicate. A message that fails is not an error; it is somebody else's, and is dropped.
72
+
73
+ ```js
74
+ if (JSON_Ack.validate(json)) { /* json.ack.ok, json._cause */ }
75
+ else if (JSON_Prog.validate(json)) { /* json.progress.ttl_msec, json._cause */ }
76
+ else if (JSON_Eff.validate(json)) { /* json._cause */ }
77
+ else if (JSON_Msg.validate(json)) { /* a message */ }
78
+ ```
79
+
80
+ Test for an acknowledgement before a progress: a message that carries both is an acknowledgement.
81
+
82
+ ### Reading `dt`
83
+
84
+ ```js
85
+ const i_dt_msec = JSON_Msg.dt_epoch_msec(json_msg); // epoch milliseconds, or null
25
86
  ```
26
- {
27
- "$id": "validate_MsgAck",
28
- "$schema": "http://json-schema.org/draft-07/schema#",
29
- "type" : "object",
30
- "required" : ["ack"],
31
- "properties" : {
32
- "ack" : {
33
- "type" : "object",
34
- "required" : ["ok"],
35
- "properties" : {
36
- "ok" : { "type" : "boolean" }
37
- }
38
- }
39
- }
40
- }
87
+
88
+ It reads UTC with or without a fraction of a second (`2026-11-25T08:59:19.692Z`, `2026-11-25T08:59:19Z`),
89
+ the forms `cpp-json-msg` reads, and nothing else.
90
+
91
+ ---
92
+
93
+ ## Exports
94
+
95
+ | Export | What |
96
+ |---|---|
97
+ | `JSON_Msg.merge(json)` | a new message, with `json` merged over `id`, `dt`, `referer: null` |
98
+ | `JSON_Msg.dt_epoch_msec(json_msg)` | the message's `dt` as epoch milliseconds, or `null` |
99
+ | `JSON_Msg.validate(json)` | is it a `JSON_Msg`? |
100
+ | `JSON_Eff.create_with_cause_merge(msg_cause, json)` | a new effect of `msg_cause` |
101
+ | `JSON_Eff.validate(json)` | is it an effect? |
102
+ | `JSON_Ack.create_with_cause_Ok(msg_cause, b_ok = true)` | a new acknowledgement, with the verdict |
103
+ | `JSON_Ack.create_with_cause_Expired(msg_cause)` | a new acknowledgement, `ok: false` and `expired: true` |
104
+ | `JSON_Ack.validate(json)` | is it an acknowledgement? |
105
+ | `JSON_Prog.create_with_cause_ttl(msg_cause, ttl_msec, progress = {})` | a new progress: the lease, with the members of `progress` merged in |
106
+ | `JSON_Prog.lease_epoch_msec(json_prog)` | the instant the lease ends, or `null` |
107
+ | `JSON_Prog.validate(json)` | is it a progress? |
108
+ | `logr` | the package's `@knev/bitlogr` logger (name `npm:json-msg`, label `VALIDATION`) |
109
+
110
+ Each of the four names is also a TypeScript type, for a protocol's own messages to extend.
111
+
112
+ ---
113
+
114
+ ## Develop
115
+
116
+ ```sh
117
+ npm ci
118
+ npm run build # compile the validators from the schema, then rollup -> dist
119
+ npm test # jasmine, against dist
41
120
  ```
42
121
 
43
- TODO: add optional expired property
122
+ The validators are compiled, not interpreted: `build` turns `schema/schema_json-msg.json` into standalone
123
+ Ajv code (`src/validate-esm.mjs`), which is bundled into `dist`. The schema's SHA-256 is next to it, in
124
+ `schema/schema_json-msg.json.sha256`, and is the one `cpp-json-msg` carries.
@@ -111,6 +111,128 @@ function validate20(data, { instancePath = "", parentData, parentDataProperty, r
111
111
  }
112
112
  } validate20.errors = vErrors; return errors === 0; }
113
113
  validate20.evaluated = { "props": { "ack": true }, "dynamicProps": false, "dynamicItems": false };
114
+ const schema_Prog = validate21;
115
+ function validate21(data, { instancePath = "", parentData, parentDataProperty, rootData = data, dynamicAnchors = {} } = {}) { let vErrors = null; let errors = 0; const evaluated0 = validate21.evaluated; if (evaluated0.dynamicProps) {
116
+ evaluated0.props = undefined;
117
+ } if (evaluated0.dynamicItems) {
118
+ evaluated0.items = undefined;
119
+ } {
120
+ if (data && typeof data == "object" && !Array.isArray(data)) {
121
+ let missing0;
122
+ if ((data.progress === undefined) && (missing0 = "progress")) {
123
+ validate21.errors = [{ instancePath, schemaPath: "#/required", keyword: "required", params: { missingProperty: missing0 }, message: "must have required property '" + missing0 + "'" }];
124
+ return false;
125
+ }
126
+ else {
127
+ if (data.progress !== undefined) {
128
+ let data0 = data.progress;
129
+ {
130
+ if (data0 && typeof data0 == "object" && !Array.isArray(data0)) {
131
+ let missing1;
132
+ if ((data0.ttl_msec === undefined) && (missing1 = "ttl_msec")) {
133
+ validate21.errors = [{ instancePath: instancePath + "/progress", schemaPath: "#/properties/progress/required", keyword: "required", params: { missingProperty: missing1 }, message: "must have required property '" + missing1 + "'" }];
134
+ return false;
135
+ }
136
+ else {
137
+ if (data0.ttl_msec !== undefined) {
138
+ let data1 = data0.ttl_msec;
139
+ const _errs4 = errors;
140
+ if (!(((typeof data1 == "number") && (!(data1 % 1) && !isNaN(data1))) && (isFinite(data1)))) {
141
+ validate21.errors = [{ instancePath: instancePath + "/progress/ttl_msec", schemaPath: "#/properties/progress/properties/ttl_msec/type", keyword: "type", params: { type: "integer" }, message: "must be integer" }];
142
+ return false;
143
+ }
144
+ {
145
+ if ((typeof data1 == "number") && (isFinite(data1))) {
146
+ if (data1 <= 0 || isNaN(data1)) {
147
+ validate21.errors = [{ instancePath: instancePath + "/progress/ttl_msec", schemaPath: "#/properties/progress/properties/ttl_msec/exclusiveMinimum", keyword: "exclusiveMinimum", params: { comparison: ">", limit: 0 }, message: "must be > 0" }];
148
+ return false;
149
+ }
150
+ }
151
+ }
152
+ var valid1 = _errs4 === errors;
153
+ }
154
+ else {
155
+ var valid1 = true;
156
+ }
157
+ if (valid1) {
158
+ if (data0.eta_msec !== undefined) {
159
+ let data2 = data0.eta_msec;
160
+ const _errs6 = errors;
161
+ if (!(((typeof data2 == "number") && (!(data2 % 1) && !isNaN(data2))) && (isFinite(data2)))) {
162
+ validate21.errors = [{ instancePath: instancePath + "/progress/eta_msec", schemaPath: "#/properties/progress/properties/eta_msec/type", keyword: "type", params: { type: "integer" }, message: "must be integer" }];
163
+ return false;
164
+ }
165
+ {
166
+ if ((typeof data2 == "number") && (isFinite(data2))) {
167
+ if (data2 < 0 || isNaN(data2)) {
168
+ validate21.errors = [{ instancePath: instancePath + "/progress/eta_msec", schemaPath: "#/properties/progress/properties/eta_msec/minimum", keyword: "minimum", params: { comparison: ">=", limit: 0 }, message: "must be >= 0" }];
169
+ return false;
170
+ }
171
+ }
172
+ }
173
+ var valid1 = _errs6 === errors;
174
+ }
175
+ else {
176
+ var valid1 = true;
177
+ }
178
+ if (valid1) {
179
+ if (data0.done_pct !== undefined) {
180
+ let data3 = data0.done_pct;
181
+ const _errs8 = errors;
182
+ {
183
+ if ((typeof data3 == "number") && (isFinite(data3))) {
184
+ if (data3 > 100 || isNaN(data3)) {
185
+ validate21.errors = [{ instancePath: instancePath + "/progress/done_pct", schemaPath: "#/properties/progress/properties/done_pct/maximum", keyword: "maximum", params: { comparison: "<=", limit: 100 }, message: "must be <= 100" }];
186
+ return false;
187
+ }
188
+ else {
189
+ if (data3 < 0 || isNaN(data3)) {
190
+ validate21.errors = [{ instancePath: instancePath + "/progress/done_pct", schemaPath: "#/properties/progress/properties/done_pct/minimum", keyword: "minimum", params: { comparison: ">=", limit: 0 }, message: "must be >= 0" }];
191
+ return false;
192
+ }
193
+ }
194
+ }
195
+ else {
196
+ validate21.errors = [{ instancePath: instancePath + "/progress/done_pct", schemaPath: "#/properties/progress/properties/done_pct/type", keyword: "type", params: { type: "number" }, message: "must be number" }];
197
+ return false;
198
+ }
199
+ }
200
+ var valid1 = _errs8 === errors;
201
+ }
202
+ else {
203
+ var valid1 = true;
204
+ }
205
+ if (valid1) {
206
+ if (data0.stage !== undefined) {
207
+ const _errs10 = errors;
208
+ if (typeof data0.stage !== "string") {
209
+ validate21.errors = [{ instancePath: instancePath + "/progress/stage", schemaPath: "#/properties/progress/properties/stage/type", keyword: "type", params: { type: "string" }, message: "must be string" }];
210
+ return false;
211
+ }
212
+ var valid1 = _errs10 === errors;
213
+ }
214
+ else {
215
+ var valid1 = true;
216
+ }
217
+ }
218
+ }
219
+ }
220
+ }
221
+ }
222
+ else {
223
+ validate21.errors = [{ instancePath: instancePath + "/progress", schemaPath: "#/properties/progress/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
224
+ return false;
225
+ }
226
+ }
227
+ }
228
+ }
229
+ }
230
+ else {
231
+ validate21.errors = [{ instancePath, schemaPath: "#/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
232
+ return false;
233
+ }
234
+ } validate21.errors = vErrors; return errors === 0; }
235
+ validate21.evaluated = { "props": { "progress": true }, "dynamicProps": false, "dynamicItems": false };
114
236
 
115
237
  const byteToHex = [];
116
238
  for (let i = 0; i < 256; ++i) {
@@ -166,6 +288,10 @@ const LOGR_ = bitlogr.LOGR.get_instance();
166
288
  const logr_ = LOGR_.create({ name: "npm:json-msg", labels: bitlogr.l_array(['VALIDATION']) });
167
289
  const l_ = logr_.l;
168
290
  // TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
291
+ // `dt` on the wire: UTC, this calendar format, an optional fraction of a second, then 'Z'. merge() writes it
292
+ // with milliseconds (toISOString); dt_epoch_msec() reads that, any fraction, and the form without one. These are
293
+ // the forms cpp-json-msg reads, and no other: Date.parse alone would accept more than the twin does.
294
+ const kre_DT_ = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/;
169
295
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
170
296
  var JSON_Msg = (function () {
171
297
  return {
@@ -176,6 +302,14 @@ var JSON_Msg = (function () {
176
302
  dt: new Date().toISOString()
177
303
  }, json || {});
178
304
  },
305
+ // when the message was created, read back from `dt` as epoch milliseconds; null when there is no `dt` or
306
+ // it does not read as the format merge() writes (see kre_DT_)
307
+ dt_epoch_msec: function (json_msg) {
308
+ if (!json_msg || typeof json_msg.dt !== 'string' || !kre_DT_.test(json_msg.dt))
309
+ return null;
310
+ const i_dt_msec = Date.parse(json_msg.dt);
311
+ return Number.isNaN(i_dt_msec) ? null : i_dt_msec;
312
+ },
179
313
  validate: function (json) {
180
314
  const b_valid = schema_Msg(json);
181
315
  if (!b_valid)
@@ -233,8 +367,34 @@ var JSON_Ack = (function () {
233
367
  }
234
368
  };
235
369
  })();
370
+ var JSON_Prog = (function () {
371
+ return {
372
+ // progress: { ttl_msec }, with the members of `progress` merged into it (eta_msec, done_pct, stage, a
373
+ // protocol's own). The lease is the parameter, whatever `progress` carried.
374
+ create_with_cause_ttl: function (msg_cause, ttl_msec, progress) {
375
+ return JSON_Eff.create_with_cause_merge(msg_cause, {
376
+ progress: Object.assign({}, progress || {}, { ttl_msec }),
377
+ });
378
+ },
379
+ // the instant the lease ends, as epoch milliseconds: the message's own dt + progress.ttl_msec. null when the
380
+ // dt does not read: a lease with no anchor, which the caller cannot honour.
381
+ lease_epoch_msec: function (json_prog) {
382
+ const i_dt_msec = JSON_Msg.dt_epoch_msec(json_prog);
383
+ return (i_dt_msec === null) ? null : i_dt_msec + json_prog.progress.ttl_msec;
384
+ },
385
+ validate: function (json) {
386
+ if (!JSON_Eff.validate(json))
387
+ return false;
388
+ const b_valid = schema_Prog(json);
389
+ if (!b_valid)
390
+ logr_.log(l_.VALIDATION, () => ['JSON_Prog: validate: ', schema_Prog.errors]);
391
+ return b_valid;
392
+ }
393
+ };
394
+ })();
236
395
 
237
396
  exports.JSON_Ack = JSON_Ack;
238
397
  exports.JSON_Eff = JSON_Eff;
239
398
  exports.JSON_Msg = JSON_Msg;
399
+ exports.JSON_Prog = JSON_Prog;
240
400
  exports.logr = logr_;
@@ -109,6 +109,128 @@ function validate20(data, { instancePath = "", parentData, parentDataProperty, r
109
109
  }
110
110
  } validate20.errors = vErrors; return errors === 0; }
111
111
  validate20.evaluated = { "props": { "ack": true }, "dynamicProps": false, "dynamicItems": false };
112
+ const schema_Prog = validate21;
113
+ function validate21(data, { instancePath = "", parentData, parentDataProperty, rootData = data, dynamicAnchors = {} } = {}) { let vErrors = null; let errors = 0; const evaluated0 = validate21.evaluated; if (evaluated0.dynamicProps) {
114
+ evaluated0.props = undefined;
115
+ } if (evaluated0.dynamicItems) {
116
+ evaluated0.items = undefined;
117
+ } {
118
+ if (data && typeof data == "object" && !Array.isArray(data)) {
119
+ let missing0;
120
+ if ((data.progress === undefined) && (missing0 = "progress")) {
121
+ validate21.errors = [{ instancePath, schemaPath: "#/required", keyword: "required", params: { missingProperty: missing0 }, message: "must have required property '" + missing0 + "'" }];
122
+ return false;
123
+ }
124
+ else {
125
+ if (data.progress !== undefined) {
126
+ let data0 = data.progress;
127
+ {
128
+ if (data0 && typeof data0 == "object" && !Array.isArray(data0)) {
129
+ let missing1;
130
+ if ((data0.ttl_msec === undefined) && (missing1 = "ttl_msec")) {
131
+ validate21.errors = [{ instancePath: instancePath + "/progress", schemaPath: "#/properties/progress/required", keyword: "required", params: { missingProperty: missing1 }, message: "must have required property '" + missing1 + "'" }];
132
+ return false;
133
+ }
134
+ else {
135
+ if (data0.ttl_msec !== undefined) {
136
+ let data1 = data0.ttl_msec;
137
+ const _errs4 = errors;
138
+ if (!(((typeof data1 == "number") && (!(data1 % 1) && !isNaN(data1))) && (isFinite(data1)))) {
139
+ validate21.errors = [{ instancePath: instancePath + "/progress/ttl_msec", schemaPath: "#/properties/progress/properties/ttl_msec/type", keyword: "type", params: { type: "integer" }, message: "must be integer" }];
140
+ return false;
141
+ }
142
+ {
143
+ if ((typeof data1 == "number") && (isFinite(data1))) {
144
+ if (data1 <= 0 || isNaN(data1)) {
145
+ validate21.errors = [{ instancePath: instancePath + "/progress/ttl_msec", schemaPath: "#/properties/progress/properties/ttl_msec/exclusiveMinimum", keyword: "exclusiveMinimum", params: { comparison: ">", limit: 0 }, message: "must be > 0" }];
146
+ return false;
147
+ }
148
+ }
149
+ }
150
+ var valid1 = _errs4 === errors;
151
+ }
152
+ else {
153
+ var valid1 = true;
154
+ }
155
+ if (valid1) {
156
+ if (data0.eta_msec !== undefined) {
157
+ let data2 = data0.eta_msec;
158
+ const _errs6 = errors;
159
+ if (!(((typeof data2 == "number") && (!(data2 % 1) && !isNaN(data2))) && (isFinite(data2)))) {
160
+ validate21.errors = [{ instancePath: instancePath + "/progress/eta_msec", schemaPath: "#/properties/progress/properties/eta_msec/type", keyword: "type", params: { type: "integer" }, message: "must be integer" }];
161
+ return false;
162
+ }
163
+ {
164
+ if ((typeof data2 == "number") && (isFinite(data2))) {
165
+ if (data2 < 0 || isNaN(data2)) {
166
+ validate21.errors = [{ instancePath: instancePath + "/progress/eta_msec", schemaPath: "#/properties/progress/properties/eta_msec/minimum", keyword: "minimum", params: { comparison: ">=", limit: 0 }, message: "must be >= 0" }];
167
+ return false;
168
+ }
169
+ }
170
+ }
171
+ var valid1 = _errs6 === errors;
172
+ }
173
+ else {
174
+ var valid1 = true;
175
+ }
176
+ if (valid1) {
177
+ if (data0.done_pct !== undefined) {
178
+ let data3 = data0.done_pct;
179
+ const _errs8 = errors;
180
+ {
181
+ if ((typeof data3 == "number") && (isFinite(data3))) {
182
+ if (data3 > 100 || isNaN(data3)) {
183
+ validate21.errors = [{ instancePath: instancePath + "/progress/done_pct", schemaPath: "#/properties/progress/properties/done_pct/maximum", keyword: "maximum", params: { comparison: "<=", limit: 100 }, message: "must be <= 100" }];
184
+ return false;
185
+ }
186
+ else {
187
+ if (data3 < 0 || isNaN(data3)) {
188
+ validate21.errors = [{ instancePath: instancePath + "/progress/done_pct", schemaPath: "#/properties/progress/properties/done_pct/minimum", keyword: "minimum", params: { comparison: ">=", limit: 0 }, message: "must be >= 0" }];
189
+ return false;
190
+ }
191
+ }
192
+ }
193
+ else {
194
+ validate21.errors = [{ instancePath: instancePath + "/progress/done_pct", schemaPath: "#/properties/progress/properties/done_pct/type", keyword: "type", params: { type: "number" }, message: "must be number" }];
195
+ return false;
196
+ }
197
+ }
198
+ var valid1 = _errs8 === errors;
199
+ }
200
+ else {
201
+ var valid1 = true;
202
+ }
203
+ if (valid1) {
204
+ if (data0.stage !== undefined) {
205
+ const _errs10 = errors;
206
+ if (typeof data0.stage !== "string") {
207
+ validate21.errors = [{ instancePath: instancePath + "/progress/stage", schemaPath: "#/properties/progress/properties/stage/type", keyword: "type", params: { type: "string" }, message: "must be string" }];
208
+ return false;
209
+ }
210
+ var valid1 = _errs10 === errors;
211
+ }
212
+ else {
213
+ var valid1 = true;
214
+ }
215
+ }
216
+ }
217
+ }
218
+ }
219
+ }
220
+ else {
221
+ validate21.errors = [{ instancePath: instancePath + "/progress", schemaPath: "#/properties/progress/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
222
+ return false;
223
+ }
224
+ }
225
+ }
226
+ }
227
+ }
228
+ else {
229
+ validate21.errors = [{ instancePath, schemaPath: "#/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
230
+ return false;
231
+ }
232
+ } validate21.errors = vErrors; return errors === 0; }
233
+ validate21.evaluated = { "props": { "progress": true }, "dynamicProps": false, "dynamicItems": false };
112
234
 
113
235
  const byteToHex = [];
114
236
  for (let i = 0; i < 256; ++i) {
@@ -164,6 +286,10 @@ const LOGR_ = LOGR.get_instance();
164
286
  const logr_ = LOGR_.create({ name: "npm:json-msg", labels: l_array(['VALIDATION']) });
165
287
  const l_ = logr_.l;
166
288
  // TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
289
+ // `dt` on the wire: UTC, this calendar format, an optional fraction of a second, then 'Z'. merge() writes it
290
+ // with milliseconds (toISOString); dt_epoch_msec() reads that, any fraction, and the form without one. These are
291
+ // the forms cpp-json-msg reads, and no other: Date.parse alone would accept more than the twin does.
292
+ const kre_DT_ = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z$/;
167
293
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
168
294
  var JSON_Msg = (function () {
169
295
  return {
@@ -174,6 +300,14 @@ var JSON_Msg = (function () {
174
300
  dt: new Date().toISOString()
175
301
  }, json || {});
176
302
  },
303
+ // when the message was created, read back from `dt` as epoch milliseconds; null when there is no `dt` or
304
+ // it does not read as the format merge() writes (see kre_DT_)
305
+ dt_epoch_msec: function (json_msg) {
306
+ if (!json_msg || typeof json_msg.dt !== 'string' || !kre_DT_.test(json_msg.dt))
307
+ return null;
308
+ const i_dt_msec = Date.parse(json_msg.dt);
309
+ return Number.isNaN(i_dt_msec) ? null : i_dt_msec;
310
+ },
177
311
  validate: function (json) {
178
312
  const b_valid = schema_Msg(json);
179
313
  if (!b_valid)
@@ -231,5 +365,30 @@ var JSON_Ack = (function () {
231
365
  }
232
366
  };
233
367
  })();
368
+ var JSON_Prog = (function () {
369
+ return {
370
+ // progress: { ttl_msec }, with the members of `progress` merged into it (eta_msec, done_pct, stage, a
371
+ // protocol's own). The lease is the parameter, whatever `progress` carried.
372
+ create_with_cause_ttl: function (msg_cause, ttl_msec, progress) {
373
+ return JSON_Eff.create_with_cause_merge(msg_cause, {
374
+ progress: Object.assign({}, progress || {}, { ttl_msec }),
375
+ });
376
+ },
377
+ // the instant the lease ends, as epoch milliseconds: the message's own dt + progress.ttl_msec. null when the
378
+ // dt does not read: a lease with no anchor, which the caller cannot honour.
379
+ lease_epoch_msec: function (json_prog) {
380
+ const i_dt_msec = JSON_Msg.dt_epoch_msec(json_prog);
381
+ return (i_dt_msec === null) ? null : i_dt_msec + json_prog.progress.ttl_msec;
382
+ },
383
+ validate: function (json) {
384
+ if (!JSON_Eff.validate(json))
385
+ return false;
386
+ const b_valid = schema_Prog(json);
387
+ if (!b_valid)
388
+ logr_.log(l_.VALIDATION, () => ['JSON_Prog: validate: ', schema_Prog.errors]);
389
+ return b_valid;
390
+ }
391
+ };
392
+ })();
234
393
 
235
- export { JSON_Ack, JSON_Eff, JSON_Msg, logr_ as logr };
394
+ export { JSON_Ack, JSON_Eff, JSON_Msg, JSON_Prog, logr_ as logr };
@@ -13,8 +13,19 @@ interface JSON_Ack extends JSON_Eff {
13
13
  expired?: boolean;
14
14
  };
15
15
  }
16
+ interface JSON_Prog extends JSON_Eff {
17
+ progress: {
18
+ ttl_msec: number;
19
+ eta_msec?: number;
20
+ done_pct?: number;
21
+ stage?: string;
22
+ };
23
+ }
16
24
  declare var JSON_Msg: {
17
25
  merge: <T extends object>(json: T) => JSON_Msg & T;
26
+ dt_epoch_msec: (json_msg: {
27
+ dt?: unknown;
28
+ }) => number | null;
18
29
  validate: (json: unknown) => json is JSON_Msg;
19
30
  };
20
31
  declare var JSON_Eff: {
@@ -26,4 +37,9 @@ declare var JSON_Ack: {
26
37
  create_with_cause_Expired: (msg_cause: JSON_Msg) => JSON_Ack;
27
38
  validate: (json: unknown) => json is JSON_Ack;
28
39
  };
29
- export { JSON_Msg, JSON_Eff, JSON_Ack, logr_ as logr };
40
+ declare var JSON_Prog: {
41
+ create_with_cause_ttl: <T extends object>(msg_cause: JSON_Msg, ttl_msec: number, progress?: T) => JSON_Prog;
42
+ lease_epoch_msec: (json_prog: JSON_Prog) => number | null;
43
+ validate: (json: unknown) => json is JSON_Prog;
44
+ };
45
+ export { JSON_Msg, JSON_Eff, JSON_Ack, JSON_Prog, logr_ as logr };
package/package.json CHANGED
@@ -1,58 +1,58 @@
1
- {
2
- "name": "@rootintf/json-msg",
3
- "version": "4.0.2-api.4",
4
- "description": "",
5
- "main": "dist/json-msg.cjs.js",
6
- "module": "dist/json-msg.es.mjs",
7
- "browser": "dist/json-msg.es.mjs",
8
- "types": "dist/json_msg+ack.d.ts",
9
- "exports": {
10
- ".": {
11
- "types": "./dist/json_msg+ack.d.ts",
12
- "import": "./dist/json-msg.es.mjs",
13
- "require": "./dist/json-msg.cjs.js"
14
- },
15
- "./package.json": "./package.json"
16
- },
17
- "files": [
18
- "dist/json-msg.cjs.js",
19
- "dist/json-msg.es.mjs",
20
- "dist/json_msg+ack.d.ts"
21
- ],
22
- "scripts": {
23
- "build": "node src/ajv-gen-validate.cjs && rollup --config",
24
- "script": "node --experimental-json-modules tests/json-msg.test.mjs",
25
- "test": "jasmine --config=jasmine.json",
26
- "clean": "del-cli dist && del-cli src/validate-esm.mjs && echo OK"
27
- },
28
- "repository": {
29
- "type": "git",
30
- "url": "git+https://gitlab.com/interface-x/npm-json-msg.git"
31
- },
32
- "keywords": [
33
- "json"
34
- ],
35
- "author": "K.Nevelsteen, PhD",
36
- "license": "ISC",
37
- "bugs": {
38
- "url": "https://gitlab.com/interface-x/npm-json-msg.git/-/issues"
39
- },
40
- "homepage": "https://gitlab.com/interface-x/npm-json-msg.git#readme",
41
- "dependencies": {
42
- "@knev/bitlogr": "^3.4.8",
43
- "ajv": "^8.17.1",
44
- "uuid": "^14.0.1"
45
- },
46
- "devDependencies": {
47
- "@rollup/plugin-commonjs": "^29.0.0",
48
- "@rollup/plugin-json": "^6.1.0",
49
- "@rollup/plugin-node-resolve": "^16.0.3",
50
- "@rollup/plugin-typescript": "^12.3.0",
51
- "@types/node": "^24.10.1",
52
- "del-cli": "^7.0.0",
53
- "jasmine": "^6.3.0",
54
- "rollup": "^4.53.3",
55
- "tslib": "^2.8.1",
56
- "typescript": "^5.9.3"
57
- }
58
- }
1
+ {
2
+ "name": "@rootintf/json-msg",
3
+ "version": "4.0.6-api.5",
4
+ "description": "",
5
+ "main": "dist/json-msg.cjs.js",
6
+ "module": "dist/json-msg.es.mjs",
7
+ "browser": "dist/json-msg.es.mjs",
8
+ "types": "dist/json_msg+ack.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/json_msg+ack.d.ts",
12
+ "import": "./dist/json-msg.es.mjs",
13
+ "require": "./dist/json-msg.cjs.js"
14
+ },
15
+ "./package.json": "./package.json"
16
+ },
17
+ "files": [
18
+ "dist/json-msg.cjs.js",
19
+ "dist/json-msg.es.mjs",
20
+ "dist/json_msg+ack.d.ts"
21
+ ],
22
+ "scripts": {
23
+ "build": "node src/ajv-gen-validate.cjs && rollup --config",
24
+ "script": "node --experimental-json-modules tests/json-msg.test.mjs",
25
+ "test": "jasmine --config=jasmine.json",
26
+ "clean": "del-cli dist && del-cli src/validate-esm.mjs && echo OK"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://gitlab.com/interface-x/npm-json-msg.git"
31
+ },
32
+ "keywords": [
33
+ "json"
34
+ ],
35
+ "author": "K.Nevelsteen, PhD",
36
+ "license": "ISC",
37
+ "bugs": {
38
+ "url": "https://gitlab.com/interface-x/npm-json-msg.git/-/issues"
39
+ },
40
+ "homepage": "https://gitlab.com/interface-x/npm-json-msg.git#readme",
41
+ "dependencies": {
42
+ "@knev/bitlogr": "^3.4.8",
43
+ "ajv": "^8.17.1",
44
+ "uuid": "^14.0.1"
45
+ },
46
+ "devDependencies": {
47
+ "@rollup/plugin-commonjs": "^29.0.0",
48
+ "@rollup/plugin-json": "^6.1.0",
49
+ "@rollup/plugin-node-resolve": "^16.0.3",
50
+ "@rollup/plugin-typescript": "^12.3.0",
51
+ "@types/node": "^24.10.1",
52
+ "del-cli": "^7.0.0",
53
+ "jasmine": "^6.3.0",
54
+ "rollup": "^4.53.3",
55
+ "tslib": "^2.8.1",
56
+ "typescript": "^5.9.3"
57
+ }
58
+ }