@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.
Files changed (183) hide show
  1. package/LICENSE +9 -0
  2. package/README.md +105 -0
  3. package/lib/array.d.ts +28 -0
  4. package/lib/array.js +36 -0
  5. package/lib/arraybuffer.d.ts +138 -0
  6. package/lib/arraybuffer.js +141 -0
  7. package/lib/async/asynctrigger.d.ts +50 -0
  8. package/lib/async/asynctrigger.js +108 -0
  9. package/lib/async/deferredpromise.d.ts +32 -0
  10. package/lib/async/deferredpromise.js +66 -0
  11. package/lib/async/keycache.d.ts +56 -0
  12. package/lib/async/keycache.js +103 -0
  13. package/lib/async/queues.d.ts +69 -0
  14. package/lib/async/queues.js +135 -0
  15. package/lib/async/timecache.d.ts +58 -0
  16. package/lib/async/timecache.js +118 -0
  17. package/lib/base58.d.ts +27 -0
  18. package/lib/base58.js +83 -0
  19. package/lib/base64.d.ts +51 -0
  20. package/lib/base64.js +126 -0
  21. package/lib/benchmark.d.ts +126 -0
  22. package/lib/benchmark.js +177 -0
  23. package/lib/bits/arraybuffer.d.ts +35 -0
  24. package/lib/bits/arraybuffer.js +64 -0
  25. package/lib/bits/base64.d.ts +35 -0
  26. package/lib/bits/base64.js +70 -0
  27. package/lib/bits/hex.d.ts +17 -0
  28. package/lib/bits/hex.js +30 -0
  29. package/lib/bits/uint8array.d.ts +28 -0
  30. package/lib/bits/uint8array.js +42 -0
  31. package/lib/bytebuffer.d.ts +27 -0
  32. package/lib/bytebuffer.js +29 -0
  33. package/lib/consts.d.ts +33 -0
  34. package/lib/consts.js +33 -0
  35. package/lib/cron/logger.d.ts +22 -0
  36. package/lib/cron/logger.js +31 -0
  37. package/lib/cron/scheduledtask.d.ts +71 -0
  38. package/lib/cron/scheduledtask.js +137 -0
  39. package/lib/cron/types.d.ts +53 -0
  40. package/lib/cron/types.js +31 -0
  41. package/lib/cron.d.ts +29 -0
  42. package/lib/cron.js +47 -0
  43. package/lib/dict.d.ts +56 -0
  44. package/lib/dict.js +74 -0
  45. package/lib/error.d.ts +25 -0
  46. package/lib/error.js +41 -0
  47. package/lib/global.d.ts +27 -0
  48. package/lib/global.js +53 -0
  49. package/lib/hex.d.ts +32 -0
  50. package/lib/hex.js +58 -0
  51. package/lib/idx.d.ts +51 -0
  52. package/lib/idx.js +81 -0
  53. package/lib/json.d.ts +57 -0
  54. package/lib/json.js +116 -0
  55. package/lib/marshalling/marshaller.d.ts +51 -0
  56. package/lib/marshalling/marshaller.js +155 -0
  57. package/lib/marshalling/unmarshaller.d.ts +53 -0
  58. package/lib/marshalling/unmarshaller.js +124 -0
  59. package/lib/marshalling/util.d.ts +25 -0
  60. package/lib/marshalling/util.js +25 -0
  61. package/lib/number.d.ts +17 -0
  62. package/lib/number.js +21 -0
  63. package/lib/path.d.ts +25 -0
  64. package/lib/path.js +29 -0
  65. package/lib/promise.d.ts +42 -0
  66. package/lib/promise.js +78 -0
  67. package/lib/starttime.d.ts +23 -0
  68. package/lib/starttime.js +29 -0
  69. package/lib/string.d.ts +65 -0
  70. package/lib/string.js +108 -0
  71. package/lib/types/array.d.ts +34 -0
  72. package/lib/types/array.js +64 -0
  73. package/lib/types/enum.d.ts +30 -0
  74. package/lib/types/enum.js +44 -0
  75. package/lib/types/predicateerror.d.ts +40 -0
  76. package/lib/types/predicateerror.js +107 -0
  77. package/lib/types/primitive.d.ts +23 -0
  78. package/lib/types/primitive.js +34 -0
  79. package/lib/types/record.d.ts +67 -0
  80. package/lib/types/record.js +235 -0
  81. package/lib/types/types.d.ts +64 -0
  82. package/lib/types/types.js +115 -0
  83. package/lib/types/utils.d.ts +18 -0
  84. package/lib/types/utils.js +67 -0
  85. package/lib/uint8array.d.ts +176 -0
  86. package/lib/uint8array.js +438 -0
  87. package/lib/units.d.ts +159 -0
  88. package/lib/units.js +290 -0
  89. package/lib/utils/buffer.d.ts +49 -0
  90. package/lib/utils/buffer.js +79 -0
  91. package/lib/utils/fourbytes.d.ts +29 -0
  92. package/lib/utils/fourbytes.js +45 -0
  93. package/package.json +1 -0
  94. package/web/array.d.ts +28 -0
  95. package/web/array.js +34 -0
  96. package/web/arraybuffer.d.ts +138 -0
  97. package/web/arraybuffer.js +141 -0
  98. package/web/async/asynctrigger.d.ts +50 -0
  99. package/web/async/asynctrigger.js +106 -0
  100. package/web/async/deferredpromise.d.ts +32 -0
  101. package/web/async/deferredpromise.js +65 -0
  102. package/web/async/keycache.d.ts +56 -0
  103. package/web/async/keycache.js +97 -0
  104. package/web/async/queues.d.ts +69 -0
  105. package/web/async/queues.js +131 -0
  106. package/web/async/timecache.d.ts +58 -0
  107. package/web/async/timecache.js +107 -0
  108. package/web/base58.d.ts +27 -0
  109. package/web/base58.js +78 -0
  110. package/web/base64.d.ts +51 -0
  111. package/web/base64.js +136 -0
  112. package/web/benchmark.d.ts +126 -0
  113. package/web/benchmark.js +183 -0
  114. package/web/bits/arraybuffer.d.ts +35 -0
  115. package/web/bits/arraybuffer.js +59 -0
  116. package/web/bits/base64.d.ts +35 -0
  117. package/web/bits/base64.js +67 -0
  118. package/web/bits/hex.d.ts +17 -0
  119. package/web/bits/hex.js +27 -0
  120. package/web/bits/uint8array.d.ts +28 -0
  121. package/web/bits/uint8array.js +41 -0
  122. package/web/bytebuffer.d.ts +27 -0
  123. package/web/bytebuffer.js +29 -0
  124. package/web/consts.d.ts +33 -0
  125. package/web/consts.js +33 -0
  126. package/web/cron/logger.d.ts +22 -0
  127. package/web/cron/logger.js +30 -0
  128. package/web/cron/scheduledtask.d.ts +71 -0
  129. package/web/cron/scheduledtask.js +136 -0
  130. package/web/cron/types.d.ts +53 -0
  131. package/web/cron/types.js +31 -0
  132. package/web/cron.d.ts +29 -0
  133. package/web/cron.js +47 -0
  134. package/web/dict.d.ts +56 -0
  135. package/web/dict.js +67 -0
  136. package/web/error.d.ts +25 -0
  137. package/web/error.js +39 -0
  138. package/web/global.d.ts +27 -0
  139. package/web/global.js +49 -0
  140. package/web/hex.d.ts +32 -0
  141. package/web/hex.js +52 -0
  142. package/web/idx.d.ts +51 -0
  143. package/web/idx.js +76 -0
  144. package/web/json.d.ts +57 -0
  145. package/web/json.js +98 -0
  146. package/web/marshalling/marshaller.d.ts +51 -0
  147. package/web/marshalling/marshaller.js +150 -0
  148. package/web/marshalling/unmarshaller.d.ts +53 -0
  149. package/web/marshalling/unmarshaller.js +115 -0
  150. package/web/marshalling/util.d.ts +25 -0
  151. package/web/marshalling/util.js +25 -0
  152. package/web/number.d.ts +17 -0
  153. package/web/number.js +21 -0
  154. package/web/path.d.ts +25 -0
  155. package/web/path.js +26 -0
  156. package/web/promise.d.ts +42 -0
  157. package/web/promise.js +74 -0
  158. package/web/starttime.d.ts +23 -0
  159. package/web/starttime.js +29 -0
  160. package/web/string.d.ts +65 -0
  161. package/web/string.js +101 -0
  162. package/web/types/array.d.ts +34 -0
  163. package/web/types/array.js +63 -0
  164. package/web/types/enum.d.ts +30 -0
  165. package/web/types/enum.js +40 -0
  166. package/web/types/predicateerror.d.ts +40 -0
  167. package/web/types/predicateerror.js +128 -0
  168. package/web/types/primitive.d.ts +23 -0
  169. package/web/types/primitive.js +33 -0
  170. package/web/types/record.d.ts +67 -0
  171. package/web/types/record.js +213 -0
  172. package/web/types/types.d.ts +64 -0
  173. package/web/types/types.js +123 -0
  174. package/web/types/utils.d.ts +18 -0
  175. package/web/types/utils.js +30 -0
  176. package/web/uint8array.d.ts +176 -0
  177. package/web/uint8array.js +412 -0
  178. package/web/units.d.ts +159 -0
  179. package/web/units.js +312 -0
  180. package/web/utils/buffer.d.ts +49 -0
  181. package/web/utils/buffer.js +76 -0
  182. package/web/utils/fourbytes.d.ts +29 -0
  183. package/web/utils/fourbytes.js +45 -0
@@ -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);
@@ -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
+ };