fix(memory): preserve frozen non-stateful invalidation patterns

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
Rudy Celekli committed 2026-10-07 16:35:12 -04:00
1 parent 097974836a
commit 681f0b488b
2 files changed
+11 -2

No files matched your search

@@ -27,4 +27,12 @@ describe('stateful cache invalidation patterns', () => {
expect(cache.invalidatePattern(pattern)).toBe(1);
expect(pattern.lastIndex).toBe(4);
});
it('accepts a frozen non-global pattern without mutating its cursor', () => {
const cache = new CacheManager<string>();
caches.push(cache);
cache.set('article-one', 'old');
const pattern = Object.freeze(/article/);
expect(cache.invalidatePattern(pattern)).toBe(1);
expect(pattern.lastIndex).toBe(0);
});
});
+3 -2
View File
@@ -297,10 +297,11 @@ export class CacheManager<T = MemoryEntry> extends EventEmitter {
for (const key of this.cache.keys()) {
// RegExp cursors belong to a single input, not the whole key sequence.
const stateful = regex.global || regex.sticky;
const lastIndex = regex.lastIndex;
regex.lastIndex = 0;
if (stateful) regex.lastIndex = 0;
const matches = regex.test(key);
regex.lastIndex = lastIndex;
if (stateful) regex.lastIndex = lastIndex;
if (matches) {
this.delete(key);
invalidated++;