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