From 8450e10020ccb72e35bfa1644ac7b5ed387450af Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 23 Nov 2024 09:41:13 +0100 Subject: [PATCH] fixup --- lib/cache/sqlite-cache-store.js | 37 ++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/cache/sqlite-cache-store.js b/lib/cache/sqlite-cache-store.js index 7bc0de10679..e6254f39975 100644 --- a/lib/cache/sqlite-cache-store.js +++ b/lib/cache/sqlite-cache-store.js @@ -366,14 +366,14 @@ class SqliteCacheStore { */ #findValue (key, canBeExpired = false) { const url = this.#makeValueUrl(key) + const { headers, method } = key /** * @type {SqliteStoreValue[]} */ - const values = this.#getValuesQuery.all(url, key.method) + const values = this.#getValuesQuery.all(url, method) if (values.length === 0) { - // No responses, let's just return early return undefined } @@ -386,16 +386,14 @@ class SqliteCacheStore { let matches = true if (value.vary) { - if (!key.headers) { - // Request doesn't have headers so it can't fulfill the vary - // requirements no matter what, let's return early + if (!headers) { return undefined } - value.vary = JSON.parse(value.vary) + const vary = JSON.parse(value.vary) - for (const header in value.vary) { - if (key.headers[header] !== value.vary[header]) { + for (const header in vary) { + if (headerValueEquals(headers[header], vary[header])) { matches = false break } @@ -411,6 +409,29 @@ class SqliteCacheStore { } } +/** + * @param {string|string[]|null|undefined} lhs + * @param {string|string[]|null|undefined} rhs + * @returns {boolean} + */ +function headerValueEquals (lhs, rhs) { + if (Array.isArray(lhs) && Array.isArray(rhs)) { + if (lhs.length !== rhs.length) { + return false + } + + for (let i = 0; i < lhs.length; i++) { + if (lhs[i] !== rhs[i]) { + return false + } + } + + return true + } + + return lhs === rhs +} + /** * @param {Buffer[]} buffers * @returns {string[]}