Skip to content

Commit

Permalink
code review, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
flakey5 committed Dec 18, 2024
1 parent 01fc302 commit 4e0b4b0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/update-cache-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2

- name: Update WPT
- name: Update Cache Tests
run: |
rm -rf test/fixtures/cache-tests && mkdir test/fixtures/cache-tests &&
Expand Down
10 changes: 8 additions & 2 deletions lib/handler/cache-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const HEURISTICALLY_CACHEABLE_STATUS_CODES = [
200, 203, 204, 206, 300, 301, 308, 404, 405, 410, 414, 501
]

const MAX_RESPONSE_AGE = 2147483647000

/**
* @typedef {import('../../types/dispatcher.d.ts').default.DispatchHandler} DispatchHandler
*
Expand Down Expand Up @@ -129,7 +131,7 @@ class CacheHandler {

const now = Date.now()
const resAge = resHeaders.age ? getAge(resHeaders.age) : undefined
if (resAge && resAge >= 2147483647000) {
if (resAge && resAge >= MAX_RESPONSE_AGE) {
// Response considered stale
return downstreamOnHeaders()
}
Expand All @@ -144,9 +146,9 @@ class CacheHandler {
if (staleAt === undefined || (resAge && resAge > staleAt)) {
return downstreamOnHeaders()
}

const baseTime = resDate ? resDate.getTime() : now
const absoluteStaleAt = staleAt + baseTime

if (now >= absoluteStaleAt) {
// Response is already stale
return downstreamOnHeaders()
Expand Down Expand Up @@ -193,6 +195,10 @@ class CacheHandler {
.on('error', function () {
// TODO (fix): Make error somehow observable?
handler.#writeStream = undefined

// Delete the value in case the cache store is holding onto state from
// the call to createWriteStream
handler.#store.delete(handler.#cacheKey)
})
.on('close', function () {
if (handler.#writeStream === this) {
Expand Down

0 comments on commit 4e0b4b0

Please sign in to comment.