@keeex/utils 7.0.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/LICENSE +9 -0
- package/README.md +105 -0
- package/lib/array.d.ts +28 -0
- package/lib/array.js +36 -0
- package/lib/arraybuffer.d.ts +138 -0
- package/lib/arraybuffer.js +141 -0
- package/lib/async/asynctrigger.d.ts +50 -0
- package/lib/async/asynctrigger.js +108 -0
- package/lib/async/deferredpromise.d.ts +32 -0
- package/lib/async/deferredpromise.js +66 -0
- package/lib/async/keycache.d.ts +56 -0
- package/lib/async/keycache.js +103 -0
- package/lib/async/queues.d.ts +69 -0
- package/lib/async/queues.js +135 -0
- package/lib/async/timecache.d.ts +58 -0
- package/lib/async/timecache.js +118 -0
- package/lib/base58.d.ts +27 -0
- package/lib/base58.js +83 -0
- package/lib/base64.d.ts +51 -0
- package/lib/base64.js +126 -0
- package/lib/benchmark.d.ts +126 -0
- package/lib/benchmark.js +177 -0
- package/lib/bits/arraybuffer.d.ts +35 -0
- package/lib/bits/arraybuffer.js +64 -0
- package/lib/bits/base64.d.ts +35 -0
- package/lib/bits/base64.js +70 -0
- package/lib/bits/hex.d.ts +17 -0
- package/lib/bits/hex.js +30 -0
- package/lib/bits/uint8array.d.ts +28 -0
- package/lib/bits/uint8array.js +42 -0
- package/lib/bytebuffer.d.ts +27 -0
- package/lib/bytebuffer.js +29 -0
- package/lib/consts.d.ts +33 -0
- package/lib/consts.js +33 -0
- package/lib/cron/logger.d.ts +22 -0
- package/lib/cron/logger.js +31 -0
- package/lib/cron/scheduledtask.d.ts +71 -0
- package/lib/cron/scheduledtask.js +137 -0
- package/lib/cron/types.d.ts +53 -0
- package/lib/cron/types.js +31 -0
- package/lib/cron.d.ts +29 -0
- package/lib/cron.js +47 -0
- package/lib/dict.d.ts +56 -0
- package/lib/dict.js +74 -0
- package/lib/error.d.ts +25 -0
- package/lib/error.js +41 -0
- package/lib/global.d.ts +27 -0
- package/lib/global.js +53 -0
- package/lib/hex.d.ts +32 -0
- package/lib/hex.js +58 -0
- package/lib/idx.d.ts +51 -0
- package/lib/idx.js +81 -0
- package/lib/json.d.ts +57 -0
- package/lib/json.js +116 -0
- package/lib/marshalling/marshaller.d.ts +51 -0
- package/lib/marshalling/marshaller.js +155 -0
- package/lib/marshalling/unmarshaller.d.ts +53 -0
- package/lib/marshalling/unmarshaller.js +124 -0
- package/lib/marshalling/util.d.ts +25 -0
- package/lib/marshalling/util.js +25 -0
- package/lib/number.d.ts +17 -0
- package/lib/number.js +21 -0
- package/lib/path.d.ts +25 -0
- package/lib/path.js +29 -0
- package/lib/promise.d.ts +42 -0
- package/lib/promise.js +78 -0
- package/lib/starttime.d.ts +23 -0
- package/lib/starttime.js +29 -0
- package/lib/string.d.ts +65 -0
- package/lib/string.js +108 -0
- package/lib/types/array.d.ts +34 -0
- package/lib/types/array.js +64 -0
- package/lib/types/enum.d.ts +30 -0
- package/lib/types/enum.js +44 -0
- package/lib/types/predicateerror.d.ts +40 -0
- package/lib/types/predicateerror.js +107 -0
- package/lib/types/primitive.d.ts +23 -0
- package/lib/types/primitive.js +34 -0
- package/lib/types/record.d.ts +67 -0
- package/lib/types/record.js +235 -0
- package/lib/types/types.d.ts +64 -0
- package/lib/types/types.js +115 -0
- package/lib/types/utils.d.ts +18 -0
- package/lib/types/utils.js +67 -0
- package/lib/uint8array.d.ts +176 -0
- package/lib/uint8array.js +438 -0
- package/lib/units.d.ts +159 -0
- package/lib/units.js +290 -0
- package/lib/utils/buffer.d.ts +49 -0
- package/lib/utils/buffer.js +79 -0
- package/lib/utils/fourbytes.d.ts +29 -0
- package/lib/utils/fourbytes.js +45 -0
- package/package.json +1 -0
- package/web/array.d.ts +28 -0
- package/web/array.js +34 -0
- package/web/arraybuffer.d.ts +138 -0
- package/web/arraybuffer.js +141 -0
- package/web/async/asynctrigger.d.ts +50 -0
- package/web/async/asynctrigger.js +106 -0
- package/web/async/deferredpromise.d.ts +32 -0
- package/web/async/deferredpromise.js +65 -0
- package/web/async/keycache.d.ts +56 -0
- package/web/async/keycache.js +97 -0
- package/web/async/queues.d.ts +69 -0
- package/web/async/queues.js +131 -0
- package/web/async/timecache.d.ts +58 -0
- package/web/async/timecache.js +107 -0
- package/web/base58.d.ts +27 -0
- package/web/base58.js +78 -0
- package/web/base64.d.ts +51 -0
- package/web/base64.js +136 -0
- package/web/benchmark.d.ts +126 -0
- package/web/benchmark.js +183 -0
- package/web/bits/arraybuffer.d.ts +35 -0
- package/web/bits/arraybuffer.js +59 -0
- package/web/bits/base64.d.ts +35 -0
- package/web/bits/base64.js +67 -0
- package/web/bits/hex.d.ts +17 -0
- package/web/bits/hex.js +27 -0
- package/web/bits/uint8array.d.ts +28 -0
- package/web/bits/uint8array.js +41 -0
- package/web/bytebuffer.d.ts +27 -0
- package/web/bytebuffer.js +29 -0
- package/web/consts.d.ts +33 -0
- package/web/consts.js +33 -0
- package/web/cron/logger.d.ts +22 -0
- package/web/cron/logger.js +30 -0
- package/web/cron/scheduledtask.d.ts +71 -0
- package/web/cron/scheduledtask.js +136 -0
- package/web/cron/types.d.ts +53 -0
- package/web/cron/types.js +31 -0
- package/web/cron.d.ts +29 -0
- package/web/cron.js +47 -0
- package/web/dict.d.ts +56 -0
- package/web/dict.js +67 -0
- package/web/error.d.ts +25 -0
- package/web/error.js +39 -0
- package/web/global.d.ts +27 -0
- package/web/global.js +49 -0
- package/web/hex.d.ts +32 -0
- package/web/hex.js +52 -0
- package/web/idx.d.ts +51 -0
- package/web/idx.js +76 -0
- package/web/json.d.ts +57 -0
- package/web/json.js +98 -0
- package/web/marshalling/marshaller.d.ts +51 -0
- package/web/marshalling/marshaller.js +150 -0
- package/web/marshalling/unmarshaller.d.ts +53 -0
- package/web/marshalling/unmarshaller.js +115 -0
- package/web/marshalling/util.d.ts +25 -0
- package/web/marshalling/util.js +25 -0
- package/web/number.d.ts +17 -0
- package/web/number.js +21 -0
- package/web/path.d.ts +25 -0
- package/web/path.js +26 -0
- package/web/promise.d.ts +42 -0
- package/web/promise.js +74 -0
- package/web/starttime.d.ts +23 -0
- package/web/starttime.js +29 -0
- package/web/string.d.ts +65 -0
- package/web/string.js +101 -0
- package/web/types/array.d.ts +34 -0
- package/web/types/array.js +63 -0
- package/web/types/enum.d.ts +30 -0
- package/web/types/enum.js +40 -0
- package/web/types/predicateerror.d.ts +40 -0
- package/web/types/predicateerror.js +128 -0
- package/web/types/primitive.d.ts +23 -0
- package/web/types/primitive.js +33 -0
- package/web/types/record.d.ts +67 -0
- package/web/types/record.js +213 -0
- package/web/types/types.d.ts +64 -0
- package/web/types/types.js +123 -0
- package/web/types/utils.d.ts +18 -0
- package/web/types/utils.js +30 -0
- package/web/uint8array.d.ts +176 -0
- package/web/uint8array.js +412 -0
- package/web/units.d.ts +159 -0
- package/web/units.js +312 -0
- package/web/utils/buffer.d.ts +49 -0
- package/web/utils/buffer.js +76 -0
- package/web/utils/fourbytes.d.ts +29 -0
- package/web/utils/fourbytes.js +45 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/** @internal */
|
|
17
|
+
export declare const B64_CHARBLOCK = 4;
|
|
18
|
+
/** @internal */
|
|
19
|
+
export declare const B64_PAD2 = 2;
|
|
20
|
+
/** @internal */
|
|
21
|
+
export declare const B64_PAD1 = 1;
|
|
22
|
+
/** @internal */
|
|
23
|
+
export declare const B64_PAD0 = 0;
|
|
24
|
+
/**
|
|
25
|
+
* Return the canonical data from a b64 string
|
|
26
|
+
*
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
export declare const b64EffectiveData: (data: string, strict?: boolean) => string;
|
|
30
|
+
/**
|
|
31
|
+
* Return the length of the data in a b64 string.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export declare const b64Len: (data: string, strict: boolean) => number;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/** @internal */
|
|
17
|
+
export const B64_CHARBLOCK = 4;
|
|
18
|
+
const B64_PAD3 = 3;
|
|
19
|
+
/** @internal */
|
|
20
|
+
export const B64_PAD2 = 2;
|
|
21
|
+
/** @internal */
|
|
22
|
+
export const B64_PAD1 = 1;
|
|
23
|
+
/** @internal */
|
|
24
|
+
export const B64_PAD0 = 0;
|
|
25
|
+
/**
|
|
26
|
+
* Return the canonical data from a b64 string
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export const b64EffectiveData = (data, strict = false) => {
|
|
31
|
+
let effectiveData = data.indexOf("\n") === data.indexOf("\r") ? data : data.replace(/[\n\r]/gu, "");
|
|
32
|
+
if (effectiveData.length % B64_CHARBLOCK !== 0) {
|
|
33
|
+
if (strict)
|
|
34
|
+
throw new Error("Invalid input length");
|
|
35
|
+
const extraSize = effectiveData.length % B64_CHARBLOCK;
|
|
36
|
+
switch (extraSize) {
|
|
37
|
+
case B64_PAD1:
|
|
38
|
+
effectiveData = effectiveData.substring(0, Math.floor(effectiveData.length / B64_CHARBLOCK) * B64_CHARBLOCK);
|
|
39
|
+
break;
|
|
40
|
+
case B64_PAD2:
|
|
41
|
+
effectiveData = `${effectiveData}==`;
|
|
42
|
+
break;
|
|
43
|
+
case B64_PAD3:
|
|
44
|
+
effectiveData = `${effectiveData}=`;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return effectiveData;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Return the length of the data in a b64 string.
|
|
52
|
+
*
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
export const b64Len = (data, strict) => {
|
|
56
|
+
const effectiveData = b64EffectiveData(data, strict);
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
58
|
+
let padding;
|
|
59
|
+
if (effectiveData.endsWith("==")) {
|
|
60
|
+
padding = B64_PAD2;
|
|
61
|
+
}
|
|
62
|
+
else if (effectiveData.endsWith("=")) {
|
|
63
|
+
padding = B64_PAD1;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
padding = B64_PAD0;
|
|
67
|
+
}
|
|
68
|
+
const outputLength = (effectiveData.length / B64_CHARBLOCK) * (B64_CHARBLOCK - 1) - padding;
|
|
69
|
+
return outputLength;
|
|
70
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/** Clean an hex string input; make it even and remove 0x prefix if present */
|
|
17
|
+
export declare const clearHexInput: (input: string) => string;
|
package/lib/bits/hex.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
const CHARACTER_PER_BYTE = 2;
|
|
17
|
+
const HEX_PREFIX = "0x";
|
|
18
|
+
const HEX_PREFIX_LENGTH = HEX_PREFIX.length;
|
|
19
|
+
/** Clean an hex string input; make it even and remove 0x prefix if present */
|
|
20
|
+
export const clearHexInput = (input) => {
|
|
21
|
+
const noprefix = !input.startsWith(HEX_PREFIX);
|
|
22
|
+
const even = input.length % CHARACTER_PER_BYTE === 0;
|
|
23
|
+
if (noprefix && even)
|
|
24
|
+
return input;
|
|
25
|
+
if (noprefix)
|
|
26
|
+
return `0${input}`;
|
|
27
|
+
if (even)
|
|
28
|
+
return input.substring(HEX_PREFIX_LENGTH);
|
|
29
|
+
return `0${input.substring(HEX_PREFIX_LENGTH)}`;
|
|
30
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Return a Uint8Array view on the provided ArrayBuffer.
|
|
18
|
+
*
|
|
19
|
+
* If clone is set to true, return a clone of the source array. Otherwise a view is returned.
|
|
20
|
+
*
|
|
21
|
+
@public
|
|
22
|
+
*/
|
|
23
|
+
export declare const getUint8Array: (arrayBuffer: ArrayBuffer | Uint8Array, offset?: number, length?: number, clone?: boolean) => Uint8Array;
|
|
24
|
+
export declare const pureBuf82B64: (buf8: Uint8Array) => string;
|
|
25
|
+
/**
|
|
26
|
+
* Convert an Uint8Array to a B64 string
|
|
27
|
+
*/
|
|
28
|
+
export declare const buf82b64: (buf8: Uint8Array) => string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import { bufferBuf82B64, haveBufferBase64 } from "../utils/buffer.js";
|
|
17
|
+
import { getArrayBuffer, ab2b64 } from "./arraybuffer.js";
|
|
18
|
+
/**
|
|
19
|
+
* Return a Uint8Array view on the provided ArrayBuffer.
|
|
20
|
+
*
|
|
21
|
+
* If clone is set to true, return a clone of the source array. Otherwise a view is returned.
|
|
22
|
+
*
|
|
23
|
+
@public
|
|
24
|
+
*/
|
|
25
|
+
export const getUint8Array = (arrayBuffer, offset = 0, length = -1, clone = false) => {
|
|
26
|
+
const realLength = length === -1 ? arrayBuffer.byteLength - offset : length;
|
|
27
|
+
if (arrayBuffer instanceof ArrayBuffer) {
|
|
28
|
+
if (clone) {
|
|
29
|
+
const copyBuffer = arrayBuffer.slice(offset, offset + realLength);
|
|
30
|
+
return new Uint8Array(copyBuffer);
|
|
31
|
+
}
|
|
32
|
+
return new Uint8Array(arrayBuffer, offset, realLength);
|
|
33
|
+
}
|
|
34
|
+
if (clone)
|
|
35
|
+
return arrayBuffer.slice(offset, realLength + offset);
|
|
36
|
+
return new Uint8Array(arrayBuffer.buffer, offset + arrayBuffer.byteOffset, realLength);
|
|
37
|
+
};
|
|
38
|
+
export const pureBuf82B64 = (buf8) => ab2b64(getArrayBuffer(buf8));
|
|
39
|
+
/**
|
|
40
|
+
* Convert an Uint8Array to a B64 string
|
|
41
|
+
*/
|
|
42
|
+
export const buf82b64 = haveBufferBase64() ? bufferBuf82B64 : pureBuf82B64;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/** @public */
|
|
17
|
+
export declare const readInt32LE: (byteArray: ArrayBuffer | Uint8Array, offset: number) => number;
|
|
18
|
+
/** @public */
|
|
19
|
+
export declare const readUint32LE: (byteArray: ArrayBuffer | Uint8Array, offset: number) => number;
|
|
20
|
+
/** @public */
|
|
21
|
+
export declare const readUint32BE: (byteArray: ArrayBuffer | Uint8Array, offset: number) => number;
|
|
22
|
+
/** @public */
|
|
23
|
+
export declare const writeUint32LE: (byteArray: ArrayBuffer | Uint8Array, offset: number, value: number) => void;
|
|
24
|
+
/** @public */
|
|
25
|
+
export declare const writeInt32LE: (byteArray: ArrayBuffer | Uint8Array, offset: number, value: number) => void;
|
|
26
|
+
/** @public */
|
|
27
|
+
export declare const writeUint32BE: (byteArray: ArrayBuffer | Uint8Array, offset: number, value: number) => void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
const getDataView = (byteArray, offset, length) => new DataView(byteArray instanceof Uint8Array ? byteArray.buffer : byteArray, byteArray instanceof Uint8Array ? byteArray.byteOffset + offset : offset, length);
|
|
17
|
+
const BYTES_32BIT = 4;
|
|
18
|
+
/** @public */
|
|
19
|
+
export const readInt32LE = (byteArray, offset) => getDataView(byteArray, offset, BYTES_32BIT).getInt32(0, true);
|
|
20
|
+
/** @public */
|
|
21
|
+
export const readUint32LE = (byteArray, offset) => getDataView(byteArray, offset, BYTES_32BIT).getUint32(0, true);
|
|
22
|
+
/** @public */
|
|
23
|
+
export const readUint32BE = (byteArray, offset) => getDataView(byteArray, offset, BYTES_32BIT).getUint32(0, false);
|
|
24
|
+
/** @public */
|
|
25
|
+
export const writeUint32LE = (byteArray, offset, value) => getDataView(byteArray, offset, BYTES_32BIT).setUint32(0, value, true);
|
|
26
|
+
/** @public */
|
|
27
|
+
export const writeInt32LE = (byteArray, offset, value) => getDataView(byteArray, offset, BYTES_32BIT).setInt32(0, value, true);
|
|
28
|
+
/** @public */
|
|
29
|
+
export const writeUint32BE = (byteArray, offset, value) => getDataView(byteArray, offset, BYTES_32BIT).setUint32(0, value, false);
|
package/lib/consts.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Number of bytes in the hex-representation of a byte.
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export declare const HEXBYTE_CHR_COUNT = 2;
|
|
22
|
+
/**
|
|
23
|
+
* Number of digits in hex.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export declare const HEX_DIGITS = 16;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum radix for a number to string conversion.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export declare const MAX_RADIX = 36;
|
package/lib/consts.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Number of bytes in the hex-representation of a byte.
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export const HEXBYTE_CHR_COUNT = 2;
|
|
22
|
+
/**
|
|
23
|
+
* Number of digits in hex.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export const HEX_DIGITS = 16;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum radix for a number to string conversion.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
export const MAX_RADIX = 36;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import type { LoggerFunction } from "./types.js";
|
|
17
|
+
/**
|
|
18
|
+
* Change the function used to log errors
|
|
19
|
+
* @param logFunc - Callback that will receive errors
|
|
20
|
+
*/
|
|
21
|
+
export declare const setLoggerFunction: (logFunc?: LoggerFunction) => void;
|
|
22
|
+
export declare const logError: (error: Error) => void;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
// eslint-disable-next-line no-console
|
|
17
|
+
let loggerFunction = console.error;
|
|
18
|
+
/**
|
|
19
|
+
* Change the function used to log errors
|
|
20
|
+
* @param logFunc - Callback that will receive errors
|
|
21
|
+
*/
|
|
22
|
+
export const setLoggerFunction = (logFunc) => {
|
|
23
|
+
if (logFunc) {
|
|
24
|
+
loggerFunction = logFunc;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
// eslint-disable-next-line no-console
|
|
28
|
+
loggerFunction = console.error;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
export const logError = (error) => loggerFunction(error);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import { TaskFunction, CompleteTaskOptions } from "./types.js";
|
|
17
|
+
/**
|
|
18
|
+
* Manage a single scheduled task
|
|
19
|
+
*/
|
|
20
|
+
export default class ScheduledTask {
|
|
21
|
+
private readonly _cronDefinition;
|
|
22
|
+
private readonly _func;
|
|
23
|
+
private readonly _opts;
|
|
24
|
+
/**
|
|
25
|
+
* Indicate that the task missed a previous tick
|
|
26
|
+
*/
|
|
27
|
+
private readonly _restartImmediate;
|
|
28
|
+
/**
|
|
29
|
+
* If the task is canceled (prevent looping)
|
|
30
|
+
*/
|
|
31
|
+
private _canceled;
|
|
32
|
+
/**
|
|
33
|
+
* Timeout handler
|
|
34
|
+
*/
|
|
35
|
+
private _timeoutHandler;
|
|
36
|
+
/**
|
|
37
|
+
* Last run time (timestamp)
|
|
38
|
+
*/
|
|
39
|
+
private _lastRun;
|
|
40
|
+
constructor(cronDefinition: string, func: TaskFunction, opts: CompleteTaskOptions);
|
|
41
|
+
/**
|
|
42
|
+
* Determine if the task should be running right now
|
|
43
|
+
*/
|
|
44
|
+
get runnable(): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Cancel the task from the scheduling
|
|
47
|
+
*/
|
|
48
|
+
cancel(): void;
|
|
49
|
+
/**
|
|
50
|
+
* Run the task
|
|
51
|
+
*
|
|
52
|
+
* All special cases are handled there (task already running).
|
|
53
|
+
* Updating the next value is also handled here.
|
|
54
|
+
*/
|
|
55
|
+
private _run;
|
|
56
|
+
/**
|
|
57
|
+
* Reschedule the task for the next occurrence
|
|
58
|
+
*/
|
|
59
|
+
private _reschedule;
|
|
60
|
+
/**
|
|
61
|
+
* Return the timeout delay before the next task scheduling.
|
|
62
|
+
*
|
|
63
|
+
* This can return a lower value if the task is scheduled in the distant
|
|
64
|
+
* future.
|
|
65
|
+
*/
|
|
66
|
+
private _getTimeoutDelay;
|
|
67
|
+
/**
|
|
68
|
+
* Return the next scheduled time starting from the given point in time
|
|
69
|
+
*/
|
|
70
|
+
private _getNextSchedule;
|
|
71
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import { CronExpressionParser } from "cron-parser";
|
|
17
|
+
import { dropPromise } from "../promise.js";
|
|
18
|
+
import { logError } from "./logger.js";
|
|
19
|
+
import { Overrun } from "./types.js";
|
|
20
|
+
/**
|
|
21
|
+
* Maximum delay for a timeout.
|
|
22
|
+
* Done because setTimeout() sometimes can only accept a 32bit integer
|
|
23
|
+
*/
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
|
|
25
|
+
const maxTimeoutDelay = 2 ** 31;
|
|
26
|
+
/**
|
|
27
|
+
* Manage a single scheduled task
|
|
28
|
+
*/
|
|
29
|
+
export default class ScheduledTask {
|
|
30
|
+
_cronDefinition;
|
|
31
|
+
_func;
|
|
32
|
+
_opts;
|
|
33
|
+
/**
|
|
34
|
+
* Indicate that the task missed a previous tick
|
|
35
|
+
*/
|
|
36
|
+
_restartImmediate = false;
|
|
37
|
+
/**
|
|
38
|
+
* If the task is canceled (prevent looping)
|
|
39
|
+
*/
|
|
40
|
+
_canceled = false;
|
|
41
|
+
/**
|
|
42
|
+
* Timeout handler
|
|
43
|
+
*/
|
|
44
|
+
_timeoutHandler;
|
|
45
|
+
/**
|
|
46
|
+
* Last run time (timestamp)
|
|
47
|
+
*/
|
|
48
|
+
_lastRun;
|
|
49
|
+
constructor(cronDefinition, func, opts) {
|
|
50
|
+
this._cronDefinition = cronDefinition;
|
|
51
|
+
this._func = func;
|
|
52
|
+
this._opts = opts;
|
|
53
|
+
this._lastRun = Date.now();
|
|
54
|
+
this._timeoutHandler = setTimeout(() => dropPromise(this._run()), Math.max(0, this._getTimeoutDelay()));
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Determine if the task should be running right now
|
|
58
|
+
*/
|
|
59
|
+
get runnable() {
|
|
60
|
+
return this._getNextSchedule() <= Date.now();
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Cancel the task from the scheduling
|
|
64
|
+
*/
|
|
65
|
+
cancel() {
|
|
66
|
+
if (this._timeoutHandler) {
|
|
67
|
+
clearTimeout(this._timeoutHandler);
|
|
68
|
+
this._timeoutHandler = null;
|
|
69
|
+
}
|
|
70
|
+
this._canceled = true;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Run the task
|
|
74
|
+
*
|
|
75
|
+
* All special cases are handled there (task already running).
|
|
76
|
+
* Updating the next value is also handled here.
|
|
77
|
+
*/
|
|
78
|
+
async _run() {
|
|
79
|
+
this._timeoutHandler = null;
|
|
80
|
+
if (this.runnable) {
|
|
81
|
+
this._lastRun = Date.now();
|
|
82
|
+
try {
|
|
83
|
+
await this._func();
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (error instanceof Error) {
|
|
87
|
+
logError(error);
|
|
88
|
+
}
|
|
89
|
+
logError(new Error(`Unknown error: ${error.toString()}`));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
this._reschedule();
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Reschedule the task for the next occurrence
|
|
96
|
+
*/
|
|
97
|
+
_reschedule() {
|
|
98
|
+
if (this._canceled) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
let nextDelay = this._getTimeoutDelay();
|
|
102
|
+
if (nextDelay < 0) {
|
|
103
|
+
// We overshot
|
|
104
|
+
switch (this._opts.overrun) {
|
|
105
|
+
case Overrun.AFTER:
|
|
106
|
+
// Reschedule immediately
|
|
107
|
+
nextDelay = 0;
|
|
108
|
+
break;
|
|
109
|
+
case Overrun.SKIP:
|
|
110
|
+
// Reschedule for the next time slot
|
|
111
|
+
this._lastRun = Date.now();
|
|
112
|
+
nextDelay = this._getTimeoutDelay();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
this._timeoutHandler = setTimeout(() => dropPromise(this._run()), Math.max(0, nextDelay));
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Return the timeout delay before the next task scheduling.
|
|
119
|
+
*
|
|
120
|
+
* This can return a lower value if the task is scheduled in the distant
|
|
121
|
+
* future.
|
|
122
|
+
*/
|
|
123
|
+
_getTimeoutDelay() {
|
|
124
|
+
const current = Date.now();
|
|
125
|
+
const next = this._getNextSchedule();
|
|
126
|
+
return Math.min(next - current, maxTimeoutDelay);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Return the next scheduled time starting from the given point in time
|
|
130
|
+
*/
|
|
131
|
+
_getNextSchedule() {
|
|
132
|
+
const nextOccurrence = CronExpressionParser.parse(this._cronDefinition, {
|
|
133
|
+
currentDate: this._lastRun,
|
|
134
|
+
}).next();
|
|
135
|
+
return nextOccurrence.getTime();
|
|
136
|
+
}
|
|
137
|
+
}
|