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 * as 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'
11 export interface APICacheOptions {
12 headerBlacklist?: string[]
13 excludeStatus?: HttpStatusCode[]
16 interface CacheObject {
18 headers: OutgoingHttpHeaders
20 encoding: BufferEncoding
24 export class ApiCache {
26 private readonly options: APICacheOptions
27 private readonly timers: { [ id: string ]: NodeJS.Timeout } = {}
29 private index: { all: string[] } = { all: [] }
31 constructor (options: APICacheOptions) {
40 buildMiddleware (strDuration: string) {
41 const duration = parseDurationToMs(strDuration)
43 return (req: express.Request, res: express.Response, next: express.NextFunction) => {
44 const key = Redis.Instance.getPrefix() + 'api-cache-' + req.originalUrl
45 const redis = Redis.Instance.getClient()
47 if (!redis.connected) return this.makeResponseCacheable(res, next, key, duration)
50 redis.hgetall(key, (err, obj) => {
51 if (!err && obj && obj.response) {
52 return this.sendCachedResponse(req, res, JSON.parse(obj.response), duration)
55 return this.makeResponseCacheable(res, next, key, duration)
58 return this.makeResponseCacheable(res, next, key, duration)
63 private shouldCacheResponse (response: express.Response) {
64 if (!response) return false
65 if (this.options.excludeStatus.includes(response.statusCode)) return false
70 private addIndexEntries (key: string) {
71 this.index.all.unshift(key)
74 private filterBlacklistedHeaders (headers: OutgoingHttpHeaders) {
75 return Object.keys(headers)
76 .filter(key => !this.options.headerBlacklist.includes(key))
77 .reduce((acc, header) => {
78 acc[header] = headers[header]
84 private createCacheObject (status: number, headers: OutgoingHttpHeaders, data: any, encoding: BufferEncoding) {
87 headers: this.filterBlacklistedHeaders(headers),
91 // Seconds since epoch, used to properly decrement max-age headers in cached responses.
92 timestamp: new Date().getTime() / 1000
96 private cacheResponse (key: string, value: object, duration: number) {
97 const redis = Redis.Instance.getClient()
99 if (redis.connected) {
101 redis.hset(key, 'response', JSON.stringify(value))
102 redis.hset(key, 'duration', duration + '')
103 redis.expire(key, duration / 1000)
105 logger.error('Cannot set cache in redis.', { err })
109 // add automatic cache clearing from duration, includes max limit on setTimeout
110 this.timers[key] = setTimeout(() => this.clear(key), Math.min(duration, 2147483647))
113 private accumulateContent (res: express.Response, content: any) {
116 if (typeof content === 'string') {
117 res.locals.apicache.content = (res.locals.apicache.content || '') + content
121 if (Buffer.isBuffer(content)) {
122 let oldContent = res.locals.apicache.content
124 if (typeof oldContent === 'string') {
125 oldContent = Buffer.from(oldContent)
129 oldContent = Buffer.alloc(0)
132 res.locals.apicache.content = Buffer.concat(
133 [ oldContent, content ],
134 oldContent.length + content.length
140 res.locals.apicache.content = content
143 private makeResponseCacheable (res: express.Response, next: express.NextFunction, key: string, duration: number) {
146 res.locals.apicache = {
148 writeHead: res.writeHead,
156 res.writeHead = function () {
157 if (self.shouldCacheResponse(res)) {
158 res.setHeader('cache-control', 'max-age=' + (duration / 1000).toFixed(0))
160 res.setHeader('cache-control', 'no-cache, no-store, must-revalidate')
163 res.locals.apicache.headers = Object.assign({}, res.getHeaders())
164 return res.locals.apicache.writeHead.apply(this, arguments as any)
167 res.write = function (chunk: any) {
168 self.accumulateContent(res, chunk)
169 return res.locals.apicache.write.apply(this, arguments as any)
172 res.end = function (content: any, encoding: BufferEncoding) {
173 if (self.shouldCacheResponse(res)) {
174 self.accumulateContent(res, content)
176 if (res.locals.apicache.cacheable && res.locals.apicache.content) {
177 self.addIndexEntries(key)
179 const headers = res.locals.apicache.headers || res.getHeaders()
180 const cacheObject = self.createCacheObject(
183 res.locals.apicache.content,
186 self.cacheResponse(key, cacheObject, duration)
190 res.locals.apicache.end.apply(this, arguments as any)
196 private sendCachedResponse (request: express.Request, response: express.Response, cacheObject: CacheObject, duration: number) {
197 const headers = response.getHeaders()
199 if (isTestInstance()) {
200 Object.assign(headers, {
201 'x-api-cache-cached': 'true'
205 Object.assign(headers, this.filterBlacklistedHeaders(cacheObject.headers || {}), {
206 // Set properly decremented max-age header
207 // This ensures that max-age is in sync with the cache expiration
212 (duration / 1000 - (new Date().getTime() / 1000 - cacheObject.timestamp))
216 // unstringify buffers
217 let data = cacheObject.data
218 if (data && data.type === 'Buffer') {
219 data = typeof data.data === 'number'
220 ? Buffer.alloc(data.data)
221 : Buffer.from(data.data)
224 // Test Etag against If-None-Match for 304
225 const cachedEtag = cacheObject.headers.etag
226 const requestEtag = request.headers['if-none-match']
228 if (requestEtag && cachedEtag === requestEtag) {
229 response.writeHead(304, headers)
230 return response.end()
233 response.writeHead(cacheObject.status || 200, headers)
235 return response.end(data, cacheObject.encoding)
238 private clear (target: string) {
239 const redis = Redis.Instance.getClient()
242 clearTimeout(this.timers[target])
243 delete this.timers[target]
248 logger.error('Cannot delete %s in redis cache.', target, { err })
251 this.index.all = this.index.all.filter(key => key !== target)
253 for (const key of this.index.all) {
254 clearTimeout(this.timers[key])
255 delete this.timers[key]
260 logger.error('Cannot delete %s in redis cache.', key, { err })