From 6f2be0c8a4245581aa54df6876ee5d37d54a63cb Mon Sep 17 00:00:00 2001 From: Aaron Shafovaloff Date: Mon, 25 Nov 2024 20:26:36 -0700 Subject: [PATCH] feature: support optionality of cacheKey and cacheBuster --- packages/idb-cache/src/encryptionWorkerFn.ts | 3 --- packages/idb-cache/src/index.ts | 20 ++++++++++---------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/packages/idb-cache/src/encryptionWorkerFn.ts b/packages/idb-cache/src/encryptionWorkerFn.ts index fccae1e..0196371 100644 --- a/packages/idb-cache/src/encryptionWorkerFn.ts +++ b/packages/idb-cache/src/encryptionWorkerFn.ts @@ -229,9 +229,6 @@ export function encryptionWorkerFunction() { } cacheKey = new TextEncoder().encode(incomingCacheKey); pbkdf2Iterations = incomingIterations || 100000; - if (!cacheBuster) { - cacheBuster = crypto.randomUUID(); - } fixedSalt = new TextEncoder().encode(cacheBuster).buffer; initializeKey(port).catch((error) => { console.error("Worker: Initialization failed:", error); diff --git a/packages/idb-cache/src/index.ts b/packages/idb-cache/src/index.ts index 00a8558..6745eb4 100644 --- a/packages/idb-cache/src/index.ts +++ b/packages/idb-cache/src/index.ts @@ -45,11 +45,11 @@ export interface IDBCacheConfig { /** * Sensitive identifier used for securely encrypting data. */ - cacheKey: string; + cacheKey?: string; /** * Unique value (not sensitive) used to invalidate old cache entries. */ - cacheBuster: string; + cacheBuster?: string; /** * Size of each chunk in bytes. When an item exceeds this size, * it splits into multiple chunks. Defaults to 25000 bytes. @@ -114,11 +114,11 @@ export class IDBCache implements IDBCacheInterface { private gcTime: number; private cleanupIntervalId: number | undefined; - private cacheKey: string; + private cacheKey?: string; + private cacheBuster: string; private chunkSize: number; private cleanupInterval: number; private pbkdf2Iterations: number; - private cacheBuster: string; private debug: boolean; private maxTotalChunks?: number; private priority: "normal" | "low" = "normal"; @@ -139,7 +139,7 @@ export class IDBCache implements IDBCacheInterface { this.storeName = "cache"; this.cacheKey = cacheKey; - this.cacheBuster = cacheBuster; + this.cacheBuster = cacheBuster || ""; this.debug = debug; this.gcTime = gcTime; this.chunkSize = chunkSize; @@ -189,8 +189,8 @@ export class IDBCache implements IDBCacheInterface { * @throws {WorkerInitializationError} If the worker fails to initialize. */ private async initWorker( - cacheKey: string, - cacheBuster: string + cacheKey?: string, + cacheBuster?: string ): Promise { if (this.workerReadyPromise) { return this.workerReadyPromise; @@ -612,9 +612,6 @@ export class IDBCache implements IDBCacheInterface { const isLastChunk = chunkIndex === totalChunks - 1; if (existingChunkKeysSet.has(chunkKey)) { - if (this.priority === "low") { - await waitForAnimationFrame(); - } const existingChunk = await db.get(this.storeName, chunkKey); if (existingChunk) { chunksToUpdate.push({ @@ -625,6 +622,9 @@ export class IDBCache implements IDBCacheInterface { }); } } else { + if (this.priority === "low") { + await waitForAnimationFrame(); + } const encryptedChunk = await encryptChunk( this.getPort(), chunk,