From e5c1d4792cf8c891585b4b6c031bf04502add6cc Mon Sep 17 00:00:00 2001 From: Aaron Shafovaloff Date: Sat, 23 Nov 2024 22:42:13 -0700 Subject: [PATCH] refuse to setItem when number of chunks for new item exceeds maxTotalChunks config --- packages/idb-cache/src/index.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/idb-cache/src/index.ts b/packages/idb-cache/src/index.ts index b8f2121..4bd0aaa 100644 --- a/packages/idb-cache/src/index.ts +++ b/packages/idb-cache/src/index.ts @@ -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(); }