Skip to content

Commit

Permalink
feature: support optionality of cacheKey and cacheBuster
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronshaf committed Nov 26, 2024
1 parent af2dc21 commit 6f2be0c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
3 changes: 0 additions & 3 deletions packages/idb-cache/src/encryptionWorkerFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions packages/idb-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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<void> {
if (this.workerReadyPromise) {
return this.workerReadyPromise;
Expand Down Expand Up @@ -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({
Expand All @@ -625,6 +622,9 @@ export class IDBCache implements IDBCacheInterface {
});
}
} else {
if (this.priority === "low") {
await waitForAnimationFrame();
}
const encryptedChunk = await encryptChunk(
this.getPort(),
chunk,
Expand Down

0 comments on commit 6f2be0c

Please sign in to comment.