| 1 |
'use strict' |
| 2 |
|
| 3 |
const { test, after, describe } = require('node:test') |
| 4 |
const { strictEqual, notStrictEqual } = require('node:assert') |
| 5 |
const { createServer } = require('node:http') |
| 6 |
const { once } = require('node:events') |
| 7 |
const { Readable } = require('node:stream') |
| 8 |
const { request, Client, interceptors } = require('../../index') |
| 9 |
const MemoryCacheStore = require('../../lib/cache/memory-cache-store') |
| 10 |
const FakeTimers = require('@sinonjs/fake-timers') |
| 11 |
const { setTimeout } = require('node:timers/promises') |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class AsyncCacheStore { |
| 19 |
#inner |
| 20 |
|
| 21 |
constructor () { |
| 22 |
this.#inner = new MemoryCacheStore() |
| 23 |
} |
| 24 |
|
| 25 |
async get (key) { |
| 26 |
const result = this.#inner.get(key) |
| 27 |
if (!result) return undefined |
| 28 |
|
| 29 |
const { body, ...rest } = result |
| 30 |
const readable = new Readable({ read () {} }) |
| 31 |
if (body) { |
| 32 |
for (const chunk of body) { |
| 33 |
readable.push(chunk) |
| 34 |
} |
| 35 |
} |
| 36 |
readable.push(null) |
| 37 |
|
| 38 |
return { ...rest, body: readable } |
| 39 |
} |
| 40 |
|
| 41 |
createWriteStream (key, value) { |
| 42 |
return this.#inner.createWriteStream(key, value) |
| 43 |
} |
| 44 |
|
| 45 |
delete (key) { |
| 46 |
return this.#inner.delete(key) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
describe('cache interceptor with async store', () => { |
| 51 |
test('stale-while-revalidate 304 refreshes cache with async store', async () => { |
| •52 |
const clock = FakeTimers.install({ now: 1, toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }) |
| 53 |
after(() => clock.uninstall()) |
| 54 |
|
| 55 |
let count200 = 0 |
| 56 |
let count304 = 0 |
| 57 |
|
| 58 |
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { |
| 59 |
res.sendDate = false |
| 60 |
res.setHeader('Date', new Date(clock.now).toUTCString()) |
| 61 |
res.setHeader('Cache-Control', 'public, max-age=10, stale-while-revalidate=3600') |
| 62 |
res.setHeader('ETag', '"test-etag"') |
| 63 |
|
| 64 |
if (req.headers['if-none-match']) { |
| 65 |
count304++ |
| 66 |
res.statusCode = 304 |
| 67 |
res.end() |
| 68 |
} else { |
| 69 |
res.end('hello world ' + count200++) |
| 70 |
} |
| 71 |
}) |
| 72 |
|
| 73 |
server.listen(0) |
| 74 |
await once(server, 'listening') |
| 75 |
|
| 76 |
const store = new AsyncCacheStore() |
| 77 |
const dispatcher = new Client(`http://localhost:${server.address().port}`) |
| 78 |
.compose(interceptors.cache({ store })) |
| 79 |
|
| 80 |
after(async () => { |
| 81 |
server.close() |
| 82 |
await dispatcher.close() |
| 83 |
}) |
| 84 |
|
| 85 |
const url = `http://localhost:${server.address().port}` |
| 86 |
|
| 87 |
|
| 88 |
{ |
| 89 |
const res = await request(url, { dispatcher }) |
| 90 |
strictEqual(await res.body.text(), 'hello world 0') |
| 91 |
strictEqual(res.statusCode, 200) |
| 92 |
strictEqual(res.headers.warning, undefined) |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
clock.tick(12000) |
| 97 |
|
| 98 |
|
| 99 |
{ |
| 100 |
const res = await request(url, { dispatcher }) |
| 101 |
strictEqual(await res.body.text(), 'hello world 0') |
| 102 |
strictEqual(res.statusCode, 200) |
| 103 |
strictEqual(res.headers.warning, '110 - "response is stale"') |
| 104 |
await setTimeout(100) |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
{ |
| 109 |
clock.tick(10) |
| 110 |
const res = await request(url, { dispatcher }) |
| 111 |
strictEqual(await res.body.text(), 'hello world 0') |
| 112 |
strictEqual(res.statusCode, 200) |
| 113 |
strictEqual(res.headers.warning, undefined) |
| 114 |
} |
| 115 |
|
| 116 |
strictEqual(count200, 1) |
| 117 |
strictEqual(count304, 1) |
| 118 |
}) |
| 119 |
|
| 120 |
test('stale-while-revalidate 200 refreshes cache with async store', async () => { |
| •121 |
const clock = FakeTimers.install({ now: 1, toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }) |
| 122 |
after(() => clock.uninstall()) |
| 123 |
|
| 124 |
let requestCount = 0 |
| 125 |
|
| 126 |
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { |
| 127 |
res.sendDate = false |
| 128 |
res.setHeader('Date', new Date(clock.now).toUTCString()) |
| 129 |
res.setHeader('Cache-Control', 'public, max-age=10, stale-while-revalidate=3600') |
| 130 |
res.setHeader('ETag', `"etag-${requestCount}"`) |
| 131 |
res.end('hello world ' + requestCount++) |
| 132 |
}) |
| 133 |
|
| 134 |
server.listen(0) |
| 135 |
await once(server, 'listening') |
| 136 |
|
| 137 |
const store = new AsyncCacheStore() |
| 138 |
const dispatcher = new Client(`http://localhost:${server.address().port}`) |
| 139 |
.compose(interceptors.cache({ store })) |
| 140 |
|
| 141 |
after(async () => { |
| 142 |
server.close() |
| 143 |
await dispatcher.close() |
| 144 |
}) |
| 145 |
|
| 146 |
const url = `http://localhost:${server.address().port}` |
| 147 |
|
| 148 |
|
| 149 |
{ |
| 150 |
const res = await request(url, { dispatcher }) |
| 151 |
strictEqual(await res.body.text(), 'hello world 0') |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
clock.tick(12000) |
| 156 |
|
| 157 |
|
| 158 |
{ |
| 159 |
const res = await request(url, { dispatcher }) |
| 160 |
strictEqual(await res.body.text(), 'hello world 0') |
| 161 |
strictEqual(res.headers.warning, '110 - "response is stale"') |
| 162 |
await setTimeout(100) |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
{ |
| 167 |
clock.tick(10) |
| 168 |
const res = await request(url, { dispatcher }) |
| 169 |
strictEqual(await res.body.text(), 'hello world 1') |
| 170 |
strictEqual(res.headers.warning, undefined) |
| 171 |
} |
| 172 |
}) |
| 173 |
|
| 174 |
test('null vary values are not sent in revalidation headers', async () => { |
| •175 |
const clock = FakeTimers.install({ now: 1, toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'clearInterval', 'setImmediate', 'clearImmediate', 'Date', 'hrtime', 'performance', 'Intl'] }) |
| 176 |
after(() => clock.uninstall()) |
| 177 |
|
| 178 |
let revalidationHeaders = null |
| 179 |
|
| 180 |
const server = createServer({ joinDuplicateHeaders: true }, (req, res) => { |
| 181 |
res.sendDate = false |
| 182 |
res.setHeader('Date', new Date(clock.now).toUTCString()) |
| 183 |
res.setHeader('Cache-Control', 'public, max-age=10, stale-while-revalidate=3600') |
| 184 |
res.setHeader('ETag', '"test-etag"') |
| 185 |
res.setHeader('Vary', 'X-Custom-Header, X-Another-Header') |
| 186 |
|
| 187 |
if (req.headers['if-none-match']) { |
| 188 |
revalidationHeaders = { ...req.headers } |
| 189 |
res.statusCode = 304 |
| 190 |
res.end() |
| 191 |
} else { |
| 192 |
res.end('hello world') |
| 193 |
} |
| 194 |
}) |
| 195 |
|
| 196 |
server.listen(0) |
| 197 |
await once(server, 'listening') |
| 198 |
|
| 199 |
const store = new AsyncCacheStore() |
| 200 |
const dispatcher = new Client(`http://localhost:${server.address().port}`) |
| 201 |
.compose(interceptors.cache({ store })) |
| 202 |
|
| 203 |
after(async () => { |
| 204 |
server.close() |
| 205 |
await dispatcher.close() |
| 206 |
}) |
| 207 |
|
| 208 |
const url = `http://localhost:${server.address().port}` |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
{ |
| 213 |
const res = await request(url, { dispatcher }) |
| 214 |
strictEqual(await res.body.text(), 'hello world') |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
clock.tick(12000) |
| 219 |
|
| 220 |
|
| 221 |
{ |
| 222 |
const res = await request(url, { dispatcher }) |
| 223 |
strictEqual(res.headers.warning, '110 - "response is stale"') |
| 224 |
await setTimeout(100) |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
notStrictEqual(revalidationHeaders, null) |
| 229 |
strictEqual(revalidationHeaders['x-custom-header'], undefined) |
| 230 |
strictEqual(revalidationHeaders['x-another-header'], undefined) |
| 231 |
strictEqual(revalidationHeaders['if-none-match'], '"test-etag"') |
| 232 |
}) |
| 233 |
}) |