@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/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/web/array.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
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) return false;
|
|
24
|
+
for (let index = 0; index < op1.length; ++index) {
|
|
25
|
+
if (!eqFunc(op1[index], op2[index])) return false;
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Return either the array provided, or an array containing the single element provided.
|
|
31
|
+
*
|
|
32
|
+
* @public
|
|
33
|
+
*/
|
|
34
|
+
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,106 @@
|
|
|
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) clearTimeout(this.#timeoutInstance);
|
|
57
|
+
this.#realSchedule();
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* Cancel a pending trigger.
|
|
61
|
+
* Can safely be called even if no pending trigger exists.
|
|
62
|
+
*
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
cancel = () => {
|
|
66
|
+
if (this.#running && this.#reschedule) this.#reschedule = false;
|
|
67
|
+
if (this.#timeoutInstance) {
|
|
68
|
+
clearTimeout(this.#timeoutInstance);
|
|
69
|
+
this.#timeoutInstance = undefined;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
#realSchedule = () => {
|
|
73
|
+
this.#timeoutInstance = setTimeout(this.#doTrigger, this.#delayMs);
|
|
74
|
+
};
|
|
75
|
+
/** Only called by setTimeout() */
|
|
76
|
+
#doTrigger = () => {
|
|
77
|
+
this.#timeoutInstance = undefined;
|
|
78
|
+
this.#running = true;
|
|
79
|
+
const res = this.#triggerFunction();
|
|
80
|
+
if (res instanceof Promise) {
|
|
81
|
+
// We're waiting a promise
|
|
82
|
+
res
|
|
83
|
+
// eslint-disable-next-line promise/prefer-await-to-then
|
|
84
|
+
.then(() => {
|
|
85
|
+
this.#running = false;
|
|
86
|
+
// eslint-disable-next-line promise/always-return
|
|
87
|
+
if (this.#reschedule) {
|
|
88
|
+
this.#reschedule = false;
|
|
89
|
+
this.#realSchedule();
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
// eslint-disable-next-line promise/prefer-await-to-then
|
|
93
|
+
.catch(error => {
|
|
94
|
+
// eslint-disable-next-line no-console
|
|
95
|
+
console.error("An error occurred during an async trigger:", error);
|
|
96
|
+
});
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
// Not a promise
|
|
100
|
+
this.#running = false;
|
|
101
|
+
if (this.#reschedule) {
|
|
102
|
+
this.#reschedule = false;
|
|
103
|
+
this.#realSchedule();
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
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 no-console */
|
|
17
|
+
// Heavily based on https://stackoverflow.com/a/34637436/2059163
|
|
18
|
+
/**
|
|
19
|
+
* Allows returning a promise and resolving it in another scope
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export class DeferredPromise {
|
|
24
|
+
/** The actual promise that can be awaited */
|
|
25
|
+
promise;
|
|
26
|
+
#resolveFunc;
|
|
27
|
+
#rejectFunc;
|
|
28
|
+
#resolved = false;
|
|
29
|
+
constructor() {
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
31
|
+
let resolveFunc;
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
33
|
+
let rejectFunc;
|
|
34
|
+
// eslint-disable-next-line promise/avoid-new
|
|
35
|
+
this.promise = new Promise((resolve, reject) => {
|
|
36
|
+
resolveFunc = resolve;
|
|
37
|
+
rejectFunc = reject;
|
|
38
|
+
});
|
|
39
|
+
if (!resolveFunc || !rejectFunc) throw new Error("Unexpected state");
|
|
40
|
+
this.#resolveFunc = resolveFunc;
|
|
41
|
+
this.#rejectFunc = rejectFunc;
|
|
42
|
+
}
|
|
43
|
+
/** Indicate if the promise already resolved or rejected. */
|
|
44
|
+
get resolved() {
|
|
45
|
+
return this.#resolved;
|
|
46
|
+
}
|
|
47
|
+
/** Can be called from anywhere; will resolve the promise. */
|
|
48
|
+
resolve = value => {
|
|
49
|
+
if (this.#resolved) {
|
|
50
|
+
console.warn("Promise resolved multiple time; ignoring");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
this.#resolved = true;
|
|
54
|
+
this.#resolveFunc(value);
|
|
55
|
+
};
|
|
56
|
+
/** Can be called from anywhere; will reject the promise. */
|
|
57
|
+
reject = reason => {
|
|
58
|
+
if (this.#resolved) {
|
|
59
|
+
console.warn("Promise resolved multiple time; ignoring");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.#resolved = true;
|
|
63
|
+
this.#rejectFunc(reason);
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
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 { Awaitable } from "../types/types.js";
|
|
17
|
+
/**
|
|
18
|
+
* Function used to fill a cache entry on a cache miss.
|
|
19
|
+
*
|
|
20
|
+
* @param key - The requested key
|
|
21
|
+
*
|
|
22
|
+
* @returns
|
|
23
|
+
* The requested entry, or undefined if the value does not exist.
|
|
24
|
+
*/
|
|
25
|
+
export type CacheGetFunc<ValueType> = (key: string) => Awaitable<ValueType | undefined>;
|
|
26
|
+
/**
|
|
27
|
+
* Basic key-value cache mechanism.
|
|
28
|
+
*
|
|
29
|
+
* Discards old data based on last access.
|
|
30
|
+
* Cache entries can be fetched asynchronously; multiple fetch on the same value will share the same
|
|
31
|
+
* promise.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export default class KeyCache<ValueType = string> {
|
|
36
|
+
#private;
|
|
37
|
+
/**
|
|
38
|
+
* Constructor.
|
|
39
|
+
*
|
|
40
|
+
* @param maxEntries - Maximum number of entries kept. Once more entries are added, oldest entries
|
|
41
|
+
* are discarded based on last access time.
|
|
42
|
+
*
|
|
43
|
+
* @param getFunc - A function to fetch missing cache entries.
|
|
44
|
+
* If the function returns undefined, the value is not cached. If the function throws, the error
|
|
45
|
+
* is kept (and requesting this entry again will return the rejected promise.
|
|
46
|
+
*/
|
|
47
|
+
constructor(maxEntries?: number, getFunc?: CacheGetFunc<ValueType>);
|
|
48
|
+
get cache(): Record<string, Promise<ValueType | undefined>>;
|
|
49
|
+
get keys(): Array<string>;
|
|
50
|
+
/** Return a cache entry if present. */
|
|
51
|
+
get: (key: string) => Promise<ValueType | undefined>;
|
|
52
|
+
/** Manually set a cache entry. */
|
|
53
|
+
set: (key: string, value: Awaitable<ValueType>) => this;
|
|
54
|
+
/** Remove a stale entry. */
|
|
55
|
+
remove: (key: string) => this;
|
|
56
|
+
}
|