]>
Commit | Line | Data |
---|---|---|
20bafcb6 C |
1 | // Thanks: https://github.com/kwhitley/apicache |
2 | // We duplicated the library because it is unmaintened and prevent us to upgrade to recent NodeJS versions | |
3 | ||
41fb13c3 | 4 | import express from 'express' |
20bafcb6 C |
5 | import { OutgoingHttpHeaders } from 'http' |
6 | import { isTestInstance, parseDurationToMs } from '@server/helpers/core-utils' | |
7 | import { logger } from '@server/helpers/logger' | |
8 | import { Redis } from '@server/lib/redis' | |
9 | import { HttpStatusCode } from '@shared/models' | |
e5d91a9b | 10 | import { asyncMiddleware } from '@server/middlewares' |
20bafcb6 C |
11 | |
12 | export interface APICacheOptions { | |
13 | headerBlacklist?: string[] | |
14 | excludeStatus?: HttpStatusCode[] | |
15 | } | |
16 | ||
17 | interface CacheObject { | |
18 | status: number | |
19 | headers: OutgoingHttpHeaders | |
20 | data: any | |
21 | encoding: BufferEncoding | |
22 | timestamp: number | |
23 | } | |
24 | ||
25 | export class ApiCache { | |
26 | ||
27 | private readonly options: APICacheOptions | |
28 | private readonly timers: { [ id: string ]: NodeJS.Timeout } = {} | |
29 | ||
30 | private index: { all: string[] } = { all: [] } | |
31 | ||
32 | constructor (options: APICacheOptions) { | |
33 | this.options = { | |
34 | headerBlacklist: [], | |
35 | excludeStatus: [], | |
36 | ||
37 | ...options | |
38 | } | |
39 | } | |
40 | ||
41 | buildMiddleware (strDuration: string) { | |
42 | const duration = parseDurationToMs(strDuration) | |
43 | ||
e5d91a9b C |
44 | return asyncMiddleware( |
45 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
46 | const key = Redis.Instance.getPrefix() + 'api-cache-' + req.originalUrl | |
47 | const redis = Redis.Instance.getClient() | |
20bafcb6 | 48 | |
e5d91a9b | 49 | if (!Redis.Instance.isConnected()) return this.makeResponseCacheable(res, next, key, duration) |
20bafcb6 | 50 | |
e5d91a9b C |
51 | try { |
52 | const obj = await redis.hGetAll(key) | |
53 | if (obj?.response) { | |
20bafcb6 C |
54 | return this.sendCachedResponse(req, res, JSON.parse(obj.response), duration) |
55 | } | |
56 | ||
57 | return this.makeResponseCacheable(res, next, key, duration) | |
e5d91a9b C |
58 | } catch (err) { |
59 | return this.makeResponseCacheable(res, next, key, duration) | |
60 | } | |
20bafcb6 | 61 | } |
e5d91a9b | 62 | ) |
20bafcb6 C |
63 | } |
64 | ||
65 | private shouldCacheResponse (response: express.Response) { | |
66 | if (!response) return false | |
67 | if (this.options.excludeStatus.includes(response.statusCode)) return false | |
68 | ||
69 | return true | |
70 | } | |
71 | ||
72 | private addIndexEntries (key: string) { | |
73 | this.index.all.unshift(key) | |
74 | } | |
75 | ||
76 | private filterBlacklistedHeaders (headers: OutgoingHttpHeaders) { | |
77 | return Object.keys(headers) | |
78 | .filter(key => !this.options.headerBlacklist.includes(key)) | |
79 | .reduce((acc, header) => { | |
80 | acc[header] = headers[header] | |
81 | ||
82 | return acc | |
83 | }, {}) | |
84 | } | |
85 | ||
86 | private createCacheObject (status: number, headers: OutgoingHttpHeaders, data: any, encoding: BufferEncoding) { | |
87 | return { | |
88 | status, | |
89 | headers: this.filterBlacklistedHeaders(headers), | |
90 | data, | |
91 | encoding, | |
92 | ||
93 | // Seconds since epoch, used to properly decrement max-age headers in cached responses. | |
94 | timestamp: new Date().getTime() / 1000 | |
95 | } as CacheObject | |
96 | } | |
97 | ||
e5d91a9b | 98 | private async cacheResponse (key: string, value: object, duration: number) { |
20bafcb6 C |
99 | const redis = Redis.Instance.getClient() |
100 | ||
e5d91a9b C |
101 | if (Redis.Instance.isConnected()) { |
102 | await Promise.all([ | |
103 | redis.hSet(key, 'response', JSON.stringify(value)), | |
104 | redis.hSet(key, 'duration', duration + ''), | |
20bafcb6 | 105 | redis.expire(key, duration / 1000) |
e5d91a9b | 106 | ]) |
20bafcb6 C |
107 | } |
108 | ||
109 | // add automatic cache clearing from duration, includes max limit on setTimeout | |
e5d91a9b C |
110 | this.timers[key] = setTimeout(() => { |
111 | this.clear(key) | |
112 | .catch(err => logger.error('Cannot clear Redis key %s.', key, { err })) | |
113 | }, Math.min(duration, 2147483647)) | |
20bafcb6 C |
114 | } |
115 | ||
116 | private accumulateContent (res: express.Response, content: any) { | |
117 | if (!content) return | |
118 | ||
119 | if (typeof content === 'string') { | |
120 | res.locals.apicache.content = (res.locals.apicache.content || '') + content | |
121 | return | |
122 | } | |
123 | ||
124 | if (Buffer.isBuffer(content)) { | |
125 | let oldContent = res.locals.apicache.content | |
126 | ||
127 | if (typeof oldContent === 'string') { | |
128 | oldContent = Buffer.from(oldContent) | |
129 | } | |
130 | ||
131 | if (!oldContent) { | |
132 | oldContent = Buffer.alloc(0) | |
133 | } | |
134 | ||
135 | res.locals.apicache.content = Buffer.concat( | |
136 | [ oldContent, content ], | |
137 | oldContent.length + content.length | |
138 | ) | |
139 | ||
140 | return | |
141 | } | |
142 | ||
143 | res.locals.apicache.content = content | |
144 | } | |
145 | ||
146 | private makeResponseCacheable (res: express.Response, next: express.NextFunction, key: string, duration: number) { | |
147 | const self = this | |
148 | ||
149 | res.locals.apicache = { | |
150 | write: res.write, | |
151 | writeHead: res.writeHead, | |
152 | end: res.end, | |
153 | cacheable: true, | |
154 | content: undefined, | |
155 | headers: {} | |
156 | } | |
157 | ||
158 | // Patch express | |
159 | res.writeHead = function () { | |
160 | if (self.shouldCacheResponse(res)) { | |
161 | res.setHeader('cache-control', 'max-age=' + (duration / 1000).toFixed(0)) | |
162 | } else { | |
163 | res.setHeader('cache-control', 'no-cache, no-store, must-revalidate') | |
164 | } | |
165 | ||
166 | res.locals.apicache.headers = Object.assign({}, res.getHeaders()) | |
167 | return res.locals.apicache.writeHead.apply(this, arguments as any) | |
168 | } | |
169 | ||
170 | res.write = function (chunk: any) { | |
171 | self.accumulateContent(res, chunk) | |
172 | return res.locals.apicache.write.apply(this, arguments as any) | |
173 | } | |
174 | ||
175 | res.end = function (content: any, encoding: BufferEncoding) { | |
176 | if (self.shouldCacheResponse(res)) { | |
177 | self.accumulateContent(res, content) | |
178 | ||
179 | if (res.locals.apicache.cacheable && res.locals.apicache.content) { | |
180 | self.addIndexEntries(key) | |
181 | ||
182 | const headers = res.locals.apicache.headers || res.getHeaders() | |
183 | const cacheObject = self.createCacheObject( | |
184 | res.statusCode, | |
185 | headers, | |
186 | res.locals.apicache.content, | |
187 | encoding | |
188 | ) | |
189 | self.cacheResponse(key, cacheObject, duration) | |
e5d91a9b | 190 | .catch(err => logger.error('Cannot cache response', { err })) |
20bafcb6 C |
191 | } |
192 | } | |
193 | ||
194 | res.locals.apicache.end.apply(this, arguments as any) | |
195 | } as any | |
196 | ||
197 | next() | |
198 | } | |
199 | ||
200 | private sendCachedResponse (request: express.Request, response: express.Response, cacheObject: CacheObject, duration: number) { | |
201 | const headers = response.getHeaders() | |
202 | ||
203 | if (isTestInstance()) { | |
204 | Object.assign(headers, { | |
205 | 'x-api-cache-cached': 'true' | |
206 | }) | |
207 | } | |
208 | ||
209 | Object.assign(headers, this.filterBlacklistedHeaders(cacheObject.headers || {}), { | |
210 | // Set properly decremented max-age header | |
211 | // This ensures that max-age is in sync with the cache expiration | |
212 | 'cache-control': | |
213 | 'max-age=' + | |
214 | Math.max( | |
215 | 0, | |
216 | (duration / 1000 - (new Date().getTime() / 1000 - cacheObject.timestamp)) | |
217 | ).toFixed(0) | |
218 | }) | |
219 | ||
220 | // unstringify buffers | |
221 | let data = cacheObject.data | |
222 | if (data && data.type === 'Buffer') { | |
223 | data = typeof data.data === 'number' | |
224 | ? Buffer.alloc(data.data) | |
225 | : Buffer.from(data.data) | |
226 | } | |
227 | ||
228 | // Test Etag against If-None-Match for 304 | |
229 | const cachedEtag = cacheObject.headers.etag | |
230 | const requestEtag = request.headers['if-none-match'] | |
231 | ||
232 | if (requestEtag && cachedEtag === requestEtag) { | |
233 | response.writeHead(304, headers) | |
234 | return response.end() | |
235 | } | |
236 | ||
237 | response.writeHead(cacheObject.status || 200, headers) | |
238 | ||
239 | return response.end(data, cacheObject.encoding) | |
240 | } | |
241 | ||
e5d91a9b | 242 | private async clear (target: string) { |
20bafcb6 C |
243 | const redis = Redis.Instance.getClient() |
244 | ||
245 | if (target) { | |
246 | clearTimeout(this.timers[target]) | |
247 | delete this.timers[target] | |
248 | ||
249 | try { | |
e5d91a9b | 250 | await redis.del(target) |
20bafcb6 C |
251 | } catch (err) { |
252 | logger.error('Cannot delete %s in redis cache.', target, { err }) | |
253 | } | |
254 | ||
255 | this.index.all = this.index.all.filter(key => key !== target) | |
256 | } else { | |
257 | for (const key of this.index.all) { | |
258 | clearTimeout(this.timers[key]) | |
259 | delete this.timers[key] | |
260 | ||
261 | try { | |
e5d91a9b | 262 | await redis.del(key) |
20bafcb6 C |
263 | } catch (err) { |
264 | logger.error('Cannot delete %s in redis cache.', key, { err }) | |
265 | } | |
266 | } | |
267 | ||
268 | this.index.all = [] | |
269 | } | |
270 | ||
271 | return this.index | |
272 | } | |
273 | } |