Skip to content

Commit 6f2be0c

Browse files
committed
feature: support optionality of cacheKey and cacheBuster
1 parent af2dc21 commit 6f2be0c

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

packages/idb-cache/src/encryptionWorkerFn.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,6 @@ export function encryptionWorkerFunction() {
229229
}
230230
cacheKey = new TextEncoder().encode(incomingCacheKey);
231231
pbkdf2Iterations = incomingIterations || 100000;
232-
if (!cacheBuster) {
233-
cacheBuster = crypto.randomUUID();
234-
}
235232
fixedSalt = new TextEncoder().encode(cacheBuster).buffer;
236233
initializeKey(port).catch((error) => {
237234
console.error("Worker: Initialization failed:", error);

packages/idb-cache/src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ export interface IDBCacheConfig {
4545
/**
4646
* Sensitive identifier used for securely encrypting data.
4747
*/
48-
cacheKey: string;
48+
cacheKey?: string;
4949
/**
5050
* Unique value (not sensitive) used to invalidate old cache entries.
5151
*/
52-
cacheBuster: string;
52+
cacheBuster?: string;
5353
/**
5454
* Size of each chunk in bytes. When an item exceeds this size,
5555
* it splits into multiple chunks. Defaults to 25000 bytes.
@@ -114,11 +114,11 @@ export class IDBCache implements IDBCacheInterface {
114114
private gcTime: number;
115115
private cleanupIntervalId: number | undefined;
116116

117-
private cacheKey: string;
117+
private cacheKey?: string;
118+
private cacheBuster: string;
118119
private chunkSize: number;
119120
private cleanupInterval: number;
120121
private pbkdf2Iterations: number;
121-
private cacheBuster: string;
122122
private debug: boolean;
123123
private maxTotalChunks?: number;
124124
private priority: "normal" | "low" = "normal";
@@ -139,7 +139,7 @@ export class IDBCache implements IDBCacheInterface {
139139

140140
this.storeName = "cache";
141141
this.cacheKey = cacheKey;
142-
this.cacheBuster = cacheBuster;
142+
this.cacheBuster = cacheBuster || "";
143143
this.debug = debug;
144144
this.gcTime = gcTime;
145145
this.chunkSize = chunkSize;
@@ -189,8 +189,8 @@ export class IDBCache implements IDBCacheInterface {
189189
* @throws {WorkerInitializationError} If the worker fails to initialize.
190190
*/
191191
private async initWorker(
192-
cacheKey: string,
193-
cacheBuster: string
192+
cacheKey?: string,
193+
cacheBuster?: string
194194
): Promise<void> {
195195
if (this.workerReadyPromise) {
196196
return this.workerReadyPromise;
@@ -612,9 +612,6 @@ export class IDBCache implements IDBCacheInterface {
612612
const isLastChunk = chunkIndex === totalChunks - 1;
613613

614614
if (existingChunkKeysSet.has(chunkKey)) {
615-
if (this.priority === "low") {
616-
await waitForAnimationFrame();
617-
}
618615
const existingChunk = await db.get(this.storeName, chunkKey);
619616
if (existingChunk) {
620617
chunksToUpdate.push({
@@ -625,6 +622,9 @@ export class IDBCache implements IDBCacheInterface {
625622
});
626623
}
627624
} else {
625+
if (this.priority === "low") {
626+
await waitForAnimationFrame();
627+
}
628628
const encryptedChunk = await encryptChunk(
629629
this.getPort(),
630630
chunk,

0 commit comments

Comments
 (0)