@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/lib/json.d.ts ADDED
@@ -0,0 +1,57 @@
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 { TypePredicate } from "./types/types.js";
17
+ /**
18
+ * All the primitive types supported in a JSON file.
19
+ *
20
+ * @public
21
+ */
22
+ export type JSONPrimitiveType = null | string | number | boolean;
23
+ /**
24
+ * All possible value type in a JSON file.
25
+ *
26
+ * @public
27
+ */
28
+ export type JSONValueType = JSONPrimitiveType | JSONArray | JSONObject;
29
+ /**
30
+ * All supported arrays in a JSON files.
31
+ *
32
+ * @public
33
+ */
34
+ export type JSONArray = Array<JSONValueType>;
35
+ /**
36
+ * All supported objects in a JSON files.
37
+ *
38
+ * @public
39
+ */
40
+ export interface JSONObject {
41
+ [key: string]: JSONValueType;
42
+ }
43
+ /**
44
+ * JSON.parse() with typed return.
45
+ *
46
+ * @public
47
+ */
48
+ export declare const jsonParse: <T>(value: unknown, predicate?: TypePredicate<T>) => T;
49
+ /**
50
+ * Do a JSON stringify operation with optional canonization of properties' order.
51
+ *
52
+ * @param order - Sort all properties using their UTF-8 point value.
53
+ * This is roughly alphabetical order when there are no non-ASCII characters.
54
+ *
55
+ * @public
56
+ */
57
+ export declare const jsonStringify: (source: unknown, order?: boolean, prettyIndent?: boolean | number) => string;
package/lib/json.js ADDED
@@ -0,0 +1,116 @@
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 JSON_DEFAULT_INDENT = 2;
17
+ /**
18
+ * JSON.parse() with typed return.
19
+ *
20
+ * @public
21
+ */
22
+ export const jsonParse = (value, predicate) => {
23
+ const res = typeof value === "string" ? JSON.parse(value) : value;
24
+ if (predicate && !predicate(res, { raise: true }))
25
+ throw new Error("Unexpected state");
26
+ return res;
27
+ };
28
+ /** Return the expected indentation step */
29
+ const indentStep = (prettyIndent) => {
30
+ if (prettyIndent === false)
31
+ return 0;
32
+ if (prettyIndent === true)
33
+ return JSON_DEFAULT_INDENT;
34
+ return prettyIndent;
35
+ };
36
+ /** Indent a string */
37
+ const indentStr = (level, value) => {
38
+ const lines = value.split("\n");
39
+ const indentedLines = lines.map((c) => `${" ".repeat(level)}${c}`);
40
+ return indentedLines.join("\n");
41
+ };
42
+ /** Helper for Array.map() */
43
+ const indenter = (level) => (value) => indentStr(level, value);
44
+ const processArray = (source, prettyIndent) => {
45
+ const outputElem = source.map((c) => {
46
+ if (c === undefined)
47
+ return "null";
48
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
49
+ return jsonStringify(c, true, prettyIndent);
50
+ });
51
+ if (outputElem.length === 0)
52
+ return "[]";
53
+ if (prettyIndent === false)
54
+ return `[${outputElem.join(",")}]`;
55
+ return `[\n${outputElem
56
+ .map(indenter(indentStep(prettyIndent)))
57
+ .map((c, id) => (id === outputElem.length - 1 ? `${c}\n` : `${c},\n`))
58
+ .join("")}]`;
59
+ };
60
+ const utf8Sort = (a, b) => {
61
+ const maxLen = Math.max(a.length, b.length);
62
+ for (let cursor = 0; cursor < maxLen; ++cursor) {
63
+ const aCode = a.charCodeAt(cursor);
64
+ const bCode = b.charCodeAt(cursor);
65
+ if (isNaN(aCode) || aCode < bCode)
66
+ return -1;
67
+ if (isNaN(bCode) || aCode > bCode)
68
+ return 1;
69
+ }
70
+ return 0;
71
+ };
72
+ /**
73
+ * Do a JSON stringify operation with optional canonization of properties' order.
74
+ *
75
+ * @param order - Sort all properties using their UTF-8 point value.
76
+ * This is roughly alphabetical order when there are no non-ASCII characters.
77
+ *
78
+ * @public
79
+ */
80
+ export const jsonStringify = (source, order = false, prettyIndent = false) => {
81
+ if (!order) {
82
+ if (prettyIndent) {
83
+ return JSON.stringify(source, null, typeof prettyIndent === "boolean" ? JSON_DEFAULT_INDENT : prettyIndent);
84
+ }
85
+ return JSON.stringify(source);
86
+ }
87
+ if (Array.isArray(source))
88
+ return processArray(source, prettyIndent);
89
+ const sourceType = typeof source;
90
+ if (["string", "number", "boolean"].includes(sourceType) || source === null) {
91
+ return JSON.stringify(source);
92
+ }
93
+ if (sourceType !== "object")
94
+ throw new Error("Invalid object input");
95
+ const casted = source;
96
+ const outputElem = [];
97
+ for (const key of Object.keys(casted).sort(utf8Sort)) {
98
+ const value = casted[key];
99
+ if (value === undefined)
100
+ continue;
101
+ if (prettyIndent === false) {
102
+ outputElem.push(`${JSON.stringify(key)}:${jsonStringify(value, true, prettyIndent)}`);
103
+ }
104
+ else {
105
+ outputElem.push(`${JSON.stringify(key)}: ${jsonStringify(value, true, prettyIndent)}`);
106
+ }
107
+ }
108
+ if (outputElem.length === 0)
109
+ return "{}";
110
+ if (prettyIndent === false)
111
+ return `{${outputElem.join(",")}}`;
112
+ return `{\n${outputElem
113
+ .map(indenter(indentStep(prettyIndent)))
114
+ .map((c, id) => (id === outputElem.length - 1 ? `${c}\n` : `${c},\n`))
115
+ .join("")}}`;
116
+ };
@@ -0,0 +1,51 @@
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
+ * Concatenate values into a buffer
18
+ *
19
+ * @public
20
+ */
21
+ export default class Marshaller {
22
+ #private;
23
+ private constructor();
24
+ static readonly start: (magicNumbers?: string) => Marshaller;
25
+ /** End building the buffer and returns the result */
26
+ readonly end: () => ArrayBuffer;
27
+ /** Append an utf-8 string */
28
+ readonly str: (value: string) => this;
29
+ /** Append a 32-bit unsigned int */
30
+ readonly u32: (value: number) => this;
31
+ /** Append a 8-bit unsigned int */
32
+ readonly u8: (value: number) => this;
33
+ /** Append an arraybuffer */
34
+ readonly buf: (value: ArrayBuffer | Uint8Array) => this;
35
+ /** Append a byte buffer from a hex string */
36
+ readonly hex: (value: string) => this;
37
+ /** Append a Raw IDX (101 bytes) */
38
+ readonly idx: (value: string) => this;
39
+ /** Append an array of strings */
40
+ readonly arrStr: (values: Array<string>) => this;
41
+ /** Append an array of 32bit unsigned numbers */
42
+ readonly arrU32: (values: Array<number>) => this;
43
+ /** Append an array of 8bit unsigned numbers */
44
+ readonly arrU8: (values: Array<number>) => this;
45
+ /** Append an array of buffers */
46
+ readonly arrBuf: (values: Array<ArrayBuffer | Uint8Array>) => this;
47
+ /** Append an array of hexadecimal strings saved as buffers */
48
+ readonly arrHex: (values: Array<string>) => this;
49
+ /** Append an array of IDX strings */
50
+ readonly arrIDX: (values: Array<string>) => this;
51
+ }
@@ -0,0 +1,155 @@
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 { writeUint32LE } from "../bytebuffer.js";
17
+ import { getUint8Array, hex2buf8, str2buf8, utf82buf8 } from "../uint8array.js";
18
+ import { KX_IDX_LEN, UINT32_BYTES } from "./util.js";
19
+ /**
20
+ * Concatenate values into a buffer
21
+ *
22
+ * @public
23
+ */
24
+ export default class Marshaller {
25
+ #values;
26
+ #buffer;
27
+ #magicNumbers;
28
+ constructor(magicNumbers) {
29
+ // Array of Uint8Array
30
+ this.#values = [];
31
+ // Header, in bytes
32
+ if (magicNumbers)
33
+ this.#magicNumbers = str2buf8(magicNumbers);
34
+ }
35
+ static start = (magicNumbers) => new Marshaller(magicNumbers);
36
+ /** End building the buffer and returns the result */
37
+ end = () => this.#getBuffer();
38
+ /** Append an utf-8 string */
39
+ str = (value) => {
40
+ this.#appendData(utf82buf8(value), true);
41
+ return this;
42
+ };
43
+ /** Append a 32-bit unsigned int */
44
+ u32 = (value) => {
45
+ const uintBuffer = new Uint8Array(UINT32_BYTES);
46
+ writeUint32LE(uintBuffer, 0, value);
47
+ this.#appendData(uintBuffer);
48
+ return this;
49
+ };
50
+ /** Append a 8-bit unsigned int */
51
+ u8 = (value) => {
52
+ const uintBuffer = new Uint8Array([value]);
53
+ this.#appendData(uintBuffer);
54
+ return this;
55
+ };
56
+ /** Append an arraybuffer */
57
+ buf = (value) => {
58
+ this.#appendData(getUint8Array(value), true);
59
+ return this;
60
+ };
61
+ /** Append a byte buffer from a hex string */
62
+ hex = (value) => {
63
+ this.buf(hex2buf8(value));
64
+ return this;
65
+ };
66
+ /** Append a Raw IDX (101 bytes) */
67
+ idx = (value) => {
68
+ const idxBuffer = str2buf8(value);
69
+ if (idxBuffer.length !== KX_IDX_LEN)
70
+ throw new Error("Invalid IDX length");
71
+ this.#appendData(idxBuffer);
72
+ return this;
73
+ };
74
+ /** Append an array of strings */
75
+ arrStr = (values) => {
76
+ this.#arrayPrefix(values);
77
+ for (const value of values)
78
+ this.str(value);
79
+ return this;
80
+ };
81
+ /** Append an array of 32bit unsigned numbers */
82
+ arrU32 = (values) => {
83
+ this.#arrayPrefix(values);
84
+ for (const value of values)
85
+ this.u32(value);
86
+ return this;
87
+ };
88
+ /** Append an array of 8bit unsigned numbers */
89
+ arrU8 = (values) => {
90
+ this.#arrayPrefix(values);
91
+ for (const value of values)
92
+ this.u8(value);
93
+ return this;
94
+ };
95
+ /** Append an array of buffers */
96
+ arrBuf = (values) => {
97
+ this.#arrayPrefix(values);
98
+ for (const value of values)
99
+ this.buf(value);
100
+ return this;
101
+ };
102
+ /** Append an array of hexadecimal strings saved as buffers */
103
+ arrHex = (values) => {
104
+ this.#arrayPrefix(values);
105
+ for (const value of values)
106
+ this.hex(value);
107
+ return this;
108
+ };
109
+ /** Append an array of IDX strings */
110
+ arrIDX = (values) => {
111
+ this.#arrayPrefix(values);
112
+ for (const value of values)
113
+ this.idx(value);
114
+ return this;
115
+ };
116
+ /** Write the beginning of an array */
117
+ #arrayPrefix = (values) => {
118
+ this.u32(values.length);
119
+ };
120
+ /** Construct the output buffer with all the previously set values */
121
+ #getBuffer = () => {
122
+ this.#buffer ??= this.#buildBuffer();
123
+ return this.#buffer;
124
+ };
125
+ /** Build the buffer */
126
+ #buildBuffer = () => {
127
+ const dataSize = this.#values.reduce((acc, cur) => {
128
+ const curSize = cur.dataBuffer.byteLength;
129
+ const prefixSize = cur.prefixWithSize ? UINT32_BYTES : 0;
130
+ return acc + curSize + prefixSize;
131
+ }, 0);
132
+ const magicSize = this.#magicNumbers?.length ?? 0;
133
+ const totalSize = magicSize + dataSize;
134
+ this.#buffer = new Uint8Array(totalSize);
135
+ let cursor = 0;
136
+ if (this.#magicNumbers) {
137
+ this.#buffer.set(this.#magicNumbers, cursor);
138
+ cursor += this.#magicNumbers.length;
139
+ }
140
+ for (const valueInfo of this.#values) {
141
+ if (valueInfo.prefixWithSize) {
142
+ writeUint32LE(this.#buffer, cursor, valueInfo.dataBuffer.length);
143
+ cursor += UINT32_BYTES;
144
+ }
145
+ this.#buffer.set(valueInfo.dataBuffer, cursor);
146
+ cursor += valueInfo.dataBuffer.length;
147
+ }
148
+ return this.#buffer;
149
+ };
150
+ /** Internally append a data to the list */
151
+ #appendData = (dataBuffer, prefixWithSize = false) => {
152
+ this.#buffer = undefined;
153
+ this.#values.push({ prefixWithSize, dataBuffer });
154
+ };
155
+ }
@@ -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
+ * Read data from an arraybuffer
18
+ *
19
+ * Expect input in the form of the output of `Marshaller`.
20
+ *
21
+ * @public
22
+ */
23
+ export default class Unmarshaller {
24
+ #private;
25
+ /**
26
+ * @param magicNumber - Expected magic number
27
+ */
28
+ constructor(buffer: ArrayBuffer | Uint8Array, magicNumber?: string);
29
+ /** Read a 32bit unsigned integer */
30
+ u32: () => number;
31
+ /** Read a 8bit unsigned integer */
32
+ u8: () => number;
33
+ /** Read an arraybuffer */
34
+ buf: () => Uint8Array;
35
+ /** Read a string */
36
+ str: () => string;
37
+ /** Read a buffer as an hex string */
38
+ hex: () => string;
39
+ /** Read an array of strings */
40
+ arrStr: () => Array<string>;
41
+ /** Read an array of numbers as unsigned 32bit values */
42
+ arrU32: () => Array<number>;
43
+ /** Read an array of numbers as unsigned 8bit values */
44
+ arrU8: () => Array<number>;
45
+ /** Read an array of buffers */
46
+ arrBuf: () => Array<Uint8Array>;
47
+ /** Read an array of hexadecimal strings stored as buffers */
48
+ arrHex: () => Array<string>;
49
+ /** Read an array of IDX */
50
+ arrIDX: () => Array<string>;
51
+ /** Read an IDX */
52
+ idx: () => string;
53
+ }
@@ -0,0 +1,124 @@
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 { readUint32LE } from "../bytebuffer.js";
17
+ import { buf82hex, buf82str, buf82utf8, getUint8Array } from "../uint8array.js";
18
+ import { KX_IDX_LEN, UINT32_BYTES } from "./util.js";
19
+ /**
20
+ * Read data from an arraybuffer
21
+ *
22
+ * Expect input in the form of the output of `Marshaller`.
23
+ *
24
+ * @public
25
+ */
26
+ export default class Unmarshaller {
27
+ #buffer;
28
+ #offset;
29
+ /**
30
+ * @param magicNumber - Expected magic number
31
+ */
32
+ constructor(buffer, magicNumber) {
33
+ this.#buffer = getUint8Array(buffer);
34
+ this.#offset = 0;
35
+ if (magicNumber) {
36
+ if (this.#buffer.length < magicNumber.length)
37
+ throw new Error("No magic number available");
38
+ const readMagicNumber = buf82str(this.#buffer.slice(0, magicNumber.length));
39
+ if (magicNumber !== readMagicNumber)
40
+ throw new Error("Incorrect magic number");
41
+ this.#offset += magicNumber.length;
42
+ }
43
+ }
44
+ /** Read a 32bit unsigned integer */
45
+ u32 = () => {
46
+ this.#checkSize(UINT32_BYTES);
47
+ const res = readUint32LE(this.#buffer, this.#offset);
48
+ this.#offset += 4;
49
+ return res;
50
+ };
51
+ /** Read a 8bit unsigned integer */
52
+ u8 = () => {
53
+ this.#checkSize(1);
54
+ const res = this.#buffer[this.#offset];
55
+ this.#offset += 1;
56
+ return res;
57
+ };
58
+ /** Read an arraybuffer */
59
+ buf = () => {
60
+ const bufferLength = this.u32();
61
+ this.#checkSize(bufferLength);
62
+ const result = this.#buffer.slice(this.#offset, this.#offset + bufferLength);
63
+ this.#offset += bufferLength;
64
+ return result;
65
+ };
66
+ /** Read a string */
67
+ str = () => buf82utf8(this.buf());
68
+ /** Read a buffer as an hex string */
69
+ hex = () => buf82hex(this.buf());
70
+ /** Read an array of strings */
71
+ arrStr = () => {
72
+ const result = new Array(this.u32());
73
+ for (let index = 0; index < result.length; ++index)
74
+ result[index] = this.str();
75
+ return result;
76
+ };
77
+ /** Read an array of numbers as unsigned 32bit values */
78
+ arrU32 = () => {
79
+ const result = new Array(this.u32());
80
+ for (let index = 0; index < result.length; ++index)
81
+ result[index] = this.u32();
82
+ return result;
83
+ };
84
+ /** Read an array of numbers as unsigned 8bit values */
85
+ arrU8 = () => {
86
+ const result = new Array(this.u32());
87
+ for (let index = 0; index < result.length; ++index)
88
+ result[index] = this.u8();
89
+ return result;
90
+ };
91
+ /** Read an array of buffers */
92
+ arrBuf = () => {
93
+ const result = new Array(this.u32());
94
+ for (let index = 0; index < result.length; ++index)
95
+ result[index] = this.buf();
96
+ return result;
97
+ };
98
+ /** Read an array of hexadecimal strings stored as buffers */
99
+ arrHex = () => {
100
+ const result = new Array(this.u32());
101
+ for (let index = 0; index < result.length; ++index)
102
+ result[index] = this.hex();
103
+ return result;
104
+ };
105
+ /** Read an array of IDX */
106
+ arrIDX = () => {
107
+ const result = new Array(this.u32());
108
+ for (let index = 0; index < result.length; ++index)
109
+ result[index] = this.idx();
110
+ return result;
111
+ };
112
+ /** Read an IDX */
113
+ idx = () => {
114
+ this.#checkSize(KX_IDX_LEN);
115
+ const strBuffer = this.#buffer.slice(this.#offset, this.#offset + KX_IDX_LEN);
116
+ const result = buf82str(strBuffer);
117
+ this.#offset += KX_IDX_LEN;
118
+ return result;
119
+ };
120
+ #checkSize = (dataSize) => {
121
+ if (this.#offset + dataSize > this.#buffer.length)
122
+ throw new Error("Buffer too short");
123
+ };
124
+ }
@@ -0,0 +1,25 @@
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
+ * Length of a legacy IDX
18
+ *
19
+ * Should not be used anymore, as IDX could have variable length in the future.
20
+ *
21
+ * @internal
22
+ */
23
+ export declare const KX_IDX_LEN = 101;
24
+ /** @internal */
25
+ export declare const UINT32_BYTES = 4;
@@ -0,0 +1,25 @@
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
+ * Length of a legacy IDX
18
+ *
19
+ * Should not be used anymore, as IDX could have variable length in the future.
20
+ *
21
+ * @internal
22
+ */
23
+ export const KX_IDX_LEN = 101;
24
+ /** @internal */
25
+ export const UINT32_BYTES = 4;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @license
3
+ * @preserve
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2023 KeeeX SAS
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14
+ *
15
+ */
16
+ /** Round a number value to the given number of decimal places */
17
+ export declare const roundDecimals: (value: number, decimalPlaces: number) => number;
package/lib/number.js ADDED
@@ -0,0 +1,21 @@
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
+ /** Round a number value to the given number of decimal places */
17
+ export const roundDecimals = (value, decimalPlaces) => {
18
+ // eslint-disable-next-line @typescript-eslint/no-magic-numbers
19
+ const multiplier = 10 ** decimalPlaces;
20
+ return Math.round(value * multiplier) / multiplier;
21
+ };
package/lib/path.d.ts ADDED
@@ -0,0 +1,25 @@
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
+ * Sanitize a filename
18
+ *
19
+ * Removes some characters known to cause issues with file names.
20
+ *
21
+ * Does not work on path.
22
+ *
23
+ * @public
24
+ */
25
+ export declare const sanitizeFilename: (filename: string, replacement?: string) => string;