Skip to content

Commit 0d48b01

Browse files
committed
Work on chunk hashing
1 parent 7d32028 commit 0d48b01

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

packages/idb-cache/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ export class IDBCache implements AsyncStorage {
302302
await this.ensureWorkerInitialized();
303303

304304
const db = await this.dbReadyPromise;
305-
const baseKey = await deterministicUUID(this.cacheKey, itemKey);
305+
const baseKey = await deterministicUUID(`${this.cacheKey}:${itemKey}`);
306306
const now = Date.now();
307307

308308
const chunkKeys = await getAllChunkKeysForBaseKey(
@@ -397,7 +397,7 @@ export class IDBCache implements AsyncStorage {
397397
await this.ensureWorkerInitialized();
398398

399399
const db = await this.dbReadyPromise;
400-
const baseKey = await deterministicUUID(this.cacheKey, itemKey);
400+
const baseKey = await deterministicUUID(`${this.cacheKey}:${itemKey}`);
401401
const expirationTimestamp = Date.now() + this.gcTime;
402402

403403
const existingChunkKeys = await getAllChunkKeysForBaseKey(
@@ -516,7 +516,7 @@ export class IDBCache implements AsyncStorage {
516516
async removeItem(itemKey: string): Promise<void> {
517517
try {
518518
const db = await this.dbReadyPromise;
519-
const baseKey = await deterministicUUID(this.cacheKey, itemKey);
519+
const baseKey = await deterministicUUID(`${this.cacheKey}:${itemKey}`);
520520

521521
const chunkKeys = await getAllChunkKeysForBaseKey(
522522
db,

packages/idb-cache/src/utils.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@ import type { IDBCacheSchema, STORE } from "./types";
99

1010
const uuidCache = new Map<string, string>();
1111

12+
export function generateUUIDFromHash(hashHex: string): string {
13+
return [
14+
hashHex.slice(0, 8),
15+
hashHex.slice(8, 12),
16+
`4${hashHex.slice(13, 16)}`,
17+
((Number.parseInt(hashHex.slice(16, 17), 16) & 0x3) | 0x8).toString(16) +
18+
hashHex.slice(17, 20),
19+
hashHex.slice(20, 32),
20+
].join("-");
21+
}
22+
1223
/**
1324
* Generates a deterministic UUID based on SHA-512 hash.
1425
* @param cacheKey - The cache key.
1526
* @param itemKey - The item key.
1627
* @returns A deterministic UUID string.
1728
*/
18-
export async function deterministicUUID(
19-
cacheKey: string,
20-
itemKey: string
21-
): Promise<string> {
22-
const key = `${cacheKey}:${itemKey}`;
23-
29+
export async function deterministicUUID(key: string): Promise<string> {
2430
if (uuidCache.has(key)) {
2531
const uuid = uuidCache.get(key);
2632
if (typeof uuid === "string") {
@@ -36,14 +42,7 @@ export async function deterministicUUID(
3642
.map((b) => b.toString(16).padStart(2, "0"))
3743
.join("");
3844

39-
const uuid = [
40-
hashHex.slice(0, 8),
41-
hashHex.slice(8, 12),
42-
`4${hashHex.slice(13, 16)}`,
43-
((Number.parseInt(hashHex.slice(16, 17), 16) & 0x3) | 0x8).toString(16) +
44-
hashHex.slice(17, 20),
45-
hashHex.slice(20, 32),
46-
].join("-");
45+
const uuid = generateUUIDFromHash(hashHex);
4746
uuidCache.set(key, uuid);
4847
return uuid;
4948
}
@@ -67,7 +66,7 @@ export async function computeChunkHash(
6766
const hashHex = hashArray
6867
.map((b) => b.toString(16).padStart(2, "0"))
6968
.join("");
70-
return hashHex;
69+
return generateUUIDFromHash(hashHex);
7170
}
7271

7372
export function parseChunkIndexFromKey(chunkKey: string): number {

0 commit comments

Comments
 (0)