forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastly-cache-test.js
100 lines (83 loc) · 3.06 KB
/
fastly-cache-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// This middleware function is intended to be used for testing caching behavior with Fastly.
// It will intercept ALL URLs that are routed to it and respond with a simple HTML body
// containing a timestamp.
// The logic will detect certain values in the path and set the HTTP status and/or the
// Surrogate-Control header value.
//
// NOTE: This middleware is intended to be removed once testing is complete!
//
import express from 'express'
import crypto from 'crypto'
const router = express.Router()
router.get('/*', function (req, res) {
// If X-CacheTest-Error is set, simulate the site being down (regardless of URL)
if (req.get('X-CacheTest-Error')) {
res.status(parseInt(req.get('X-CacheTest-Error'))).end()
return
}
const cacheControlModeParam = req.get('X-CacheTest-CCMode') ?? 'none'
const staleIfErrorParam = req.get('X-CacheTest-StaleIfError') ?? '300'
const staleWhileRevalidateParam = req.get('X-CacheTest-StaleWhileRevalidate') ?? '60'
const maxAgeParam = req.get('X-CacheTest-MaxAge') ?? '300'
const path = req.params[0]
const content = `<html>
<body>
<p>Timestamp: ${new Date()}</p>
</body>
</html>`
let status = 200
const surrogateControlValues = []
if (path.includes('max-age')) surrogateControlValues.push(`max-age=${maxAgeParam}`)
if (path.includes('must-revalidate')) surrogateControlValues.push('must-revalidate')
if (path.includes('no-cache')) surrogateControlValues.push('no-cache')
if (path.includes('no-store')) surrogateControlValues.push('no-store')
if (path.includes('private')) surrogateControlValues.push('private')
if (path.includes('proxy-revalidate')) surrogateControlValues.push('proxy-revalidate')
if (path.includes('public')) surrogateControlValues.push('public')
if (path.includes('stale-if-error'))
surrogateControlValues.push(`stale-if-error=${staleIfErrorParam}`)
if (path.includes('stale-while-revalidate'))
surrogateControlValues.push(`stale-while-revalidate=${staleWhileRevalidateParam}`)
if (path.includes('no-cookies')) {
res.removeHeader('Set-Cookie')
}
if (path.includes('etag')) {
res.set({
ETag: `"${crypto.createHash('md5').update(content).digest('hex')}"`,
})
}
if (path.includes('error500')) {
status = 500
} else if (path.includes('error502')) {
status = 502
} else if (path.includes('error503')) {
status = 503
} else if (path.includes('error504')) {
status = 504
}
if (surrogateControlValues.length > 0) {
switch (cacheControlModeParam) {
case 'none':
res.set({
'surrogate-control': surrogateControlValues.join(', '),
})
break
case 'both':
res.set({
'surrogate-control': surrogateControlValues.join(', '),
'cache-control': surrogateControlValues.join(', ').replace('max-age', 's-maxage'),
})
break
case 'only':
res.set({
'cache-control': surrogateControlValues.join(', ').replace('max-age', 's-maxage'),
})
break
}
}
res.status(status)
res.send(content)
res.end()
})
export default router