From d6fd36fe94b7b1d77e91202f0469aaf3c60ee692 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sat, 18 Jan 2025 14:48:00 +0100 Subject: [PATCH] feat: add setter to sqlite cache store --- lib/cache/sqlite-cache-store.js | 94 ++++++++++++++++++++------------- lib/util/cache.js | 4 ++ 2 files changed, 61 insertions(+), 37 deletions(-) diff --git a/lib/cache/sqlite-cache-store.js b/lib/cache/sqlite-cache-store.js index a5afc829413..6f568c4199b 100644 --- a/lib/cache/sqlite-cache-store.js +++ b/lib/cache/sqlite-cache-store.js @@ -251,6 +251,62 @@ module.exports = class SqliteCacheStore { return result } + /** + * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key + * @param {import('../../types/cache-interceptor.d.ts').default.CacheValue & { body: Buffer | null }} value + */ + set (key, value) { + assertCacheKey(key) + assertCacheValue(value) + + const body = value.body + const size = body ? body.byteLength : 0 + + if (size > this.#maxEntrySize) { + return false + } + + const url = this.#makeValueUrl(key) + + const existingValue = this.#findValue(key, true) + if (existingValue) { + // Updating an existing response, let's overwrite it + this.#updateValueQuery.run( + body, + value.deleteAt, + value.statusCode, + value.statusMessage, + value.headers ? JSON.stringify(value.headers) : null, + value.etag ? value.etag : null, + value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null, + value.cachedAt, + value.staleAt, + value.deleteAt, + existingValue.id + ) + } else { + this.#prune() + // New response, let's insert it + this.#insertValueQuery.run( + url, + key.method, + body, + value.deleteAt, + value.statusCode, + value.statusMessage, + value.headers ? JSON.stringify(value.headers) : null, + value.etag ? value.etag : null, + value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null, + value.vary ? JSON.stringify(value.vary) : null, + value.cachedAt, + value.staleAt, + value.deleteAt + ) + } + + return true + } + /** * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key * @param {import('../../types/cache-interceptor.d.ts').default.CacheValue} value @@ -260,7 +316,6 @@ module.exports = class SqliteCacheStore { assertCacheKey(key) assertCacheValue(value) - const url = this.#makeValueUrl(key) let size = 0 /** * @type {Buffer[] | null} @@ -285,42 +340,7 @@ module.exports = class SqliteCacheStore { callback() }, final (callback) { - const existingValue = store.#findValue(key, true) - if (existingValue) { - // Updating an existing response, let's overwrite it - store.#updateValueQuery.run( - Buffer.concat(body), - value.deleteAt, - value.statusCode, - value.statusMessage, - value.headers ? JSON.stringify(value.headers) : null, - value.etag ? value.etag : null, - value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null, - value.cachedAt, - value.staleAt, - value.deleteAt, - existingValue.id - ) - } else { - store.#prune() - // New response, let's insert it - store.#insertValueQuery.run( - url, - key.method, - Buffer.concat(body), - value.deleteAt, - value.statusCode, - value.statusMessage, - value.headers ? JSON.stringify(value.headers) : null, - value.etag ? value.etag : null, - value.cacheControlDirectives ? JSON.stringify(value.cacheControlDirectives) : null, - value.vary ? JSON.stringify(value.vary) : null, - value.cachedAt, - value.staleAt, - value.deleteAt - ) - } - + store.set(key, { ...value, body: Buffer.concat(body) }) callback() } }) diff --git a/lib/util/cache.js b/lib/util/cache.js index 35c53512b2a..bab77eae11f 100644 --- a/lib/util/cache.js +++ b/lib/util/cache.js @@ -90,6 +90,10 @@ function assertCacheValue (value) { if (value.etag !== undefined && typeof value.etag !== 'string') { throw new TypeError(`expected value.etag to be string, got ${typeof value.etag}`) } + + if (value.body !== undefined && !Buffer.isBuffer(value.body)) { + throw new TypeError(`expected value.body to be Buffer, got ${typeof value.body}`) + } } /**