-
Notifications
You must be signed in to change notification settings - Fork 40
/
handler.ts
120 lines (104 loc) · 3.96 KB
/
handler.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { IncomingMessage } from 'http'
import { gzipSync } from 'zlib'
import { lock, send, serveCache, unlock } from './cache-manager'
import { forMetrics, Metrics, serveMetrics } from './metrics'
import { encodePayload } from './payload'
import Renderer, { InitArgs } from './renderer'
import { CacheAdapter, HandlerConfig, WrappedHandler } from './types'
import { filterUrl, isZipped, log, mergeConfig, serve } from './utils'
function matchRules(conf: HandlerConfig, req: IncomingMessage) {
const err = ['GET', 'HEAD'].indexOf(req.method ?? '') === -1
if (err) return { matched: false, ttl: -1 }
if (typeof conf.rules === 'function') {
const ttl = conf.rules(req)
if (ttl) return { matched: true, ttl }
} else {
for (const rule of conf.rules ?? []) {
if (req.url && new RegExp(rule.regex).test(req.url)) {
return { matched: true, ttl: rule.ttl }
}
}
}
return { matched: false, ttl: 0 }
}
/**
* Wrap a http listener to serve cached response
*
* @param cache the cache
* @param conf conf of next-boost
* @param renderer the SSR renderer runs in worker thread
* @param next pass-through handler
*
* @returns a request listener to use in http server
*/
const wrap: WrappedHandler = (cache, conf, renderer, next, metrics) => {
return async (req, res) => {
if (conf.metrics && forMetrics(req)) return serveMetrics(metrics, res)
req.url = filterUrl(req.url ?? '', conf.paramFilter)
const key = conf.cacheKey ? conf.cacheKey(req) : req.url
const { matched, ttl } = matchRules(conf, req)
if (!matched) {
metrics.inc('bypass')
res.setHeader('x-next-boost-status', 'bypass')
return next(req, res)
}
const start = process.hrtime()
const forced = req.headers['x-next-boost'] === 'update' // forced
const state = await serveCache(cache, key, forced)
res.setHeader('x-next-boost-status', state.status)
metrics.inc(state.status)
if (state.status === 'stale' || state.status === 'hit' || state.status === 'fulfill') {
send(state.payload, res)
if (!conf.quiet) log(start, state.status, req.url) // record time for stale and hit
if (state.status !== 'stale') return // stop here
} else if (state.status === 'timeout') {
send({ body: null, headers: null }, res)
return // prevent adding pressure to server
}
try {
await lock(key, cache)
const args = { path: req.url, headers: req.headers, method: req.method }
const rv = await renderer.render(args)
// rv.body is a Buffer in JSON format: { type: 'Buffer', data: [...] }
const body = Buffer.from(rv.body)
// stale has been served
if (state.status !== 'stale') serve(res, rv)
// when in stale, there will 2 log output. The latter is the rendering time on server
if (!conf.quiet) log(start, state.status, req.url)
if (rv.statusCode === 200) {
// save gzipped data
const payload = { headers: rv.headers, body: isZipped(rv.headers) ? body : gzipSync(body) }
await cache.set('payload:' + key, encodePayload(payload), ttl)
}
} catch (e) {
console.error('Error saving payload to cache', e)
} finally {
await unlock(key, cache)
}
}
}
export default async function CachedHandler(args: InitArgs, options?: HandlerConfig) {
console.log('> Preparing cached handler')
// merge config
const conf = mergeConfig(options)
// the cache
if (!conf.cacheAdapter) {
const { Adapter } = require('@next-boost/hybrid-disk-cache')
conf.cacheAdapter = new Adapter() as CacheAdapter
}
const adapter = conf.cacheAdapter
const cache = await adapter.init()
const renderer = Renderer()
await renderer.init(args)
const plain = await require(args.script).default(args)
const metrics = new Metrics()
// init the child process for revalidate and cache purge
return {
handler: wrap(cache, conf, renderer, plain, metrics),
cache,
close: async () => {
renderer.kill()
await adapter.shutdown()
},
}
}