@kwiz/common 1.0.78 → 1.0.80
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/.github/workflows/npm-publish.yml +24 -0
- package/.madgerc +2 -2
- package/LICENSE +21 -21
- package/fix-folder-imports.js +26 -26
- package/lib/cjs/helpers/sharepoint.js +5 -1
- package/lib/cjs/helpers/sharepoint.js.map +1 -1
- package/lib/cjs/helpers/typecheckers.js +5 -1
- package/lib/cjs/helpers/typecheckers.js.map +1 -1
- package/lib/cjs/types/libs/msal.types.js +26 -26
- package/lib/cjs/utils/sharepoint.rest/list.js +1 -1
- package/lib/cjs/utils/sharepoint.rest/list.js.map +1 -1
- package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
- package/lib/esm/helpers/sharepoint.js +3 -0
- package/lib/esm/helpers/sharepoint.js.map +1 -1
- package/lib/esm/helpers/typecheckers.js +3 -0
- package/lib/esm/helpers/typecheckers.js.map +1 -1
- package/lib/esm/types/libs/msal.types.js +26 -26
- package/lib/esm/utils/sharepoint.rest/list.js +2 -2
- package/lib/esm/utils/sharepoint.rest/list.js.map +1 -1
- package/lib/esm/utils/sharepoint.rest/user.js +11 -11
- package/lib/types/helpers/sharepoint.d.ts +1 -0
- package/lib/types/helpers/typecheckers.d.ts +1 -0
- package/package.json +77 -77
- package/readme.md +17 -17
- package/src/_dependencies.ts +12 -12
- package/src/config.ts +17 -17
- package/src/helpers/Guid.ts +181 -181
- package/src/helpers/base64.ts +173 -173
- package/src/helpers/browser.test.js +13 -13
- package/src/helpers/browser.ts +1348 -1348
- package/src/helpers/browserinfo.ts +292 -292
- package/src/helpers/collections.base.test.js +25 -25
- package/src/helpers/collections.base.ts +437 -437
- package/src/helpers/collections.ts +107 -107
- package/src/helpers/color.ts +54 -54
- package/src/helpers/cookies.ts +59 -59
- package/src/helpers/date.test.js +119 -119
- package/src/helpers/date.ts +188 -188
- package/src/helpers/debug.ts +186 -186
- package/src/helpers/emails.ts +6 -6
- package/src/helpers/eval.ts +5 -5
- package/src/helpers/file.test.js +50 -50
- package/src/helpers/file.ts +58 -58
- package/src/helpers/flatted.ts +149 -149
- package/src/helpers/functions.ts +16 -16
- package/src/helpers/graph/calendar.types.ts +10 -10
- package/src/helpers/http.ts +69 -69
- package/src/helpers/images.ts +22 -22
- package/src/helpers/json.ts +38 -38
- package/src/helpers/md5.ts +189 -189
- package/src/helpers/objects.test.js +33 -33
- package/src/helpers/objects.ts +270 -270
- package/src/helpers/promises.test.js +37 -37
- package/src/helpers/promises.ts +165 -165
- package/src/helpers/random.ts +27 -27
- package/src/helpers/scheduler/scheduler.test.js +103 -103
- package/src/helpers/scheduler/scheduler.ts +131 -131
- package/src/helpers/sharepoint.ts +776 -772
- package/src/helpers/strings.test.js +101 -101
- package/src/helpers/strings.ts +317 -317
- package/src/helpers/typecheckers.test.js +34 -34
- package/src/helpers/typecheckers.ts +266 -262
- package/src/helpers/url.test.js +43 -43
- package/src/helpers/url.ts +207 -207
- package/src/helpers/urlhelper.ts +111 -111
- package/src/index.ts +6 -6
- package/src/types/auth.ts +54 -54
- package/src/types/common.types.ts +15 -15
- package/src/types/flatted.types.ts +59 -59
- package/src/types/globals.types.ts +6 -6
- package/src/types/graph/calendar.types.ts +80 -80
- package/src/types/knownscript.types.ts +18 -18
- package/src/types/libs/datajs.types.ts +28 -28
- package/src/types/libs/ics.types.ts +30 -30
- package/src/types/libs/msal.types.ts +49 -49
- package/src/types/locales.ts +124 -124
- package/src/types/localstoragecache.types.ts +8 -8
- package/src/types/location.types.ts +27 -27
- package/src/types/moment.ts +11 -11
- package/src/types/regex.types.ts +16 -16
- package/src/types/rest.types.ts +95 -95
- package/src/types/sharepoint.types.ts +1465 -1465
- package/src/types/sharepoint.utils.types.ts +287 -287
- package/src/utils/auth/common.ts +74 -74
- package/src/utils/auth/discovery.test.js +12 -12
- package/src/utils/auth/discovery.ts +132 -132
- package/src/utils/base64.ts +27 -27
- package/src/utils/consolelogger.ts +320 -320
- package/src/utils/date.ts +35 -35
- package/src/utils/emails.ts +24 -24
- package/src/utils/knownscript.ts +286 -286
- package/src/utils/localstoragecache.ts +441 -441
- package/src/utils/rest.ts +501 -501
- package/src/utils/script.ts +170 -170
- package/src/utils/sharepoint.rest/common.ts +154 -154
- package/src/utils/sharepoint.rest/date.ts +62 -62
- package/src/utils/sharepoint.rest/file.folder.ts +598 -598
- package/src/utils/sharepoint.rest/item.ts +547 -547
- package/src/utils/sharepoint.rest/list.ts +1388 -1388
- package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
- package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
- package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
- package/src/utils/sharepoint.rest/location.ts +141 -141
- package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
- package/src/utils/sharepoint.rest/user-search.ts +252 -252
- package/src/utils/sharepoint.rest/user.ts +491 -491
- package/src/utils/sharepoint.rest/web.ts +1384 -1384
- package/src/utils/sod.ts +194 -194
- package/lib/cjs/helpers/_dependencies.js +0 -21
- package/lib/cjs/helpers/_dependencies.js.map +0 -1
- package/lib/cjs/utils/_dependencies.js +0 -24
- package/lib/cjs/utils/_dependencies.js.map +0 -1
- package/lib/esm/helpers/_dependencies.js +0 -3
- package/lib/esm/helpers/_dependencies.js.map +0 -1
- package/lib/esm/utils/_dependencies.js +0 -4
- package/lib/esm/utils/_dependencies.js.map +0 -1
- package/lib/types/helpers/_dependencies.d.ts +0 -2
- package/lib/types/utils/_dependencies.d.ts +0 -3
package/src/helpers/promises.ts
CHANGED
|
@@ -1,166 +1,166 @@
|
|
|
1
|
-
import { IDictionary } from "../types/common.types";
|
|
2
|
-
import { getGlobal, hasOwnProperty } from "./objects";
|
|
3
|
-
import { isFunction, isNullOrUndefined, isNumber } from "./typecheckers";
|
|
4
|
-
|
|
5
|
-
function _getGlobal() {
|
|
6
|
-
let _global = getGlobal<{ promises: IDictionary<Promise<any>> }>("helpers_promises",
|
|
7
|
-
{
|
|
8
|
-
promises: {}
|
|
9
|
-
});
|
|
10
|
-
return _global;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Lock all concurrent calls to a resource to one promise for a set duration of time.
|
|
15
|
-
* @param {string} key - Unique key to identify the promise.
|
|
16
|
-
* @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
|
|
17
|
-
* @param {number} duration - Duration to hold on to the promise result. (default=1)
|
|
18
|
-
* @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
|
|
19
|
-
* @example
|
|
20
|
-
* // returns Promise<string>
|
|
21
|
-
* var initTests = await promiseLock<string>("initTests", async () => { ... }, 2);
|
|
22
|
-
*/
|
|
23
|
-
export async function promiseLock<T>(key: string, promiseFunc: () => Promise<T>, duration = 1): Promise<T> {
|
|
24
|
-
return promiseOnce(key, promiseFunc).then((result) => {
|
|
25
|
-
(globalThis || window).setTimeout(() => {
|
|
26
|
-
_deletePromiseByKey(key);
|
|
27
|
-
}, isNumber(duration) && duration >= 1 ? duration : 1);
|
|
28
|
-
return result;
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Ensures that a promise runs only once
|
|
33
|
-
* @param {string} key - Unique key to identify the promise.
|
|
34
|
-
* @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
|
|
35
|
-
* @param {(result: T) => Promise<boolean>} isValidResult - Optional function that returns boolean to indicate if the result returned
|
|
36
|
-
* by the promise is valid and should be kepy in memory.
|
|
37
|
-
* @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
|
|
38
|
-
* @example
|
|
39
|
-
* // returns Promise<string>
|
|
40
|
-
* var initTests = await promiseOnce<string>("initTests", async () => { ... });
|
|
41
|
-
*/
|
|
42
|
-
export async function promiseOnce<T>(key: string, promiseFunc: () => Promise<T>, isValidResult?: (result: T) => Promise<boolean>): Promise<T> {
|
|
43
|
-
let _global = _getGlobal();
|
|
44
|
-
let promises = _global.promises;
|
|
45
|
-
|
|
46
|
-
if (hasOwnProperty(promises, key) && isFunction(isValidResult)) {
|
|
47
|
-
//we have en existing pending promise...
|
|
48
|
-
let queuedResult: T = null;
|
|
49
|
-
try { queuedResult = await promises[key]; } catch (e) { }
|
|
50
|
-
if ((await isValidResult(queuedResult)) !== true) {
|
|
51
|
-
_deletePromiseByKey(key);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (!hasOwnProperty(promises, key)) {
|
|
56
|
-
promises[key] = promiseFunc();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return promises[key];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Runs all promises in sequential order.
|
|
64
|
-
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
65
|
-
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
66
|
-
* provided promise functions
|
|
67
|
-
*/
|
|
68
|
-
export function promiseAllSequential<T = any>(asyncFuncs: (() => Promise<T>)[]): Promise<T[]> {
|
|
69
|
-
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
70
|
-
return Promise.resolve([]);
|
|
71
|
-
}
|
|
72
|
-
return asyncFuncs.reduce((promiseChain, currentTaskcurrentTask) => (
|
|
73
|
-
promiseChain.then((result) => {
|
|
74
|
-
let taskResult = currentTaskcurrentTask();
|
|
75
|
-
if (isNullOrUndefined(taskResult) || !isFunction(taskResult.then))//culprit - found one that did not return a promise?! make one.
|
|
76
|
-
taskResult = Promise.resolve() as any as Promise<T>;
|
|
77
|
-
|
|
78
|
-
return taskResult.then(Array.prototype.concat.bind(result));
|
|
79
|
-
})
|
|
80
|
-
), Promise.resolve([]));
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Runs N promises in parallel.
|
|
85
|
-
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
86
|
-
* @param {number} [maxParallel] - Max number of promises to run in parallel (default=8).
|
|
87
|
-
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
88
|
-
* provided promise functions
|
|
89
|
-
*/
|
|
90
|
-
export function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParallel: number = 8): Promise<T[]> {
|
|
91
|
-
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
92
|
-
return Promise.resolve([]);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
let startChain = () => {
|
|
96
|
-
let chainData = [];
|
|
97
|
-
|
|
98
|
-
if (asyncFuncs.length) {
|
|
99
|
-
let next = (data: T) => {
|
|
100
|
-
chainData.push(data);
|
|
101
|
-
return asyncFuncs.length ? (asyncFuncs.shift())().then(next) : chainData;
|
|
102
|
-
};
|
|
103
|
-
return (asyncFuncs.shift())().then(next);
|
|
104
|
-
} else {
|
|
105
|
-
return Promise.resolve(chainData);
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
let chains = [];
|
|
110
|
-
for (let k = 0; k < maxParallel; k += 1) {
|
|
111
|
-
chains.push(startChain());
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
return Promise.all(chains).then(d => {
|
|
115
|
-
//flatten results
|
|
116
|
-
return d.reduce((acc, val) => acc.concat(val), []);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Provides an asnyc sleep function that allows you to delay async/wait calls.
|
|
122
|
-
* @param {number} [seconds] - Time to sleep in seconds.
|
|
123
|
-
*/
|
|
124
|
-
export function sleepAsync(seconds?: number): Promise<void> {
|
|
125
|
-
return new Promise(resolve => {
|
|
126
|
-
(globalThis || window).setTimeout(() => resolve(), seconds > 0 ? seconds * 1000 : 3000);
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Provides the ability to retry an async function n times with a optional delay between calls
|
|
132
|
-
* @param {(...args) => Promise<T>} fn - Function to retry,
|
|
133
|
-
* @param {number} numberOfRetries - Number of times to retry.
|
|
134
|
-
* @param {number} [seconds] - Delay between retries in seconds (default=1).
|
|
135
|
-
*/
|
|
136
|
-
export async function retryAsync<T>(fn: (...args) => Promise<T>, numberOfRetries: number, seconds = 1) {
|
|
137
|
-
let error: Error = null;
|
|
138
|
-
|
|
139
|
-
for (let i = 0; i < numberOfRetries; i++) {
|
|
140
|
-
try {
|
|
141
|
-
error = null;
|
|
142
|
-
await sleepAsync(i === 0 ? 0 : seconds);
|
|
143
|
-
return await fn();
|
|
144
|
-
} catch (ex) {
|
|
145
|
-
error = ex;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (error) {
|
|
150
|
-
throw error;
|
|
151
|
-
}
|
|
152
|
-
throw new Error(`Failed retrying ${numberOfRetries} times`);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function _deletePromiseByKey(key: string) {
|
|
156
|
-
let _global = _getGlobal();
|
|
157
|
-
let promises = _global.promises;
|
|
158
|
-
if (hasOwnProperty(promises, key)) {
|
|
159
|
-
try {
|
|
160
|
-
delete promises[key];
|
|
161
|
-
return true;
|
|
162
|
-
} catch {
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
1
|
+
import { IDictionary } from "../types/common.types";
|
|
2
|
+
import { getGlobal, hasOwnProperty } from "./objects";
|
|
3
|
+
import { isFunction, isNullOrUndefined, isNumber } from "./typecheckers";
|
|
4
|
+
|
|
5
|
+
function _getGlobal() {
|
|
6
|
+
let _global = getGlobal<{ promises: IDictionary<Promise<any>> }>("helpers_promises",
|
|
7
|
+
{
|
|
8
|
+
promises: {}
|
|
9
|
+
});
|
|
10
|
+
return _global;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Lock all concurrent calls to a resource to one promise for a set duration of time.
|
|
15
|
+
* @param {string} key - Unique key to identify the promise.
|
|
16
|
+
* @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
|
|
17
|
+
* @param {number} duration - Duration to hold on to the promise result. (default=1)
|
|
18
|
+
* @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
|
|
19
|
+
* @example
|
|
20
|
+
* // returns Promise<string>
|
|
21
|
+
* var initTests = await promiseLock<string>("initTests", async () => { ... }, 2);
|
|
22
|
+
*/
|
|
23
|
+
export async function promiseLock<T>(key: string, promiseFunc: () => Promise<T>, duration = 1): Promise<T> {
|
|
24
|
+
return promiseOnce(key, promiseFunc).then((result) => {
|
|
25
|
+
(globalThis || window).setTimeout(() => {
|
|
26
|
+
_deletePromiseByKey(key);
|
|
27
|
+
}, isNumber(duration) && duration >= 1 ? duration : 1);
|
|
28
|
+
return result;
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Ensures that a promise runs only once
|
|
33
|
+
* @param {string} key - Unique key to identify the promise.
|
|
34
|
+
* @param {() => Promise<T>} promiseFunc - Function that will return the promise to be run only once.
|
|
35
|
+
* @param {(result: T) => Promise<boolean>} isValidResult - Optional function that returns boolean to indicate if the result returned
|
|
36
|
+
* by the promise is valid and should be kepy in memory.
|
|
37
|
+
* @returns {Promise<T>} Returns the single promise that will be fullfilled for all promises with the same key.
|
|
38
|
+
* @example
|
|
39
|
+
* // returns Promise<string>
|
|
40
|
+
* var initTests = await promiseOnce<string>("initTests", async () => { ... });
|
|
41
|
+
*/
|
|
42
|
+
export async function promiseOnce<T>(key: string, promiseFunc: () => Promise<T>, isValidResult?: (result: T) => Promise<boolean>): Promise<T> {
|
|
43
|
+
let _global = _getGlobal();
|
|
44
|
+
let promises = _global.promises;
|
|
45
|
+
|
|
46
|
+
if (hasOwnProperty(promises, key) && isFunction(isValidResult)) {
|
|
47
|
+
//we have en existing pending promise...
|
|
48
|
+
let queuedResult: T = null;
|
|
49
|
+
try { queuedResult = await promises[key]; } catch (e) { }
|
|
50
|
+
if ((await isValidResult(queuedResult)) !== true) {
|
|
51
|
+
_deletePromiseByKey(key);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!hasOwnProperty(promises, key)) {
|
|
56
|
+
promises[key] = promiseFunc();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return promises[key];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Runs all promises in sequential order.
|
|
64
|
+
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
65
|
+
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
66
|
+
* provided promise functions
|
|
67
|
+
*/
|
|
68
|
+
export function promiseAllSequential<T = any>(asyncFuncs: (() => Promise<T>)[]): Promise<T[]> {
|
|
69
|
+
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
70
|
+
return Promise.resolve([]);
|
|
71
|
+
}
|
|
72
|
+
return asyncFuncs.reduce((promiseChain, currentTaskcurrentTask) => (
|
|
73
|
+
promiseChain.then((result) => {
|
|
74
|
+
let taskResult = currentTaskcurrentTask();
|
|
75
|
+
if (isNullOrUndefined(taskResult) || !isFunction(taskResult.then))//culprit - found one that did not return a promise?! make one.
|
|
76
|
+
taskResult = Promise.resolve() as any as Promise<T>;
|
|
77
|
+
|
|
78
|
+
return taskResult.then(Array.prototype.concat.bind(result));
|
|
79
|
+
})
|
|
80
|
+
), Promise.resolve([]));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Runs N promises in parallel.
|
|
85
|
+
* @param {(() => Promise<T>)[]} asyncFuncs - Array of functions that return the promises to fullfill.
|
|
86
|
+
* @param {number} [maxParallel] - Max number of promises to run in parallel (default=8).
|
|
87
|
+
* @returns {Promise<T[]>} Returns a single promise with a merged array of results that are in the same order as the
|
|
88
|
+
* provided promise functions
|
|
89
|
+
*/
|
|
90
|
+
export function promiseNParallel<T>(asyncFuncs: (() => Promise<T>)[], maxParallel: number = 8): Promise<T[]> {
|
|
91
|
+
if (!Array.isArray(asyncFuncs) || !asyncFuncs.length) {
|
|
92
|
+
return Promise.resolve([]);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let startChain = () => {
|
|
96
|
+
let chainData = [];
|
|
97
|
+
|
|
98
|
+
if (asyncFuncs.length) {
|
|
99
|
+
let next = (data: T) => {
|
|
100
|
+
chainData.push(data);
|
|
101
|
+
return asyncFuncs.length ? (asyncFuncs.shift())().then(next) : chainData;
|
|
102
|
+
};
|
|
103
|
+
return (asyncFuncs.shift())().then(next);
|
|
104
|
+
} else {
|
|
105
|
+
return Promise.resolve(chainData);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
let chains = [];
|
|
110
|
+
for (let k = 0; k < maxParallel; k += 1) {
|
|
111
|
+
chains.push(startChain());
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return Promise.all(chains).then(d => {
|
|
115
|
+
//flatten results
|
|
116
|
+
return d.reduce((acc, val) => acc.concat(val), []);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Provides an asnyc sleep function that allows you to delay async/wait calls.
|
|
122
|
+
* @param {number} [seconds] - Time to sleep in seconds.
|
|
123
|
+
*/
|
|
124
|
+
export function sleepAsync(seconds?: number): Promise<void> {
|
|
125
|
+
return new Promise(resolve => {
|
|
126
|
+
(globalThis || window).setTimeout(() => resolve(), seconds > 0 ? seconds * 1000 : 3000);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Provides the ability to retry an async function n times with a optional delay between calls
|
|
132
|
+
* @param {(...args) => Promise<T>} fn - Function to retry,
|
|
133
|
+
* @param {number} numberOfRetries - Number of times to retry.
|
|
134
|
+
* @param {number} [seconds] - Delay between retries in seconds (default=1).
|
|
135
|
+
*/
|
|
136
|
+
export async function retryAsync<T>(fn: (...args) => Promise<T>, numberOfRetries: number, seconds = 1) {
|
|
137
|
+
let error: Error = null;
|
|
138
|
+
|
|
139
|
+
for (let i = 0; i < numberOfRetries; i++) {
|
|
140
|
+
try {
|
|
141
|
+
error = null;
|
|
142
|
+
await sleepAsync(i === 0 ? 0 : seconds);
|
|
143
|
+
return await fn();
|
|
144
|
+
} catch (ex) {
|
|
145
|
+
error = ex;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (error) {
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
throw new Error(`Failed retrying ${numberOfRetries} times`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function _deletePromiseByKey(key: string) {
|
|
156
|
+
let _global = _getGlobal();
|
|
157
|
+
let promises = _global.promises;
|
|
158
|
+
if (hasOwnProperty(promises, key)) {
|
|
159
|
+
try {
|
|
160
|
+
delete promises[key];
|
|
161
|
+
return true;
|
|
162
|
+
} catch {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
166
|
}
|
package/src/helpers/random.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
export function getRandomId() {
|
|
2
|
-
return Date.now().toString(36) + Math.random().toString(36).substring(2);
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export function getUniqueId() {
|
|
6
|
-
var _s_hexcode = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
|
|
7
|
-
for (var a = "", c = 0; c < 32; c++) {
|
|
8
|
-
var b = Math.floor(Math.random() * 16);
|
|
9
|
-
switch (c) {
|
|
10
|
-
case 8:
|
|
11
|
-
a += "-";
|
|
12
|
-
break;
|
|
13
|
-
case 12:
|
|
14
|
-
b = 4;
|
|
15
|
-
a += "-";
|
|
16
|
-
break;
|
|
17
|
-
case 16:
|
|
18
|
-
b = b & 3 | 8;
|
|
19
|
-
a += "-";
|
|
20
|
-
break;
|
|
21
|
-
case 20:
|
|
22
|
-
a += "-";
|
|
23
|
-
}
|
|
24
|
-
a += _s_hexcode[b];
|
|
25
|
-
}
|
|
26
|
-
return a;
|
|
27
|
-
}
|
|
1
|
+
export function getRandomId() {
|
|
2
|
+
return Date.now().toString(36) + Math.random().toString(36).substring(2);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function getUniqueId() {
|
|
6
|
+
var _s_hexcode = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
|
|
7
|
+
for (var a = "", c = 0; c < 32; c++) {
|
|
8
|
+
var b = Math.floor(Math.random() * 16);
|
|
9
|
+
switch (c) {
|
|
10
|
+
case 8:
|
|
11
|
+
a += "-";
|
|
12
|
+
break;
|
|
13
|
+
case 12:
|
|
14
|
+
b = 4;
|
|
15
|
+
a += "-";
|
|
16
|
+
break;
|
|
17
|
+
case 16:
|
|
18
|
+
b = b & 3 | 8;
|
|
19
|
+
a += "-";
|
|
20
|
+
break;
|
|
21
|
+
case 20:
|
|
22
|
+
a += "-";
|
|
23
|
+
}
|
|
24
|
+
a += _s_hexcode[b];
|
|
25
|
+
}
|
|
26
|
+
return a;
|
|
27
|
+
}
|
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
import assert from 'assert/strict';
|
|
2
|
-
import test from 'node:test';
|
|
3
|
-
import { isValidSchedule, getNextUTC } from "./scheduler";
|
|
4
|
-
import { ScheduleTypes } from './scheduler';
|
|
5
|
-
|
|
6
|
-
var startDate = "2020-01-01T05:00Z";
|
|
7
|
-
var invalidOutput = "9999999999";
|
|
8
|
-
var schedules = [
|
|
9
|
-
{ schedule: {}, output: invalidOutput, valid: false },
|
|
10
|
-
{ schedule: { ScheduleType: "x" }, output: invalidOutput, valid: false },
|
|
11
|
-
{
|
|
12
|
-
schedule: {
|
|
13
|
-
ScheduleType: ScheduleTypes.hourly,
|
|
14
|
-
interval: 24
|
|
15
|
-
}, output: invalidOutput, valid: false
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
name: "move 2 hours",
|
|
19
|
-
schedule: {
|
|
20
|
-
ScheduleType: ScheduleTypes.hourly,
|
|
21
|
-
interval: 2
|
|
22
|
-
}, output: "2020010107", valid: true
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
schedule: {
|
|
26
|
-
ScheduleType: ScheduleTypes.daily,
|
|
27
|
-
hours: []
|
|
28
|
-
}, output: invalidOutput, valid: false
|
|
29
|
-
},
|
|
30
|
-
{
|
|
31
|
-
schedule: {
|
|
32
|
-
ScheduleType: ScheduleTypes.daily,
|
|
33
|
-
hours: [5, 2, 9, 26]
|
|
34
|
-
}, output: invalidOutput, valid: false
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
name: "move to next hour in same day",
|
|
38
|
-
schedule: {
|
|
39
|
-
ScheduleType: ScheduleTypes.daily,
|
|
40
|
-
hours: [5, 2, 9, 22]
|
|
41
|
-
}, output: "2020010109", valid: true
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
name: "passed last hour, move to next day",
|
|
45
|
-
schedule: {
|
|
46
|
-
ScheduleType: ScheduleTypes.daily,
|
|
47
|
-
hours: [3, 1, 5, 2]
|
|
48
|
-
}, output: "2020010201", valid: true
|
|
49
|
-
},
|
|
50
|
-
{
|
|
51
|
-
schedule: {
|
|
52
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
53
|
-
hours: []
|
|
54
|
-
}, output: invalidOutput, valid: false
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
schedule: {
|
|
58
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
59
|
-
hours: [5, 2, 9, 26],
|
|
60
|
-
days: [2],
|
|
61
|
-
}, output: invalidOutput, valid: false
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
schedule: {
|
|
65
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
66
|
-
hours: [5, 2, 9, 22],
|
|
67
|
-
days: [5, 2, 7]
|
|
68
|
-
}, output: invalidOutput, valid: false
|
|
69
|
-
},
|
|
70
|
-
{
|
|
71
|
-
name: "day is no ok, move to next day first hour",
|
|
72
|
-
schedule: {
|
|
73
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
74
|
-
hours: [5, 2, 9, 22],
|
|
75
|
-
days: [5, 2, 4]
|
|
76
|
-
}, output: "2020010202", valid: true
|
|
77
|
-
},
|
|
78
|
-
{
|
|
79
|
-
name: "day is not ok, move to first day in next week",
|
|
80
|
-
schedule: {
|
|
81
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
82
|
-
hours: [5, 2, 9, 22],
|
|
83
|
-
days: [2, 0, 1]
|
|
84
|
-
}, output: "2020010502", valid: true
|
|
85
|
-
},
|
|
86
|
-
{
|
|
87
|
-
name: "day is ok, move to next hour",
|
|
88
|
-
schedule: {
|
|
89
|
-
ScheduleType: ScheduleTypes.weekly,
|
|
90
|
-
hours: [5, 2, 9, 22],
|
|
91
|
-
days: [5, 2, 3, 4]
|
|
92
|
-
}, output: "2020010109", valid: true
|
|
93
|
-
}
|
|
94
|
-
];
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
test('isValidSchedule', t => {
|
|
98
|
-
schedules.forEach(s => assert.strictEqual(isValidSchedule(s.schedule), s.valid));
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
test('getNextUTC', async t => {
|
|
102
|
-
let date = new Date(startDate);
|
|
103
|
-
schedules.forEach(s => assert.strictEqual(getNextUTC(date, s.schedule), s.output));
|
|
1
|
+
import assert from 'assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import { isValidSchedule, getNextUTC } from "./scheduler";
|
|
4
|
+
import { ScheduleTypes } from './scheduler';
|
|
5
|
+
|
|
6
|
+
var startDate = "2020-01-01T05:00Z";
|
|
7
|
+
var invalidOutput = "9999999999";
|
|
8
|
+
var schedules = [
|
|
9
|
+
{ schedule: {}, output: invalidOutput, valid: false },
|
|
10
|
+
{ schedule: { ScheduleType: "x" }, output: invalidOutput, valid: false },
|
|
11
|
+
{
|
|
12
|
+
schedule: {
|
|
13
|
+
ScheduleType: ScheduleTypes.hourly,
|
|
14
|
+
interval: 24
|
|
15
|
+
}, output: invalidOutput, valid: false
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
name: "move 2 hours",
|
|
19
|
+
schedule: {
|
|
20
|
+
ScheduleType: ScheduleTypes.hourly,
|
|
21
|
+
interval: 2
|
|
22
|
+
}, output: "2020010107", valid: true
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
schedule: {
|
|
26
|
+
ScheduleType: ScheduleTypes.daily,
|
|
27
|
+
hours: []
|
|
28
|
+
}, output: invalidOutput, valid: false
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
schedule: {
|
|
32
|
+
ScheduleType: ScheduleTypes.daily,
|
|
33
|
+
hours: [5, 2, 9, 26]
|
|
34
|
+
}, output: invalidOutput, valid: false
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "move to next hour in same day",
|
|
38
|
+
schedule: {
|
|
39
|
+
ScheduleType: ScheduleTypes.daily,
|
|
40
|
+
hours: [5, 2, 9, 22]
|
|
41
|
+
}, output: "2020010109", valid: true
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
name: "passed last hour, move to next day",
|
|
45
|
+
schedule: {
|
|
46
|
+
ScheduleType: ScheduleTypes.daily,
|
|
47
|
+
hours: [3, 1, 5, 2]
|
|
48
|
+
}, output: "2020010201", valid: true
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
schedule: {
|
|
52
|
+
ScheduleType: ScheduleTypes.weekly,
|
|
53
|
+
hours: []
|
|
54
|
+
}, output: invalidOutput, valid: false
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
schedule: {
|
|
58
|
+
ScheduleType: ScheduleTypes.weekly,
|
|
59
|
+
hours: [5, 2, 9, 26],
|
|
60
|
+
days: [2],
|
|
61
|
+
}, output: invalidOutput, valid: false
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
schedule: {
|
|
65
|
+
ScheduleType: ScheduleTypes.weekly,
|
|
66
|
+
hours: [5, 2, 9, 22],
|
|
67
|
+
days: [5, 2, 7]
|
|
68
|
+
}, output: invalidOutput, valid: false
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "day is no ok, move to next day first hour",
|
|
72
|
+
schedule: {
|
|
73
|
+
ScheduleType: ScheduleTypes.weekly,
|
|
74
|
+
hours: [5, 2, 9, 22],
|
|
75
|
+
days: [5, 2, 4]
|
|
76
|
+
}, output: "2020010202", valid: true
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: "day is not ok, move to first day in next week",
|
|
80
|
+
schedule: {
|
|
81
|
+
ScheduleType: ScheduleTypes.weekly,
|
|
82
|
+
hours: [5, 2, 9, 22],
|
|
83
|
+
days: [2, 0, 1]
|
|
84
|
+
}, output: "2020010502", valid: true
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
name: "day is ok, move to next hour",
|
|
88
|
+
schedule: {
|
|
89
|
+
ScheduleType: ScheduleTypes.weekly,
|
|
90
|
+
hours: [5, 2, 9, 22],
|
|
91
|
+
days: [5, 2, 3, 4]
|
|
92
|
+
}, output: "2020010109", valid: true
|
|
93
|
+
}
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
test('isValidSchedule', t => {
|
|
98
|
+
schedules.forEach(s => assert.strictEqual(isValidSchedule(s.schedule), s.valid));
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('getNextUTC', async t => {
|
|
102
|
+
let date = new Date(startDate);
|
|
103
|
+
schedules.forEach(s => assert.strictEqual(getNextUTC(date, s.schedule), s.output));
|
|
104
104
|
});
|