Skip to content

Commit

Permalink
refuse to setItem when number of chunks for new item exceeds maxTotal…
Browse files Browse the repository at this point in the history
…Chunks config
  • Loading branch information
aaronshaf committed Nov 24, 2024
1 parent 25ffa0a commit e5c1d47
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/idb-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,16 @@ export class IDBCache implements AsyncStorage {
if (!this.dbReadyPromise) return;
await this.ensureWorkerInitialized();

// Check if the new item's chunks would exceed maxTotalChunks config
if (this.maxTotalChunks !== undefined) {
const newItemChunks = Math.ceil(value.length / this.chunkSize);
if (newItemChunks > this.maxTotalChunks) {
throw new IDBCacheError(
`Cannot store item: chunks needed (${newItemChunks}) exceeds maxTotalChunks (${this.maxTotalChunks})`
);
}
}

if (this.priority === "low") {
await waitForAnimationFrame();
}
Expand Down

0 comments on commit e5c1d47

Please sign in to comment.