From 68ecca5cd2e3d269ba999eed2c9805d3faa868a4 Mon Sep 17 00:00:00 2001 From: BlueX888 <140241684+BlueX888@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:19:42 +0800 Subject: [PATCH 1/3] Throw when maxEntrySize is larger than maxSize A per-entry limit larger than the total size can never take effect and only loosens the maxEntrySize guard past maxSize. Reject the contradictory configuration at construction instead of carrying a runtime branch for it. --- src/index.ts | 3 +++ test/basic.test.ts | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/index.ts b/src/index.ts index ecf2cf6..5d4a16b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1486,6 +1486,9 @@ export class LRUCache { this.#max = max this.#maxSize = maxSize this.maxEntrySize = maxEntrySize || this.#maxSize + if (maxSize !== 0 && maxEntrySize !== 0 && maxEntrySize > maxSize) { + throw new TypeError('maxEntrySize cannot be larger than maxSize') + } this.sizeCalculation = sizeCalculation if (this.sizeCalculation) { if (!this.#maxSize && !this.maxEntrySize) { diff --git a/test/basic.test.ts b/test/basic.test.ts index da6c0b7..2193f86 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -190,6 +190,19 @@ test('bad max values', () => { assert(sizeTTL instanceof LRU) }) +test('maxEntrySize larger than maxSize is rejected', () => { + assert.throws(() => new LRU({ maxSize: 100, maxEntrySize: 200 }), { + name: 'TypeError', + message: 'maxEntrySize cannot be larger than maxSize', + }) + assert.throws(() => new LRU({ maxSize: 100, maxEntrySize: 101 }), TypeError) + // equal or smaller per-entry limits remain valid + assert(new LRU({ maxSize: 100, maxEntrySize: 100 }) instanceof LRU) + assert(new LRU({ maxSize: 100, maxEntrySize: 50 }) instanceof LRU) + // an explicit per-entry limit without a total size is still supported + assert(new LRU({ max: 10, maxEntrySize: 50 }) instanceof LRU) +}) + test('setting ttl with non-integer values', () => { assert.throws(() => new LRU({ max: 10, ttl: 10.5 }), TypeError) assert.throws(() => new LRU({ max: 10, ttl: -10 }), TypeError) From 5bd654455bf7f93c1459d0fa5bbbfcab74bd0a15 Mon Sep 17 00:00:00 2001 From: BlueX888 <140241684+BlueX888@users.noreply.github.com> Date: Sat, 5 Dec 2026 14:09:13 +0800 Subject: [PATCH 2/3] test: record tracing snapshot for the constructor guard --- ubq-snapshots/test/tracing.test.ts.snap | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ubq-snapshots/test/tracing.test.ts.snap b/ubq-snapshots/test/tracing.test.ts.snap index bfcbb97..8c9b8e1 100644 --- a/ubq-snapshots/test/tracing.test.ts.snap +++ b/ubq-snapshots/test/tracing.test.ts.snap @@ -465160,6 +465160,14 @@ exports[`max above 2**16 uses a larger index type > traces 1`] = ` [] ` +exports[`maxEntrySize larger than maxSize is rejected > metrics 1`] = ` +[] +` + +exports[`maxEntrySize larger than maxSize is rejected > traces 1`] = ` +[] +` + exports[`maxEntrySize, no maxSize > metrics 1`] = ` [ { From 5748bccd8c5743413a9d0e91cf582b26648b9e82 Mon Sep 17 00:00:00 2001 From: BlueX888 <140241684+BlueX888@users.noreply.github.com> Date: Sat, 5 Dec 2026 14:20:11 +0800 Subject: [PATCH 3/3] fix: validate size options before rejecting maxEntrySize > maxSize --- src/index.ts | 6 +++--- test/basic.test.ts | 5 ++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5d4a16b..beaa61f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1486,9 +1486,6 @@ export class LRUCache { this.#max = max this.#maxSize = maxSize this.maxEntrySize = maxEntrySize || this.#maxSize - if (maxSize !== 0 && maxEntrySize !== 0 && maxEntrySize > maxSize) { - throw new TypeError('maxEntrySize cannot be larger than maxSize') - } this.sizeCalculation = sizeCalculation if (this.sizeCalculation) { if (!this.#maxSize && !this.maxEntrySize) { @@ -1567,6 +1564,9 @@ export class LRUCache { 'maxEntrySize must be a positive integer if specified', ) } + if (this.#maxSize !== 0 && this.maxEntrySize > this.#maxSize) { + throw new TypeError('maxEntrySize cannot be larger than maxSize') + } this.#initializeSizeTracking() } diff --git a/test/basic.test.ts b/test/basic.test.ts index 2193f86..b286dfc 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -195,7 +195,10 @@ test('maxEntrySize larger than maxSize is rejected', () => { name: 'TypeError', message: 'maxEntrySize cannot be larger than maxSize', }) - assert.throws(() => new LRU({ maxSize: 100, maxEntrySize: 101 }), TypeError) + assert.throws( + () => new LRU({ maxSize: 100, maxEntrySize: 101 }), + TypeError, + ) // equal or smaller per-entry limits remain valid assert(new LRU({ maxSize: 100, maxEntrySize: 100 }) instanceof LRU) assert(new LRU({ maxSize: 100, maxEntrySize: 50 }) instanceof LRU)