From 72386dfe0246af000cd672f8d36d183a9c151349 Mon Sep 17 00:00:00 2001 From: Fanis Tharropoulos Date: Mon, 18 Nov 2024 10:23:47 +0200 Subject: [PATCH] fix(req-cache): ensure oldest entry is defined before deleting it --- src/Typesense/RequestWithCache.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Typesense/RequestWithCache.ts b/src/Typesense/RequestWithCache.ts index 47ac66af..2ce95d35 100644 --- a/src/Typesense/RequestWithCache.ts +++ b/src/Typesense/RequestWithCache.ts @@ -83,13 +83,17 @@ export default class RequestWithCache { const isCacheOverMaxSize = this.responseCache.size > maxSize; if (isCacheOverMaxSize) { const oldestEntry = this.responseCache.keys().next().value; - this.responseCache.delete(oldestEntry); + if (oldestEntry) { + this.responseCache.delete(oldestEntry); + } } const isResponsePromiseCacheOverMaxSize = this.responsePromiseCache.size > maxSize; if (isResponsePromiseCacheOverMaxSize) { const oldestEntry = this.responsePromiseCache.keys().next().value; - this.responsePromiseCache.delete(oldestEntry); + if (oldestEntry) { + this.responsePromiseCache.delete(oldestEntry); + } } return response as T; }