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