@fastify/caching 9.0.0-pre.fv5.2 → 9.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/test/cache.test.js +52 -51
- package/test/headers.test.js +41 -39
- package/.taprc +0 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastify/caching",
|
|
3
|
-
"version": "9.0.
|
|
3
|
+
"version": "9.0.1",
|
|
4
4
|
"description": "A plugin for Fastify to enable management of cache control headers",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"lint:fix": "standard --verbose --fix | snazzy",
|
|
11
11
|
"test": "npm run test:unit && npm run test:typescript",
|
|
12
12
|
"test:typescript": "tsd",
|
|
13
|
-
"test:unit": "
|
|
13
|
+
"test:unit": "c8 --100 node --test",
|
|
14
14
|
"test:unit:report": "npm run test:unit -- --coverage-report=html",
|
|
15
15
|
"test:unit:verbose": "npm run test:unit -- -Rspec"
|
|
16
16
|
},
|
|
@@ -36,15 +36,15 @@
|
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@fastify/pre-commit": "^2.1.0",
|
|
38
38
|
"@types/node": "^22.0.0",
|
|
39
|
-
"
|
|
39
|
+
"c8": "^10.1.2",
|
|
40
|
+
"fastify": "^5.0.0",
|
|
40
41
|
"snazzy": "^9.0.0",
|
|
41
42
|
"standard": "^17.1.0",
|
|
42
|
-
"tap": "^18.7.0",
|
|
43
43
|
"tsd": "^0.31.0"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"abstract-cache": "^1.0.1",
|
|
47
|
-
"fastify-plugin": "^5.0.0
|
|
47
|
+
"fastify-plugin": "^5.0.0",
|
|
48
48
|
"uid-safe": "^2.1.5"
|
|
49
49
|
},
|
|
50
50
|
"publishConfig": {
|
package/test/cache.test.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const plugin = require('..')
|
|
5
5
|
|
|
6
6
|
const Fastify = require('fastify')
|
|
7
|
+
const { setTimeout: sleep } = require('node:timers/promises')
|
|
7
8
|
|
|
8
9
|
test('cache property gets added to instance', async (t) => {
|
|
9
10
|
t.plan(2)
|
|
@@ -12,8 +13,8 @@ test('cache property gets added to instance', async (t) => {
|
|
|
12
13
|
await fastify.register(plugin)
|
|
13
14
|
await fastify.ready()
|
|
14
15
|
|
|
15
|
-
t.ok(fastify.cache)
|
|
16
|
-
t.ok(fastify.cache.set)
|
|
16
|
+
t.assert.ok(fastify.cache)
|
|
17
|
+
t.assert.ok(fastify.cache.set)
|
|
17
18
|
})
|
|
18
19
|
|
|
19
20
|
test('cache is usable', async (t) => {
|
|
@@ -22,13 +23,13 @@ test('cache is usable', async (t) => {
|
|
|
22
23
|
const fastify = Fastify()
|
|
23
24
|
await fastify.register(async (instance, options) => {
|
|
24
25
|
instance.addHook('onRequest', async function (req, reply) {
|
|
25
|
-
t.
|
|
26
|
+
t.assert.ifError(instance[Symbol.for('fastify-caching.registered')])
|
|
26
27
|
})
|
|
27
28
|
})
|
|
28
29
|
await fastify.register(plugin)
|
|
29
30
|
|
|
30
31
|
fastify.addHook('onRequest', async function (req, reply) {
|
|
31
|
-
t.
|
|
32
|
+
t.assert.strictEqual(this[Symbol.for('fastify-caching.registered')], true)
|
|
32
33
|
})
|
|
33
34
|
|
|
34
35
|
fastify.get('/one', (req, reply) => {
|
|
@@ -40,9 +41,8 @@ test('cache is usable', async (t) => {
|
|
|
40
41
|
|
|
41
42
|
fastify.get('/two', (req, reply) => {
|
|
42
43
|
fastify.cache.get('one', (err, obj) => {
|
|
43
|
-
t.
|
|
44
|
-
t.
|
|
45
|
-
|
|
44
|
+
t.assert.ifError(err)
|
|
45
|
+
t.assert.strictEqual(obj.item, { one: true })
|
|
46
46
|
reply.send()
|
|
47
47
|
})
|
|
48
48
|
})
|
|
@@ -72,13 +72,13 @@ test('cache is usable with function as plugin default options input', async (t)
|
|
|
72
72
|
const fastify = Fastify()
|
|
73
73
|
await fastify.register(async (instance, options) => {
|
|
74
74
|
instance.addHook('onRequest', async function (req, reply) {
|
|
75
|
-
t.
|
|
75
|
+
t.assert.failure(instance[Symbol.for('fastify-caching.registered')])
|
|
76
76
|
})
|
|
77
77
|
})
|
|
78
|
-
await fastify.register(plugin, () => () => {
|
|
78
|
+
await fastify.register(plugin, () => () => {})
|
|
79
79
|
|
|
80
80
|
fastify.addHook('onRequest', async function (req, reply) {
|
|
81
|
-
t.
|
|
81
|
+
t.assert.strictEqual(this[Symbol.for('fastify-caching.registered')], true)
|
|
82
82
|
})
|
|
83
83
|
|
|
84
84
|
fastify.get('/one', (req, reply) => {
|
|
@@ -90,8 +90,8 @@ test('cache is usable with function as plugin default options input', async (t)
|
|
|
90
90
|
|
|
91
91
|
fastify.get('/two', (req, reply) => {
|
|
92
92
|
fastify.cache.get('one', (err, obj) => {
|
|
93
|
-
t.
|
|
94
|
-
t.
|
|
93
|
+
t.assert.ifError(err)
|
|
94
|
+
t.assert.strictEqual(obj.item, { one: true })
|
|
95
95
|
|
|
96
96
|
reply.send()
|
|
97
97
|
})
|
|
@@ -130,16 +130,14 @@ test('getting cache item with error returns error', async (t) => {
|
|
|
130
130
|
fastify.get('/one', (req, reply) => {
|
|
131
131
|
fastify.cache.set('one', { one: true }, 1000, (err) => {
|
|
132
132
|
if (err) return reply.send(err)
|
|
133
|
-
return reply
|
|
134
|
-
.etag('123456')
|
|
135
|
-
.send({ hello: 'world' })
|
|
133
|
+
return reply.etag('123456').send({ hello: 'world' })
|
|
136
134
|
})
|
|
137
135
|
})
|
|
138
136
|
|
|
139
137
|
fastify.get('/two', (req, reply) => {
|
|
140
138
|
fastify.cache.get('one', (err, obj) => {
|
|
141
|
-
t.
|
|
142
|
-
t.
|
|
139
|
+
t.assert.failure(err)
|
|
140
|
+
t.assert.failure(obj)
|
|
143
141
|
})
|
|
144
142
|
})
|
|
145
143
|
|
|
@@ -157,7 +155,7 @@ test('getting cache item with error returns error', async (t) => {
|
|
|
157
155
|
'if-none-match': '123456'
|
|
158
156
|
}
|
|
159
157
|
})
|
|
160
|
-
t.
|
|
158
|
+
t.assert.strictEqual(response.statusCode, 500)
|
|
161
159
|
})
|
|
162
160
|
|
|
163
161
|
test('etags get stored in cache', async (t) => {
|
|
@@ -167,9 +165,7 @@ test('etags get stored in cache', async (t) => {
|
|
|
167
165
|
await fastify.register(plugin)
|
|
168
166
|
|
|
169
167
|
fastify.get('/one', (req, reply) => {
|
|
170
|
-
reply
|
|
171
|
-
.etag('123456')
|
|
172
|
-
.send({ hello: 'world' })
|
|
168
|
+
reply.etag('123456').send({ hello: 'world' })
|
|
173
169
|
})
|
|
174
170
|
|
|
175
171
|
await fastify.ready()
|
|
@@ -186,14 +182,14 @@ test('etags get stored in cache', async (t) => {
|
|
|
186
182
|
'if-none-match': '123456'
|
|
187
183
|
}
|
|
188
184
|
})
|
|
189
|
-
t.
|
|
185
|
+
t.assert.strictEqual(response.statusCode, 304)
|
|
190
186
|
})
|
|
191
187
|
|
|
192
|
-
test('etag cache life is customizable', (t) => {
|
|
188
|
+
test('etag cache life is customizable', async (t) => {
|
|
193
189
|
t.plan(4)
|
|
194
190
|
|
|
195
191
|
const fastify = Fastify()
|
|
196
|
-
fastify.register(plugin)
|
|
192
|
+
await fastify.register(plugin)
|
|
197
193
|
|
|
198
194
|
fastify.get('/one', function (req, reply) {
|
|
199
195
|
reply
|
|
@@ -203,29 +199,36 @@ test('etag cache life is customizable', (t) => {
|
|
|
203
199
|
})
|
|
204
200
|
|
|
205
201
|
fastify.ready((err) => {
|
|
206
|
-
t.
|
|
207
|
-
|
|
208
|
-
fastify.inject(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
202
|
+
t.assert.ifError(err)
|
|
203
|
+
|
|
204
|
+
fastify.inject(
|
|
205
|
+
{
|
|
206
|
+
method: 'GET',
|
|
207
|
+
path: '/one'
|
|
208
|
+
},
|
|
209
|
+
(err, _response) => {
|
|
210
|
+
t.assert.ifError(err)
|
|
211
|
+
|
|
212
|
+
// We wait 70 milliseconds that the cache expires
|
|
213
|
+
setTimeout(() => {
|
|
214
|
+
fastify.inject(
|
|
215
|
+
{
|
|
216
|
+
method: 'GET',
|
|
217
|
+
path: '/one',
|
|
218
|
+
headers: {
|
|
219
|
+
'if-none-match': '123456'
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
(err, response) => {
|
|
223
|
+
t.assert.ifError(err)
|
|
224
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
225
|
+
}
|
|
226
|
+
)
|
|
227
|
+
}, 70)
|
|
228
|
+
}
|
|
229
|
+
)
|
|
228
230
|
})
|
|
231
|
+
await sleep(100)
|
|
229
232
|
})
|
|
230
233
|
|
|
231
234
|
test('returns response payload', async (t) => {
|
|
@@ -235,9 +238,7 @@ test('returns response payload', async (t) => {
|
|
|
235
238
|
await fastify.register(plugin)
|
|
236
239
|
|
|
237
240
|
fastify.get('/one', (req, reply) => {
|
|
238
|
-
reply
|
|
239
|
-
.etag('123456', 300)
|
|
240
|
-
.send({ hello: 'world' })
|
|
241
|
+
reply.etag('123456', 300).send({ hello: 'world' })
|
|
241
242
|
})
|
|
242
243
|
|
|
243
244
|
await fastify.ready()
|
|
@@ -252,5 +253,5 @@ test('returns response payload', async (t) => {
|
|
|
252
253
|
path: '/one'
|
|
253
254
|
})
|
|
254
255
|
|
|
255
|
-
t.
|
|
256
|
+
t.assert.deepStrictEqual(JSON.parse(response.payload), { hello: 'world' })
|
|
256
257
|
})
|
package/test/headers.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { test } = require('
|
|
3
|
+
const { test } = require('node:test')
|
|
4
4
|
const Fastify = require('fastify')
|
|
5
5
|
const plugin = require('..')
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@ test('decorators get added', async (t) => {
|
|
|
11
11
|
await fastify.register(plugin)
|
|
12
12
|
|
|
13
13
|
fastify.get('/', (req, reply) => {
|
|
14
|
-
t.ok(reply.etag)
|
|
14
|
+
t.assert.ok(reply.etag)
|
|
15
15
|
reply.send()
|
|
16
16
|
})
|
|
17
17
|
|
|
@@ -32,9 +32,7 @@ test('decorators add headers', async (t) => {
|
|
|
32
32
|
await fastify.register(plugin)
|
|
33
33
|
|
|
34
34
|
fastify.get('/', (req, reply) => {
|
|
35
|
-
reply
|
|
36
|
-
.etag(tag)
|
|
37
|
-
.send()
|
|
35
|
+
reply.etag(tag).send()
|
|
38
36
|
})
|
|
39
37
|
|
|
40
38
|
await fastify.ready()
|
|
@@ -43,8 +41,8 @@ test('decorators add headers', async (t) => {
|
|
|
43
41
|
method: 'GET',
|
|
44
42
|
path: '/'
|
|
45
43
|
})
|
|
46
|
-
t.ok(response.headers.etag)
|
|
47
|
-
t.
|
|
44
|
+
t.assert.ok(response.headers.etag)
|
|
45
|
+
t.assert.strictEqual(response.headers.etag, tag)
|
|
48
46
|
})
|
|
49
47
|
|
|
50
48
|
test('sets etag header for falsy argument', async (t) => {
|
|
@@ -54,9 +52,7 @@ test('sets etag header for falsy argument', async (t) => {
|
|
|
54
52
|
await fastify.register(plugin)
|
|
55
53
|
|
|
56
54
|
fastify.get('/', (req, reply) => {
|
|
57
|
-
reply
|
|
58
|
-
.etag()
|
|
59
|
-
.send()
|
|
55
|
+
reply.etag().send()
|
|
60
56
|
})
|
|
61
57
|
|
|
62
58
|
await fastify.ready()
|
|
@@ -65,7 +61,7 @@ test('sets etag header for falsy argument', async (t) => {
|
|
|
65
61
|
method: 'GET',
|
|
66
62
|
path: '/'
|
|
67
63
|
})
|
|
68
|
-
t.ok(response.headers.etag)
|
|
64
|
+
t.assert.ok(response.headers.etag)
|
|
69
65
|
})
|
|
70
66
|
|
|
71
67
|
test('sets no-cache header', async (t) => {
|
|
@@ -84,8 +80,8 @@ test('sets no-cache header', async (t) => {
|
|
|
84
80
|
method: 'GET',
|
|
85
81
|
path: '/'
|
|
86
82
|
})
|
|
87
|
-
t.ok(response.headers['cache-control'])
|
|
88
|
-
t.
|
|
83
|
+
t.assert.ok(response.headers['cache-control'])
|
|
84
|
+
t.assert.strictEqual(response.headers['cache-control'], 'no-cache')
|
|
89
85
|
})
|
|
90
86
|
|
|
91
87
|
test('sets private with max-age header', async (t) => {
|
|
@@ -109,8 +105,11 @@ test('sets private with max-age header', async (t) => {
|
|
|
109
105
|
method: 'GET',
|
|
110
106
|
path: '/'
|
|
111
107
|
})
|
|
112
|
-
t.ok(response.headers['cache-control'])
|
|
113
|
-
t.
|
|
108
|
+
t.assert.ok(response.headers['cache-control'])
|
|
109
|
+
t.assert.strictEqual(
|
|
110
|
+
response.headers['cache-control'],
|
|
111
|
+
'private, max-age=300'
|
|
112
|
+
)
|
|
114
113
|
})
|
|
115
114
|
|
|
116
115
|
test('sets public with max-age and s-maxage header', async (t) => {
|
|
@@ -135,8 +134,11 @@ test('sets public with max-age and s-maxage header', async (t) => {
|
|
|
135
134
|
method: 'GET',
|
|
136
135
|
path: '/'
|
|
137
136
|
})
|
|
138
|
-
t.ok(response.headers['cache-control'])
|
|
139
|
-
t.
|
|
137
|
+
t.assert.ok(response.headers['cache-control'])
|
|
138
|
+
t.assert.strictEqual(
|
|
139
|
+
response.headers['cache-control'],
|
|
140
|
+
'public, max-age=300, s-maxage=12345'
|
|
141
|
+
)
|
|
140
142
|
})
|
|
141
143
|
|
|
142
144
|
test('do not set headers if another upstream plugin already sets it', async (t) => {
|
|
@@ -164,8 +166,8 @@ test('do not set headers if another upstream plugin already sets it', async (t)
|
|
|
164
166
|
method: 'GET',
|
|
165
167
|
path: '/'
|
|
166
168
|
})
|
|
167
|
-
t.ok(response.headers['cache-control'])
|
|
168
|
-
t.
|
|
169
|
+
t.assert.ok(response.headers['cache-control'])
|
|
170
|
+
t.assert.strictEqual(response.headers['cache-control'], 'do not override')
|
|
169
171
|
})
|
|
170
172
|
|
|
171
173
|
test('only sets max-age and ignores s-maxage with private header', async (t) => {
|
|
@@ -190,8 +192,11 @@ test('only sets max-age and ignores s-maxage with private header', async (t) =>
|
|
|
190
192
|
method: 'GET',
|
|
191
193
|
path: '/'
|
|
192
194
|
})
|
|
193
|
-
t.ok(response.headers['cache-control'])
|
|
194
|
-
t.
|
|
195
|
+
t.assert.ok(response.headers['cache-control'])
|
|
196
|
+
t.assert.strictEqual(
|
|
197
|
+
response.headers['cache-control'],
|
|
198
|
+
'private, max-age=300'
|
|
199
|
+
)
|
|
195
200
|
})
|
|
196
201
|
|
|
197
202
|
test('s-maxage is optional with public header', async (t) => {
|
|
@@ -215,8 +220,8 @@ test('s-maxage is optional with public header', async (t) => {
|
|
|
215
220
|
method: 'GET',
|
|
216
221
|
path: '/'
|
|
217
222
|
})
|
|
218
|
-
t.ok(response.headers['cache-control'])
|
|
219
|
-
t.
|
|
223
|
+
t.assert.ok(response.headers['cache-control'])
|
|
224
|
+
t.assert.strictEqual(response.headers['cache-control'], 'public, max-age=300')
|
|
220
225
|
})
|
|
221
226
|
|
|
222
227
|
test('sets no-store with max-age header', async (t) => {
|
|
@@ -235,8 +240,11 @@ test('sets no-store with max-age header', async (t) => {
|
|
|
235
240
|
method: 'GET',
|
|
236
241
|
path: '/'
|
|
237
242
|
})
|
|
238
|
-
t.ok(response.headers['cache-control'])
|
|
239
|
-
t.
|
|
243
|
+
t.assert.ok(response.headers['cache-control'])
|
|
244
|
+
t.assert.strictEqual(
|
|
245
|
+
response.headers['cache-control'],
|
|
246
|
+
'no-store, max-age=300'
|
|
247
|
+
)
|
|
240
248
|
})
|
|
241
249
|
|
|
242
250
|
test('sets the expires header', async (t) => {
|
|
@@ -248,9 +256,7 @@ test('sets the expires header', async (t) => {
|
|
|
248
256
|
await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE })
|
|
249
257
|
|
|
250
258
|
fastify.get('/', (req, reply) => {
|
|
251
|
-
reply
|
|
252
|
-
.expires(now)
|
|
253
|
-
.send({ hello: 'world' })
|
|
259
|
+
reply.expires(now).send({ hello: 'world' })
|
|
254
260
|
})
|
|
255
261
|
|
|
256
262
|
await fastify.ready()
|
|
@@ -259,8 +265,8 @@ test('sets the expires header', async (t) => {
|
|
|
259
265
|
method: 'GET',
|
|
260
266
|
path: '/'
|
|
261
267
|
})
|
|
262
|
-
t.ok(response.headers.expires)
|
|
263
|
-
t.
|
|
268
|
+
t.assert.ok(response.headers.expires)
|
|
269
|
+
t.assert.strictEqual(response.headers.expires, now.toUTCString())
|
|
264
270
|
})
|
|
265
271
|
|
|
266
272
|
test('sets the expires header to a falsy value', async (t) => {
|
|
@@ -270,9 +276,7 @@ test('sets the expires header to a falsy value', async (t) => {
|
|
|
270
276
|
await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE })
|
|
271
277
|
|
|
272
278
|
fastify.get('/', (req, reply) => {
|
|
273
|
-
reply
|
|
274
|
-
.expires()
|
|
275
|
-
.send({ hello: 'world' })
|
|
279
|
+
reply.expires().send({ hello: 'world' })
|
|
276
280
|
})
|
|
277
281
|
|
|
278
282
|
await fastify.ready()
|
|
@@ -281,7 +285,7 @@ test('sets the expires header to a falsy value', async (t) => {
|
|
|
281
285
|
method: 'GET',
|
|
282
286
|
path: '/'
|
|
283
287
|
})
|
|
284
|
-
t.
|
|
288
|
+
t.assert.ifError(response.headers.expires)
|
|
285
289
|
})
|
|
286
290
|
|
|
287
291
|
test('sets the expires header to a custom value', async (t) => {
|
|
@@ -291,9 +295,7 @@ test('sets the expires header to a custom value', async (t) => {
|
|
|
291
295
|
await fastify.register(plugin, { privacy: plugin.privacy.NOCACHE })
|
|
292
296
|
|
|
293
297
|
fastify.get('/', (req, reply) => {
|
|
294
|
-
reply
|
|
295
|
-
.expires('foobar')
|
|
296
|
-
.send({ hello: 'world' })
|
|
298
|
+
reply.expires('foobar').send({ hello: 'world' })
|
|
297
299
|
})
|
|
298
300
|
|
|
299
301
|
await fastify.ready()
|
|
@@ -302,6 +304,6 @@ test('sets the expires header to a custom value', async (t) => {
|
|
|
302
304
|
method: 'GET',
|
|
303
305
|
path: '/'
|
|
304
306
|
})
|
|
305
|
-
t.ok(response.headers.expires)
|
|
306
|
-
t.
|
|
307
|
+
t.assert.ok(response.headers.expires)
|
|
308
|
+
t.assert.strictEqual(response.headers.expires, 'foobar')
|
|
307
309
|
})
|
package/.taprc
DELETED