Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache fixes #3871

Merged
merged 8 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/cache/memory-cache-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ class MemoryCacheStore {
this.#maxCount = opts.maxCount
}

if (opts.maxSize !== undefined) {
if (
typeof opts.maxSize !== 'number' ||
!Number.isInteger(opts.maxSize) ||
opts.maxSize < 0
) {
throw new TypeError('MemoryCacheStore options.maxSize must be a non-negative integer')
}
this.#maxSize = opts.maxSize
}

if (opts.maxEntrySize !== undefined) {
if (
typeof opts.maxEntrySize !== 'number' ||
Expand Down Expand Up @@ -126,7 +137,7 @@ class MemoryCacheStore {
store.#size += entry.size
store.#count += 1

if (store.#size > store.#maxEntrySize || store.#count > store.#maxCount) {
if (store.#size > store.#maxSize || store.#count > store.#maxCount) {
for (const [key, entries] of store.#entries) {
for (const entry of entries.splice(0, entries.length / 2)) {
store.#size -= entry.size
Expand Down
21 changes: 5 additions & 16 deletions lib/handler/cache-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,11 @@ class CacheHandler extends DecoratorHandler {
const headers = util.parseHeaders(parsedRawHeaders)

const cacheControlHeader = headers['cache-control']
const isCacheFull = typeof this.#store.isFull !== 'undefined'
? this.#store.isFull
: false

if (
!cacheControlHeader ||
isCacheFull
) {
if (!cacheControlHeader) {
// Don't have the cache control header or the cache is full
return downstreamOnHeaders()
}

const cacheControlDirectives = parseCacheControlHeader(cacheControlHeader)
if (!canCacheResponse(statusCode, headers, cacheControlDirectives)) {
return downstreamOnHeaders()
Expand Down Expand Up @@ -236,10 +230,7 @@ class CacheHandler extends DecoratorHandler {
* @param {import('../util/cache.js').CacheControlDirectives} cacheControlDirectives
*/
function canCacheResponse (statusCode, headers, cacheControlDirectives) {
if (
statusCode !== 200 &&
statusCode !== 307
) {
if (statusCode !== 200 && statusCode !== 307) {
return false
}

Expand Down Expand Up @@ -309,7 +300,7 @@ function determineStaleAt (now, headers, cacheControlDirectives) {
if (headers.expire && typeof headers.expire === 'string') {
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.3
const expiresDate = new Date(headers.expire)
if (expiresDate instanceof Date && !isNaN(expiresDate)) {
if (expiresDate instanceof Date && Number.isFinite(expiresDate.valueOf())) {
return now + (Date.now() - expiresDate.getTime())
}
}
Expand Down Expand Up @@ -391,9 +382,7 @@ function stripNecessaryHeaders (rawHeaders, parsedRawHeaders, cacheControlDirect
strippedHeaders.length -= offset
}

return strippedHeaders
? util.encodeRawHeaders(strippedHeaders)
: rawHeaders
return strippedHeaders ? util.encodeRawHeaders(strippedHeaders) : rawHeaders
}

module.exports = CacheHandler
7 changes: 6 additions & 1 deletion types/cache-interceptor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare namespace CacheHandler {
export type CacheMethods = 'GET' | 'HEAD' | 'OPTIONS' | 'TRACE'

export interface CacheOptions {
store?: CacheStore
store: CacheStore
ronag marked this conversation as resolved.
Show resolved Hide resolved
mcollina marked this conversation as resolved.
Show resolved Hide resolved

/**
* The methods to cache
Expand Down Expand Up @@ -75,6 +75,11 @@ declare namespace CacheHandler {
*/
maxSize?: number

/**
* @default Infinity
*/
maxEntrySize?: number

errorCallback?: (err: Error) => void
}

Expand Down
Loading