@rootintf/json-msg 0.1.0 → 1.0.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.
@@ -0,0 +1,5 @@
1
+ name: npm-json-msg
2
+ title: npm-json-msg
3
+ version: "0.2"
4
+ nav:
5
+ - modules/ROOT/nav.adoc
@@ -0,0 +1 @@
1
+ // * xref:index.adoc[]
@@ -0,0 +1,76 @@
1
+ = npm-json-msg
2
+
3
+ Protocols: xref:protocols::JSON_Msg.adoc[]
4
+
5
+ [cols="24h,~"]
6
+ |===
7
+ |npm-json-msg
8
+ |https://gitlab.com/interop-protocols/npm-json-msg.git[^]
9
+ |===
10
+
11
+ == `JSON_Msg`
12
+
13
+ .JSON Schema, Draft-07
14
+ https://json-schema.org/specification-links.html#draft-7[^]
15
+ [source,json-schema/draft-07]
16
+ ----
17
+ {
18
+ "$id": "validate_Msg",
19
+ "$schema": "http://json-schema.org/draft-07/schema#",
20
+ "type" : "object",
21
+ "required" : ["id", "referer", "dt"],
22
+ "properties" : {
23
+ "id" : { "type" : "string"},
24
+ "referer" : {
25
+ "type" : "array",
26
+ "minItems" : 2,
27
+ "maxItems": 2,
28
+ "items": { "type" : "string" }
29
+ },
30
+ "dt" : { "type" : "string" }
31
+ }
32
+ }
33
+ ----
34
+
35
+ * *referer* [_sic_] --
36
+ ** `[0]` -- package, product or service name.
37
+ ** `[1]` -- instance specifier.
38
+
39
+ == `JSON_Msg_msg`
40
+
41
+ [source,json-schema/draft-07]
42
+ ----
43
+ {
44
+ "$id": "validate_Msg_msg",
45
+ "$schema": "http://json-schema.org/draft-07/schema#",
46
+ "type" : "object",
47
+ "required" : ["_msg"]
48
+
49
+ // ... JSON_Msg format
50
+ }
51
+ ----
52
+
53
+ * *_msg* -- original message this message is in regards to.
54
+
55
+ == `JSON_MsgAck`
56
+
57
+ [source,json-schema/draft-07]
58
+ ----
59
+ {
60
+ "$id": "validate_MsgAck",
61
+ "$schema": "http://json-schema.org/draft-07/schema#",
62
+ "type" : "object",
63
+ "required" : ["ack"],
64
+ "properties" : {
65
+ "ack" : {
66
+ "type" : "object",
67
+ "required" : ["ok"],
68
+ "properties" : {
69
+ "ok" : { "type" : "boolean" }
70
+ }
71
+ }
72
+ }
73
+
74
+ // ... JSON_Msg_msg format
75
+ }
76
+ ----
@@ -1,72 +1,61 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
3
+ const schema_Msg = validate18;function validate18(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate18.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate18.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate18.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs3 = errors;if(typeof data.dt !== "string"){validate18.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}}}}else {validate18.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate18.errors = vErrors;return errors === 0;}validate18.evaluated = {"props":{"id":true,"dt":true},"dynamicProps":false,"dynamicItems":false};const schema_Msg_msg = validate19;function validate19(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate19.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate19.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate19.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate19.errors = vErrors;return errors === 0;}validate19.evaluated = {"dynamicProps":false,"dynamicItems":false};const schema_MsgAck_msg = validate20;function validate20(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate20.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate20.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate20.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate20.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate20.errors = vErrors;return errors === 0;}validate20.evaluated = {"props":{"ack":true},"dynamicProps":false,"dynamicItems":false};
4
4
 
5
- const validate_Msg = validate10;function validate10(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate10.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate10.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.referer !== undefined){const _errs3 = errors;if(typeof data.referer !== "string"){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs5 = errors;if(typeof data.dt !== "string"){validate10.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs5 === errors;}else {var valid0 = true;}}}}}else {validate10.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10.errors = vErrors;return errors === 0;}const validate_Msg_msg = validate11;function validate11(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate11.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate11.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11.errors = vErrors;return errors === 0;}const validate_MsgAck = validate12;function validate12(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate12.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate12.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate12.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12.errors = vErrors;return errors === 0;}
5
+ const byteToHex = [];
6
+ for (let i = 0; i < 256; ++i) {
7
+ byteToHex.push((i + 0x100).toString(16).slice(1));
8
+ }
9
+ function unsafeStringify(arr, offset = 0) {
10
+ return (byteToHex[arr[offset + 0]] +
11
+ byteToHex[arr[offset + 1]] +
12
+ byteToHex[arr[offset + 2]] +
13
+ byteToHex[arr[offset + 3]] +
14
+ '-' +
15
+ byteToHex[arr[offset + 4]] +
16
+ byteToHex[arr[offset + 5]] +
17
+ '-' +
18
+ byteToHex[arr[offset + 6]] +
19
+ byteToHex[arr[offset + 7]] +
20
+ '-' +
21
+ byteToHex[arr[offset + 8]] +
22
+ byteToHex[arr[offset + 9]] +
23
+ '-' +
24
+ byteToHex[arr[offset + 10]] +
25
+ byteToHex[arr[offset + 11]] +
26
+ byteToHex[arr[offset + 12]] +
27
+ byteToHex[arr[offset + 13]] +
28
+ byteToHex[arr[offset + 14]] +
29
+ byteToHex[arr[offset + 15]]).toLowerCase();
30
+ }
6
31
 
7
- // Unique ID creation requires a high quality random # generator. In the browser we therefore
8
- // require the crypto API and do not support built-in fallback to lower quality random number
9
- // generators (like Math.random()).
10
32
  let getRandomValues;
11
33
  const rnds8 = new Uint8Array(16);
12
34
  function rng() {
13
- // lazy load so that environments that need to polyfill have a chance to do so
14
- if (!getRandomValues) {
15
- // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
16
- getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
17
-
18
35
  if (!getRandomValues) {
19
- throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
36
+ if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
37
+ throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
38
+ }
39
+ getRandomValues = crypto.getRandomValues.bind(crypto);
20
40
  }
21
- }
22
-
23
- return getRandomValues(rnds8);
24
- }
25
-
26
- /**
27
- * Convert array of 16 byte values to UUID string format of the form:
28
- * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
29
- */
30
-
31
- const byteToHex = [];
32
-
33
- for (let i = 0; i < 256; ++i) {
34
- byteToHex.push((i + 0x100).toString(16).slice(1));
35
- }
36
-
37
- function unsafeStringify(arr, offset = 0) {
38
- // Note: Be careful editing this code! It's been tuned for performance
39
- // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
40
- return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
41
+ return getRandomValues(rnds8);
41
42
  }
42
43
 
43
44
  const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
44
- var native = {
45
- randomUUID
46
- };
45
+ var native = { randomUUID };
47
46
 
48
47
  function v4(options, buf, offset) {
49
- if (native.randomUUID && !buf && !options) {
50
- return native.randomUUID();
51
- }
52
-
53
- options = options || {};
54
- const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
55
-
56
- rnds[6] = rnds[6] & 0x0f | 0x40;
57
- rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
58
-
59
- if (buf) {
60
- offset = offset || 0;
61
-
62
- for (let i = 0; i < 16; ++i) {
63
- buf[offset + i] = rnds[i];
48
+ if (native.randomUUID && true && !options) {
49
+ return native.randomUUID();
64
50
  }
65
-
66
- return buf;
67
- }
68
-
69
- return unsafeStringify(rnds);
51
+ options = options || {};
52
+ const rnds = options.random ?? options.rng?.() ?? rng();
53
+ if (rnds.length < 16) {
54
+ throw new Error('Random bytes length must be >= 16');
55
+ }
56
+ rnds[6] = (rnds[6] & 0x0f) | 0x40;
57
+ rnds[8] = (rnds[8] & 0x3f) | 0x80;
58
+ return unsafeStringify(rnds);
70
59
  }
71
60
 
72
61
  // console.log('OUT', __name({variableName}) );
@@ -153,81 +142,79 @@ const l_= {
153
142
 
154
143
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
155
144
  var JSON_Msg = (function() {
156
- return {
157
- merge : function(json) {
158
- const dt_Msg= new Date();
159
- let json_msg= {
160
- id : v4(),
161
- referer : null,
162
- dt : dt_Msg.toISOString(),
163
- };
164
- // https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object
165
- for (var key of Object.keys(json)) {
166
- json_msg[key]= json[key];
167
- }
168
- return json_msg;
169
- },
170
- validate : function(json) {
171
- const b_valid= validate_Msg(json);
172
- if (!b_valid)
173
- LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', validate_Msg.errors);
174
- return b_valid;
175
- }
176
- // TODO: requires a separate ajv module require, and JTD specific schema
177
- // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
178
- // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
179
- // static parse(json) {
180
- // return this.#ajv_parse_(json);
181
- // }
182
- }
145
+ function JSON_Msg() {}
146
+ JSON_Msg.prototype.merge = function (json) {
147
+ const dt_Msg = new Date();
148
+ let json_msg = {
149
+ id: v4(),
150
+ referer: null,
151
+ dt: dt_Msg.toISOString(),
152
+ };
153
+ const instance = Object.create(JSON_Msg.prototype);
154
+ Object.assign(instance, json_msg, json);
155
+ return instance;
156
+ };
157
+ JSON_Msg.prototype.validate = function (json) {
158
+ const b_valid = schema_Msg(json);
159
+ if (!b_valid)
160
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
161
+ return b_valid;
162
+ };
163
+ // TODO: requires a separate ajv module require, and JTD specific schema
164
+ // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
165
+ // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
166
+ // static parse(json) {
167
+ // return this.#ajv_parse_(json);
168
+ // }
169
+ return new JSON_Msg();
183
170
  })();
184
-
171
+
185
172
  var JSON_Msg_msg = (function() {
186
- return {
187
- create_with_msg_merge : function(_msg, json) {
188
- let json_msg= JSON_Msg.merge(json);
189
- json_msg._msg= _msg;
190
- return json_msg;
191
- },
192
- validate : function(json) {
193
- if (! JSON_Msg.validate(json))
194
- return false;
195
- const b_valid= validate_Msg_msg(json);
196
- if (!b_valid)
197
- LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', validate_Msg_msg.errors);
198
- return b_valid;
199
- }
200
- }
173
+ function JSON_Msg_msg() {}
174
+ JSON_Msg_msg.prototype.create_with_msg_merge = function(_msg, json) {
175
+ let json_msg= JSON_Msg.merge(json);
176
+ json_msg._msg= _msg;
177
+ return json_msg;
178
+ };
179
+ JSON_Msg_msg.prototype.validate = function(json) {
180
+ if (! JSON_Msg.validate(json))
181
+ return false;
182
+ const b_valid= schema_Msg_msg(json);
183
+ if (!b_valid)
184
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
185
+ return b_valid;
186
+ };
187
+ return new JSON_Msg_msg()
201
188
  })();
202
189
 
203
- var JSON_MsgAck = (function() {
204
- return {
205
- create_with_msgOk : function(_msg, b_ok= true) {
206
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
207
- ack : {
208
- "ok" : b_ok,
209
- },
210
- });
211
- },
212
- create_with_msgExpired : function(_msg) {
213
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
214
- ack : {
215
- "ok" : false,
216
- "expired" : true
217
- },
218
- });
219
- },
220
- validate : function(json) {
221
- if (! JSON_Msg_msg.validate(json))
222
- return false;
223
- const b_valid= validate_MsgAck(json);
224
- if (!b_valid)
225
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', validate_MsgAck.errors);
226
- return b_valid;
227
- }
228
- }
190
+ var JSON_MsgAck_msg = (function() {
191
+ function JSON_MsgAck_msg() {}
192
+ JSON_MsgAck_msg.prototype.create_with_msgOk = function(_msg, b_ok= true) {
193
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
194
+ ack : {
195
+ "ok" : b_ok,
196
+ },
197
+ });
198
+ },
199
+ JSON_MsgAck_msg.prototype.create_with_msgExpired = function(_msg) {
200
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
201
+ ack : {
202
+ "ok" : false,
203
+ "expired" : true
204
+ },
205
+ });
206
+ },
207
+ JSON_MsgAck_msg.prototype.validate = function(json) {
208
+ if (! JSON_Msg_msg.validate(json))
209
+ return false;
210
+ const b_valid= schema_MsgAck_msg(json);
211
+ if (!b_valid)
212
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
213
+ return b_valid;
214
+ };
215
+ return new JSON_MsgAck_msg();
229
216
  })();
230
217
 
231
218
  exports.JSON_Msg = JSON_Msg;
232
- exports.JSON_MsgAck = JSON_MsgAck;
219
+ exports.JSON_MsgAck_msg = JSON_MsgAck_msg;
233
220
  exports.JSON_Msg_msg = JSON_Msg_msg;
@@ -1,68 +1,59 @@
1
- const validate_Msg = validate10;function validate10(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate10.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate10.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.referer !== undefined){const _errs3 = errors;if(typeof data.referer !== "string"){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs5 = errors;if(typeof data.dt !== "string"){validate10.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs5 === errors;}else {var valid0 = true;}}}}}else {validate10.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10.errors = vErrors;return errors === 0;}const validate_Msg_msg = validate11;function validate11(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate11.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate11.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11.errors = vErrors;return errors === 0;}const validate_MsgAck = validate12;function validate12(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate12.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate12.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate12.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12.errors = vErrors;return errors === 0;}
1
+ const schema_Msg = validate18;function validate18(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate18.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate18.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate18.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs3 = errors;if(typeof data.dt !== "string"){validate18.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}}}}else {validate18.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate18.errors = vErrors;return errors === 0;}validate18.evaluated = {"props":{"id":true,"dt":true},"dynamicProps":false,"dynamicItems":false};const schema_Msg_msg = validate19;function validate19(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate19.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate19.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate19.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate19.errors = vErrors;return errors === 0;}validate19.evaluated = {"dynamicProps":false,"dynamicItems":false};const schema_MsgAck_msg = validate20;function validate20(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){let vErrors = null;let errors = 0;const evaluated0 = validate20.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}{if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate20.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;{if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate20.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate20.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate20.errors = vErrors;return errors === 0;}validate20.evaluated = {"props":{"ack":true},"dynamicProps":false,"dynamicItems":false};
2
+
3
+ const byteToHex = [];
4
+ for (let i = 0; i < 256; ++i) {
5
+ byteToHex.push((i + 0x100).toString(16).slice(1));
6
+ }
7
+ function unsafeStringify(arr, offset = 0) {
8
+ return (byteToHex[arr[offset + 0]] +
9
+ byteToHex[arr[offset + 1]] +
10
+ byteToHex[arr[offset + 2]] +
11
+ byteToHex[arr[offset + 3]] +
12
+ '-' +
13
+ byteToHex[arr[offset + 4]] +
14
+ byteToHex[arr[offset + 5]] +
15
+ '-' +
16
+ byteToHex[arr[offset + 6]] +
17
+ byteToHex[arr[offset + 7]] +
18
+ '-' +
19
+ byteToHex[arr[offset + 8]] +
20
+ byteToHex[arr[offset + 9]] +
21
+ '-' +
22
+ byteToHex[arr[offset + 10]] +
23
+ byteToHex[arr[offset + 11]] +
24
+ byteToHex[arr[offset + 12]] +
25
+ byteToHex[arr[offset + 13]] +
26
+ byteToHex[arr[offset + 14]] +
27
+ byteToHex[arr[offset + 15]]).toLowerCase();
28
+ }
2
29
 
3
- // Unique ID creation requires a high quality random # generator. In the browser we therefore
4
- // require the crypto API and do not support built-in fallback to lower quality random number
5
- // generators (like Math.random()).
6
30
  let getRandomValues;
7
31
  const rnds8 = new Uint8Array(16);
8
32
  function rng() {
9
- // lazy load so that environments that need to polyfill have a chance to do so
10
- if (!getRandomValues) {
11
- // getRandomValues needs to be invoked in a context where "this" is a Crypto implementation.
12
- getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto);
13
-
14
33
  if (!getRandomValues) {
15
- throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
34
+ if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
35
+ throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
36
+ }
37
+ getRandomValues = crypto.getRandomValues.bind(crypto);
16
38
  }
17
- }
18
-
19
- return getRandomValues(rnds8);
20
- }
21
-
22
- /**
23
- * Convert array of 16 byte values to UUID string format of the form:
24
- * XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
25
- */
26
-
27
- const byteToHex = [];
28
-
29
- for (let i = 0; i < 256; ++i) {
30
- byteToHex.push((i + 0x100).toString(16).slice(1));
31
- }
32
-
33
- function unsafeStringify(arr, offset = 0) {
34
- // Note: Be careful editing this code! It's been tuned for performance
35
- // and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
36
- return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
39
+ return getRandomValues(rnds8);
37
40
  }
38
41
 
39
42
  const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
40
- var native = {
41
- randomUUID
42
- };
43
+ var native = { randomUUID };
43
44
 
44
45
  function v4(options, buf, offset) {
45
- if (native.randomUUID && !buf && !options) {
46
- return native.randomUUID();
47
- }
48
-
49
- options = options || {};
50
- const rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
51
-
52
- rnds[6] = rnds[6] & 0x0f | 0x40;
53
- rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
54
-
55
- if (buf) {
56
- offset = offset || 0;
57
-
58
- for (let i = 0; i < 16; ++i) {
59
- buf[offset + i] = rnds[i];
46
+ if (native.randomUUID && true && !options) {
47
+ return native.randomUUID();
60
48
  }
61
-
62
- return buf;
63
- }
64
-
65
- return unsafeStringify(rnds);
49
+ options = options || {};
50
+ const rnds = options.random ?? options.rng?.() ?? rng();
51
+ if (rnds.length < 16) {
52
+ throw new Error('Random bytes length must be >= 16');
53
+ }
54
+ rnds[6] = (rnds[6] & 0x0f) | 0x40;
55
+ rnds[8] = (rnds[8] & 0x3f) | 0x80;
56
+ return unsafeStringify(rnds);
66
57
  }
67
58
 
68
59
  // console.log('OUT', __name({variableName}) );
@@ -149,79 +140,77 @@ const l_= {
149
140
 
150
141
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
151
142
  var JSON_Msg = (function() {
152
- return {
153
- merge : function(json) {
154
- const dt_Msg= new Date();
155
- let json_msg= {
156
- id : v4(),
157
- referer : null,
158
- dt : dt_Msg.toISOString(),
159
- };
160
- // https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object
161
- for (var key of Object.keys(json)) {
162
- json_msg[key]= json[key];
163
- }
164
- return json_msg;
165
- },
166
- validate : function(json) {
167
- const b_valid= validate_Msg(json);
168
- if (!b_valid)
169
- LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', validate_Msg.errors);
170
- return b_valid;
171
- }
172
- // TODO: requires a separate ajv module require, and JTD specific schema
173
- // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
174
- // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
175
- // static parse(json) {
176
- // return this.#ajv_parse_(json);
177
- // }
178
- }
143
+ function JSON_Msg() {}
144
+ JSON_Msg.prototype.merge = function (json) {
145
+ const dt_Msg = new Date();
146
+ let json_msg = {
147
+ id: v4(),
148
+ referer: null,
149
+ dt: dt_Msg.toISOString(),
150
+ };
151
+ const instance = Object.create(JSON_Msg.prototype);
152
+ Object.assign(instance, json_msg, json);
153
+ return instance;
154
+ };
155
+ JSON_Msg.prototype.validate = function (json) {
156
+ const b_valid = schema_Msg(json);
157
+ if (!b_valid)
158
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
159
+ return b_valid;
160
+ };
161
+ // TODO: requires a separate ajv module require, and JTD specific schema
162
+ // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
163
+ // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
164
+ // static parse(json) {
165
+ // return this.#ajv_parse_(json);
166
+ // }
167
+ return new JSON_Msg();
179
168
  })();
180
-
169
+
181
170
  var JSON_Msg_msg = (function() {
182
- return {
183
- create_with_msg_merge : function(_msg, json) {
184
- let json_msg= JSON_Msg.merge(json);
185
- json_msg._msg= _msg;
186
- return json_msg;
187
- },
188
- validate : function(json) {
189
- if (! JSON_Msg.validate(json))
190
- return false;
191
- const b_valid= validate_Msg_msg(json);
192
- if (!b_valid)
193
- LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', validate_Msg_msg.errors);
194
- return b_valid;
195
- }
196
- }
171
+ function JSON_Msg_msg() {}
172
+ JSON_Msg_msg.prototype.create_with_msg_merge = function(_msg, json) {
173
+ let json_msg= JSON_Msg.merge(json);
174
+ json_msg._msg= _msg;
175
+ return json_msg;
176
+ };
177
+ JSON_Msg_msg.prototype.validate = function(json) {
178
+ if (! JSON_Msg.validate(json))
179
+ return false;
180
+ const b_valid= schema_Msg_msg(json);
181
+ if (!b_valid)
182
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
183
+ return b_valid;
184
+ };
185
+ return new JSON_Msg_msg()
197
186
  })();
198
187
 
199
- var JSON_MsgAck = (function() {
200
- return {
201
- create_with_msgOk : function(_msg, b_ok= true) {
202
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
203
- ack : {
204
- "ok" : b_ok,
205
- },
206
- });
207
- },
208
- create_with_msgExpired : function(_msg) {
209
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
210
- ack : {
211
- "ok" : false,
212
- "expired" : true
213
- },
214
- });
215
- },
216
- validate : function(json) {
217
- if (! JSON_Msg_msg.validate(json))
218
- return false;
219
- const b_valid= validate_MsgAck(json);
220
- if (!b_valid)
221
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', validate_MsgAck.errors);
222
- return b_valid;
223
- }
224
- }
188
+ var JSON_MsgAck_msg = (function() {
189
+ function JSON_MsgAck_msg() {}
190
+ JSON_MsgAck_msg.prototype.create_with_msgOk = function(_msg, b_ok= true) {
191
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
192
+ ack : {
193
+ "ok" : b_ok,
194
+ },
195
+ });
196
+ },
197
+ JSON_MsgAck_msg.prototype.create_with_msgExpired = function(_msg) {
198
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
199
+ ack : {
200
+ "ok" : false,
201
+ "expired" : true
202
+ },
203
+ });
204
+ },
205
+ JSON_MsgAck_msg.prototype.validate = function(json) {
206
+ if (! JSON_Msg_msg.validate(json))
207
+ return false;
208
+ const b_valid= schema_MsgAck_msg(json);
209
+ if (!b_valid)
210
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
211
+ return b_valid;
212
+ };
213
+ return new JSON_MsgAck_msg();
225
214
  })();
226
215
 
227
- export { JSON_Msg, JSON_MsgAck, JSON_Msg_msg };
216
+ export { JSON_Msg, JSON_MsgAck_msg, JSON_Msg_msg };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rootintf/json-msg",
3
- "version": "0.1.0",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "dist/json-msg.cjs.js",
6
6
  "browser": "dist/json-msg.es.mjs",
@@ -8,7 +8,7 @@
8
8
  "build": "node src/ajv-gen-validate.cjs && rollup --config",
9
9
  "script": "node --experimental-json-modules tests/json-msg.test.mjs",
10
10
  "test": "jasmine --config=jasmine.json",
11
- "clean": "del dist && del src/validate-esm.mjs && echo OK"
11
+ "clean": "del-cli dist && del-cli src/validate-esm.mjs && echo OK"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
@@ -25,15 +25,16 @@
25
25
  "homepage": "https://gitlab.com/interface-x/npm-json-msg.git#readme",
26
26
  "dependencies": {
27
27
  "@knev/bitlogr": "^0.2.12",
28
- "uuid": "^9.0.0"
28
+ "ajv": "^8.17.1",
29
+ "uuid": "^11.1.0"
29
30
  },
30
31
  "devDependencies": {
31
- "@rollup/plugin-commonjs": "^23.0.0",
32
- "@rollup/plugin-json": "^5.0.0",
33
- "@rollup/plugin-node-resolve": "^15.0.0",
32
+ "@rollup/plugin-commonjs": "^28.0.3",
33
+ "@rollup/plugin-json": "^6.1.0",
34
+ "@rollup/plugin-node-resolve": "^16.0.1",
34
35
  "ajv-cli": "^5.0.0",
35
- "del-cli": "^5.0.0",
36
- "jasmine": "^4.5.0",
37
- "rollup": "^2.79.1"
36
+ "del-cli": "^6.0.0",
37
+ "jasmine": "^5.6.0",
38
+ "rollup": "^4.35.0"
38
39
  }
39
40
  }
@@ -1,11 +1,10 @@
1
1
  {
2
- "$id": "validate_Msg",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/JSON_Msg/schema_Msg",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["id", "referer", "dt"],
6
6
  "properties" : {
7
7
  "id" : { "type" : "string"},
8
- "referer" : { "type" : "string"},
9
8
  "dt" : { "type" : "string" }
10
9
  }
11
10
  }
@@ -1,6 +1,6 @@
1
1
  {
2
- "$id": "validate_MsgAck",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/JSON_Msg/schema_MsgAck_msg",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["ack"],
6
6
  "properties" : {
@@ -1,6 +1,6 @@
1
1
  {
2
- "$id": "validate_Msg_msg",
3
- "$schema": "http://json-schema.org/draft-07/schema#",
2
+ "$id" : "https://root-interface.se/protocols/JSON_Msg/schema_Msg_msg",
3
+ "$schema": "https://json-schema.org/draft/2019-09/schema",
4
4
  "type" : "object",
5
5
  "required" : ["_msg"]
6
6
  }
@@ -1,13 +1,13 @@
1
1
 
2
- var schema_json_msg_= require('../schema/schema_Msg.json');
3
- var schema_json_msg_msg_= require('../schema/schema_Msg_msg.json');
4
- var schema_json_msgAck_= require('../schema/schema_MsgAck.json');
2
+ var json_schema_msg_= require('../schema/schema_Msg.json');
3
+ var json_schema_msg_msg_= require('../schema/schema_Msg_msg.json');
4
+ var json_schema__msgAck_msg_= require('../schema/schema_MsgAck_msg.json');
5
5
 
6
6
  // https://ajv.js.org/standalone.html
7
7
 
8
8
  const fs = require("fs")
9
9
  const path = require("path")
10
- const Ajv = require("ajv")
10
+ const Ajv2019 = require("ajv/dist/2019");
11
11
  const standaloneCode = require("ajv/dist/standalone").default
12
12
 
13
13
  // For ESM, the export name needs to be a valid export name, it can not be `export const #/definitions/Foo = ...;` so we
@@ -15,8 +15,18 @@ const standaloneCode = require("ajv/dist/standalone").default
15
15
  // `export const Foo = ...;export const Bar = ...;`
16
16
  // This mapping would not have been needed if the `$ids` was just `Bar` and `Foo` instead of `#/definitions/Foo`
17
17
  // and `#/definitions/Bar` respectfully
18
- const ajv = new Ajv({schemas: [schema_json_msg_, schema_json_msg_msg_, schema_json_msgAck_], code: {source: true, esm: true}})
19
- let moduleCode = standaloneCode(ajv)
18
+ const ajv = new Ajv2019({schemas: [
19
+ json_schema_msg_,
20
+ json_schema_msg_msg_,
21
+ json_schema__msgAck_msg_
22
+ ],
23
+ code: {source: true, esm: true}
24
+ });
25
+ let moduleCode = standaloneCode(ajv, {
26
+ "schema_Msg": "https://root-interface.se/protocols/JSON_Msg/schema_Msg",
27
+ "schema_Msg_msg" : "https://root-interface.se/protocols/JSON_Msg/schema_Msg_msg",
28
+ "schema_MsgAck_msg" : "https://root-interface.se/protocols/JSON_Msg/schema_MsgAck_msg"
29
+ })
20
30
 
21
31
  // Now you can write the module code to file
22
32
  fs.writeFileSync(path.join(__dirname, "./validate-esm.mjs"), moduleCode)
@@ -1,5 +1,5 @@
1
1
 
2
- import { validate_Msg, validate_Msg_msg, validate_MsgAck } from './validate-esm.mjs';
2
+ import { schema_Msg, schema_Msg_msg, schema_MsgAck_msg } from './validate-esm.mjs';
3
3
 
4
4
  import { v4 as uuidv4 } from 'uuid';
5
5
  import { BitLogr, l_LL } from '@knev/bitlogr';
@@ -14,79 +14,77 @@ const l_= {
14
14
 
15
15
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
16
16
  var JSON_Msg = (function() {
17
- return {
18
- merge : function(json) {
19
- const dt_Msg= new Date();
20
- let json_msg= {
21
- id : uuidv4(),
22
- referer : null,
23
- dt : dt_Msg.toISOString(),
24
- }
25
- // https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object
26
- for (var key of Object.keys(json)) {
27
- json_msg[key]= json[key];
28
- }
29
- return json_msg;
30
- },
31
- validate : function(json) {
32
- const b_valid= validate_Msg(json);
33
- if (!b_valid)
34
- LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', validate_Msg.errors)
35
- return b_valid;
17
+ function JSON_Msg() {}
18
+ JSON_Msg.prototype.merge = function (json) {
19
+ const dt_Msg = new Date();
20
+ let json_msg = {
21
+ id: uuidv4(),
22
+ referer: null,
23
+ dt: dt_Msg.toISOString(),
36
24
  }
37
- // TODO: requires a separate ajv module require, and JTD specific schema
38
- // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
39
- // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
40
- // static parse(json) {
41
- // return this.#ajv_parse_(json);
42
- // }
43
- }
25
+ const instance = Object.create(JSON_Msg.prototype);
26
+ Object.assign(instance, json_msg, json);
27
+ return instance;
28
+ };
29
+ JSON_Msg.prototype.validate = function (json) {
30
+ const b_valid = schema_Msg(json);
31
+ if (!b_valid)
32
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors)
33
+ return b_valid;
34
+ };
35
+ // TODO: requires a separate ajv module require, and JTD specific schema
36
+ // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
37
+ // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
38
+ // static parse(json) {
39
+ // return this.#ajv_parse_(json);
40
+ // }
41
+ return new JSON_Msg();
44
42
  })();
45
-
43
+
46
44
  var JSON_Msg_msg = (function() {
47
- return {
48
- create_with_msg_merge : function(_msg, json) {
49
- let json_msg= JSON_Msg.merge(json);
50
- json_msg._msg= _msg;
51
- return json_msg;
52
- },
53
- validate : function(json) {
54
- if (! JSON_Msg.validate(json))
55
- return false;
56
- const b_valid= validate_Msg_msg(json);
57
- if (!b_valid)
58
- LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', validate_Msg_msg.errors)
59
- return b_valid;
60
- }
45
+ function JSON_Msg_msg() {}
46
+ JSON_Msg_msg.prototype.create_with_msg_merge = function(_msg, json) {
47
+ let json_msg= JSON_Msg.merge(json);
48
+ json_msg._msg= _msg;
49
+ return json_msg;
50
+ };
51
+ JSON_Msg_msg.prototype.validate = function(json) {
52
+ if (! JSON_Msg.validate(json))
53
+ return false;
54
+ const b_valid= schema_Msg_msg(json);
55
+ if (!b_valid)
56
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors)
57
+ return b_valid;
61
58
  }
59
+ return new JSON_Msg_msg()
62
60
  })();
63
61
 
64
- var JSON_MsgAck = (function() {
65
- return {
66
- create_with_msgOk : function(_msg, b_ok= true) {
67
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
68
- ack : {
69
- "ok" : b_ok,
70
- },
71
- });
72
- },
73
- create_with_msgExpired : function(_msg) {
74
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
75
- ack : {
76
- "ok" : false,
77
- "expired" : true
78
- },
79
- });
80
- },
81
- validate : function(json) {
82
- if (! JSON_Msg_msg.validate(json))
83
- return false;
84
- const b_valid= validate_MsgAck(json);
85
- if (!b_valid)
86
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', validate_MsgAck.errors)
87
- return b_valid;
88
- }
62
+ var JSON_MsgAck_msg = (function() {
63
+ function JSON_MsgAck_msg() {}
64
+ JSON_MsgAck_msg.prototype.create_with_msgOk = function(_msg, b_ok= true) {
65
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
66
+ ack : {
67
+ "ok" : b_ok,
68
+ },
69
+ });
70
+ },
71
+ JSON_MsgAck_msg.prototype.create_with_msgExpired = function(_msg) {
72
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
73
+ ack : {
74
+ "ok" : false,
75
+ "expired" : true
76
+ },
77
+ });
78
+ },
79
+ JSON_MsgAck_msg.prototype.validate = function(json) {
80
+ if (! JSON_Msg_msg.validate(json))
81
+ return false;
82
+ const b_valid= schema_MsgAck_msg(json);
83
+ if (!b_valid)
84
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors)
85
+ return b_valid;
89
86
  }
87
+ return new JSON_MsgAck_msg();
90
88
  })();
91
89
 
92
90
  //-------------------------------------------------------------------------------------------------
@@ -94,5 +92,5 @@ var JSON_MsgAck = (function() {
94
92
  export {
95
93
  JSON_Msg,
96
94
  JSON_Msg_msg,
97
- JSON_MsgAck
95
+ JSON_MsgAck_msg
98
96
  }
@@ -1 +1 @@
1
- "use strict";export const validate_Msg = validate10;const schema11 = {"$id":"validate_Msg","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["id","referer","dt"],"properties":{"id":{"type":"string"},"referer":{"type":"string"},"dt":{"type":"string"}}};function validate10(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_Msg" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate10.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate10.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.referer !== undefined){const _errs3 = errors;if(typeof data.referer !== "string"){validate10.errors = [{instancePath:instancePath+"/referer",schemaPath:"#/properties/referer/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs5 = errors;if(typeof data.dt !== "string"){validate10.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs5 === errors;}else {var valid0 = true;}}}}}else {validate10.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate10.errors = vErrors;return errors === 0;}export const validate_Msg_msg = validate11;const schema12 = {"$id":"validate_Msg_msg","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["_msg"]};function validate11(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_Msg_msg" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate11.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate11.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate11.errors = vErrors;return errors === 0;}export const validate_MsgAck = validate12;const schema13 = {"$id":"validate_MsgAck","$schema":"http://json-schema.org/draft-07/schema#","type":"object","required":["ack"],"properties":{"ack":{"type":"object","required":["ok"],"properties":{"ok":{"type":"boolean"}}}}};function validate12(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){/*# sourceURL="validate_MsgAck" */;let vErrors = null;let errors = 0;if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate12.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;const _errs1 = errors;if(errors === _errs1){if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate12.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate12.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate12.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate12.errors = vErrors;return errors === 0;}
1
+ "use strict";export const schema_Msg = validate18;const schema21 = {"$id":"https://root-interface.se/protocols/JSON_Msg/schema_Msg","$schema":"https://json-schema.org/draft/2019-09/schema","type":"object","required":["id","referer","dt"],"properties":{"id":{"type":"string"},"dt":{"type":"string"}}};function validate18(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){/*# sourceURL="https://root-interface.se/protocols/JSON_Msg/schema_Msg" */;let vErrors = null;let errors = 0;const evaluated0 = validate18.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((((data.id === undefined) && (missing0 = "id")) || ((data.referer === undefined) && (missing0 = "referer"))) || ((data.dt === undefined) && (missing0 = "dt"))){validate18.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.id !== undefined){const _errs1 = errors;if(typeof data.id !== "string"){validate18.errors = [{instancePath:instancePath+"/id",schemaPath:"#/properties/id/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs1 === errors;}else {var valid0 = true;}if(valid0){if(data.dt !== undefined){const _errs3 = errors;if(typeof data.dt !== "string"){validate18.errors = [{instancePath:instancePath+"/dt",schemaPath:"#/properties/dt/type",keyword:"type",params:{type: "string"},message:"must be string"}];return false;}var valid0 = _errs3 === errors;}else {var valid0 = true;}}}}else {validate18.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate18.errors = vErrors;return errors === 0;}validate18.evaluated = {"props":{"id":true,"dt":true},"dynamicProps":false,"dynamicItems":false};export const schema_Msg_msg = validate19;const schema22 = {"$id":"https://root-interface.se/protocols/JSON_Msg/schema_Msg_msg","$schema":"https://json-schema.org/draft/2019-09/schema","type":"object","required":["_msg"]};function validate19(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){/*# sourceURL="https://root-interface.se/protocols/JSON_Msg/schema_Msg_msg" */;let vErrors = null;let errors = 0;const evaluated0 = validate19.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data._msg === undefined) && (missing0 = "_msg")){validate19.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}}else {validate19.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate19.errors = vErrors;return errors === 0;}validate19.evaluated = {"dynamicProps":false,"dynamicItems":false};export const schema_MsgAck_msg = validate20;const schema23 = {"$id":"https://root-interface.se/protocols/JSON_Msg/schema_MsgAck_msg","$schema":"https://json-schema.org/draft/2019-09/schema","type":"object","required":["ack"],"properties":{"ack":{"type":"object","required":["ok"],"properties":{"ok":{"type":"boolean"}}}}};function validate20(data, {instancePath="", parentData, parentDataProperty, rootData=data, dynamicAnchors={}}={}){/*# sourceURL="https://root-interface.se/protocols/JSON_Msg/schema_MsgAck_msg" */;let vErrors = null;let errors = 0;const evaluated0 = validate20.evaluated;if(evaluated0.dynamicProps){evaluated0.props = undefined;}if(evaluated0.dynamicItems){evaluated0.items = undefined;}if(errors === 0){if(data && typeof data == "object" && !Array.isArray(data)){let missing0;if((data.ack === undefined) && (missing0 = "ack")){validate20.errors = [{instancePath,schemaPath:"#/required",keyword:"required",params:{missingProperty: missing0},message:"must have required property '"+missing0+"'"}];return false;}else {if(data.ack !== undefined){let data0 = data.ack;const _errs1 = errors;if(errors === _errs1){if(data0 && typeof data0 == "object" && !Array.isArray(data0)){let missing1;if((data0.ok === undefined) && (missing1 = "ok")){validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/required",keyword:"required",params:{missingProperty: missing1},message:"must have required property '"+missing1+"'"}];return false;}else {if(data0.ok !== undefined){if(typeof data0.ok !== "boolean"){validate20.errors = [{instancePath:instancePath+"/ack/ok",schemaPath:"#/properties/ack/properties/ok/type",keyword:"type",params:{type: "boolean"},message:"must be boolean"}];return false;}}}}else {validate20.errors = [{instancePath:instancePath+"/ack",schemaPath:"#/properties/ack/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}}}}else {validate20.errors = [{instancePath,schemaPath:"#/type",keyword:"type",params:{type: "object"},message:"must be object"}];return false;}}validate20.errors = vErrors;return errors === 0;}validate20.evaluated = {"props":{"ack":true},"dynamicProps":false,"dynamicItems":false};
@@ -1,50 +1,82 @@
1
1
 
2
- import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck } from '../src/json_msg+ack.mjs';
2
+ import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck_msg } from '../src/json_msg+ack.mjs';
3
3
 
4
- const json_msgAckWarp = {
4
+ const json_msgWarp_nomsg = {
5
+ "id" : "C995A2D2-49BB-4D41-B1D3-C12648652B47",
6
+ "referer": [ "ipsme.dev/js-blue/blue.html", "afab6b6b-2284-4abd-b71e-1e369b7e9220" ],
7
+ "dt": "2023-03-10T20:10:39.408Z",
8
+ "ack": {
9
+ "ok": true,
10
+ }
11
+ };
12
+
13
+ const json_msgWarp_msg_null = {
5
14
  "id" : "C995A2D2-49BB-4D41-B1D3-C12648652B47",
6
- "referer": "ipsme.dev/js-blue/blue.html/?uuid=afab6b6b-2284-4abd-b71e-1e369b7e9220",
15
+ "referer": [ "ipsme.dev/js-blue/blue.html", "afab6b6b-2284-4abd-b71e-1e369b7e9220" ],
7
16
  "dt": "2023-03-10T20:10:39.408Z",
8
17
  "ack": {
9
18
  "ok": true,
10
19
  },
11
- "_msg": "{\"id\":\"50673b91-d4f3-4ff9-a80b-9d16961c0368\",\"warp\":{\"user\":{\"id\":\"did:example:123456789abcdefghi\",\"authentication\":[{\"id\":\"did:example:123456789abcdefghi#keys-1\",\"publicKeyMultibase\":\"zH3C2AVvLMv6gmMNam3uVAjZpfkcJCwDwnZn6z3wXmqPV\",\"type\":\"Ed25519VerificationKey2020\",\"controller\":\"did:example:123456789abcdefghi\"}]},\"hyperport\":\"secondlife:\/\/Ahern\/128\/128\"},\"authorization\":{\"access_control\":\"ok\"},\"referer\":\"localhost:5000\/orange.html\/?uuid=d29c6150-51da-4a39-b50a-f75883f730ae\",\"dt\":\"2023-04-05T16:15:41.387Z\"}"
20
+ "_msg": null
12
21
  };
13
22
 
14
- let json_msgAck_msg;
15
- try {
16
- json_msgAck_msg= JSON.parse(json_msgAckWarp._msg);
17
- }
18
- catch (err) {
19
- }
23
+ const json_msgAckWarp = {
24
+ "id" : "C995A2D2-49BB-4D41-B1D3-C12648652B47",
25
+ "referer": [ "ipsme.dev/js-blue/blue.html", "afab6b6b-2284-4abd-b71e-1e369b7e9220" ],
26
+ "dt": "2023-03-10T20:10:39.408Z",
27
+ "ack": {
28
+ "ok": true,
29
+ },
30
+ "_msg": {}
31
+ };
20
32
 
21
33
  //-------------------------------------------------------------------------------------------------
22
34
  // JSON_Msg
23
35
 
24
- describe("Validation", function () {
36
+ describe("JSON_Msg validation", function () {
37
+ it("JSON_Msg.validate({})", function () {
38
+ const merged_msg = JSON_Msg.merge({});
39
+ expect( JSON_Msg.validate(merged_msg) ).toBe(true);
40
+ });
41
+
42
+ it("JSON_Msg.merge({ foo: \"bar\" })", function () {
43
+ const merged_msg = JSON_Msg.merge({ foo: "bar" });
44
+
45
+ expect( merged_msg.constructor.name ).toBe('JSON_Msg');
46
+ expect( merged_msg.foo ).toBe('bar');
47
+ expect( JSON_Msg.validate(merged_msg) ).toBe(true);
48
+ });
49
+
50
+ // console.log(); // true
51
+
25
52
  it("JSON_Msg.validate(json_msgAckWarp)", function () {
26
53
  expect( JSON_Msg.validate(json_msgAckWarp) ).toBe(true);
27
54
  });
28
55
  });
29
56
 
30
- describe("Validation", function () {
57
+ //-------------------------------------------------------------------------------------------------
58
+ // JSON_Msg_msg
59
+
60
+ describe("JSON_Msg_msg validation", function () {
31
61
  it("JSON_Msg_msg.validate(json_msgAckWarp)", function () {
32
62
  expect( JSON_Msg_msg.validate(json_msgAckWarp) ).toBe(true);
33
63
  });
34
- });
35
64
 
36
- describe("Validation", function () {
37
- it("JSON_MsgAck.validate(json_msgAckWarp)", function () {
38
- expect( JSON_MsgAck.validate(json_msgAckWarp) ).toBe(true);
65
+ it("JSON_Msg_msg.validate(json_msgWarp_nomsg)", function () {
66
+ expect( JSON_Msg_msg.validate(json_msgWarp_nomsg) ).toBe(false);
67
+ });
68
+
69
+ it("JSON_Msg_msg.validate(json_msgWarp_msg_null)", function () {
70
+ expect( JSON_Msg_msg.validate(json_msgWarp_msg_null) ).toBe(true);
39
71
  });
40
72
  });
41
73
 
42
74
  //-------------------------------------------------------------------------------------------------
43
- // JSON_Msg._msg
75
+ // JSON_MsgAck_msg
44
76
 
45
- describe("Validation", function () {
46
- it("JSON_Msg.validate(json_msgAck_msg)", function () {
47
- expect( JSON_Msg.validate(json_msgAck_msg) ).toBe(true);
77
+ describe("JSON_MsgAck_msg validation", function () {
78
+ it("JSON_MsgAck.validate(json_msgAckWarp)", function () {
79
+ expect( JSON_MsgAck_msg.validate(json_msgAckWarp) ).toBe(true);
48
80
  });
49
81
  });
50
82