Skip to content

Commit 88d99e5

Browse files
authored
Revert nowAbsolute, add regression test (#3850)
Signed-off-by: Matteo Collina <[email protected]>
1 parent b93a834 commit 88d99e5

File tree

9 files changed

+69
-70
lines changed

9 files changed

+69
-70
lines changed

benchmarks/timers/now-absolute.mjs

-13
This file was deleted.

lib/cache/memory-cache-store.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const { Writable } = require('node:stream')
4-
const { nowAbsolute } = require('../util/timers.js')
54

65
/**
76
* @typedef {import('../../types/cache-interceptor.d.ts').default.CacheKey} CacheKey
@@ -77,7 +76,7 @@ class MemoryCacheStore {
7776

7877
const topLevelKey = `${key.origin}:${key.path}`
7978

80-
const now = nowAbsolute()
79+
const now = Date.now()
8180
const entry = this.#entries.get(topLevelKey)?.find((entry) => (
8281
entry.deleteAt > now &&
8382
entry.method === key.method &&

lib/handler/cache-handler.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
parseVaryHeader,
88
isEtagUsable
99
} = require('../util/cache')
10-
const { nowAbsolute } = require('../util/timers.js')
1110

1211
function noop () {}
1312

@@ -123,7 +122,7 @@ class CacheHandler extends DecoratorHandler {
123122
return downstreamOnHeaders()
124123
}
125124

126-
const now = nowAbsolute()
125+
const now = Date.now()
127126
const staleAt = determineStaleAt(now, headers, cacheControlDirectives)
128127
if (staleAt) {
129128
const varyDirectives = this.#cacheKey.headers && headers.vary
@@ -311,7 +310,7 @@ function determineStaleAt (now, headers, cacheControlDirectives) {
311310
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.3
312311
const expiresDate = new Date(headers.expire)
313312
if (expiresDate instanceof Date && !isNaN(expiresDate)) {
314-
return now + (nowAbsolute() - expiresDate.getTime())
313+
return now + (Date.now() - expiresDate.getTime())
315314
}
316315
}
317316

lib/interceptor/cache.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const CacheHandler = require('../handler/cache-handler')
77
const MemoryCacheStore = require('../cache/memory-cache-store')
88
const CacheRevalidationHandler = require('../handler/cache-revalidation-handler')
99
const { assertCacheStore, assertCacheMethods, makeCacheKey, parseCacheControlHeader } = require('../util/cache.js')
10-
const { nowAbsolute } = require('../util/timers.js')
1110

1211
const AGE_HEADER = Buffer.from('age')
1312

@@ -56,7 +55,7 @@ function needsRevalidation (result, age, cacheControlDirectives) {
5655
return true
5756
}
5857

59-
const now = nowAbsolute()
58+
const now = Date.now()
6059
if (now > result.staleAt) {
6160
// Response is stale
6261
if (cacheControlDirectives?.['max-stale']) {
@@ -186,6 +185,7 @@ module.exports = (opts = {}) => {
186185
if (typeof handler.onHeaders === 'function') {
187186
// Add the age header
188187
// https://www.rfc-editor.org/rfc/rfc9111.html#name-age
188+
const age = Math.round((Date.now() - result.cachedAt) / 1000)
189189

190190
// TODO (fix): What if rawHeaders already contains age header?
191191
rawHeaders = [...rawHeaders, AGE_HEADER, Buffer.from(`${age}`)]
@@ -216,7 +216,7 @@ module.exports = (opts = {}) => {
216216
throw new Error('stream is undefined but method isn\'t HEAD')
217217
}
218218

219-
const age = Math.round((nowAbsolute() - result.cachedAt) / 1000)
219+
const age = Math.round((Date.now() - result.cachedAt) / 1000)
220220
if (requestCacheControl?.['max-age'] && age >= requestCacheControl['max-age']) {
221221
// Response is considered expired for this specific request
222222
// https://www.rfc-editor.org/rfc/rfc9111.html#section-5.2.1.1

lib/util/timers.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@
2121
*/
2222
let fastNow = 0
2323

24-
/**
25-
* The fastNowAbsolute variable contains the rough result of Date.now()
26-
*
27-
* @type {number}
28-
*/
29-
let fastNowAbsolute = Date.now()
30-
3124
/**
3225
* RESOLUTION_MS represents the target resolution time in milliseconds.
3326
*
@@ -129,8 +122,6 @@ function onTick () {
129122
*/
130123
fastNow += TICK_MS
131124

132-
fastNowAbsolute = Date.now()
133-
134125
/**
135126
* The `idx` variable is used to iterate over the `fastTimers` array.
136127
* Expired timers are removed by replacing them with the last element in the array.
@@ -399,9 +390,6 @@ module.exports = {
399390
now () {
400391
return fastNow
401392
},
402-
nowAbsolute () {
403-
return fastNowAbsolute
404-
},
405393
/**
406394
* Trigger the onTick function to process the fastTimers array.
407395
* Exported for testing purposes only.
@@ -431,11 +419,5 @@ module.exports = {
431419
* Marking as deprecated to discourage any use outside of testing.
432420
* @deprecated
433421
*/
434-
kFastTimer,
435-
/**
436-
* Exporting for testing purposes only.
437-
* Marking as deprecated to discourage any use outside of testing.
438-
* @deprecated
439-
*/
440-
RESOLUTION_MS
422+
kFastTimer
441423
}

test/cache-interceptor/cache-stores.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const { deepStrictEqual, notEqual, equal } = require('node:assert')
55
const { Readable } = require('node:stream')
66
const { once } = require('node:events')
77
const MemoryCacheStore = require('../../lib/cache/memory-cache-store')
8-
const { nowAbsolute } = require('../../lib/util/timers.js')
98

109
cacheStoreTests(MemoryCacheStore)
1110

@@ -33,9 +32,9 @@ function cacheStoreTests (CacheStore) {
3332
statusCode: 200,
3433
statusMessage: '',
3534
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
36-
cachedAt: nowAbsolute(),
37-
staleAt: nowAbsolute() + 10000,
38-
deleteAt: nowAbsolute() + 20000
35+
cachedAt: Date.now(),
36+
staleAt: Date.now() + 10000,
37+
deleteAt: Date.now() + 20000
3938
}
4039
const requestBody = ['asd', '123']
4140

@@ -73,9 +72,9 @@ function cacheStoreTests (CacheStore) {
7372
statusCode: 200,
7473
statusMessage: '',
7574
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
76-
cachedAt: nowAbsolute(),
77-
staleAt: nowAbsolute() + 10000,
78-
deleteAt: nowAbsolute() + 20000
75+
cachedAt: Date.now(),
76+
staleAt: Date.now() + 10000,
77+
deleteAt: Date.now() + 20000
7978
}
8079
const anotherBody = ['asd2', '1234']
8180

@@ -111,9 +110,9 @@ function cacheStoreTests (CacheStore) {
111110
statusCode: 200,
112111
statusMessage: '',
113112
rawHeaders: [Buffer.from('1'), Buffer.from('2'), Buffer.from('3')],
114-
cachedAt: nowAbsolute() - 10000,
115-
staleAt: nowAbsolute() - 1,
116-
deleteAt: nowAbsolute() + 20000
113+
cachedAt: Date.now() - 10000,
114+
staleAt: Date.now() - 1,
115+
deleteAt: Date.now() + 20000
117116
}
118117
const requestBody = ['part1', 'part2']
119118

@@ -144,9 +143,9 @@ function cacheStoreTests (CacheStore) {
144143
const requestValue = {
145144
statusCode: 200,
146145
statusMessage: '',
147-
cachedAt: nowAbsolute() - 20000,
148-
staleAt: nowAbsolute() - 10000,
149-
deleteAt: nowAbsolute() - 5
146+
cachedAt: Date.now() - 20000,
147+
staleAt: Date.now() - 10000,
148+
deleteAt: Date.now() - 5
150149
}
151150
const requestBody = ['part1', 'part2']
152151

@@ -178,9 +177,9 @@ function cacheStoreTests (CacheStore) {
178177
vary: {
179178
'some-header': 'hello world'
180179
},
181-
cachedAt: nowAbsolute(),
182-
staleAt: nowAbsolute() + 10000,
183-
deleteAt: nowAbsolute() + 20000
180+
cachedAt: Date.now(),
181+
staleAt: Date.now() + 10000,
182+
deleteAt: Date.now() + 20000
184183
}
185184
const requestBody = ['part1', 'part2']
186185

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
3+
const { test, after } = require('node:test')
4+
const { strictEqual } = require('node:assert')
5+
const { createServer } = require('node:http')
6+
const { once } = require('node:events')
7+
const { request, Client, interceptors } = require('../../index')
8+
const { setTimeout: sleep } = require('timers/promises')
9+
10+
test('revalidates the request when the response is stale', async () => {
11+
let count = 0
12+
const server = createServer((req, res) => {
13+
res.setHeader('Cache-Control', 'public, max-age=1')
14+
res.end('hello world ' + count++)
15+
})
16+
17+
server.listen(0)
18+
await once(server, 'listening')
19+
20+
const dispatcher = new Client(`http://localhost:${server.address().port}`)
21+
.compose(interceptors.cache())
22+
23+
after(async () => {
24+
server.close()
25+
await dispatcher.close()
26+
})
27+
28+
const url = `http://localhost:${server.address().port}`
29+
30+
{
31+
const res = await request(url, { dispatcher })
32+
strictEqual(await res.body.text(), 'hello world 0')
33+
}
34+
35+
{
36+
const res = await request(url, { dispatcher })
37+
strictEqual(await res.body.text(), 'hello world 0')
38+
}
39+
40+
await sleep(1000)
41+
42+
{
43+
const res = await request(url, { dispatcher })
44+
45+
strictEqual(await res.body.text(), 'hello world 1')
46+
}
47+
})

test/interceptors/cache.js

-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ describe('Cache Interceptor', () => {
161161
const clock = FakeTimers.install({
162162
shouldClearNativeTimers: true
163163
})
164-
tick(0)
165164

166165
const server = createServer((req, res) => {
167166
res.setHeader('cache-control', 'public, s-maxage=1, stale-while-revalidate=10')
@@ -207,7 +206,6 @@ describe('Cache Interceptor', () => {
207206
strictEqual(await response.body.text(), 'asd')
208207

209208
clock.tick(1500)
210-
tick(1500)
211209

212210
// Now we send two more requests. Both of these should reach the origin,
213211
// but now with a conditional header asking if the resource has been

test/timers.js

-12
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,4 @@ describe('timers', () => {
255255

256256
await t.completed
257257
})
258-
259-
test('nowAbsolute', (t) => {
260-
t = tspl(t, { plan: 1 })
261-
262-
const actualNow = Date.now()
263-
264-
const lowerBound = actualNow - timers.RESOLUTION_MS
265-
const upperBound = actualNow + timers.RESOLUTION_MS
266-
const fastNowAbsolute = timers.nowAbsolute()
267-
268-
t.equal(fastNowAbsolute >= lowerBound && fastNowAbsolute <= upperBound, true)
269-
})
270258
})

0 commit comments

Comments
 (0)