@stryke/hash 0.12.52 → 0.13.0

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 (118) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/index.cjs +20 -19
  3. package/dist/index.d.cts +2 -7
  4. package/dist/index.d.mts +2 -7
  5. package/dist/index.mjs +2 -7
  6. package/dist/neutral-CDggl_ee.cjs +739 -0
  7. package/dist/neutral-D7D-RtyE.mjs +601 -0
  8. package/dist/neutral-D7D-RtyE.mjs.map +1 -0
  9. package/dist/neutral-DqpQs0yN.d.cts +162 -0
  10. package/dist/neutral-DqpQs0yN.d.cts.map +1 -0
  11. package/dist/neutral-DvfYl4mT.d.mts +162 -0
  12. package/dist/neutral-DvfYl4mT.d.mts.map +1 -0
  13. package/dist/neutral.cjs +20 -14
  14. package/dist/neutral.d.cts +2 -5
  15. package/dist/neutral.d.mts +2 -5
  16. package/dist/neutral.mjs +2 -5
  17. package/dist/node.cjs +659 -0
  18. package/dist/node.d.cts +172 -0
  19. package/dist/node.d.cts.map +1 -0
  20. package/dist/node.d.mts +172 -0
  21. package/dist/node.d.mts.map +1 -0
  22. package/dist/node.mjs +613 -0
  23. package/dist/node.mjs.map +1 -0
  24. package/package.json +41 -24
  25. package/dist/_virtual/rolldown_runtime.cjs +0 -29
  26. package/dist/convert/src/array-buffer-to-string.cjs +0 -19
  27. package/dist/convert/src/array-buffer-to-string.mjs +0 -19
  28. package/dist/convert/src/array-buffer-to-string.mjs.map +0 -1
  29. package/dist/convert/src/neutral.cjs +0 -5
  30. package/dist/convert/src/neutral.mjs +0 -7
  31. package/dist/convert/src/parse-type-definition.cjs +0 -1
  32. package/dist/convert/src/parse-type-definition.mjs +0 -3
  33. package/dist/convert/src/string-to-uint8-array.cjs +0 -14
  34. package/dist/convert/src/string-to-uint8-array.mjs +0 -14
  35. package/dist/convert/src/string-to-uint8-array.mjs.map +0 -1
  36. package/dist/convert/src/string-to-utf8-array.cjs +0 -5
  37. package/dist/convert/src/string-to-utf8-array.mjs +0 -6
  38. package/dist/convert/src/string-to-utf8-array.mjs.map +0 -1
  39. package/dist/convert/src/utf8-array-to-string.cjs +0 -5
  40. package/dist/convert/src/utf8-array-to-string.mjs +0 -6
  41. package/dist/convert/src/utf8-array-to-string.mjs.map +0 -1
  42. package/dist/digest.cjs +0 -56
  43. package/dist/digest.d.cts +0 -32
  44. package/dist/digest.d.cts.map +0 -1
  45. package/dist/digest.d.mts +0 -32
  46. package/dist/digest.d.mts.map +0 -1
  47. package/dist/digest.mjs +0 -54
  48. package/dist/digest.mjs.map +0 -1
  49. package/dist/etag.cjs +0 -53
  50. package/dist/etag.d.cts +0 -22
  51. package/dist/etag.d.cts.map +0 -1
  52. package/dist/etag.d.mts +0 -22
  53. package/dist/etag.d.mts.map +0 -1
  54. package/dist/etag.mjs +0 -52
  55. package/dist/etag.mjs.map +0 -1
  56. package/dist/fs/src/list-files.cjs +0 -36
  57. package/dist/fs/src/list-files.d.cts +0 -7
  58. package/dist/fs/src/list-files.d.cts.map +0 -1
  59. package/dist/fs/src/list-files.d.mts +0 -7
  60. package/dist/fs/src/list-files.d.mts.map +0 -1
  61. package/dist/fs/src/list-files.mjs +0 -35
  62. package/dist/fs/src/list-files.mjs.map +0 -1
  63. package/dist/fs/src/read-file.cjs +0 -16
  64. package/dist/fs/src/read-file.mjs +0 -16
  65. package/dist/fs/src/read-file.mjs.map +0 -1
  66. package/dist/hash-files.cjs +0 -41
  67. package/dist/hash-files.d.cts +0 -24
  68. package/dist/hash-files.d.cts.map +0 -1
  69. package/dist/hash-files.d.mts +0 -24
  70. package/dist/hash-files.d.mts.map +0 -1
  71. package/dist/hash-files.mjs +0 -41
  72. package/dist/hash-files.mjs.map +0 -1
  73. package/dist/md5.cjs +0 -17
  74. package/dist/md5.d.cts +0 -12
  75. package/dist/md5.d.cts.map +0 -1
  76. package/dist/md5.d.mts +0 -12
  77. package/dist/md5.d.mts.map +0 -1
  78. package/dist/md5.mjs +0 -17
  79. package/dist/md5.mjs.map +0 -1
  80. package/dist/murmurhash.cjs +0 -22
  81. package/dist/murmurhash.d.cts +0 -23
  82. package/dist/murmurhash.d.cts.map +0 -1
  83. package/dist/murmurhash.d.mts +0 -23
  84. package/dist/murmurhash.d.mts.map +0 -1
  85. package/dist/murmurhash.mjs +0 -22
  86. package/dist/murmurhash.mjs.map +0 -1
  87. package/dist/path/src/is-type.cjs +0 -28
  88. package/dist/path/src/is-type.mjs +0 -29
  89. package/dist/path/src/is-type.mjs.map +0 -1
  90. package/dist/path/src/join-paths.cjs +0 -122
  91. package/dist/path/src/join-paths.mjs +0 -123
  92. package/dist/path/src/join-paths.mjs.map +0 -1
  93. package/dist/path/src/regex.cjs +0 -12
  94. package/dist/path/src/regex.mjs +0 -9
  95. package/dist/path/src/regex.mjs.map +0 -1
  96. package/dist/path/src/slash.cjs +0 -15
  97. package/dist/path/src/slash.mjs +0 -15
  98. package/dist/path/src/slash.mjs.map +0 -1
  99. package/dist/type-checks/src/index.cjs +0 -4
  100. package/dist/type-checks/src/index.mjs +0 -6
  101. package/dist/type-checks/src/is-buffer.cjs +0 -12
  102. package/dist/type-checks/src/is-buffer.mjs +0 -12
  103. package/dist/type-checks/src/is-buffer.mjs.map +0 -1
  104. package/dist/type-checks/src/is-collection.cjs +0 -1
  105. package/dist/type-checks/src/is-collection.mjs +0 -3
  106. package/dist/type-checks/src/is-string.cjs +0 -12
  107. package/dist/type-checks/src/is-string.mjs +0 -12
  108. package/dist/type-checks/src/is-string.mjs.map +0 -1
  109. package/dist/type-checks/src/type-detect.cjs +0 -15
  110. package/dist/type-checks/src/type-detect.mjs +0 -17
  111. package/dist/type-checks/src/type-detect.mjs.map +0 -1
  112. package/dist/xx-hash.cjs +0 -25
  113. package/dist/xx-hash.d.cts +0 -7
  114. package/dist/xx-hash.d.cts.map +0 -1
  115. package/dist/xx-hash.d.mts +0 -7
  116. package/dist/xx-hash.d.mts.map +0 -1
  117. package/dist/xx-hash.mjs +0 -23
  118. package/dist/xx-hash.mjs.map +0 -1
package/dist/node.cjs ADDED
@@ -0,0 +1,659 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+ let defu = require("defu");
29
+ defu = __toESM(defu);
30
+ let glob = require("glob");
31
+ let node_fs_promises = require("node:fs/promises");
32
+ let ohash = require("ohash");
33
+ let js_xxhash = require("js-xxhash");
34
+ let node_crypto = require("node:crypto");
35
+
36
+ //#region ../type-checks/src/is-null.ts
37
+ const isNull = (value) => {
38
+ try {
39
+ return value === null;
40
+ } catch {
41
+ return false;
42
+ }
43
+ };
44
+
45
+ //#endregion
46
+ //#region ../type-checks/src/is-undefined.ts
47
+ const isUndefined = (value) => {
48
+ return value === void 0;
49
+ };
50
+
51
+ //#endregion
52
+ //#region ../type-checks/src/is-empty.ts
53
+ /**
54
+ * Check if the provided value's type is `null` or `undefined`
55
+ *
56
+ * @param value - The value to type check
57
+ * @returns An indicator specifying if the value provided is of type `null` or `undefined`
58
+ */
59
+ const isEmpty = (value) => {
60
+ try {
61
+ return isUndefined(value) || isNull(value);
62
+ } catch {
63
+ return false;
64
+ }
65
+ };
66
+
67
+ //#endregion
68
+ //#region ../type-checks/src/is-buffer.ts
69
+ const isBufferExists = typeof Buffer !== "undefined";
70
+ /**
71
+ * Check if the provided value's type is `Buffer`
72
+ */
73
+ const isBuffer = isBufferExists ? Buffer.isBuffer.bind(Buffer) : function isBuffer$1(value) {
74
+ return false;
75
+ };
76
+
77
+ //#endregion
78
+ //#region ../type-checks/src/type-detect.ts
79
+ const globalObject = ((Obj) => {
80
+ if (typeof globalThis === "object") return globalThis;
81
+ Object.defineProperty(Obj, "typeDetectGlobalObject", {
82
+ get() {
83
+ return this;
84
+ },
85
+ configurable: true
86
+ });
87
+ return globalThis;
88
+ })(Object.prototype);
89
+
90
+ //#endregion
91
+ //#region ../type-checks/src/is-string.ts
92
+ const isString = (value) => {
93
+ try {
94
+ return typeof value === "string";
95
+ } catch {
96
+ return false;
97
+ }
98
+ };
99
+
100
+ //#endregion
101
+ //#region ../type-checks/src/is-set.ts
102
+ /**
103
+ * The inverse of the `isEmpty` function
104
+ *
105
+ * @param value - The value to type check
106
+ * @returns An indicator specifying if the value provided is **NOT** of type `null` or `undefined`
107
+ */
108
+ const isSet = (value) => {
109
+ try {
110
+ return !isEmpty(value);
111
+ } catch {
112
+ return false;
113
+ }
114
+ };
115
+
116
+ //#endregion
117
+ //#region ../type-checks/src/is-set-string.ts
118
+ /**
119
+ * Determine if the type is string and is not empty (length greater than zero)
120
+ *
121
+ * @param value - The value to type check
122
+ * @returns An indicator specifying if the value provided is of type `string` and length greater than zero
123
+ */
124
+ const isSetString = (value) => {
125
+ try {
126
+ return isSet(value) && isString(value) && value.length > 0;
127
+ } catch {
128
+ return false;
129
+ }
130
+ };
131
+
132
+ //#endregion
133
+ //#region src/digest.ts
134
+ /**
135
+ * Creates a new hash object for the specified algorithm.
136
+ *
137
+ * @param algorithm - The algorithm to use for the hash.
138
+ * @returns A new hash object.
139
+ */
140
+ function createHasher(algorithm) {
141
+ return new Hasher(algorithm);
142
+ }
143
+ /**
144
+ * Creates a new hash object for the specified algorithm.
145
+ *
146
+ * @remarks
147
+ * This function uses the Web Crypto API to create a hash of the input data.
148
+ *
149
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
150
+ *
151
+ * @param data - The data to hash.
152
+ * @param algorithm - The algorithm to use for the hash.
153
+ * @returns A hash string representation of the `data` parameter.
154
+ */
155
+ async function digest(data, algorithm = "SHA-512") {
156
+ const encoder = new TextEncoder();
157
+ const arrayBuffer = await globalThis.crypto.subtle.digest(algorithm, isSetString(data) ? encoder.encode(data) : data);
158
+ return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
159
+ }
160
+ /**
161
+ * Alias for {@link digest}.
162
+ */
163
+ const hash = digest;
164
+ /**
165
+ * Hash a string or Uint8Array using SHA-256 and return the result as a base64url-encoded string.
166
+ *
167
+ * @param data - The data to hash.
168
+ * @returns A hash string representation of the `data` parameter.
169
+ */
170
+ const sha256 = async (data) => digest(data, "SHA-256");
171
+ /**
172
+ * Hash a string or Uint8Array using SHA-384 and return the result as a base64url-encoded string.
173
+ *
174
+ * @param data - The data to hash.
175
+ * @returns A hash string representation of the `data` parameter.
176
+ */
177
+ const sha384 = async (data) => digest(data, "SHA-384");
178
+ /**
179
+ * Hash a string or Uint8Array using SHA-512 and return the result as a base64url-encoded string.
180
+ *
181
+ * @param data - The data to hash.
182
+ * @returns A hash string representation of the `data` parameter.
183
+ */
184
+ const sha512 = async (data) => digest(data, "SHA-512");
185
+ var Hasher = class {
186
+ #chunks = [];
187
+ #algorithm;
188
+ constructor(algorithm) {
189
+ this.#algorithm = algorithm;
190
+ }
191
+ update(data) {
192
+ this.#chunks.push(data);
193
+ }
194
+ async digest() {
195
+ const data = new Uint8Array(this.#chunks.reduce((acc, chunk) => acc + chunk.length, 0));
196
+ let offset = 0;
197
+ for (const chunk of this.#chunks) {
198
+ data.set(chunk, offset);
199
+ offset += chunk.length;
200
+ }
201
+ const arrayBuffer = await globalThis.crypto.subtle.digest(this.#algorithm, data);
202
+ return new Uint8Array(arrayBuffer);
203
+ }
204
+ };
205
+
206
+ //#endregion
207
+ //#region src/etag.ts
208
+ /**
209
+ * FNV-1a Hash implementation
210
+ *
211
+ * Ported from https://github.com/tjwebb/fnv-plus/blob/master/index.js
212
+ *
213
+ * @remarks
214
+ * Simplified, optimized and add modified for 52 bit, which provides a larger hash space
215
+ * and still making use of Javascript's 53-bit integer space.
216
+ */
217
+ const fnv1a52 = (str) => {
218
+ const len = str.length;
219
+ let i = 0;
220
+ let t0 = 0;
221
+ let v0 = 8997;
222
+ let t1 = 0;
223
+ let v1 = 33826;
224
+ let t2 = 0;
225
+ let v2 = 40164;
226
+ let t3 = 0;
227
+ let v3 = 52210;
228
+ while (i < len) {
229
+ v0 ^= str.charCodeAt(i++);
230
+ t0 = v0 * 435;
231
+ t1 = v1 * 435;
232
+ t2 = v2 * 435;
233
+ t3 = v3 * 435;
234
+ t2 += v0 << 8;
235
+ t3 += v1 << 8;
236
+ t1 += t0 >>> 16;
237
+ v0 = t0 & 65535;
238
+ t2 += t1 >>> 16;
239
+ v1 = t1 & 65535;
240
+ v3 = t3 + (t2 >>> 16) & 65535;
241
+ v2 = t2 & 65535;
242
+ }
243
+ return (v3 & 15) * 281474976710656 + v2 * 4294967296 + v1 * 65536 + (v0 ^ v3 >> 4);
244
+ };
245
+ /**
246
+ * Generates an ETag for the given payload.
247
+ *
248
+ * @param payload - The payload to generate an ETag for.
249
+ * @param weak - Whether to generate a weak ETag.
250
+ * @returns The generated ETag.
251
+ */
252
+ const generateETag = (payload, weak = false) => {
253
+ return `${(weak ? "W/\"" : "\"") + fnv1a52(payload).toString(36) + payload.length.toString(36)}"`;
254
+ };
255
+
256
+ //#endregion
257
+ //#region ../json/src/canonical.ts
258
+ /**
259
+ * Converts a JavaScript value to a canonical JSON string representation. This function is used for signing JSON objects in a consistent way, ensuring that the same input will always produce the same output string. The canonicalization process includes:
260
+ * - Sorting object keys in lexicographical order.
261
+ * - Removing whitespace and line breaks.
262
+ * - Representing primitive values (null, boolean, number, string) in their standard JSON format.
263
+ * - Recursively applying these rules to nested objects and arrays.
264
+ *
265
+ * This function is designed to produce a deterministic string representation of a JSON value, which is essential for cryptographic signing and verification processes where the exact byte representation of the data must be consistent across different environments and implementations.
266
+ *
267
+ * @param obj - The JavaScript value to convert to a canonical JSON string.
268
+ * @returns A canonical JSON string representation of the input value.
269
+ */
270
+ function canonicalJson(obj) {
271
+ if (obj === null || obj === void 0) return "null";
272
+ if (typeof obj === "boolean" || typeof obj === "number") return JSON.stringify(obj);
273
+ if (typeof obj === "string") return JSON.stringify(obj);
274
+ if (Array.isArray(obj)) return `[${obj.map((item) => canonicalJson(item)).join(",")}]`;
275
+ if (typeof obj === "object") return `{${Object.keys(obj).sort().map((key) => {
276
+ const value = canonicalJson(obj[key]);
277
+ return `${JSON.stringify(key)}:${value}`;
278
+ }).join(",")}}`;
279
+ return "null";
280
+ }
281
+
282
+ //#endregion
283
+ //#region ../path/src/regex.ts
284
+ const DRIVE_LETTER_START_REGEX = /^[A-Z]:\//i;
285
+ const DRIVE_LETTER_REGEX = /^[A-Z]:$/i;
286
+ const UNC_REGEX = /^[/\\]{2}/;
287
+ const ABSOLUTE_PATH_REGEX = /^[/\\](?![/\\])|^[/\\]{2}(?!\.)|^~[/\\]|^[A-Z]:[/\\]/i;
288
+
289
+ //#endregion
290
+ //#region ../path/src/is-type.ts
291
+ /**
292
+ * Check if the path is an absolute path.
293
+ *
294
+ * @param path - The path to check
295
+ * @returns An indicator specifying if the path is an absolute path
296
+ */
297
+ function isAbsolutePath(path) {
298
+ return ABSOLUTE_PATH_REGEX.test(slash(path));
299
+ }
300
+ /**
301
+ * Check if the path is an absolute path.
302
+ *
303
+ * @remarks
304
+ * This is an alias for {@link isAbsolutePath}.
305
+ *
306
+ * @param path - The path to check
307
+ * @returns An indicator specifying if the path is an absolute path
308
+ */
309
+ function isAbsolute(path) {
310
+ return isAbsolutePath(path);
311
+ }
312
+
313
+ //#endregion
314
+ //#region ../path/src/slash.ts
315
+ /**
316
+ * Replace backslash to slash
317
+ *
318
+ * @param path - The string to replace
319
+ * @returns The string with replaced backslashes
320
+ */
321
+ function slash(path) {
322
+ if (path.startsWith("\\\\?\\")) return path;
323
+ return path.replace(/\\/g, "/");
324
+ }
325
+
326
+ //#endregion
327
+ //#region ../path/src/join-paths.ts
328
+ function normalizeWindowsPath(input = "") {
329
+ if (!input) return input;
330
+ return input.replace(/\\/g, "/").replace(DRIVE_LETTER_START_REGEX, (r) => r.toUpperCase());
331
+ }
332
+ function correctPaths(path) {
333
+ if (!path || path.length === 0) return ".";
334
+ path = normalizeWindowsPath(path);
335
+ const isUNCPath = path.match(UNC_REGEX);
336
+ const isPathAbsolute = isAbsolute(path);
337
+ const trailingSeparator = path[path.length - 1] === "/";
338
+ path = normalizeString(path, !isPathAbsolute);
339
+ if (path.length === 0) {
340
+ if (isPathAbsolute) return "/";
341
+ return trailingSeparator ? "./" : ".";
342
+ }
343
+ if (trailingSeparator) path += "/";
344
+ if (DRIVE_LETTER_REGEX.test(path)) path += "/";
345
+ if (isUNCPath) {
346
+ if (!isPathAbsolute) return `//./${path}`;
347
+ return `//${path}`;
348
+ }
349
+ return isPathAbsolute && !isAbsolute(path) ? `/${path}` : path;
350
+ }
351
+ /**
352
+ * Joins all given path segments together using the platform-specific separator as a delimiter.
353
+ *
354
+ * @remarks
355
+ * Multiple segments can be provided as separate arguments. The resulting path is normalized to remove any redundant or unnecessary segments.
356
+ *
357
+ * @example
358
+ * ```ts
359
+ * import { joinPaths } from 'stryke/path';
360
+ *
361
+ * const fullPath = joinPaths('folder1', 'folder2', '..', 'folder3', 'file.txt');
362
+ * console.log(fullPath); // Output: 'folder1/folder3/file.txt'
363
+ *
364
+ * const absolutePath = joinPaths('/root', 'folder', '.', 'subfolder', 'file.txt');
365
+ * console.log(absolutePath); // Output: '/root/folder/subfolder/file.txt'
366
+ *
367
+ * const windowsPath = joinPaths('C:\\', 'Users', 'Public', '..', 'Documents', 'file.txt');
368
+ * console.log(windowsPath); // Output: 'C:/Users/Documents/file.txt'
369
+ *
370
+ * const uncPath = joinPaths('\\\\Server\\Share', 'Folder', 'File.txt');
371
+ * console.log(uncPath); // Output: '//Server/Share/Folder/File.txt'
372
+ * ```
373
+ *
374
+ * @param segments - The path segments to join.
375
+ * @returns The joined and normalized path string.
376
+ */
377
+ function joinPaths(...segments) {
378
+ let result = "";
379
+ for (const segment of segments) if (segment && slash(segment).replaceAll(/\//g, "") !== ".") {
380
+ if (result) if (slash(segment).replaceAll(/\//g, "") === "..") result = slash(result).replace(/\/+$/, "").replace(/\/*[^/]+$/, "");
381
+ else result = `${slash(result).replace(/\/+$/, "")}/${slash(segment).replace(/^\/+/, "")}`;
382
+ else if (slash(segment).replaceAll(/\//g, "") !== "..") result = segment;
383
+ }
384
+ return correctPaths(result);
385
+ }
386
+ /**
387
+ * Resolves a string path, resolving '.' and '.' segments and allowing paths above the root.
388
+ *
389
+ * @param path - The path to normalize.
390
+ * @param allowAboveRoot - Whether to allow the resulting path to be above the root directory.
391
+ * @returns the normalized path string.
392
+ */
393
+ function normalizeString(path, allowAboveRoot) {
394
+ let res = "";
395
+ let lastSegmentLength = 0;
396
+ let lastSlash = -1;
397
+ let dots = 0;
398
+ let char = null;
399
+ for (let index = 0; index <= path.length; ++index) {
400
+ if (index < path.length) char = path[index];
401
+ else if (char === "/") break;
402
+ else char = "/";
403
+ if (char === "/") {
404
+ if (lastSlash === index - 1 || dots === 1) {} else if (dots === 2) {
405
+ if (res.length < 2 || lastSegmentLength !== 2 || res[res.length - 1] !== "." || res[res.length - 2] !== ".") {
406
+ if (res.length > 2) {
407
+ const lastSlashIndex = res.lastIndexOf("/");
408
+ if (lastSlashIndex === -1) {
409
+ res = "";
410
+ lastSegmentLength = 0;
411
+ } else {
412
+ res = res.slice(0, lastSlashIndex);
413
+ lastSegmentLength = res.length - 1 - res.lastIndexOf("/");
414
+ }
415
+ lastSlash = index;
416
+ dots = 0;
417
+ continue;
418
+ } else if (res.length > 0) {
419
+ res = "";
420
+ lastSegmentLength = 0;
421
+ lastSlash = index;
422
+ dots = 0;
423
+ continue;
424
+ }
425
+ }
426
+ if (allowAboveRoot) {
427
+ res += res.length > 0 ? "/.." : "..";
428
+ lastSegmentLength = 2;
429
+ }
430
+ } else {
431
+ if (res.length > 0) res += `/${path.slice(lastSlash + 1, index)}`;
432
+ else res = path.slice(lastSlash + 1, index);
433
+ lastSegmentLength = index - lastSlash - 1;
434
+ }
435
+ lastSlash = index;
436
+ dots = 0;
437
+ } else if (char === "." && dots !== -1) ++dots;
438
+ else dots = -1;
439
+ }
440
+ return res;
441
+ }
442
+
443
+ //#endregion
444
+ //#region ../fs/src/list-files.ts
445
+ const DEFAULT_OPTIONS = { dot: true };
446
+ /**
447
+ * A files and directories listing helper function
448
+ *
449
+ * @param filesGlob - A glob pattern to match files
450
+ * @returns A list of file paths
451
+ */
452
+ async function list(filesGlob, options) {
453
+ return (0, glob.glob)(isString(filesGlob) ? filesGlob.includes("*") ? filesGlob : joinPaths(filesGlob, "**/*") : filesGlob.input ? joinPaths(filesGlob.input, filesGlob.glob) : filesGlob.glob, (0, defu.default)(isString(filesGlob) ? {} : {
454
+ dot: filesGlob.dot,
455
+ ignore: filesGlob.ignore
456
+ }, options ?? {}, DEFAULT_OPTIONS));
457
+ }
458
+ /**
459
+ * A file listing helper function
460
+ *
461
+ * @param filesGlob - A glob pattern to match files
462
+ * @returns A list of file paths
463
+ */
464
+ async function listFiles(filesGlob, options) {
465
+ const result = (await list(filesGlob, (0, defu.default)({ withFileTypes: true }, options ?? {}))).filter((ret) => ret.isFile());
466
+ if (!options?.withFileTypes) return result.map((file) => file.fullpath());
467
+ return result;
468
+ }
469
+
470
+ //#endregion
471
+ //#region ../fs/src/read-file.ts
472
+ /**
473
+ * Read the given content to the given file path
474
+ *
475
+ * @param filePath - The file path to read to
476
+ */
477
+ const readFile = async (filePath) => {
478
+ if (!filePath) throw new Error("No file path provided to read data");
479
+ return (0, node_fs_promises.readFile)(filePath, { encoding: "utf8" });
480
+ };
481
+
482
+ //#endregion
483
+ //#region src/murmurhash.ts
484
+ /**
485
+ * Use a [MurmurHash3](https://en.wikipedia.org/wiki/MurmurHash) based algorithm to hash any JS value into a string.
486
+ *
487
+ * @see https://github.com/ohash/ohash
488
+ * @see https://en.wikipedia.org/wiki/MurmurHash
489
+ *
490
+ * @param content - The value to hash
491
+ * @param options - Hashing options
492
+ * @returns A hashed string value
493
+ */
494
+ function murmurhash(content, options) {
495
+ const result = (0, ohash.hash)(content);
496
+ const maxLength = options?.maxLength ?? 32;
497
+ return result.length > maxLength ? result.slice(0, maxLength) : result;
498
+ }
499
+
500
+ //#endregion
501
+ //#region src/hash-files.ts
502
+ /**
503
+ * Hash a list of file paths into a string based on the file content
504
+ *
505
+ * @param files - The list of file paths to hash
506
+ * @param options - Hashing options
507
+ * @returns A hashed string value
508
+ */
509
+ async function hashFiles(files, options) {
510
+ const result = {};
511
+ await Promise.all(files.map(async (file) => {
512
+ result[file] = await readFile(file);
513
+ }));
514
+ return murmurhash(result, options);
515
+ }
516
+ /**
517
+ * Hash a folder path into a string based on the file content
518
+ *
519
+ * @param directoryPath - The folder path to hash
520
+ * @param options - Hashing options. By default, the `node_modules`, `.git`, `.nx`, `.cache`, and `tmp` folders is ignored.
521
+ * @returns A hashed string value
522
+ */
523
+ async function hashDirectory(directoryPath, options = {}) {
524
+ options.ignore = options.ignore ?? [
525
+ "**/node_modules/**",
526
+ "**/.git/**",
527
+ "**/.nx/**",
528
+ "**/.cache/**",
529
+ "**/.storm/**",
530
+ "**/tmp/**"
531
+ ];
532
+ return hashFiles(await listFiles(directoryPath, options), options);
533
+ }
534
+
535
+ //#endregion
536
+ //#region src/pbkdf2.ts
537
+ /**
538
+ * Hash a password using PBKDF2 (Web Crypto compatible alternative to Argon2) with SHA-256, 100,000 iterations, and a random salt. The resulting hash is formatted as: `$pbkdf2-sha256$iterations$salt$hash`.
539
+ *
540
+ * @remarks
541
+ * This function uses the Web Crypto API to perform password hashing. It generates a random salt for each password, and uses PBKDF2 with SHA-256 and 100,000 iterations to derive a secure hash. The output is a string that includes the algorithm, iteration count, salt, and hash, which can be stored in a database for later verification using the {@link verifyPassword} function.
542
+ *
543
+ * @param password - The password to hash.
544
+ * @returns A promise that resolves to the hashed password string.
545
+ */
546
+ async function hashPassword(password) {
547
+ const encoder = new TextEncoder();
548
+ const salt = crypto.getRandomValues(new Uint8Array(16));
549
+ const keyMaterial = await crypto.subtle.importKey("raw", encoder.encode(password), "PBKDF2", false, ["deriveBits"]);
550
+ const hash$1 = await crypto.subtle.deriveBits({
551
+ name: "PBKDF2",
552
+ salt,
553
+ iterations: 1e5,
554
+ hash: "SHA-256"
555
+ }, keyMaterial, 256);
556
+ return `$pbkdf2-sha256$100000$${btoa(String.fromCharCode(...salt))}$${btoa(String.fromCharCode(...new Uint8Array(hash$1)))}`;
557
+ }
558
+ /**
559
+ * Verify a password against a stored hash in the format produced by {@link hashPassword}.
560
+ *
561
+ * @param password - The password to verify.
562
+ * @param storedHash - The stored hash to verify against.
563
+ * @returns A promise that resolves to true if the password is correct, false otherwise.
564
+ */
565
+ async function verifyPassword(password, storedHash) {
566
+ const parts = storedHash.split("$");
567
+ if (parts.length !== 5 || parts[1] !== "pbkdf2-sha256" || !parts[2] || !parts[3] || !parts[4]) return false;
568
+ const iterations = Number.parseInt(parts[2], 10);
569
+ const salt = Uint8Array.from(atob(parts[3]), (c) => c.charCodeAt(0));
570
+ const expectedHash = parts[4];
571
+ const encoder = new TextEncoder();
572
+ const keyMaterial = await crypto.subtle.importKey("raw", encoder.encode(password), "PBKDF2", false, ["deriveBits"]);
573
+ const hash$1 = await crypto.subtle.deriveBits({
574
+ name: "PBKDF2",
575
+ salt,
576
+ iterations,
577
+ hash: "SHA-256"
578
+ }, keyMaterial, 256);
579
+ return btoa(String.fromCharCode(...new Uint8Array(hash$1))) === expectedHash;
580
+ }
581
+
582
+ //#endregion
583
+ //#region src/xx-hash.ts
584
+ /**
585
+ * xxHash32 only computes 32-bit values. Run it n times with different seeds to
586
+ * get a larger hash with better collision resistance.
587
+ *
588
+ * @param content - The string to hash
589
+ * @param words - The number of 32-bit words to hash
590
+ * @returns A 128-bit hash
591
+ */
592
+ function _xxHash32(content, words) {
593
+ let hash$1 = 0n;
594
+ for (let i = 0; i < words; i++) hash$1 = (hash$1 << 32n) + BigInt((0, js_xxhash.xxHash32)(content, i));
595
+ return hash$1;
596
+ }
597
+ const xxHash32 = (s) => (0, js_xxhash.xxHash32)(s, 0);
598
+ const xxHash64 = (s) => _xxHash32(s, 2);
599
+ const xxHash128 = (s) => _xxHash32(s, 4);
600
+
601
+ //#endregion
602
+ //#region src/hash-content.ts
603
+ /**
604
+ * Hash the content of a PDU (Protocol Data Unit) by removing the `signatures` and `unsigned` fields, then hashing the remaining content using SHA-256 and encoding it as a base64url string. This function is useful for generating a consistent hash of the PDU content that can be used for integrity verification or caching purposes, while ignoring any fields that may change due to signatures or unsigned data.
605
+ *
606
+ * @param content - The PDU content to hash, represented as a record of string keys and unknown values.
607
+ * @returns A promise that resolves to a base64url-encoded string representing the hash of the PDU content.
608
+ */
609
+ async function hashContent(content) {
610
+ const toHash = { ...content };
611
+ delete toHash.signatures;
612
+ delete toHash.unsigned;
613
+ return sha256(canonicalJson(toHash));
614
+ }
615
+ /**
616
+ * Verify the hash of a PDU (Protocol Data Unit) content by hashing the content using the {@link hashContent} function and comparing it to an expected hash value. This function is useful for validating the integrity of the PDU content by ensuring that the computed hash matches the expected hash, which can be used to detect any tampering or corruption of the content.
617
+ *
618
+ * @param content - The PDU content to verify, represented as a record of string keys and unknown values.
619
+ * @param expectedHash - The expected hash value to compare against, represented as a string.
620
+ * @returns A promise that resolves to a boolean indicating whether the computed hash of the content matches the expected hash value (true if they match, false otherwise).
621
+ */
622
+ async function verifyContent(content, expectedHash) {
623
+ return await hashContent(content) === expectedHash;
624
+ }
625
+
626
+ //#endregion
627
+ //#region src/md5.ts
628
+ /**
629
+ * Generate an MD5 hash of the provided content.
630
+ *
631
+ * @param content - The content to hash.
632
+ * @param length - The length of the hash to return.
633
+ * @returns The generated MD5 hash.
634
+ */
635
+ function md5(content, length = 32) {
636
+ return (0, node_crypto.createHash)("md5").update(content).digest("hex").slice(0, length);
637
+ }
638
+
639
+ //#endregion
640
+ exports.Hasher = Hasher;
641
+ exports.createHasher = createHasher;
642
+ exports.digest = digest;
643
+ exports.fnv1a52 = fnv1a52;
644
+ exports.generateETag = generateETag;
645
+ exports.hash = hash;
646
+ exports.hashContent = hashContent;
647
+ exports.hashDirectory = hashDirectory;
648
+ exports.hashFiles = hashFiles;
649
+ exports.hashPassword = hashPassword;
650
+ exports.md5 = md5;
651
+ exports.murmurhash = murmurhash;
652
+ exports.sha256 = sha256;
653
+ exports.sha384 = sha384;
654
+ exports.sha512 = sha512;
655
+ exports.verifyContent = verifyContent;
656
+ exports.verifyPassword = verifyPassword;
657
+ exports.xxHash128 = xxHash128;
658
+ exports.xxHash32 = xxHash32;
659
+ exports.xxHash64 = xxHash64;