diff --git a/src/index.ts b/src/index.ts index ecf2cf6..beaa61f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1564,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 da6c0b7..b286dfc 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -190,6 +190,22 @@ 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) 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`] = ` [ {