@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
package/web/bits/hex.js
ADDED
|
@@ -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
|
+
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) return input;
|
|
24
|
+
if (noprefix) return `0${input}`;
|
|
25
|
+
if (even) return input.substring(HEX_PREFIX_LENGTH);
|
|
26
|
+
return `0${input.substring(HEX_PREFIX_LENGTH)}`;
|
|
27
|
+
};
|
|
@@ -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,41 @@
|
|
|
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) return arrayBuffer.slice(offset, realLength + offset);
|
|
35
|
+
return new Uint8Array(arrayBuffer.buffer, offset + arrayBuffer.byteOffset, realLength);
|
|
36
|
+
};
|
|
37
|
+
export const pureBuf82B64 = buf8 => ab2b64(getArrayBuffer(buf8));
|
|
38
|
+
/**
|
|
39
|
+
* Convert an Uint8Array to a B64 string
|
|
40
|
+
*/
|
|
41
|
+
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/web/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/web/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,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
|
+
// 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
|
+
} else {
|
|
26
|
+
// eslint-disable-next-line no-console
|
|
27
|
+
loggerFunction = console.error;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
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,136 @@
|
|
|
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
|
+
} catch (error) {
|
|
85
|
+
if (error instanceof Error) {
|
|
86
|
+
logError(error);
|
|
87
|
+
}
|
|
88
|
+
logError(new Error(`Unknown error: ${error.toString()}`));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
this._reschedule();
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Reschedule the task for the next occurrence
|
|
95
|
+
*/
|
|
96
|
+
_reschedule() {
|
|
97
|
+
if (this._canceled) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
let nextDelay = this._getTimeoutDelay();
|
|
101
|
+
if (nextDelay < 0) {
|
|
102
|
+
// We overshot
|
|
103
|
+
switch (this._opts.overrun) {
|
|
104
|
+
case Overrun.AFTER:
|
|
105
|
+
// Reschedule immediately
|
|
106
|
+
nextDelay = 0;
|
|
107
|
+
break;
|
|
108
|
+
case Overrun.SKIP:
|
|
109
|
+
// Reschedule for the next time slot
|
|
110
|
+
this._lastRun = Date.now();
|
|
111
|
+
nextDelay = this._getTimeoutDelay();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
this._timeoutHandler = setTimeout(() => dropPromise(this._run()), Math.max(0, nextDelay));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Return the timeout delay before the next task scheduling.
|
|
118
|
+
*
|
|
119
|
+
* This can return a lower value if the task is scheduled in the distant
|
|
120
|
+
* future.
|
|
121
|
+
*/
|
|
122
|
+
_getTimeoutDelay() {
|
|
123
|
+
const current = Date.now();
|
|
124
|
+
const next = this._getNextSchedule();
|
|
125
|
+
return Math.min(next - current, maxTimeoutDelay);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Return the next scheduled time starting from the given point in time
|
|
129
|
+
*/
|
|
130
|
+
_getNextSchedule() {
|
|
131
|
+
const nextOccurrence = CronExpressionParser.parse(this._cronDefinition, {
|
|
132
|
+
currentDate: this._lastRun
|
|
133
|
+
}).next();
|
|
134
|
+
return nextOccurrence.getTime();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
* Function to execute a single task
|
|
18
|
+
*
|
|
19
|
+
* If it returns a promise, the task is considerred running as long as the
|
|
20
|
+
* promise didn't complete
|
|
21
|
+
*/
|
|
22
|
+
export type TaskFunction = () => Promise<void> | void;
|
|
23
|
+
export type LoggerFunction = (error: Error) => void;
|
|
24
|
+
/**
|
|
25
|
+
* Behavior when a task is scheduled to start while it's already running
|
|
26
|
+
*/
|
|
27
|
+
export declare enum Overrun {
|
|
28
|
+
/**
|
|
29
|
+
* Skip this start
|
|
30
|
+
*/
|
|
31
|
+
SKIP = "skip",
|
|
32
|
+
/**
|
|
33
|
+
* Immediately restart the task once it finish
|
|
34
|
+
*/
|
|
35
|
+
AFTER = "after"
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Options when scheduling a task
|
|
39
|
+
*/
|
|
40
|
+
export interface TaskOptions {
|
|
41
|
+
/**
|
|
42
|
+
* Behavior when the task is scheduled to restart before it have completed.
|
|
43
|
+
*
|
|
44
|
+
* Default to IGNORE when scheduling a task.
|
|
45
|
+
*/
|
|
46
|
+
overrun?: Overrun;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Same as TaskOptions, but all members are required
|
|
50
|
+
*/
|
|
51
|
+
export interface CompleteTaskOptions {
|
|
52
|
+
overrun: Overrun;
|
|
53
|
+
}
|
|
@@ -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
|
+
/**
|
|
17
|
+
* Behavior when a task is scheduled to start while it's already running
|
|
18
|
+
*/
|
|
19
|
+
export var Overrun;
|
|
20
|
+
(function (Overrun) {
|
|
21
|
+
/**
|
|
22
|
+
* Skip this start
|
|
23
|
+
*/
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
25
|
+
Overrun["SKIP"] = "skip";
|
|
26
|
+
/**
|
|
27
|
+
* Immediately restart the task once it finish
|
|
28
|
+
*/
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
30
|
+
Overrun["AFTER"] = "after";
|
|
31
|
+
})(Overrun || (Overrun = {}));
|
package/web/cron.d.ts
ADDED
|
@@ -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
|
+
import ScheduledTask from "./cron/scheduledtask.js";
|
|
17
|
+
import { TaskFunction, TaskOptions, Overrun, CompleteTaskOptions } from "./cron/types.js";
|
|
18
|
+
export { Overrun };
|
|
19
|
+
export type { ScheduledTask, TaskFunction, TaskOptions, CompleteTaskOptions };
|
|
20
|
+
export declare const setLoggerFunction: (logFunc?: import("./cron/types.js").LoggerFunction) => void;
|
|
21
|
+
/**
|
|
22
|
+
* Schedule a task for execution
|
|
23
|
+
*
|
|
24
|
+
* @param cronDefinition - CRON timing for the task
|
|
25
|
+
* @param task - The task function to run at the scheduled time
|
|
26
|
+
* @param taskOptions - The behavior of the task
|
|
27
|
+
*/
|
|
28
|
+
export declare const schedule: (cronDefinition: string, task: TaskFunction, taskOptions?: TaskOptions) => ScheduledTask;
|
|
29
|
+
export declare const stop: () => void;
|
package/web/cron.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
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 { setLoggerFunction as bitsSetLoggerFunction } from "./cron/logger.js";
|
|
17
|
+
import ScheduledTask from "./cron/scheduledtask.js";
|
|
18
|
+
import { Overrun } from "./cron/types.js";
|
|
19
|
+
const scheduledTasks = [];
|
|
20
|
+
export { Overrun };
|
|
21
|
+
export const setLoggerFunction = bitsSetLoggerFunction;
|
|
22
|
+
/**
|
|
23
|
+
* Set default values for task options
|
|
24
|
+
*/
|
|
25
|
+
const getDefaultTaskOptions = taskOptions => {
|
|
26
|
+
const result = taskOptions ?? {};
|
|
27
|
+
return {
|
|
28
|
+
overrun: Overrun.SKIP,
|
|
29
|
+
...result
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Schedule a task for execution
|
|
34
|
+
*
|
|
35
|
+
* @param cronDefinition - CRON timing for the task
|
|
36
|
+
* @param task - The task function to run at the scheduled time
|
|
37
|
+
* @param taskOptions - The behavior of the task
|
|
38
|
+
*/
|
|
39
|
+
export const schedule = (cronDefinition, task, taskOptions) => {
|
|
40
|
+
const newTask = new ScheduledTask(cronDefinition, task, getDefaultTaskOptions(taskOptions));
|
|
41
|
+
scheduledTasks.push(newTask);
|
|
42
|
+
return newTask;
|
|
43
|
+
};
|
|
44
|
+
export const stop = () => {
|
|
45
|
+
scheduledTasks.forEach(task => task.cancel());
|
|
46
|
+
scheduledTasks.length = 0;
|
|
47
|
+
};
|