@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
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 KeeeX SAS
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
package/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # @keeex/utils
2
+
3
+
4
+
5
+ Utility functions in pure JS used in many places.
6
+
7
+ You should check the API documentation for detailed informations about each functions.
8
+ This is a general feature list overview.
9
+
10
+ ## Async utility
11
+
12
+ - `AsyncTrigger`, for easy debouncing
13
+ - `DeferredPromise` to manipulate the usual `resolve()` and `reject()` functions outside of a
14
+ promise block
15
+ - `KeyCache` to create a basic key-value cache with asynchronous fetch
16
+ - `TimeCache` to create a cache that keeps a value for a given amount of time and can automatically
17
+ refresh it
18
+ - `Queues` to create parallel queues of tasks
19
+
20
+ ## Marshalling
21
+
22
+ Serialization of random data into byte buffers.
23
+
24
+ ## Types
25
+
26
+ Various type-checking helpers to create TypeScript type predicates.
27
+ Supports "shortcut" versions for performance after a full check was done.
28
+
29
+ ## Array
30
+
31
+ Generic operations on arrays, as well as array coercion.
32
+
33
+ ## Uint8Array and ArrayBuffer
34
+
35
+ Conversion between different formats and search in buffer functions.
36
+
37
+ ## base58, base64
38
+
39
+ Utility to manipulate strings encoded in these formats.
40
+
41
+ ## benchmark
42
+
43
+ Code to run a JavaScript function a lot to get performance benchmarks.
44
+ Includes some form of "automatic" probing to get good average values.
45
+
46
+ ## bytebuffer
47
+
48
+ Helper wrapping JavaScript DataView into convenient functions.
49
+
50
+ ## dict
51
+
52
+ Functions to copy records with primitive types (deep copy).
53
+
54
+ ## error
55
+
56
+ Coerce anything (`unknown`) into an `Error` object for easier error handling.
57
+
58
+ ## global
59
+
60
+ Functions to use a JavaScript environment "global" dataset.
61
+ Handles multiple environment somewhat gracefully.
62
+
63
+ ## idx
64
+
65
+ IDX check and manipulation functions
66
+
67
+ ## json
68
+
69
+ JSON parsing with type safety.
70
+ Also, a "canonical" JSON encoder that ensure property order is consistent.
71
+
72
+ ## log
73
+
74
+ Generic logging functionality.
75
+ Supports various log level, timestamping, error chaining, stacktrace stripping.
76
+
77
+ ## number
78
+
79
+ Manipulate number.
80
+ Currently only round to a given decimal place.
81
+
82
+ ## path
83
+
84
+ Generic filename sanitization.
85
+
86
+ ## promise
87
+
88
+ Helpers around promises.
89
+
90
+ - controlled async delay
91
+ - retry a promise until it succeed (with max tries)
92
+ - run an array of promise, stop at the first that succeed
93
+
94
+ ## starttime
95
+
96
+ Get the elapsed time since the JavaScript environment started.
97
+
98
+ ## string
99
+
100
+ String and UTF-8 manipulations.
101
+
102
+ ## units
103
+
104
+ Convers between various units.
105
+ Generic functions are available as well as byte and time conversions.
package/lib/array.d.ts ADDED
@@ -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
+ export type EqualityFunction<T1, T2> = (op1: T1, op2: T2) => boolean;
17
+ /**
18
+ * Compare two array-like structure for equal content
19
+ *
20
+ * @public
21
+ */
22
+ export declare const arrayEqual: <T1 = unknown, T2 = unknown>(op1: ArrayLike<T1>, op2: ArrayLike<T2>, eqFunc?: EqualityFunction<T1, T2>) => boolean;
23
+ /**
24
+ * Return either the array provided, or an array containing the single element provided.
25
+ *
26
+ * @public
27
+ */
28
+ export declare const asArray: <T>(data: Array<T> | (T extends Array<unknown> ? never : T)) => Array<T>;
package/lib/array.js ADDED
@@ -0,0 +1,36 @@
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 useEqOp = (op1, op2) => op1 === op2;
17
+ /**
18
+ * Compare two array-like structure for equal content
19
+ *
20
+ * @public
21
+ */
22
+ export const arrayEqual = (op1, op2, eqFunc = useEqOp) => {
23
+ if (op1.length !== op2.length)
24
+ return false;
25
+ for (let index = 0; index < op1.length; ++index) {
26
+ if (!eqFunc(op1[index], op2[index]))
27
+ return false;
28
+ }
29
+ return true;
30
+ };
31
+ /**
32
+ * Return either the array provided, or an array containing the single element provided.
33
+ *
34
+ * @public
35
+ */
36
+ export const asArray = (data) => Array.isArray(data) ? data : [data];
@@ -0,0 +1,138 @@
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
+ export declare const getArrayBuffer: (typedArray: ArrayBuffer | Uint8Array) => ArrayBuffer;
17
+ /**
18
+ * Convert an ArrayBuffer to a raw string.
19
+ *
20
+ * @public
21
+ */
22
+ export declare const ab2str: (ab: ArrayBuffer) => string;
23
+ /**
24
+ * Convert a raw string to an ArrayBuffer.
25
+ *
26
+ * @public
27
+ */
28
+ export declare const str2ab: (str: string) => ArrayBuffer;
29
+ /**
30
+ * Convert an ArrayBuffer to an hex string.
31
+ *
32
+ * @public
33
+ */
34
+ export declare const ab2hex: (ab: ArrayBuffer, usePrefix?: boolean) => string;
35
+ /**
36
+ * Convert an hex string to an ArrayBuffer.
37
+ *
38
+ * @public
39
+ */
40
+ export declare const hex2ab: (hex: string) => ArrayBuffer;
41
+ /**
42
+ * Concatenate multiple array buffers into one output.
43
+ *
44
+ * The output will have each buffer length put in it so the buffers can be split
45
+ * afterward using explodeBuffersList(). Simple concatenation is available with merge().
46
+ *
47
+ * @public
48
+ */
49
+ export declare const concatenateBuffersList: (buffers: Array<ArrayBuffer>, addEndMarker?: boolean) => ArrayBuffer;
50
+ /**
51
+ * Separate buffers merged with concatenateBuffersList().
52
+ *
53
+ * @public
54
+ */
55
+ export declare const explodeBuffersList: (mergedBuffers: ArrayBuffer) => Array<ArrayBuffer>;
56
+ /** @public */
57
+ export declare const concatenatedBuffersListSize: (mergedBuffers: ArrayBuffer) => number;
58
+ /**
59
+ * Merge ArrayBuffers into one.
60
+ *
61
+ * @public
62
+ */
63
+ export declare const merge: (buffers: Array<ArrayBuffer>) => ArrayBuffer;
64
+ /**
65
+ * Convert an array buffer to a b64 string.
66
+ *
67
+ * @public
68
+ */
69
+ export declare const ab2b64: (data: ArrayBuffer) => string;
70
+ /**
71
+ * Convert a b64 string to an array buffer.
72
+ *
73
+ * @param strict - If enabled, prevent input from having extra data after the B64 string.
74
+ *
75
+ * @public
76
+ */
77
+ export declare const b642ab: (data: string, strict?: boolean) => ArrayBuffer;
78
+ /**
79
+ * Convert an UTF-8 string into an arraybuffer.
80
+ *
81
+ * @public
82
+ */
83
+ export declare const utf82ab: (str: string) => ArrayBuffer;
84
+ /**
85
+ * Convert back an arraybuffer into an UTF-8 string.
86
+ *
87
+ * @public
88
+ */
89
+ export declare const ab2utf8: (ab: ArrayBuffer) => string;
90
+ /**
91
+ * Compare that the content of two ArrayBuffer is equal.
92
+ *
93
+ * @public
94
+ */
95
+ export declare const abEqual: (ab1: ArrayBuffer, ab2: ArrayBuffer) => boolean;
96
+ /**
97
+ * Find the position of a string in a buffer
98
+ *
99
+ * @param needle - String to find. Only work with 1 byte-wide characters.
100
+ *
101
+ * @param ab - Buffer to look into
102
+ *
103
+ * @param startPosition - Starting index to look from
104
+ *
105
+ * @returns
106
+ * The position of the string in the buffer. If not found returns -1
107
+ *
108
+ * @public
109
+ */
110
+ export declare const findString: (needle: string, ab: ArrayBuffer, startPosition?: number) => number;
111
+ /**
112
+ * Find the position of a string in a buffer from the end
113
+ *
114
+ * @param needle - String to find. Only work with 1 byte characters.
115
+ *
116
+ * @param ab - Buffer to look into
117
+ *
118
+ * @param startPosition - Starting index to look from
119
+ *
120
+ * @param endPosition - Last index to look from. The needle must fit completely before this.
121
+ * -1 mean end of the file.
122
+ *
123
+ * @returns
124
+ * The position of the string in the buffer. If not found returns -1
125
+ *
126
+ * @public
127
+ */
128
+ export declare const findLastString: (needle: string, ab: ArrayBuffer, startPosition?: number, endPosition?: number) => number;
129
+ /**
130
+ * Check if a byte sequence looks like a valid UTF-8 sequence that *does* have wide characters.
131
+ *
132
+ * https://en.wikipedia.org/wiki/UTF-8
133
+ *
134
+ * @returns
135
+ * `true` if the byte sequence does contain UTF-8 sequences.
136
+ * Regular ASCII string will return `false` as they won't contain such sequence.
137
+ */
138
+ export declare const isUTF8Sequence: (ab: ArrayBuffer) => boolean;
@@ -0,0 +1,141 @@
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 { ab2b64 as bitsAb2b64, b642ab as bitsB642ab, getArrayBuffer as bitsGetArrayBuffer, } from "./bits/arraybuffer.js";
17
+ import { getUint8Array, buf82str, str2buf8, buf82hex, hex2buf8, concatenateBuf8List, explodeBuf8List, merge as mergeBuf8, utf82buf8, buf82utf8, buf8Equal, findString as findStringBuf8, findLastString as findLastStringBuf8, concatenatedBuf8ListSize, isUTF8Sequence as isUTF8SequenceBuf8, } from "./uint8array.js";
18
+ import { bufferAb2B64, bufferB642Ab, haveBufferBase64 } from "./utils/buffer.js";
19
+ export const getArrayBuffer = bitsGetArrayBuffer;
20
+ /**
21
+ * Convert an ArrayBuffer to a raw string.
22
+ *
23
+ * @public
24
+ */
25
+ export const ab2str = (ab) => buf82str(getUint8Array(ab));
26
+ /**
27
+ * Convert a raw string to an ArrayBuffer.
28
+ *
29
+ * @public
30
+ */
31
+ export const str2ab = (str) => getArrayBuffer(str2buf8(str));
32
+ /**
33
+ * Convert an ArrayBuffer to an hex string.
34
+ *
35
+ * @public
36
+ */
37
+ export const ab2hex = (ab, usePrefix) => buf82hex(getUint8Array(ab), usePrefix);
38
+ /**
39
+ * Convert an hex string to an ArrayBuffer.
40
+ *
41
+ * @public
42
+ */
43
+ export const hex2ab = (hex) => getArrayBuffer(hex2buf8(hex));
44
+ /**
45
+ * Concatenate multiple array buffers into one output.
46
+ *
47
+ * The output will have each buffer length put in it so the buffers can be split
48
+ * afterward using explodeBuffersList(). Simple concatenation is available with merge().
49
+ *
50
+ * @public
51
+ */
52
+ export const concatenateBuffersList = (buffers, addEndMarker = false) => getArrayBuffer(concatenateBuf8List(buffers.map((buffer) => getUint8Array(buffer)), addEndMarker));
53
+ /**
54
+ * Separate buffers merged with concatenateBuffersList().
55
+ *
56
+ * @public
57
+ */
58
+ export const explodeBuffersList = (mergedBuffers) => explodeBuf8List(getUint8Array(mergedBuffers)).map((buf8) => getArrayBuffer(buf8));
59
+ /** @public */
60
+ export const concatenatedBuffersListSize = (mergedBuffers) => concatenatedBuf8ListSize(getUint8Array(mergedBuffers));
61
+ /**
62
+ * Merge ArrayBuffers into one.
63
+ *
64
+ * @public
65
+ */
66
+ export const merge = (buffers) => getArrayBuffer(mergeBuf8(buffers.map((buffer) => getUint8Array(buffer))));
67
+ /**
68
+ * Convert an array buffer to a b64 string.
69
+ *
70
+ * @public
71
+ */
72
+ export const ab2b64 = haveBufferBase64() ? bufferAb2B64 : bitsAb2b64;
73
+ /**
74
+ * Convert a b64 string to an array buffer.
75
+ *
76
+ * @param strict - If enabled, prevent input from having extra data after the B64 string.
77
+ *
78
+ * @public
79
+ */
80
+ export const b642ab = haveBufferBase64() ? bufferB642Ab : bitsB642ab;
81
+ /**
82
+ * Convert an UTF-8 string into an arraybuffer.
83
+ *
84
+ * @public
85
+ */
86
+ export const utf82ab = (str) => getArrayBuffer(utf82buf8(str));
87
+ /**
88
+ * Convert back an arraybuffer into an UTF-8 string.
89
+ *
90
+ * @public
91
+ */
92
+ export const ab2utf8 = (ab) => buf82utf8(getUint8Array(ab));
93
+ /**
94
+ * Compare that the content of two ArrayBuffer is equal.
95
+ *
96
+ * @public
97
+ */
98
+ export const abEqual = (ab1, ab2) => buf8Equal(getUint8Array(ab1), getUint8Array(ab2));
99
+ /**
100
+ * Find the position of a string in a buffer
101
+ *
102
+ * @param needle - String to find. Only work with 1 byte-wide characters.
103
+ *
104
+ * @param ab - Buffer to look into
105
+ *
106
+ * @param startPosition - Starting index to look from
107
+ *
108
+ * @returns
109
+ * The position of the string in the buffer. If not found returns -1
110
+ *
111
+ * @public
112
+ */
113
+ export const findString = (needle, ab, startPosition = 0) => findStringBuf8(needle, getUint8Array(ab), startPosition);
114
+ /**
115
+ * Find the position of a string in a buffer from the end
116
+ *
117
+ * @param needle - String to find. Only work with 1 byte characters.
118
+ *
119
+ * @param ab - Buffer to look into
120
+ *
121
+ * @param startPosition - Starting index to look from
122
+ *
123
+ * @param endPosition - Last index to look from. The needle must fit completely before this.
124
+ * -1 mean end of the file.
125
+ *
126
+ * @returns
127
+ * The position of the string in the buffer. If not found returns -1
128
+ *
129
+ * @public
130
+ */
131
+ export const findLastString = (needle, ab, startPosition = 0, endPosition = -1) => findLastStringBuf8(needle, getUint8Array(ab), startPosition, endPosition);
132
+ /**
133
+ * Check if a byte sequence looks like a valid UTF-8 sequence that *does* have wide characters.
134
+ *
135
+ * https://en.wikipedia.org/wiki/UTF-8
136
+ *
137
+ * @returns
138
+ * `true` if the byte sequence does contain UTF-8 sequences.
139
+ * Regular ASCII string will return `false` as they won't contain such sequence.
140
+ */
141
+ export const isUTF8Sequence = (ab) => isUTF8SequenceBuf8(getUint8Array(ab));
@@ -0,0 +1,50 @@
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
+ export type TriggerFunction = () => void | Promise<void>;
17
+ /**
18
+ * Trigger a function after a certain delay expired when you call trigger().
19
+ *
20
+ * @public
21
+ */
22
+ export default class AsyncTrigger {
23
+ #private;
24
+ /**
25
+ * @param delayMs - Delay before calling the function after the most recent call to trigger()
26
+ *
27
+ * @param triggerFunction - Function to call when the delay expires
28
+ * If it returns a promise, we wait for the promise to resolve before allowing
29
+ * the trigger to proc again.
30
+ */
31
+ constructor(delayMs: number, triggerFunction: TriggerFunction);
32
+ /**
33
+ * Trigger the function after a delay.
34
+ *
35
+ * This function can be called repeteadly; the delay will restart with each
36
+ * call.
37
+ * If the trigger function is running when trigger() is called, it is
38
+ * automatically rescheduled.
39
+ *
40
+ * @public
41
+ */
42
+ trigger: () => void;
43
+ /**
44
+ * Cancel a pending trigger.
45
+ * Can safely be called even if no pending trigger exists.
46
+ *
47
+ * @public
48
+ */
49
+ cancel: () => void;
50
+ }
@@ -0,0 +1,108 @@
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
+ * Trigger a function after a certain delay expired when you call trigger().
18
+ *
19
+ * @public
20
+ */
21
+ export default class AsyncTrigger {
22
+ #timeoutInstance;
23
+ #running;
24
+ #reschedule;
25
+ #delayMs;
26
+ #triggerFunction;
27
+ /**
28
+ * @param delayMs - Delay before calling the function after the most recent call to trigger()
29
+ *
30
+ * @param triggerFunction - Function to call when the delay expires
31
+ * If it returns a promise, we wait for the promise to resolve before allowing
32
+ * the trigger to proc again.
33
+ */
34
+ constructor(delayMs, triggerFunction) {
35
+ this.#timeoutInstance = undefined;
36
+ this.#running = false;
37
+ this.#reschedule = false;
38
+ this.#delayMs = delayMs;
39
+ this.#triggerFunction = triggerFunction;
40
+ }
41
+ /**
42
+ * Trigger the function after a delay.
43
+ *
44
+ * This function can be called repeteadly; the delay will restart with each
45
+ * call.
46
+ * If the trigger function is running when trigger() is called, it is
47
+ * automatically rescheduled.
48
+ *
49
+ * @public
50
+ */
51
+ trigger = () => {
52
+ if (this.#running) {
53
+ this.#reschedule = true;
54
+ return;
55
+ }
56
+ if (this.#timeoutInstance)
57
+ clearTimeout(this.#timeoutInstance);
58
+ this.#realSchedule();
59
+ };
60
+ /**
61
+ * Cancel a pending trigger.
62
+ * Can safely be called even if no pending trigger exists.
63
+ *
64
+ * @public
65
+ */
66
+ cancel = () => {
67
+ if (this.#running && this.#reschedule)
68
+ this.#reschedule = false;
69
+ if (this.#timeoutInstance) {
70
+ clearTimeout(this.#timeoutInstance);
71
+ this.#timeoutInstance = undefined;
72
+ }
73
+ };
74
+ #realSchedule = () => {
75
+ this.#timeoutInstance = setTimeout(this.#doTrigger, this.#delayMs);
76
+ };
77
+ /** Only called by setTimeout() */
78
+ #doTrigger = () => {
79
+ this.#timeoutInstance = undefined;
80
+ this.#running = true;
81
+ const res = this.#triggerFunction();
82
+ if (res instanceof Promise) {
83
+ // We're waiting a promise
84
+ res
85
+ // eslint-disable-next-line promise/prefer-await-to-then
86
+ .then(() => {
87
+ this.#running = false;
88
+ // eslint-disable-next-line promise/always-return
89
+ if (this.#reschedule) {
90
+ this.#reschedule = false;
91
+ this.#realSchedule();
92
+ }
93
+ })
94
+ // eslint-disable-next-line promise/prefer-await-to-then
95
+ .catch((error) => {
96
+ // eslint-disable-next-line no-console
97
+ console.error("An error occurred during an async trigger:", error);
98
+ });
99
+ return;
100
+ }
101
+ // Not a promise
102
+ this.#running = false;
103
+ if (this.#reschedule) {
104
+ this.#reschedule = false;
105
+ this.#realSchedule();
106
+ }
107
+ };
108
+ }
@@ -0,0 +1,32 @@
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
+ * Allows returning a promise and resolving it in another scope
18
+ *
19
+ * @public
20
+ */
21
+ export declare class DeferredPromise<ResultType> {
22
+ #private;
23
+ /** The actual promise that can be awaited */
24
+ promise: Promise<ResultType>;
25
+ constructor();
26
+ /** Indicate if the promise already resolved or rejected. */
27
+ get resolved(): boolean;
28
+ /** Can be called from anywhere; will resolve the promise. */
29
+ resolve: (value: ResultType | PromiseLike<ResultType>) => void;
30
+ /** Can be called from anywhere; will reject the promise. */
31
+ reject: (reason?: unknown) => void;
32
+ }