@rootintf/json-msg 1.0.4 → 1.1.3
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/dist/json-msg.cjs.js +131 -115
- package/dist/json-msg.es.mjs +131 -116
- package/package.json +3 -3
- package/src/json_msg+ack.mjs +63 -66
- package/tests/json_msg+ack.spec.mjs +0 -3
package/dist/json-msg.cjs.js
CHANGED
|
@@ -60,16 +60,6 @@ function v4(options, buf, offset) {
|
|
|
60
60
|
|
|
61
61
|
// console.log('OUT', __name({variableName}) );
|
|
62
62
|
|
|
63
|
-
function l_toBigInt_(ref, obj, ignore= false) {
|
|
64
|
-
console.assert(ref !== BigInt(0), 'no labels initialized');
|
|
65
|
-
let bigInt = BigInt(0);
|
|
66
|
-
for (const [k,v] of Object.entries(obj)) {
|
|
67
|
-
if ( ( ignore || v ) && ref[k])
|
|
68
|
-
bigInt|= BigInt( ref[k] );
|
|
69
|
-
// console.log('0b'+ bigInt.toString(2) );
|
|
70
|
-
}
|
|
71
|
-
return bigInt;
|
|
72
|
-
}
|
|
73
63
|
|
|
74
64
|
//-------------------------------------------------------------------------------------------------
|
|
75
65
|
|
|
@@ -81,140 +71,166 @@ function handler_default_( /* ... */ ) {
|
|
|
81
71
|
|
|
82
72
|
//-------------------------------------------------------------------------------------------------
|
|
83
73
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
|
|
91
|
-
// console.log('NOP')
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
set handler(fx) {
|
|
96
|
-
this._handler_log= fx;
|
|
97
|
-
}
|
|
74
|
+
function l_toBigInt_(obj_labels, obj, ignore= false) {
|
|
75
|
+
if (! obj_labels || typeof obj_labels !== 'object')
|
|
76
|
+
throw new Error('obj_labels must be an object');
|
|
77
|
+
if (! obj || typeof obj !== 'object')
|
|
78
|
+
throw new Error('obj must be an object');
|
|
98
79
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
80
|
+
let bigInt = BigInt(0);
|
|
81
|
+
for (const [k,v] of Object.entries(obj)) {
|
|
82
|
+
if ( ( ignore || v ) && obj_labels[k] !== undefined && typeof obj_labels[k] === 'number')
|
|
83
|
+
bigInt|= BigInt( obj_labels[k] );
|
|
84
|
+
// console.log('0b'+ bigInt.toString(2) );
|
|
103
85
|
}
|
|
86
|
+
return bigInt;
|
|
87
|
+
}
|
|
104
88
|
|
|
105
|
-
|
|
106
|
-
// let name= __name(label);
|
|
107
|
-
// _labels[name]= label[name];
|
|
108
|
-
// console.log(_labels);
|
|
109
|
-
// }
|
|
89
|
+
// console.log(l_toBigInt_({},{}))
|
|
110
90
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
this._Bint_toggled= l_toBigInt_(this._Bint_labels, obj);
|
|
91
|
+
const LOGR = (function () {
|
|
92
|
+
let _instance; // Private variable to hold the single instance
|
|
114
93
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
94
|
+
function _create_instance() {
|
|
95
|
+
// Private state (replacing constructor properties)
|
|
96
|
+
let _handler_log = handler_default_;
|
|
97
|
+
let _obj_labels = undefined;
|
|
98
|
+
let _Bint_toggled = BigInt(0);
|
|
119
99
|
|
|
120
|
-
|
|
121
|
-
if (
|
|
100
|
+
function _log_fxn(nr_logged, argsFn /* args */) {
|
|
101
|
+
if ((BigInt(nr_logged) & _Bint_toggled) === BigInt(0))
|
|
122
102
|
return false;
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
103
|
+
|
|
104
|
+
const args = argsFn();
|
|
105
|
+
_handler_log.apply(this, args);
|
|
106
|
+
// _handler_log.apply(this, args);
|
|
128
107
|
return true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
set handler(fx) {
|
|
112
|
+
_handler_log = fx;
|
|
113
|
+
},
|
|
114
|
+
get handler() {
|
|
115
|
+
return _handler_log;
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
get labels() { return _obj_labels; },
|
|
119
|
+
set labels(obj) {
|
|
120
|
+
_obj_labels = obj;
|
|
121
|
+
_Bint_toggled = BigInt(0);
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
// put= function(label, abbrv) {
|
|
125
|
+
// let name= __name(label);
|
|
126
|
+
// _labels[name]= label[name];
|
|
127
|
+
// console.log(_labels);
|
|
128
|
+
// }
|
|
129
|
+
|
|
130
|
+
get toggled() { return _Bint_toggled; },
|
|
131
|
+
set toggled(obj_toggled) {
|
|
132
|
+
_Bint_toggled= l_toBigInt_(_obj_labels, obj_toggled);
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
log(nr_logged, argsFn) {
|
|
136
|
+
|
|
137
|
+
return _log_fxn.call(this, nr_logged, argsFn); // Pass the thunk
|
|
138
|
+
}
|
|
129
139
|
};
|
|
130
140
|
}
|
|
131
141
|
|
|
132
|
-
//
|
|
133
|
-
|
|
142
|
+
// Public interface
|
|
143
|
+
return {
|
|
144
|
+
get_instance() {
|
|
145
|
+
if (!_instance) {
|
|
146
|
+
_instance = _create_instance(); // Lazy initialization
|
|
147
|
+
}
|
|
148
|
+
return _instance;
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
})();
|
|
134
153
|
|
|
135
|
-
let LOGR_=
|
|
154
|
+
let LOGR_= LOGR.get_instance();
|
|
155
|
+
const log_= LOGR_.log;
|
|
136
156
|
|
|
137
157
|
const l_= {
|
|
138
158
|
VALIDATION : 0b1 << 0,
|
|
139
159
|
};
|
|
160
|
+
LOGR_.labels= l_;
|
|
140
161
|
|
|
141
162
|
// TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
|
|
142
163
|
|
|
143
164
|
// "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
|
|
144
165
|
var JSON_Msg = (function() {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
// static parse(json) {
|
|
167
|
-
// return this.#ajv_parse_(json);
|
|
168
|
-
// }
|
|
169
|
-
return new JSON_Msg();
|
|
166
|
+
return {
|
|
167
|
+
merge: function(json) {
|
|
168
|
+
return Object.assign({
|
|
169
|
+
id: v4(),
|
|
170
|
+
referer: null,
|
|
171
|
+
dt: new Date().toISOString()
|
|
172
|
+
}, json || {});
|
|
173
|
+
},
|
|
174
|
+
validate : function (json) {
|
|
175
|
+
const b_valid = schema_Msg(json);
|
|
176
|
+
if (!b_valid)
|
|
177
|
+
log_(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
|
|
178
|
+
return b_valid;
|
|
179
|
+
},
|
|
180
|
+
// TODO: requires a separate ajv module require, and JTD specific schema
|
|
181
|
+
// https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
|
|
182
|
+
// static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
|
|
183
|
+
// static parse(json) {
|
|
184
|
+
// return this.#ajv_parse_(json);
|
|
185
|
+
// }
|
|
186
|
+
}
|
|
170
187
|
})();
|
|
171
188
|
|
|
172
189
|
var JSON_Msg_msg = (function() {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
};
|
|
187
|
-
return new JSON_Msg_msg()
|
|
190
|
+
return {
|
|
191
|
+
create_with_msg_merge: function(_msg, json) {
|
|
192
|
+
return Object.assign(JSON_Msg.merge(json), { _msg });
|
|
193
|
+
},
|
|
194
|
+
validate : function(json) {
|
|
195
|
+
if (! JSON_Msg.validate(json))
|
|
196
|
+
return false;
|
|
197
|
+
const b_valid= schema_Msg_msg(json);
|
|
198
|
+
if (!b_valid)
|
|
199
|
+
log_(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
|
|
200
|
+
return b_valid;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
188
203
|
})();
|
|
189
204
|
|
|
190
205
|
var JSON_MsgAck_msg = (function() {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
206
|
+
return {
|
|
207
|
+
create_with_msgOk : function(_msg, b_ok= true) {
|
|
208
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
209
|
+
ack : {
|
|
210
|
+
"ok" : b_ok,
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
},
|
|
214
|
+
create_with_msgExpired : function(_msg) {
|
|
215
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
216
|
+
ack : {
|
|
217
|
+
"ok" : false,
|
|
218
|
+
"expired" : true
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
validate : function(json) {
|
|
223
|
+
if (! JSON_Msg_msg.validate(json))
|
|
224
|
+
return false;
|
|
225
|
+
const b_valid= schema_MsgAck_msg(json);
|
|
226
|
+
if (!b_valid)
|
|
227
|
+
log_(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
|
|
228
|
+
return b_valid;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
216
231
|
})();
|
|
217
232
|
|
|
218
233
|
exports.JSON_Msg = JSON_Msg;
|
|
219
234
|
exports.JSON_MsgAck_msg = JSON_MsgAck_msg;
|
|
220
235
|
exports.JSON_Msg_msg = JSON_Msg_msg;
|
|
236
|
+
exports.l = l_;
|
package/dist/json-msg.es.mjs
CHANGED
|
@@ -58,16 +58,6 @@ function v4(options, buf, offset) {
|
|
|
58
58
|
|
|
59
59
|
// console.log('OUT', __name({variableName}) );
|
|
60
60
|
|
|
61
|
-
function l_toBigInt_(ref, obj, ignore= false) {
|
|
62
|
-
console.assert(ref !== BigInt(0), 'no labels initialized');
|
|
63
|
-
let bigInt = BigInt(0);
|
|
64
|
-
for (const [k,v] of Object.entries(obj)) {
|
|
65
|
-
if ( ( ignore || v ) && ref[k])
|
|
66
|
-
bigInt|= BigInt( ref[k] );
|
|
67
|
-
// console.log('0b'+ bigInt.toString(2) );
|
|
68
|
-
}
|
|
69
|
-
return bigInt;
|
|
70
|
-
}
|
|
71
61
|
|
|
72
62
|
//-------------------------------------------------------------------------------------------------
|
|
73
63
|
|
|
@@ -79,138 +69,163 @@ function handler_default_( /* ... */ ) {
|
|
|
79
69
|
|
|
80
70
|
//-------------------------------------------------------------------------------------------------
|
|
81
71
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
BitLogr.prototype['log']= function (nr_logged, /* ... */ ) {
|
|
89
|
-
// console.log('NOP')
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
set handler(fx) {
|
|
94
|
-
this._handler_log= fx;
|
|
95
|
-
}
|
|
72
|
+
function l_toBigInt_(obj_labels, obj, ignore= false) {
|
|
73
|
+
if (! obj_labels || typeof obj_labels !== 'object')
|
|
74
|
+
throw new Error('obj_labels must be an object');
|
|
75
|
+
if (! obj || typeof obj !== 'object')
|
|
76
|
+
throw new Error('obj must be an object');
|
|
96
77
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
78
|
+
let bigInt = BigInt(0);
|
|
79
|
+
for (const [k,v] of Object.entries(obj)) {
|
|
80
|
+
if ( ( ignore || v ) && obj_labels[k] !== undefined && typeof obj_labels[k] === 'number')
|
|
81
|
+
bigInt|= BigInt( obj_labels[k] );
|
|
82
|
+
// console.log('0b'+ bigInt.toString(2) );
|
|
101
83
|
}
|
|
84
|
+
return bigInt;
|
|
85
|
+
}
|
|
102
86
|
|
|
103
|
-
|
|
104
|
-
// let name= __name(label);
|
|
105
|
-
// _labels[name]= label[name];
|
|
106
|
-
// console.log(_labels);
|
|
107
|
-
// }
|
|
87
|
+
// console.log(l_toBigInt_({},{}))
|
|
108
88
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
this._Bint_toggled= l_toBigInt_(this._Bint_labels, obj);
|
|
89
|
+
const LOGR = (function () {
|
|
90
|
+
let _instance; // Private variable to hold the single instance
|
|
112
91
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
92
|
+
function _create_instance() {
|
|
93
|
+
// Private state (replacing constructor properties)
|
|
94
|
+
let _handler_log = handler_default_;
|
|
95
|
+
let _obj_labels = undefined;
|
|
96
|
+
let _Bint_toggled = BigInt(0);
|
|
117
97
|
|
|
118
|
-
|
|
119
|
-
if (
|
|
98
|
+
function _log_fxn(nr_logged, argsFn /* args */) {
|
|
99
|
+
if ((BigInt(nr_logged) & _Bint_toggled) === BigInt(0))
|
|
120
100
|
return false;
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
101
|
+
|
|
102
|
+
const args = argsFn();
|
|
103
|
+
_handler_log.apply(this, args);
|
|
104
|
+
// _handler_log.apply(this, args);
|
|
126
105
|
return true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
set handler(fx) {
|
|
110
|
+
_handler_log = fx;
|
|
111
|
+
},
|
|
112
|
+
get handler() {
|
|
113
|
+
return _handler_log;
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
get labels() { return _obj_labels; },
|
|
117
|
+
set labels(obj) {
|
|
118
|
+
_obj_labels = obj;
|
|
119
|
+
_Bint_toggled = BigInt(0);
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
// put= function(label, abbrv) {
|
|
123
|
+
// let name= __name(label);
|
|
124
|
+
// _labels[name]= label[name];
|
|
125
|
+
// console.log(_labels);
|
|
126
|
+
// }
|
|
127
|
+
|
|
128
|
+
get toggled() { return _Bint_toggled; },
|
|
129
|
+
set toggled(obj_toggled) {
|
|
130
|
+
_Bint_toggled= l_toBigInt_(_obj_labels, obj_toggled);
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
log(nr_logged, argsFn) {
|
|
134
|
+
|
|
135
|
+
return _log_fxn.call(this, nr_logged, argsFn); // Pass the thunk
|
|
136
|
+
}
|
|
127
137
|
};
|
|
128
138
|
}
|
|
129
139
|
|
|
130
|
-
//
|
|
131
|
-
|
|
140
|
+
// Public interface
|
|
141
|
+
return {
|
|
142
|
+
get_instance() {
|
|
143
|
+
if (!_instance) {
|
|
144
|
+
_instance = _create_instance(); // Lazy initialization
|
|
145
|
+
}
|
|
146
|
+
return _instance;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
})();
|
|
132
151
|
|
|
133
|
-
let LOGR_=
|
|
152
|
+
let LOGR_= LOGR.get_instance();
|
|
153
|
+
const log_= LOGR_.log;
|
|
134
154
|
|
|
135
155
|
const l_= {
|
|
136
156
|
VALIDATION : 0b1 << 0,
|
|
137
157
|
};
|
|
158
|
+
LOGR_.labels= l_;
|
|
138
159
|
|
|
139
160
|
// TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
|
|
140
161
|
|
|
141
162
|
// "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
|
|
142
163
|
var JSON_Msg = (function() {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
// static parse(json) {
|
|
165
|
-
// return this.#ajv_parse_(json);
|
|
166
|
-
// }
|
|
167
|
-
return new JSON_Msg();
|
|
164
|
+
return {
|
|
165
|
+
merge: function(json) {
|
|
166
|
+
return Object.assign({
|
|
167
|
+
id: v4(),
|
|
168
|
+
referer: null,
|
|
169
|
+
dt: new Date().toISOString()
|
|
170
|
+
}, json || {});
|
|
171
|
+
},
|
|
172
|
+
validate : function (json) {
|
|
173
|
+
const b_valid = schema_Msg(json);
|
|
174
|
+
if (!b_valid)
|
|
175
|
+
log_(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
|
|
176
|
+
return b_valid;
|
|
177
|
+
},
|
|
178
|
+
// TODO: requires a separate ajv module require, and JTD specific schema
|
|
179
|
+
// https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
|
|
180
|
+
// static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
|
|
181
|
+
// static parse(json) {
|
|
182
|
+
// return this.#ajv_parse_(json);
|
|
183
|
+
// }
|
|
184
|
+
}
|
|
168
185
|
})();
|
|
169
186
|
|
|
170
187
|
var JSON_Msg_msg = (function() {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
};
|
|
185
|
-
return new JSON_Msg_msg()
|
|
188
|
+
return {
|
|
189
|
+
create_with_msg_merge: function(_msg, json) {
|
|
190
|
+
return Object.assign(JSON_Msg.merge(json), { _msg });
|
|
191
|
+
},
|
|
192
|
+
validate : function(json) {
|
|
193
|
+
if (! JSON_Msg.validate(json))
|
|
194
|
+
return false;
|
|
195
|
+
const b_valid= schema_Msg_msg(json);
|
|
196
|
+
if (!b_valid)
|
|
197
|
+
log_(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
|
|
198
|
+
return b_valid;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
186
201
|
})();
|
|
187
202
|
|
|
188
203
|
var JSON_MsgAck_msg = (function() {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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= schema_MsgAck_msg(json);
|
|
224
|
+
if (!b_valid)
|
|
225
|
+
log_(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
|
|
226
|
+
return b_valid;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
214
229
|
})();
|
|
215
230
|
|
|
216
|
-
export { JSON_Msg, JSON_MsgAck_msg, JSON_Msg_msg };
|
|
231
|
+
export { JSON_Msg, JSON_MsgAck_msg, JSON_Msg_msg, l_ as l };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rootintf/json-msg",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/json-msg.cjs.js",
|
|
6
6
|
"browser": "dist/json-msg.es.mjs",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"homepage": "https://gitlab.com/interface-x/npm-json-msg.git#readme",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@knev/bitlogr": "^0.
|
|
27
|
+
"@knev/bitlogr": "^2.0.11",
|
|
28
28
|
"ajv": "^8.17.1",
|
|
29
29
|
"uuid": "^11.1.0"
|
|
30
30
|
},
|
|
@@ -35,6 +35,6 @@
|
|
|
35
35
|
"ajv-cli": "^5.0.0",
|
|
36
36
|
"del-cli": "^6.0.0",
|
|
37
37
|
"jasmine": "^5.6.0",
|
|
38
|
-
"rollup": "^4.
|
|
38
|
+
"rollup": "^4.39.0"
|
|
39
39
|
}
|
|
40
40
|
}
|
package/src/json_msg+ack.mjs
CHANGED
|
@@ -2,89 +2,85 @@
|
|
|
2
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
|
-
import {
|
|
5
|
+
import { LOGR, l_LL } from '@knev/bitlogr';
|
|
6
6
|
|
|
7
|
-
let LOGR_=
|
|
7
|
+
let LOGR_= LOGR.get_instance();
|
|
8
|
+
const log_= LOGR_.log;
|
|
8
9
|
|
|
9
10
|
const l_= {
|
|
10
11
|
VALIDATION : 0b1 << 0,
|
|
11
12
|
}
|
|
13
|
+
LOGR_.labels= l_;
|
|
12
14
|
|
|
13
15
|
// TODO: use RFC3339 "timestamp"? https://ajv.js.org/json-type-definition.html
|
|
14
16
|
|
|
15
17
|
// "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
|
|
16
18
|
var JSON_Msg = (function() {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// static parse(json) {
|
|
39
|
-
// return this.#ajv_parse_(json);
|
|
40
|
-
// }
|
|
41
|
-
return new JSON_Msg();
|
|
19
|
+
return {
|
|
20
|
+
merge: function(json) {
|
|
21
|
+
return Object.assign({
|
|
22
|
+
id: uuidv4(),
|
|
23
|
+
referer: null,
|
|
24
|
+
dt: new Date().toISOString()
|
|
25
|
+
}, json || {});
|
|
26
|
+
},
|
|
27
|
+
validate : function (json) {
|
|
28
|
+
const b_valid = schema_Msg(json);
|
|
29
|
+
if (!b_valid)
|
|
30
|
+
log_(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors)
|
|
31
|
+
return b_valid;
|
|
32
|
+
},
|
|
33
|
+
// TODO: requires a separate ajv module require, and JTD specific schema
|
|
34
|
+
// https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
|
|
35
|
+
// static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
|
|
36
|
+
// static parse(json) {
|
|
37
|
+
// return this.#ajv_parse_(json);
|
|
38
|
+
// }
|
|
39
|
+
}
|
|
42
40
|
})();
|
|
43
41
|
|
|
44
42
|
var JSON_Msg_msg = (function() {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return b_valid;
|
|
43
|
+
return {
|
|
44
|
+
create_with_msg_merge: function(_msg, json) {
|
|
45
|
+
return Object.assign(JSON_Msg.merge(json), { _msg });
|
|
46
|
+
},
|
|
47
|
+
validate : function(json) {
|
|
48
|
+
if (! JSON_Msg.validate(json))
|
|
49
|
+
return false;
|
|
50
|
+
const b_valid= schema_Msg_msg(json);
|
|
51
|
+
if (!b_valid)
|
|
52
|
+
log_(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors)
|
|
53
|
+
return b_valid;
|
|
54
|
+
}
|
|
58
55
|
}
|
|
59
|
-
return new JSON_Msg_msg()
|
|
60
56
|
})();
|
|
61
57
|
|
|
62
58
|
var JSON_MsgAck_msg = (function() {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
59
|
+
return {
|
|
60
|
+
create_with_msgOk : function(_msg, b_ok= true) {
|
|
61
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
62
|
+
ack : {
|
|
63
|
+
"ok" : b_ok,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
create_with_msgExpired : function(_msg) {
|
|
68
|
+
return JSON_Msg_msg.create_with_msg_merge(_msg, {
|
|
69
|
+
ack : {
|
|
70
|
+
"ok" : false,
|
|
71
|
+
"expired" : true
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
validate : function(json) {
|
|
76
|
+
if (! JSON_Msg_msg.validate(json))
|
|
77
|
+
return false;
|
|
78
|
+
const b_valid= schema_MsgAck_msg(json);
|
|
79
|
+
if (!b_valid)
|
|
80
|
+
log_(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors)
|
|
81
|
+
return b_valid;
|
|
82
|
+
}
|
|
86
83
|
}
|
|
87
|
-
return new JSON_MsgAck_msg();
|
|
88
84
|
})();
|
|
89
85
|
|
|
90
86
|
//-------------------------------------------------------------------------------------------------
|
|
@@ -92,5 +88,6 @@ var JSON_MsgAck_msg = (function() {
|
|
|
92
88
|
export {
|
|
93
89
|
JSON_Msg,
|
|
94
90
|
JSON_Msg_msg,
|
|
95
|
-
JSON_MsgAck_msg
|
|
91
|
+
JSON_MsgAck_msg,
|
|
92
|
+
l_ as l
|
|
96
93
|
}
|
|
@@ -42,13 +42,10 @@ describe("JSON_Msg validation", function () {
|
|
|
42
42
|
it("JSON_Msg.merge({ foo: \"bar\" })", function () {
|
|
43
43
|
const merged_msg = JSON_Msg.merge({ foo: "bar" });
|
|
44
44
|
|
|
45
|
-
expect( merged_msg.constructor.name ).toBe('JSON_Msg');
|
|
46
45
|
expect( merged_msg.foo ).toBe('bar');
|
|
47
46
|
expect( JSON_Msg.validate(merged_msg) ).toBe(true);
|
|
48
47
|
});
|
|
49
48
|
|
|
50
|
-
// console.log(); // true
|
|
51
|
-
|
|
52
49
|
it("JSON_Msg.validate(json_msgAckWarp)", function () {
|
|
53
50
|
expect( JSON_Msg.validate(json_msgAckWarp) ).toBe(true);
|
|
54
51
|
});
|