Skip to content

Commit

Permalink
fix: SqliteStoreValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jan 20, 2025
1 parent 45eaed2 commit 60610df
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions lib/cache/sqlite-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { assertCacheKey, assertCacheValue } = require('../util/cache.js')
let DatabaseSync

const VERSION = 3
const EMPTY_BUFFER = Buffer.alloc(0)

// 2gb
const MAX_ENTRY_SIZE = 2 * 1000 * 1000 * 1000
Expand All @@ -16,10 +17,17 @@ const MAX_ENTRY_SIZE = 2 * 1000 * 1000 * 1000
*
* @typedef {{
* id: Readonly<number>
* headers?: Record<string, string | string[]>
* vary?: string | object
* body: string
* } & import('../../types/cache-interceptor.d.ts').default.CacheValue} SqliteStoreValue
* body?: Buffer
* statusCode: number
* statusMessage: string
* headers?: string
* vary?: string
* etag?: string
* cacheControlDirectives?: string
* cachedAt: number
* staleAt: number
* deleteAt: number
* }} SqliteStoreValue
*/
module.exports = class SqliteCacheStore {
#maxEntrySize = MAX_ENTRY_SIZE
Expand Down Expand Up @@ -234,12 +242,12 @@ module.exports = class SqliteCacheStore {
* @type {import('../../types/cache-interceptor.d.ts').default.GetResult}
*/
const result = {
body: Buffer.from(value.body),
body: value.body ? Buffer.from(value.body) : EMPTY_BUFFER,
statusCode: value.statusCode,
statusMessage: value.statusMessage,
headers: value.headers ? JSON.parse(value.headers) : undefined,
etag: value.etag ? value.etag : undefined,
vary: value.vary ?? undefined,
vary: value.vary ? JSON.parse(value.vary) : undefined,
cacheControlDirectives: value.cacheControlDirectives
? JSON.parse(value.cacheControlDirectives)
: undefined,
Expand Down Expand Up @@ -293,9 +301,9 @@ module.exports = class SqliteCacheStore {
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.headers ?? null,
value.etag ?? null,
value.cacheControlDirectives ?? null,
value.cachedAt,
value.staleAt,
value.deleteAt,
Expand All @@ -311,10 +319,10 @@ module.exports = class SqliteCacheStore {
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.headers ?? null,
value.etag ?? null,
value.cacheControlDirectives ?? null,
value.vary ?? null,
value.cachedAt,
value.staleAt,
value.deleteAt
Expand Down Expand Up @@ -379,7 +387,7 @@ module.exports = class SqliteCacheStore {
/**
* @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} key
* @param {boolean} [canBeExpired=false]
* @returns {(SqliteStoreValue & { vary?: Record<string, string[]> }) | undefined}
* @returns {SqliteStoreValue | undefined}
*/
#findValue (key, canBeExpired = false) {
const url = this.#makeValueUrl(key)
Expand Down Expand Up @@ -407,10 +415,10 @@ module.exports = class SqliteCacheStore {
return undefined
}

value.vary = JSON.parse(value.vary)
const vary = JSON.parse(value.vary)

for (const header in value.vary) {
if (!headerValueEquals(headers[header], value.vary[header])) {
for (const header in vary) {
if (!headerValueEquals(headers[header], vary[header])) {
matches = false
break
}
Expand Down

0 comments on commit 60610df

Please sign in to comment.