@rootintf/json-msg 0.1.0

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 ADDED
@@ -0,0 +1,43 @@
1
+
2
+ ```
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
+ }
14
+ ```
15
+
16
+ ```
17
+ {
18
+ "$id": "validate_Msg_msg",
19
+ "$schema": "http://json-schema.org/draft-07/schema#",
20
+ "type" : "object",
21
+ "required" : ["_msg"]
22
+ }
23
+ ```
24
+
25
+ ```
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
+ }
41
+ ```
42
+
43
+ TODO: add optional expired property
@@ -0,0 +1,233 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
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;}
6
+
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
+ let getRandomValues;
11
+ const rnds8 = new Uint8Array(16);
12
+ 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
+ if (!getRandomValues) {
19
+ throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
20
+ }
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
+ }
42
+
43
+ const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
44
+ var native = {
45
+ randomUUID
46
+ };
47
+
48
+ 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];
64
+ }
65
+
66
+ return buf;
67
+ }
68
+
69
+ return unsafeStringify(rnds);
70
+ }
71
+
72
+ // console.log('OUT', __name({variableName}) );
73
+
74
+ function l_toBigInt_(ref, obj, ignore= false) {
75
+ console.assert(ref !== BigInt(0), 'no labels initialized');
76
+ let bigInt = BigInt(0);
77
+ for (const [k,v] of Object.entries(obj)) {
78
+ if ( ( ignore || v ) && ref[k])
79
+ bigInt|= BigInt( ref[k] );
80
+ // console.log('0b'+ bigInt.toString(2) );
81
+ }
82
+ return bigInt;
83
+ }
84
+
85
+ //-------------------------------------------------------------------------------------------------
86
+
87
+ function handler_default_( /* ... */ ) {
88
+ // https://stackoverflow.com/questions/18746440/passing-multiple-arguments-to-console-log
89
+ var args = Array.prototype.slice.call(arguments);
90
+ console.log.apply(console, args);
91
+ }
92
+
93
+ //-------------------------------------------------------------------------------------------------
94
+
95
+ class BitLogr {
96
+ constructor() {
97
+ this._handler_log= handler_default_;
98
+ this._Bint_labels= BigInt(0);
99
+ this._Bint_toggled= BigInt(0);
100
+
101
+ BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
102
+ // console.log('NOP')
103
+ };
104
+ }
105
+
106
+ set handler(fx) {
107
+ this._handler_log= fx;
108
+ }
109
+
110
+ get labels() { return this._Bint_labels; }
111
+ set labels(obj) {
112
+ this._Bint_labels= obj;
113
+ this._Bint_toggled= BigInt(0);
114
+ }
115
+
116
+ // put= function(label, abbrv) {
117
+ // let name= __name(label);
118
+ // _labels[name]= label[name];
119
+ // console.log(_labels);
120
+ // }
121
+
122
+ get toggled() { return this._Bint_toggled; }
123
+ set toggled(obj) {
124
+ this._Bint_toggled= l_toBigInt_(this._Bint_labels, obj);
125
+
126
+ if (this._Bint_toggled === BigInt(0)) {
127
+ console.log('adlkjasdlfk');
128
+ return;
129
+ }
130
+
131
+ BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
132
+ if ( (BigInt(nr_logged) & this._Bint_toggled) === BigInt(0))
133
+ return false;
134
+
135
+ var args = Array.prototype.slice.call(arguments);
136
+ args.shift(); // remove first arg: nr_logged
137
+ this._handler_log.apply(this, args);
138
+
139
+ return true;
140
+ };
141
+ }
142
+
143
+ // log= function (nr_logged, /* ... */ ) {}
144
+ }
145
+
146
+ let LOGR_= new BitLogr();
147
+
148
+ const l_= {
149
+ VALIDATION : 0b1 << 0,
150
+ };
151
+
152
+ // TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
153
+
154
+ // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
155
+ 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
+ }
183
+ })();
184
+
185
+ 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
+ }
201
+ })();
202
+
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
+ }
229
+ })();
230
+
231
+ exports.JSON_Msg = JSON_Msg;
232
+ exports.JSON_MsgAck = JSON_MsgAck;
233
+ exports.JSON_Msg_msg = JSON_Msg_msg;
@@ -0,0 +1,227 @@
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;}
2
+
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
+ let getRandomValues;
7
+ const rnds8 = new Uint8Array(16);
8
+ 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
+ if (!getRandomValues) {
15
+ throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
16
+ }
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();
37
+ }
38
+
39
+ const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
40
+ var native = {
41
+ randomUUID
42
+ };
43
+
44
+ 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];
60
+ }
61
+
62
+ return buf;
63
+ }
64
+
65
+ return unsafeStringify(rnds);
66
+ }
67
+
68
+ // console.log('OUT', __name({variableName}) );
69
+
70
+ function l_toBigInt_(ref, obj, ignore= false) {
71
+ console.assert(ref !== BigInt(0), 'no labels initialized');
72
+ let bigInt = BigInt(0);
73
+ for (const [k,v] of Object.entries(obj)) {
74
+ if ( ( ignore || v ) && ref[k])
75
+ bigInt|= BigInt( ref[k] );
76
+ // console.log('0b'+ bigInt.toString(2) );
77
+ }
78
+ return bigInt;
79
+ }
80
+
81
+ //-------------------------------------------------------------------------------------------------
82
+
83
+ function handler_default_( /* ... */ ) {
84
+ // https://stackoverflow.com/questions/18746440/passing-multiple-arguments-to-console-log
85
+ var args = Array.prototype.slice.call(arguments);
86
+ console.log.apply(console, args);
87
+ }
88
+
89
+ //-------------------------------------------------------------------------------------------------
90
+
91
+ class BitLogr {
92
+ constructor() {
93
+ this._handler_log= handler_default_;
94
+ this._Bint_labels= BigInt(0);
95
+ this._Bint_toggled= BigInt(0);
96
+
97
+ BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
98
+ // console.log('NOP')
99
+ };
100
+ }
101
+
102
+ set handler(fx) {
103
+ this._handler_log= fx;
104
+ }
105
+
106
+ get labels() { return this._Bint_labels; }
107
+ set labels(obj) {
108
+ this._Bint_labels= obj;
109
+ this._Bint_toggled= BigInt(0);
110
+ }
111
+
112
+ // put= function(label, abbrv) {
113
+ // let name= __name(label);
114
+ // _labels[name]= label[name];
115
+ // console.log(_labels);
116
+ // }
117
+
118
+ get toggled() { return this._Bint_toggled; }
119
+ set toggled(obj) {
120
+ this._Bint_toggled= l_toBigInt_(this._Bint_labels, obj);
121
+
122
+ if (this._Bint_toggled === BigInt(0)) {
123
+ console.log('adlkjasdlfk');
124
+ return;
125
+ }
126
+
127
+ BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
128
+ if ( (BigInt(nr_logged) & this._Bint_toggled) === BigInt(0))
129
+ return false;
130
+
131
+ var args = Array.prototype.slice.call(arguments);
132
+ args.shift(); // remove first arg: nr_logged
133
+ this._handler_log.apply(this, args);
134
+
135
+ return true;
136
+ };
137
+ }
138
+
139
+ // log= function (nr_logged, /* ... */ ) {}
140
+ }
141
+
142
+ let LOGR_= new BitLogr();
143
+
144
+ const l_= {
145
+ VALIDATION : 0b1 << 0,
146
+ };
147
+
148
+ // TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
149
+
150
+ // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
151
+ 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
+ }
179
+ })();
180
+
181
+ 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
+ }
197
+ })();
198
+
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
+ }
225
+ })();
226
+
227
+ export { JSON_Msg, JSON_MsgAck, JSON_Msg_msg };
package/jasmine.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "spec_dir": "tests",
3
+ "spec_files": [
4
+ "**/*[sS]pec.?(m)js",
5
+ "!**/*nospec.js"
6
+ ]
7
+ }
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@rootintf/json-msg",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "main": "dist/json-msg.cjs.js",
6
+ "browser": "dist/json-msg.es.mjs",
7
+ "scripts": {
8
+ "build": "node src/ajv-gen-validate.cjs && rollup --config",
9
+ "script": "node --experimental-json-modules tests/json-msg.test.mjs",
10
+ "test": "jasmine --config=jasmine.json",
11
+ "clean": "del dist && del src/validate-esm.mjs && echo OK"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://gitlab.com/interface-x/npm-json-msg.git"
16
+ },
17
+ "keywords": [
18
+ "json"
19
+ ],
20
+ "author": "K.Nevelsteen, PhD",
21
+ "license": "ISC",
22
+ "bugs": {
23
+ "url": "https://gitlab.com/interface-x/npm-json-msg.git/-/issues"
24
+ },
25
+ "homepage": "https://gitlab.com/interface-x/npm-json-msg.git#readme",
26
+ "dependencies": {
27
+ "@knev/bitlogr": "^0.2.12",
28
+ "uuid": "^9.0.0"
29
+ },
30
+ "devDependencies": {
31
+ "@rollup/plugin-commonjs": "^23.0.0",
32
+ "@rollup/plugin-json": "^5.0.0",
33
+ "@rollup/plugin-node-resolve": "^15.0.0",
34
+ "ajv-cli": "^5.0.0",
35
+ "del-cli": "^5.0.0",
36
+ "jasmine": "^4.5.0",
37
+ "rollup": "^2.79.1"
38
+ }
39
+ }
@@ -0,0 +1,24 @@
1
+
2
+ import resolve from '@rollup/plugin-node-resolve';
3
+ import commonjs from '@rollup/plugin-commonjs';
4
+ import json from '@rollup/plugin-json';
5
+
6
+ export default [
7
+ {
8
+ input: 'src/json_msg+ack.mjs',
9
+ output: {
10
+ file: 'dist/json-msg.es.mjs',
11
+ format: 'es'
12
+ },
13
+ plugins: [resolve(), commonjs(), json()]
14
+ },
15
+ {
16
+ input: 'src/json_msg+ack.mjs',
17
+ output: {
18
+ file: 'dist/json-msg.cjs.js',
19
+ format: 'cjs'
20
+ },
21
+ plugins: [resolve(), commonjs(), json()]
22
+ }
23
+
24
+ ];
@@ -0,0 +1,11 @@
1
+ {
2
+ "$id": "validate_Msg",
3
+ "$schema": "http://json-schema.org/draft-07/schema#",
4
+ "type" : "object",
5
+ "required" : ["id", "referer", "dt"],
6
+ "properties" : {
7
+ "id" : { "type" : "string"},
8
+ "referer" : { "type" : "string"},
9
+ "dt" : { "type" : "string" }
10
+ }
11
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "$id": "validate_MsgAck",
3
+ "$schema": "http://json-schema.org/draft-07/schema#",
4
+ "type" : "object",
5
+ "required" : ["ack"],
6
+ "properties" : {
7
+ "ack" : {
8
+ "type" : "object",
9
+ "required" : ["ok"],
10
+ "properties" : {
11
+ "ok" : { "type" : "boolean" }
12
+ }
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "$id": "validate_Msg_msg",
3
+ "$schema": "http://json-schema.org/draft-07/schema#",
4
+ "type" : "object",
5
+ "required" : ["_msg"]
6
+ }
@@ -0,0 +1,22 @@
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');
5
+
6
+ // https://ajv.js.org/standalone.html
7
+
8
+ const fs = require("fs")
9
+ const path = require("path")
10
+ const Ajv = require("ajv")
11
+ const standaloneCode = require("ajv/dist/standalone").default
12
+
13
+ // For ESM, the export name needs to be a valid export name, it can not be `export const #/definitions/Foo = ...;` so we
14
+ // need to provide a mapping between a valid name and the $id field. Below will generate
15
+ // `export const Foo = ...;export const Bar = ...;`
16
+ // This mapping would not have been needed if the `$ids` was just `Bar` and `Foo` instead of `#/definitions/Foo`
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)
20
+
21
+ // Now you can write the module code to file
22
+ fs.writeFileSync(path.join(__dirname, "./validate-esm.mjs"), moduleCode)
@@ -0,0 +1,98 @@
1
+
2
+ import { validate_Msg, validate_Msg_msg, validate_MsgAck } from './validate-esm.mjs';
3
+
4
+ import { v4 as uuidv4 } from 'uuid';
5
+ import { BitLogr, l_LL } from '@knev/bitlogr';
6
+
7
+ let LOGR_= new BitLogr();
8
+
9
+ const l_= {
10
+ VALIDATION : 0b1 << 0,
11
+ }
12
+
13
+ // TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
14
+
15
+ // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
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;
36
+ }
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
+ }
44
+ })();
45
+
46
+ 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
+ }
61
+ }
62
+ })();
63
+
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
+ }
89
+ }
90
+ })();
91
+
92
+ //-------------------------------------------------------------------------------------------------
93
+
94
+ export {
95
+ JSON_Msg,
96
+ JSON_Msg_msg,
97
+ JSON_MsgAck
98
+ }
@@ -0,0 +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;}
@@ -0,0 +1,51 @@
1
+
2
+ import { JSON_Msg, JSON_Msg_msg, JSON_MsgAck } from '../src/json_msg+ack.mjs';
3
+
4
+ const json_msgAckWarp = {
5
+ "id" : "C995A2D2-49BB-4D41-B1D3-C12648652B47",
6
+ "referer": "ipsme.dev/js-blue/blue.html/?uuid=afab6b6b-2284-4abd-b71e-1e369b7e9220",
7
+ "dt": "2023-03-10T20:10:39.408Z",
8
+ "ack": {
9
+ "ok": true,
10
+ },
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\"}"
12
+ };
13
+
14
+ let json_msgAck_msg;
15
+ try {
16
+ json_msgAck_msg= JSON.parse(json_msgAckWarp._msg);
17
+ }
18
+ catch (err) {
19
+ }
20
+
21
+ //-------------------------------------------------------------------------------------------------
22
+ // JSON_Msg
23
+
24
+ describe("Validation", function () {
25
+ it("JSON_Msg.validate(json_msgAckWarp)", function () {
26
+ expect( JSON_Msg.validate(json_msgAckWarp) ).toBe(true);
27
+ });
28
+ });
29
+
30
+ describe("Validation", function () {
31
+ it("JSON_Msg_msg.validate(json_msgAckWarp)", function () {
32
+ expect( JSON_Msg_msg.validate(json_msgAckWarp) ).toBe(true);
33
+ });
34
+ });
35
+
36
+ describe("Validation", function () {
37
+ it("JSON_MsgAck.validate(json_msgAckWarp)", function () {
38
+ expect( JSON_MsgAck.validate(json_msgAckWarp) ).toBe(true);
39
+ });
40
+ });
41
+
42
+ //-------------------------------------------------------------------------------------------------
43
+ // JSON_Msg._msg
44
+
45
+ describe("Validation", function () {
46
+ it("JSON_Msg.validate(json_msgAck_msg)", function () {
47
+ expect( JSON_Msg.validate(json_msgAck_msg) ).toBe(true);
48
+ });
49
+ });
50
+
51
+ //-------------------------------------------------------------------------------------------------