2
1

api-cache.ts 7.7 KB

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