badmfck-api-server 4.0.95 → 4.0.97

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.
@@ -97,7 +97,7 @@ async function Initializer(services) {
97
97
  }
98
98
  exports.Initializer = Initializer;
99
99
  class APIService extends BaseService_1.BaseService {
100
- version = "4.0.95";
100
+ version = "4.0.97";
101
101
  options;
102
102
  monitor = null;
103
103
  started = new Date();
@@ -284,7 +284,7 @@ class DBService extends BaseService_1.BaseService {
284
284
  return error;
285
285
  }
286
286
  async reportQuery(result, req) {
287
- const hour = (new Date().getHours() + 1).toString().padStart(2, "0");
287
+ const hour = new Date().getHours().toString().padStart(2, "0");
288
288
  if (this.queriesStat.hour !== hour) {
289
289
  this.queriesStat.hour = hour;
290
290
  this.queriesStat.stat = { failed: 0, success: 0, failedQueries: [] };
@@ -297,15 +297,14 @@ class DBService extends BaseService_1.BaseService {
297
297
  this.queriesStat.stat.success++;
298
298
  const execTime = result.execution.finished_at - result.execution.started_at;
299
299
  if (execTime && execTime > 0) {
300
- for (let i of this.longQueries) {
301
- if (execTime > i.execution) {
302
- this.longQueries.push({
303
- execution: execTime,
304
- query: req.query,
305
- started_at: result.execution.started_at,
306
- finished_at: result.execution.finished_at
307
- });
308
- }
300
+ const minLong = this.longQueries.length < 20 || execTime > this.longQueries[0].execution;
301
+ if (minLong) {
302
+ this.longQueries.push({
303
+ execution: execTime,
304
+ query: req.query,
305
+ started_at: result.execution.started_at,
306
+ finished_at: result.execution.finished_at
307
+ });
309
308
  }
310
309
  this.longQueries.sort((a, b) => a.execution - b.execution);
311
310
  if (this.longQueries.length > 20)
@@ -62,7 +62,7 @@ class MicroserviceClient extends BaseService_1.BaseService {
62
62
  const ts = Date.now().toString();
63
63
  const nonce = crypto_1.default.randomBytes(12).toString("hex");
64
64
  const maxAttempts = 10;
65
- const attemptTimeout = 500;
65
+ const attemptTimeout = 1000;
66
66
  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
67
67
  const rawSecret = Buffer.from(this.options.securityKey, "base64");
68
68
  const key = crypto_1.default.createHash("sha256").update(rawSecret).digest();
@@ -108,7 +108,10 @@ class MicroserviceClient extends BaseService_1.BaseService {
108
108
  }
109
109
  return { ...DefaultErrors_1.default.EXTERNAL_SERVICE_ERROR, details: "Microservice processing timeout, attempts: " + attempt };
110
110
  }
111
- return (body && typeof body === "object" && "error" in body) ? body.error : (body.data ?? null);
111
+ if (body && typeof body === "object") {
112
+ return "error" in body ? body.error : (body.data ?? null);
113
+ }
114
+ return body ?? null;
112
115
  }
113
116
  return { ...DefaultErrors_1.default.EXTERNAL_SERVICE_ERROR, details: "Unknown polling error" };
114
117
  }
@@ -21,8 +21,7 @@ class DataProvider {
21
21
  this.cacheTime = +cacheTime;
22
22
  if (isNaN(this.cacheTime))
23
23
  this.cacheTime = 0;
24
- if (!this.name)
25
- this.name = "DataProvider " + (DataProvider.nextID++);
24
+ this.name = name ?? "DataProvider " + (DataProvider.nextID++);
26
25
  }
27
26
  async forceUpdate() {
28
27
  if (this.busy)
@@ -58,7 +58,7 @@ class Validator {
58
58
  s_options.min = undefined;
59
59
  if (s_options.max == undefined || isNaN(s_options.max)) {
60
60
  if (structure[key + "max"] && !isNaN(structure[key + "max"]))
61
- s_options.min = parseInt(structure[key + "max"]);
61
+ s_options.max = parseInt(structure[key + "max"]);
62
62
  }
63
63
  if (s_options.max && isNaN(s_options.max))
64
64
  s_options.max = undefined;
@@ -143,6 +143,13 @@ class Validator {
143
143
  }
144
144
  }
145
145
  }
146
+ if (structureOptions.optional && typeof object[i] === "object" && object[i] !== null) {
147
+ const isEmpty = Array.isArray(object[i]) ? object[i].length === 0 : Object.keys(object[i]).length === 0;
148
+ if (isEmpty) {
149
+ foundKeys.push(i);
150
+ continue;
151
+ }
152
+ }
146
153
  if ((object[i] === null || object[i] === undefined)) {
147
154
  if (structure[i] === null || structure[i] === undefined) {
148
155
  foundKeys.push(i);
@@ -210,32 +217,43 @@ class Validator {
210
217
  if (structure['$__' + i + "_values"])
211
218
  structureOptions.values = structure['$__' + i + "_values"];
212
219
  if (Array.isArray(structureOptions.values) && structureOptions.values.length) {
213
- if (!structureOptions.values.includes(object[i])) {
214
- if (structureOptions.default && structureOptions.values.includes(structureOptions.default)) {
215
- object[i] = structureOptions.default;
216
- continue;
217
- }
220
+ const checkOneValue = (val) => {
221
+ if (structureOptions.values.includes(val))
222
+ return null;
218
223
  let caseValue = null;
219
- for (let i of structureOptions.values) {
220
- if (i === null || i === undefined)
224
+ for (const av of structureOptions.values) {
225
+ if (av === null || av === undefined)
221
226
  continue;
222
- const v = (i + "").toLowerCase().trim();
223
- if (v === (object[i] + "").toLowerCase().trim()) {
224
- caseValue = object[i];
227
+ if ((av + "").toLowerCase().trim() === (val + "").toLowerCase().trim()) {
228
+ caseValue = av;
225
229
  break;
226
230
  }
227
- const val = (object[i] + "").toLowerCase().trim();
228
- if (typeof i === "object") {
229
- const keys = Object.keys(i);
230
- for (const key of keys) {
231
- if ((i[key] + "").toLowerCase().trim() === val) {
232
- caseValue = i[key];
231
+ if (typeof av === "object") {
232
+ for (const key of Object.keys(av)) {
233
+ if ((av[key] + "").toLowerCase().trim() === (val + "").toLowerCase().trim()) {
234
+ caseValue = av[key];
233
235
  break;
234
236
  }
235
237
  }
236
238
  }
237
239
  }
238
- errors.push("wrong value for field '" + parentPath + i + "'" + (caseValue ? ", check case for: " + caseValue : ""));
240
+ return "wrong value for field '" + parentPath + i + "'" + (caseValue ? ", check case for: " + caseValue : "");
241
+ };
242
+ if (Array.isArray(object[i])) {
243
+ for (const item of object[i]) {
244
+ const err = checkOneValue(item);
245
+ if (err)
246
+ errors.push(err);
247
+ }
248
+ }
249
+ else {
250
+ if (structureOptions.default && structureOptions.values.includes(structureOptions.default)) {
251
+ object[i] = structureOptions.default;
252
+ continue;
253
+ }
254
+ const err = checkOneValue(object[i]);
255
+ if (err)
256
+ errors.push(err);
239
257
  }
240
258
  }
241
259
  if (typeof structure[i] === "string" && structureOptions.regex) {
@@ -257,7 +275,7 @@ class Validator {
257
275
  }
258
276
  if (typeof structure[i] === "number") {
259
277
  if (typeof object[i] === "string") {
260
- if (object[i].indexOf("."))
278
+ if (object[i].indexOf(".") !== -1)
261
279
  object[i] = parseFloat(object[i]);
262
280
  else
263
281
  object[i] = parseInt(object[i]);
@@ -477,7 +495,7 @@ class Validator {
477
495
  if (opt.max && value > opt.max)
478
496
  return ValidationReport.VALUE_TOO_BIG;
479
497
  if (opt.min && value < opt.min)
480
- return ValidationReport.VALUE_TOO_BIG;
498
+ return ValidationReport.VALUE_TOO_SHORT;
481
499
  return ValidationReport.OK;
482
500
  }
483
501
  if (typeof value !== "string")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "4.0.95",
3
+ "version": "4.0.97",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",