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
4 import express from 'express'
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'
10 import { asyncMiddleware } from '@server/middlewares'
12 export interface APICacheOptions {
13 headerBlacklist?: string[]
14 excludeStatus?: HttpStatusCode[]
17 interface CacheObject {
19 headers: OutgoingHttpHeaders
21 encoding: BufferEncoding
25 export class ApiCache {
27 private readonly options: APICacheOptions
28 private readonly timers: { [ id: string ]: NodeJS.Timeout } = {}
30 private index: { all: string[] } = { all: [] }
32 constructor (options: APICacheOptions) {
41 buildMiddleware (strDuration: string) {
42 const duration = parseDurationToMs(strDuration)
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()
49 if (!Redis.Instance.isConnected()) return this.makeResponseCacheable(res, next, key, duration)
52 const obj = await redis.hGetAll(key)
54 return this.sendCachedResponse(req, res, JSON.parse(obj.response), duration)
57 return this.makeResponseCacheable(res, next, key, duration)
59 return this.makeResponseCacheable(res, next, key, duration)
65 private shouldCacheResponse (response: express.Response) {
66 if (!response) return false
67 if (this.options.excludeStatus.includes(response.statusCode)) return false
72 private addIndexEntries (key: string) {
73 this.index.all.unshift(key)
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]
86 private createCacheObject (status: number, headers: OutgoingHttpHeaders, data: any, encoding: BufferEncoding) {
89 headers: this.filterBlacklistedHeaders(headers),
93 // Seconds since epoch, used to properly decrement max-age headers in cached responses.
94 timestamp: new Date().getTime() / 1000
98 private async cacheResponse (key: string, value: object, duration: number) {
99 const redis = Redis.Instance.getClient()
101 if (Redis.Instance.isConnected()) {
103 redis.hSet(key, 'response', JSON.stringify(value)),
104 redis.hSet(key, 'duration', duration + ''),
105 redis.expire(key, duration / 1000)
109 // add automatic cache clearing from duration, includes max limit on setTimeout
110 this.timers[key] = setTimeout(() => {
112 .catch(err => logger.error('Cannot clear Redis key %s.', key, { err }))
113 }, Math.min(duration, 2147483647))
116 private accumulateContent (res: express.Response, content: any) {
119 if (typeof content === 'string') {
120 res.locals.apicache.content = (res.locals.apicache.content || '') + content
124 if (Buffer.isBuffer(content)) {
125 let oldContent = res.locals.apicache.content
127 if (typeof oldContent === 'string') {
128 oldContent = Buffer.from(oldContent)
132 oldContent = Buffer.alloc(0)
135 res.locals.apicache.content = Buffer.concat(
136 [ oldContent, content ],
137 oldContent.length + content.length
143 res.locals.apicache.content = content
146 private makeResponseCacheable (res: express.Response, next: express.NextFunction, key: string, duration: number) {
149 res.locals.apicache = {
151 writeHead: res.writeHead,
159 res.writeHead = function () {
160 if (self.shouldCacheResponse(res)) {
161 res.setHeader('cache-control', 'max-age=' + (duration / 1000).toFixed(0))
163 res.setHeader('cache-control', 'no-cache, no-store, must-revalidate')
166 res.locals.apicache.headers = Object.assign({}, res.getHeaders())
167 return res.locals.apicache.writeHead.apply(this, arguments as any)
170 res.write = function (chunk: any) {
171 self.accumulateContent(res, chunk)
172 return res.locals.apicache.write.apply(this, arguments as any)
175 res.end = function (content: any, encoding: BufferEncoding) {
176 if (self.shouldCacheResponse(res)) {
177 self.accumulateContent(res, content)
179 if (res.locals.apicache.cacheable && res.locals.apicache.content) {
180 self.addIndexEntries(key)
182 const headers = res.locals.apicache.headers || res.getHeaders()
183 const cacheObject = self.createCacheObject(
186 res.locals.apicache.content,
189 self.cacheResponse(key, cacheObject, duration)
190 .catch(err => logger.error('Cannot cache response', { err }))
194 res.locals.apicache.end.apply(this, arguments as any)
200 private sendCachedResponse (request: express.Request, response: express.Response, cacheObject: CacheObject, duration: number) {
201 const headers = response.getHeaders()
203 if (isTestInstance()) {
204 Object.assign(headers, {
205 'x-api-cache-cached': 'true'
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
216 (duration / 1000 - (new Date().getTime() / 1000 - cacheObject.timestamp))
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)
228 // Test Etag against If-None-Match for 304
229 const cachedEtag = cacheObject.headers.etag
230 const requestEtag = request.headers['if-none-match']
232 if (requestEtag && cachedEtag === requestEtag) {
233 response.writeHead(304, headers)
234 return response.end()
237 response.writeHead(cacheObject.status || 200, headers)
239 return response.end(data, cacheObject.encoding)
242 private async clear (target: string) {
243 const redis = Redis.Instance.getClient()
246 clearTimeout(this.timers[target])
247 delete this.timers[target]
250 await redis.del(target)
252 logger.error('Cannot delete %s in redis cache.', target, { err })
255 this.index.all = this.index.all.filter(key => key !== target)
257 for (const key of this.index.all) {
258 clearTimeout(this.timers[key])
259 delete this.timers[key]
264 logger.error('Cannot delete %s in redis cache.', key, { err })