@@ -9,18 +9,24 @@ import type { IDBCacheSchema, STORE } from "./types";
99
1010const 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
7372export function parseChunkIndexFromKey ( chunkKey : string ) : number {
0 commit comments