api-cache.ts 8.5 KB

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