topkat-utils 1.1.13 → 1.2.1
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/CHANGELOG.md +4 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +58 -0
- package/dist/index.js.map +1 -0
- package/dist/src/array-utils.d.ts +56 -0
- package/dist/src/array-utils.js +160 -0
- package/dist/src/array-utils.js.map +1 -0
- package/dist/src/clean-stack-trace.d.ts +1 -0
- package/dist/src/clean-stack-trace.js +46 -0
- package/dist/src/clean-stack-trace.js.map +1 -0
- package/dist/src/config.d.ts +44 -0
- package/dist/src/config.js +66 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/date-utils.d.ts +100 -0
- package/dist/src/date-utils.js +407 -0
- package/dist/src/date-utils.js.map +1 -0
- package/dist/src/env-utils.d.ts +8 -0
- package/dist/src/env-utils.js +38 -0
- package/dist/src/env-utils.js.map +1 -0
- package/dist/src/error-utils.d.ts +29 -0
- package/dist/src/error-utils.js +202 -0
- package/dist/src/error-utils.js.map +1 -0
- package/dist/src/is-empty.d.ts +1 -0
- package/dist/src/is-empty.js +13 -0
- package/dist/src/is-empty.js.map +1 -0
- package/dist/src/is-object.d.ts +2 -0
- package/dist/src/is-object.js +7 -0
- package/dist/src/is-object.js.map +1 -0
- package/dist/src/isset.d.ts +1 -0
- package/dist/src/isset.js +12 -0
- package/dist/src/isset.js.map +1 -0
- package/dist/src/logger-utils.d.ts +76 -0
- package/dist/src/logger-utils.js +484 -0
- package/dist/src/logger-utils.js.map +1 -0
- package/dist/src/loop-utils.d.ts +40 -0
- package/dist/src/loop-utils.js +182 -0
- package/dist/src/loop-utils.js.map +1 -0
- package/dist/src/math-utils.d.ts +23 -0
- package/dist/src/math-utils.js +59 -0
- package/dist/src/math-utils.js.map +1 -0
- package/dist/src/mongo-utils.d.ts +11 -0
- package/dist/src/mongo-utils.js +63 -0
- package/dist/src/mongo-utils.js.map +1 -0
- package/dist/src/object-utils.d.ts +96 -0
- package/dist/src/object-utils.js +409 -0
- package/dist/src/object-utils.js.map +1 -0
- package/dist/src/private/config.d.ts +44 -0
- package/dist/src/private/config.js +66 -0
- package/dist/src/private/config.js.map +1 -0
- package/dist/src/private/error-handler.d.ts +10 -0
- package/dist/src/private/error-handler.js +46 -0
- package/dist/src/private/error-handler.js.map +1 -0
- package/dist/src/private/types.d.ts +5 -0
- package/dist/src/private/types.js +3 -0
- package/dist/src/private/types.js.map +1 -0
- package/dist/src/regexp-utils.d.ts +12 -0
- package/dist/src/regexp-utils.js +45 -0
- package/dist/src/regexp-utils.js.map +1 -0
- package/dist/src/remove-circular-json-stringify.d.ts +1 -0
- package/dist/src/remove-circular-json-stringify.js +21 -0
- package/dist/src/remove-circular-json-stringify.js.map +1 -0
- package/dist/src/string-utils.d.ts +77 -0
- package/dist/src/string-utils.js +267 -0
- package/dist/src/string-utils.js.map +1 -0
- package/dist/src/tests-utils.d.ts +21 -0
- package/dist/src/tests-utils.js +80 -0
- package/dist/src/tests-utils.js.map +1 -0
- package/dist/src/timer-utils.d.ts +16 -0
- package/dist/src/timer-utils.js +146 -0
- package/dist/src/timer-utils.js.map +1 -0
- package/dist/src/transaction-utils.d.ts +14 -0
- package/dist/src/transaction-utils.js +157 -0
- package/dist/src/transaction-utils.js.map +1 -0
- package/dist/src/types.d.ts +5 -0
- package/dist/src/types.js +3 -0
- package/dist/src/types.js.map +1 -0
- package/dist/src/validation-utils.d.ts +80 -0
- package/dist/src/validation-utils.js +249 -0
- package/dist/src/validation-utils.js.map +1 -0
- package/dist/src/wtf-utils.d.ts +7 -0
- package/dist/src/wtf-utils.js +88 -0
- package/dist/src/wtf-utils.js.map +1 -0
- package/index.ts +1 -1
- package/package.json +1 -1
- package/src/clean-stack-trace.ts +40 -0
- package/src/config.ts +1 -1
- package/src/date-utils.ts +5 -5
- package/src/error-utils.ts +86 -44
- package/src/logger-utils.ts +2 -2
- package/src/loop-utils.ts +1 -1
- package/src/object-utils.ts +6 -6
- package/src/string-utils.ts +1 -1
- package/src/tests-utils.ts +2 -2
- package/src/timer-utils.ts +2 -2
- package/src/{private/types.ts → types.ts} +0 -0
- package/src/validation-utils.ts +8 -8
- package/src/private/error-handler.ts +0 -21
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.executeInDelayedLoop = exports.waitUntilTrue = exports.runAsync = exports.timeout = void 0;
|
|
40
|
+
//----------------------------------------
|
|
41
|
+
// TIMEOUT UTILS
|
|
42
|
+
//----------------------------------------
|
|
43
|
+
var logger_utils_1 = require("./logger-utils");
|
|
44
|
+
var error_utils_1 = require("./error-utils");
|
|
45
|
+
function timeout(ms, fn) {
|
|
46
|
+
if (fn === void 0) { fn = function () { }; }
|
|
47
|
+
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
48
|
+
return [2 /*return*/, new Promise(function (res) { return setTimeout(res, ms); }).then(fn)];
|
|
49
|
+
}); });
|
|
50
|
+
}
|
|
51
|
+
exports.timeout = timeout;
|
|
52
|
+
function runAsync(callback, milliseconds$) {
|
|
53
|
+
if (milliseconds$ === void 0) { milliseconds$ = 1; }
|
|
54
|
+
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
|
|
55
|
+
return [2 /*return*/, timeout(milliseconds$, callback)];
|
|
56
|
+
}); });
|
|
57
|
+
}
|
|
58
|
+
exports.runAsync = runAsync;
|
|
59
|
+
/**
|
|
60
|
+
*
|
|
61
|
+
* @param {Function} callback function that shall return ===true asynchronously
|
|
62
|
+
* @param {Number} timeoutSec default:10; general timeout in seconds
|
|
63
|
+
* @param {Boolean|String} errorAfterNSeconds default:true output an error in case of timeout, can be the displayed error message
|
|
64
|
+
* @param {*} cliOutput write a cli progress to show that a process is running
|
|
65
|
+
*/
|
|
66
|
+
function waitUntilTrue(callback, timeoutSec, errorAfterNSeconds, cliOutput) {
|
|
67
|
+
if (timeoutSec === void 0) { timeoutSec = 10; }
|
|
68
|
+
if (errorAfterNSeconds === void 0) { errorAfterNSeconds = true; }
|
|
69
|
+
if (cliOutput === void 0) { cliOutput = true; }
|
|
70
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
71
|
+
var generalTimeout, step, errMess;
|
|
72
|
+
return __generator(this, function (_a) {
|
|
73
|
+
switch (_a.label) {
|
|
74
|
+
case 0:
|
|
75
|
+
generalTimeout = true;
|
|
76
|
+
step = 3;
|
|
77
|
+
errMess = typeof errorAfterNSeconds === 'string' ? 'Timeout: ' + errorAfterNSeconds : 'Timeout for waitUntilTrue() callback';
|
|
78
|
+
if (timeoutSec)
|
|
79
|
+
setTimeout(function () { return generalTimeout = false; }, timeoutSec * 1000);
|
|
80
|
+
_a.label = 1;
|
|
81
|
+
case 1:
|
|
82
|
+
if (!(callback() !== true && generalTimeout)) return [3 /*break*/, 3];
|
|
83
|
+
if (cliOutput)
|
|
84
|
+
(0, logger_utils_1.cliProgressBar)(step++);
|
|
85
|
+
return [4 /*yield*/, timeout(300)];
|
|
86
|
+
case 2:
|
|
87
|
+
_a.sent();
|
|
88
|
+
return [3 /*break*/, 1];
|
|
89
|
+
case 3:
|
|
90
|
+
if (cliOutput)
|
|
91
|
+
process.stdout.write("\n");
|
|
92
|
+
if (!generalTimeout && errorAfterNSeconds)
|
|
93
|
+
throw new error_utils_1.DescriptiveError(errMess, { code: 500 });
|
|
94
|
+
return [2 /*return*/];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
exports.waitUntilTrue = waitUntilTrue;
|
|
100
|
+
var delayedLoopParams = [];
|
|
101
|
+
var isExecuting = false;
|
|
102
|
+
/** Allow to perform an action in a delayed loop, useful for example to avoid reaching limits on servers. This function can be securely called multiple times.
|
|
103
|
+
* @param {Function} callback
|
|
104
|
+
* @param {Number} time default: 500ms;
|
|
105
|
+
* @param {Function} errorCallback default: e => C.error(e)
|
|
106
|
+
*/
|
|
107
|
+
function executeInDelayedLoop(callback, time, errorCallback) {
|
|
108
|
+
if (time === void 0) { time = 500; }
|
|
109
|
+
if (errorCallback === void 0) { errorCallback = function (e) { return logger_utils_1.C.error(e); }; }
|
|
110
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
111
|
+
var _a, callback_1, time_1, errorCallback_1, err_1;
|
|
112
|
+
return __generator(this, function (_b) {
|
|
113
|
+
switch (_b.label) {
|
|
114
|
+
case 0:
|
|
115
|
+
delayedLoopParams.push([callback, time, errorCallback]);
|
|
116
|
+
if (isExecuting)
|
|
117
|
+
return [2 /*return*/];
|
|
118
|
+
isExecuting = true;
|
|
119
|
+
_b.label = 1;
|
|
120
|
+
case 1:
|
|
121
|
+
if (!delayedLoopParams.length) return [3 /*break*/, 7];
|
|
122
|
+
_a = delayedLoopParams.shift(), callback_1 = _a[0], time_1 = _a[1], errorCallback_1 = _a[2];
|
|
123
|
+
_b.label = 2;
|
|
124
|
+
case 2:
|
|
125
|
+
_b.trys.push([2, 5, , 6]);
|
|
126
|
+
return [4 /*yield*/, callback_1()];
|
|
127
|
+
case 3:
|
|
128
|
+
_b.sent();
|
|
129
|
+
return [4 /*yield*/, timeout(time_1)];
|
|
130
|
+
case 4:
|
|
131
|
+
_b.sent();
|
|
132
|
+
return [3 /*break*/, 6];
|
|
133
|
+
case 5:
|
|
134
|
+
err_1 = _b.sent();
|
|
135
|
+
errorCallback_1(err_1);
|
|
136
|
+
return [3 /*break*/, 6];
|
|
137
|
+
case 6: return [3 /*break*/, 1];
|
|
138
|
+
case 7:
|
|
139
|
+
isExecuting = false;
|
|
140
|
+
return [2 /*return*/];
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
exports.executeInDelayedLoop = executeInDelayedLoop;
|
|
146
|
+
//# sourceMappingURL=timer-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timer-utils.js","sourceRoot":"","sources":["../../src/timer-utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,0CAA0C;AAC1C,gBAAgB;AAChB,0CAA0C;AAC1C,+CAAkD;AAClD,6CAAgD;AAGhD,SAAsB,OAAO,CAAC,EAAE,EAAE,EAAc;IAAd,mBAAA,EAAA,mBAAa,CAAC;;QAAI,sBAAO,IAAI,OAAO,CAAC,UAAA,GAAG,IAAI,OAAA,UAAU,CAAC,GAAG,EAAE,EAAE,CAAC,EAAnB,CAAmB,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,EAAA;;CAAE;AAA7G,0BAA6G;AAE7G,SAAsB,QAAQ,CAAC,QAAQ,EAAE,aAAiB;IAAjB,8BAAA,EAAA,iBAAiB;;QAAI,sBAAO,OAAO,CAAC,aAAa,EAAE,QAAQ,CAAC,EAAA;;CAAE;AAAvG,4BAAuG;AAEvG;;;;;;GAMG;AACH,SAAsB,aAAa,CAAC,QAAQ,EAAE,UAAe,EAAE,kBAAyB,EAAE,SAAgB;IAA5D,2BAAA,EAAA,eAAe;IAAE,mCAAA,EAAA,yBAAyB;IAAE,0BAAA,EAAA,gBAAgB;;;;;;oBAClG,cAAc,GAAG,IAAI,CAAA;oBACrB,IAAI,GAAG,CAAC,CAAA;oBACN,OAAO,GAAG,OAAO,kBAAkB,KAAK,QAAQ,CAAC,CAAC,CA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AAM,OAAO,CAAC,MAAI,CAAC,EAAA;;oBAAnB,SAAmB,CAAA;;;;oBAEnB,eAAa,CAAC,KAAG,CAAC,CAAA;;;;oBAG1B,WAAW,GAAG,KAAK,CAAA;;;;;CACtB;AAdD,oDAcC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** Allow to perform async functions in a defined order
|
|
2
|
+
* This adds the callback to a queue and is resolved when ALL previous callbacks with same name are executed
|
|
3
|
+
* Use it like: await transaction('nameOfTheFlow', async () => { ...myFunction })
|
|
4
|
+
* @param {String|Function} name name for the actions that should never happen concurrently
|
|
5
|
+
* @param {Function} asyncCallback
|
|
6
|
+
* @param {Number} timeout default: 120000 (120s) will throw an error if transaction time is higher that this amount of ms
|
|
7
|
+
* @returns {Promise}
|
|
8
|
+
*/
|
|
9
|
+
export declare function transaction(name: any, asyncCallback: any, timeout?: number, doNotThrow?: boolean): Promise<unknown>;
|
|
10
|
+
export declare function removeItemFromQueue(name: any): Promise<void>;
|
|
11
|
+
/** Wait for a transaction to complete without creating a new transaction
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
export declare function waitForTransaction(transactionName: any, forceReleaseInSeconds?: number): Promise<void>;
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.waitForTransaction = exports.removeItemFromQueue = exports.transaction = void 0;
|
|
40
|
+
//----------------------------------------
|
|
41
|
+
// TRANSACTION
|
|
42
|
+
//----------------------------------------
|
|
43
|
+
var isset_1 = require("./isset");
|
|
44
|
+
var logger_utils_1 = require("./logger-utils");
|
|
45
|
+
var timer_utils_1 = require("./timer-utils");
|
|
46
|
+
var transactionRunning = { __default: false };
|
|
47
|
+
var queue = { __default: [] };
|
|
48
|
+
/** Allow to perform async functions in a defined order
|
|
49
|
+
* This adds the callback to a queue and is resolved when ALL previous callbacks with same name are executed
|
|
50
|
+
* Use it like: await transaction('nameOfTheFlow', async () => { ...myFunction })
|
|
51
|
+
* @param {String|Function} name name for the actions that should never happen concurrently
|
|
52
|
+
* @param {Function} asyncCallback
|
|
53
|
+
* @param {Number} timeout default: 120000 (120s) will throw an error if transaction time is higher that this amount of ms
|
|
54
|
+
* @returns {Promise}
|
|
55
|
+
*/
|
|
56
|
+
function transaction(name, asyncCallback, timeout, doNotThrow) {
|
|
57
|
+
if (timeout === void 0) { timeout = 120000; }
|
|
58
|
+
if (doNotThrow === void 0) { doNotThrow = false; }
|
|
59
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
60
|
+
var _this = this;
|
|
61
|
+
return __generator(this, function (_a) {
|
|
62
|
+
switch (_a.label) {
|
|
63
|
+
case 0:
|
|
64
|
+
if (typeof name === 'function') {
|
|
65
|
+
asyncCallback = name;
|
|
66
|
+
name = '__default';
|
|
67
|
+
}
|
|
68
|
+
if (!(0, isset_1.isset)(queue[name]))
|
|
69
|
+
queue[name] = [];
|
|
70
|
+
if (!(0, isset_1.isset)(transactionRunning[name]))
|
|
71
|
+
transactionRunning[name] = false;
|
|
72
|
+
return [4 /*yield*/, new Promise(function (resolve, reject) {
|
|
73
|
+
if (doNotThrow)
|
|
74
|
+
reject = logger_utils_1.C.error;
|
|
75
|
+
queue[name].push(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
76
|
+
var res, err_1;
|
|
77
|
+
return __generator(this, function (_a) {
|
|
78
|
+
switch (_a.label) {
|
|
79
|
+
case 0:
|
|
80
|
+
_a.trys.push([0, 2, , 3]);
|
|
81
|
+
setTimeout(function () {
|
|
82
|
+
logger_utils_1.C.warning('Transaction Timout'); // in case not catched
|
|
83
|
+
reject(new Error('transactionTimeout'));
|
|
84
|
+
}, timeout);
|
|
85
|
+
return [4 /*yield*/, asyncCallback()];
|
|
86
|
+
case 1:
|
|
87
|
+
res = _a.sent();
|
|
88
|
+
resolve(res);
|
|
89
|
+
return [3 /*break*/, 3];
|
|
90
|
+
case 2:
|
|
91
|
+
err_1 = _a.sent();
|
|
92
|
+
reject(err_1);
|
|
93
|
+
return [3 /*break*/, 3];
|
|
94
|
+
case 3: return [2 /*return*/];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}); });
|
|
98
|
+
removeItemFromQueue(name);
|
|
99
|
+
})];
|
|
100
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
exports.transaction = transaction;
|
|
106
|
+
function removeItemFromQueue(name) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
108
|
+
return __generator(this, function (_a) {
|
|
109
|
+
switch (_a.label) {
|
|
110
|
+
case 0:
|
|
111
|
+
if (transactionRunning[name] === true)
|
|
112
|
+
return [2 /*return*/]; // v
|
|
113
|
+
transactionRunning[name] = true; // A A /\
|
|
114
|
+
_a.label = 1;
|
|
115
|
+
case 1:
|
|
116
|
+
if (!queue[name].length) return [3 /*break*/, 3];
|
|
117
|
+
return [4 /*yield*/, queue[name].shift()()]; // II
|
|
118
|
+
case 2:
|
|
119
|
+
_a.sent(); // II
|
|
120
|
+
return [3 /*break*/, 1];
|
|
121
|
+
case 3:
|
|
122
|
+
transactionRunning[name] = false; // \==/_______II
|
|
123
|
+
return [2 /*return*/];
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
} // l v_v_v _I
|
|
128
|
+
exports.removeItemFromQueue = removeItemFromQueue;
|
|
129
|
+
// 11 11
|
|
130
|
+
/** Wait for a transaction to complete without creating a new transaction
|
|
131
|
+
*
|
|
132
|
+
*/
|
|
133
|
+
function waitForTransaction(transactionName, forceReleaseInSeconds) {
|
|
134
|
+
if (forceReleaseInSeconds === void 0) { forceReleaseInSeconds = 30; }
|
|
135
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
136
|
+
var brk;
|
|
137
|
+
return __generator(this, function (_a) {
|
|
138
|
+
switch (_a.label) {
|
|
139
|
+
case 0:
|
|
140
|
+
brk = false;
|
|
141
|
+
setTimeout(function () { return brk = true; }, forceReleaseInSeconds * 1000);
|
|
142
|
+
_a.label = 1;
|
|
143
|
+
case 1:
|
|
144
|
+
if (!((0, isset_1.isset)(transactionRunning[transactionName]) && transactionRunning[transactionName] === true)) return [3 /*break*/, 3];
|
|
145
|
+
if (brk)
|
|
146
|
+
return [3 /*break*/, 3];
|
|
147
|
+
return [4 /*yield*/, (0, timer_utils_1.timeout)(15)];
|
|
148
|
+
case 2:
|
|
149
|
+
_a.sent();
|
|
150
|
+
return [3 /*break*/, 1];
|
|
151
|
+
case 3: return [2 /*return*/];
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
exports.waitForTransaction = waitForTransaction;
|
|
157
|
+
//# sourceMappingURL=transaction-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transaction-utils.js","sourceRoot":"","sources":["../../src/transaction-utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAA0C;AAC1C,cAAc;AACd,0CAA0C;AAC1C,iCAA+B;AAC/B,+CAAkC;AAClC,6CAAuC;AAEvC,IAAM,kBAAkB,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,CAAA;AAC/C,IAAM,KAAK,GAA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I,kBAAkB,CAAC,IAAI,CAAC,KAAK,IAAI;wBAAE,sBAAM,CAAC,WAAW;oBACzD,kBAAkB,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA,CAAC,8BAA8B;;;yBACvD,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM;oBAAE,qBAAM,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,EAAA,CAAC,OAAO;;oBAAnC,SAA2B,CAAA,CAAC,OAAO;;;oBAC9D,kBAAkB,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA,CAAC,6BAA6B;;;;;CACjE,CAAC,iEAAiE;AALnE,kDAKC;AACD,iEAAiE;AAEjE;;GAEG;AACH,SAAsB,kBAAkB,CAAC,eAAe,EAAE,qBAA0B;IAA1B,sCAAA,EAAA,0BAA0B;;;;;;oBAC5E,GAAG,GAAG,KAAK,CAAA;oBACf,UAAU,CAAC,cAAM,OAAA,GAAG,GAAG,IAAI,EAAV,CAAU,EAAE,qBAAqB,GAAG,IAAI,CAAC,CAAA;;;yBACnD,CAAA,IAAA,aAAK,EAAC,kBAAkB,CAAC,eAAe,CAAC,CAAC,IAAI,kBAAkB,CAAC,eAAe,CAAC,KAAK,IAAI,CAAA;oBAC7F,IAAI,GAAG;wBAAE,wBAAK;oBACd,qBAAM,IAAA,qBAAO,EAAC,EAAE,CAAC,EAAA;;oBAAjB,SAAiB,CAAA;;;;;;CAExB;AAPD,gDAOC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export declare type BaseTypes = 'objectId' | 'dateInt6' | 'dateInt' | 'dateInt8' | 'dateInt12' | 'time' | 'humanReadableTimestamp' | 'date' | 'dateObject' | 'array' | 'object' | 'buffer' | 'string' | 'function' | 'boolean' | 'number' | 'bigint' | 'year' | 'email' | 'any';
|
|
2
|
+
export declare function issetOr(...elms: any[]): boolean;
|
|
3
|
+
export declare function isEmptyOrNotSet(...elms: any[]): boolean;
|
|
4
|
+
export declare function isDateObject(variable: any): boolean;
|
|
5
|
+
/** Check all values are set */
|
|
6
|
+
export declare function checkAllObjectValuesAreEmpty(o: any): boolean;
|
|
7
|
+
/** Throw an error in case data passed is not a valid ctx */
|
|
8
|
+
export declare function checkCtxIntegrity(ctx: any): void;
|
|
9
|
+
/**
|
|
10
|
+
## VALIDATOR
|
|
11
|
+
|
|
12
|
+
@name validator
|
|
13
|
+
|
|
14
|
+
@description support multiple names, multiple values and multiple type check
|
|
15
|
+
@option if nameString ends by $ sign it is considered optional
|
|
16
|
+
|
|
17
|
+
@function validator([Objects])
|
|
18
|
+
@return {error|true/false|testMode} depend on mode (see prop mode)
|
|
19
|
+
@param {} mode normal (default) | test (TODO) | boolean
|
|
20
|
+
@param {} name 'myName' || [{myVar1: 'blah, myvar2: myvar2}], support multiple names / values
|
|
21
|
+
@param {} value myVar,
|
|
22
|
+
@param {string} myVar myVar, instead of name / value
|
|
23
|
+
@param {array} in ['blah', 'otherPossibleValue', true], equal ONE OF THESE values
|
|
24
|
+
@param {any} eq exactly equal to in, both support string or array of values
|
|
25
|
+
@param {any} neq not in, both support string or array of values
|
|
26
|
+
@param {number} lte 3, less than or equal
|
|
27
|
+
@param {number} gte 1, greater or equal
|
|
28
|
+
@param {number} lt 3, less than
|
|
29
|
+
@param {number} gt 1, greater
|
|
30
|
+
@param {string|string[]} type
|
|
31
|
+
* possibleTypes: object, number, string, boolean, array, date, dateInt8, dateInt12, dateInt6, time, objectId (mongo), humanReadableTimestamp, buffer
|
|
32
|
+
* Notes: multiples value is an OR, /!\ Array is type 'array' and not 'object' like in real JS /!\
|
|
33
|
+
@param {regExp} regexp /regexp/, test against regexp
|
|
34
|
+
@param {number} minLength for string, array or number length
|
|
35
|
+
@param {number} maxLength
|
|
36
|
+
@param {number} length
|
|
37
|
+
@param {boolean} optional default false
|
|
38
|
+
@param {boolean} emptyAllowed default false (to use if must be set but can be empty)
|
|
39
|
+
@param {boolean} mustNotBeSet this one must not be set
|
|
40
|
+
@param {any} includes check if array or string includes value (like js .includes())
|
|
41
|
+
|
|
42
|
+
@example
|
|
43
|
+
validator(
|
|
44
|
+
{ myNumber : 3, type: 'number', gte: 1, lte: 3 }, // use the name directly as a param
|
|
45
|
+
{ name: 'email', value: 'nameATsite.com', regexp: /[^\sAT]+AT[^\sAT]+\.[^\sAT]/},
|
|
46
|
+
{ name: [{'blahVar': blahVarValue, 'myOtherVar': myOtherVarValue}], type: 'string'} // multiple names for same check
|
|
47
|
+
)
|
|
48
|
+
----------------------------------------*/
|
|
49
|
+
export declare type ValidatorObject = {
|
|
50
|
+
name?: string;
|
|
51
|
+
value?: any;
|
|
52
|
+
type?: BaseTypes;
|
|
53
|
+
eq?: any;
|
|
54
|
+
neq?: any;
|
|
55
|
+
in?: any[];
|
|
56
|
+
lt?: number;
|
|
57
|
+
gt?: number;
|
|
58
|
+
lte?: number;
|
|
59
|
+
gte?: number;
|
|
60
|
+
length?: number;
|
|
61
|
+
minLength?: number;
|
|
62
|
+
maxLength?: number;
|
|
63
|
+
emptyAllowed?: boolean;
|
|
64
|
+
regexp?: RegExp;
|
|
65
|
+
mustNotBeSet?: boolean;
|
|
66
|
+
isset?: boolean;
|
|
67
|
+
optional?: boolean;
|
|
68
|
+
isArray?: boolean;
|
|
69
|
+
[k: string]: any;
|
|
70
|
+
};
|
|
71
|
+
export declare function validator(...paramsToValidate: ValidatorObject[]): void;
|
|
72
|
+
/** Same as validator but return a boolean
|
|
73
|
+
* See {@link validator}
|
|
74
|
+
*/
|
|
75
|
+
export declare function isValid(...paramsToValidate: any[]): boolean;
|
|
76
|
+
/** Default types + custom types
|
|
77
|
+
* 'objectId','dateInt6','dateInt','dateInt8','dateInt12','time','humanReadableTimestamp','date','array','object','buffer','string','function','boolean','number','bigint',
|
|
78
|
+
*/
|
|
79
|
+
export declare function isType(value: any, type: BaseTypes): boolean;
|
|
80
|
+
export declare function validatorReturnErrArray(...paramsToValidate: ValidatorObject[]): [string?, object?];
|