-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
449 lines (355 loc) · 14.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
'use strict'
const Store = require('./lib/store')
const responseKeys = [ 'status', 'message', 'header', 'body' ]
const querystring = require('querystring')
const StringDecoder = require('string_decoder').StringDecoder
const zlib = require('zlib')
class Cache {
configure(routes, options) {
if (options.debug) {
if (options.logging && options.logging instanceof Function) {
options._debug = options.logging
}
else {
const util = require('util')
options._debug = function() {
process.stdout.write((new Date).toISOString() + ' '
+ util.format.apply(null, arguments) + '\n')
}
}
options._debug('cache options:', options)
}
this.routes = routes
this.options = options
return this
}
init() {
if (!this.routes)
this.routes = { '*': 'increasing' }
if (!this.options)
this.options = {}
this.options.expireOpts = new Map()
this.options.defaultTimeout = 5000
this.store = new Store(this.options)
for (let key of Object.keys(this.routes)) {
// validate and reorganize route values
if (typeof this.routes[key] !== 'object'
&& typeof this.routes[key] !== 'boolean'
&& this.routes[key] !== 'increasing'
&& isNaN(this.routes[key])) {
if (this.options.debug) this.options._debug('invalid value for key', key)
delete this.routes[key]
continue
}
if (!isNaN(this.routes[key]))
this.routes[key] = {
timeout: this.routes[key]
}
else if (this.routes[key] === 'increasing') {
this.routes[key] = {
timeout: 'increasing'
}
if (!this.callCount) this.callCount = new Map()
}
if (this.routes[key].cacheKeyArgs instanceof Array) {
if (this.options.debug) this.options._debug('cacheKeyArgs of array type not supported:', key)
delete this.routes[key]
continue
}
if (this.routes[key].cacheKeyArgs) {
if (typeof this.routes[key].cacheKeyArgs === 'string') {
this.routes[key].cacheKeyArgs = {
custom: this.routes[key].cacheKeyArgs
}
}
else {
if (typeof this.routes[key].cacheKeyArgs.headers === 'string')
this.routes[key].cacheKeyArgs.headers = [ this.routes[key].cacheKeyArgs.headers ]
else if (this.routes[key].cacheKeyArgs.headers instanceof Array)
this.routes[key].cacheKeyArgs.headers = this.routes[key].cacheKeyArgs.headers.sort()
if (typeof this.routes[key].cacheKeyArgs.query === 'string')
this.routes[key].cacheKeyArgs.query = [ this.routes[key].cacheKeyArgs.query ]
else if (this.routes[key].cacheKeyArgs.query instanceof Array)
this.routes[key].cacheKeyArgs.query = this.routes[key].cacheKeyArgs.query.sort()
}
}
// parse caching route params
if (key.indexOf(':') !== -1) {
this.routes[key]['regex'] = new RegExp(
key.replace(/:[A-z0-9]+/g, '[A-z0-9\\$\\-_.+!*\'(),]+')
.replace(/^\//, '^\/')
.replace(/$/, '(?:\/)?$')
.replace(/\//g, '\\/'))
}
if (key.indexOf('*') !== -1) {
this.routes[key]['regex'] = new RegExp(
key.replace('*', '.*'))
}
}
this.routeKeys = Object.keys(this.routes)
this.routeKeysLength = this.routeKeys.length
// set default increasing options if not defined
if (this.callCount) {
if (this.options.increasing === undefined) {
this.options.increasing = {
1: 5000,
3: 15000,
10: 30000,
20: 60000,
50: 120000
}
}
this.cntStep = Object.keys(this.options.increasing)
for (let key of this.cntStep) {
if (typeof this.options.increasing[key] === 'string') {
if (this.options.increasing[key].search(/[0-9]+s$/) !== -1)
this.options.increasing[key] = Number(this.options.increasing[key].replace('s', '')) * 1000
else if (this.options.increasing[key].search(/[0-9]+m$/) !== -1)
this.options.increasing[key] = Number(this.options.increasing[key].replace('m', '')) * 60000
else if (this.options.increasing[key].search(/[0-9]+h$/) !== -1)
this.options.increasing[key] = Number(this.options.increasing[key].replace('h', '')) * 60000 * 60
else if (this.options.increasing[key].search(/[0-9]+d$/) !== -1)
this.options.increasing[key] = Number(this.options.increasing[key].replace('d', '')) * 60000 * 60 * 24
else {
if (this.options.debug) this.options._debug('increasing timeout value invalid:', this.options.increasing[key])
delete this.options.increasing[key]
}
}
}
// clear call hit counter every minute
setInterval(() => {
if (this.options.debug) this.options._debug('clearing call hit counter')
this.callCount = new Map()
}, 60000)
}
return this
}
middleware() {
var that = this
if (!this.store) this.init()
return function *(next) {
try {
// check if route is permitted to be cached FIXME?
if (!that.routeKeysLength) return yield next;
// create key
let cacheKey, requestKey = this.request.path
for (let i = 0; i < that.routeKeysLength; i++) {
cacheKey = that.routeKeys[i]
// first pass - exact match
if (cacheKey === this.request.path) {
if (that.options.debug)
that.options._debug('exact matched route:', this.request.path)
if (that.routes[cacheKey].cacheKeyArgs)
requestKey = yield setRequestKey(requestKey, cacheKey, this.request)
if (!requestKey) return yield next
let ok = yield setExpires(i, requestKey)
if (!ok) return yield next
break
}
else if (!that.routes[that.routeKeys[i]].regex) continue
// second pass - regex match
else if (that.routes[that.routeKeys[i]].regex.test(this.request.path)) {
if (that.options.debug)
that.options._debug('regex matched route:', this.request.url, that.routes[that.routeKeys[i]].regex)
if (that.routes[cacheKey].cacheKeyArgs)
requestKey = yield setRequestKey(requestKey, cacheKey, this.request)
if (!requestKey) return yield next
let ok = yield setExpires(i, requestKey)
if (!ok) return yield next
break
}
if (i === that.routeKeys.length - 1) return yield next
else continue
}
// check if no-cache is provided (if not overridden)
if (that.options.ignoreNoCache !== true && this.request.header['cache-control'] === 'no-cache') {
return yield next
}
// check if HTTP methods other than GET are sent and invalidate cache if true
if (this.request.method !== 'GET') {
for (let i = 0; i < that.routeKeysLength; i++) {
let key = that.routeKeys[i]
if (requestKey.indexOf(key) != -1) {
that.store.remove(requestKey)
}
}
return yield next
}
// return cached response
let requestKeyHeaders, requestKeyBody
if (that.options.vary)
for (let o of that.options.vary)
this.response.vary(o)
else
this.response.vary('Accept-Encoding')
let mod = '%'
for (let o of this.response.headers.vary.split(','))
mod += this.request.get(o.trim().toLowerCase())
if ('undefined' !== typeof that.routes[cacheKey].cacheKeyPrefix) {
requestKeyHeaders = that.routes[cacheKey].cacheKeyPrefix + ':' + requestKey + ':headers' + mod
requestKeyBody = that.routes[cacheKey].cacheKeyPrefix + ':' + requestKey + ':body' + mod
}
else if ('undefined' !== typeof that.options.cacheKeyPrefix) {
requestKeyHeaders = that.options.cacheKeyPrefix + ':' + requestKey + ':headers' + mod
requestKeyBody = that.options.cacheKeyPrefix + ':' + requestKey + ':body' + mod
}
else {
requestKeyHeaders = requestKey + ':headers' + mod
requestKeyBody = requestKey + ':body' + mod
}
let exists = yield that.store.has(requestKeyHeaders)
if (exists) {
let body, headers = yield that.store.get(requestKeyHeaders)
if ('string' === typeof(headers)) headers = JSON.parse(headers)
if (that.options.debug) that.options._debug('returning from cache for url', requestKey)
for (let key in headers) {
if (key === 'header') {
let value = headers[key]
for (let hkey in value) {
this.set(hkey, value[hkey])
}
continue
}
this[key] = headers[key]
}
if (this.type === 'application/octet-stream' ||
this.type.indexOf('image/') !== -1 ||
this.request.acceptsEncodings([ 'gzip', 'deflate' ]))
body = yield that.store.getBuffer(requestKeyBody)
else if (this.type === 'text/html' || this.type === 'text/plain') {
body = yield that.store.get(requestKeyBody)
body = JSON.parse(body)
}
else
body = yield that.store.get(requestKeyBody)
if (body) this['body'] = body
return
}
// call next middleware and cache response on return
yield next
if (that.options.minimumSize > this.length)
return yield next
let _response_body, _response_headers = new Object()
for (let key in this.response) {
if (key === 'body') {
if (this.response.body instanceof zlib.Gzip) {
_response_body = yield (() => {
return new Promise((resolve, reject) => {
let body = Buffer.from('')
const decoder = new StringDecoder('utf8')
this.response.body.on('data', (chunk) => body = Buffer.concat([ body, chunk ]))
this.response.body.once('end', () => {
this.response.body = body
resolve(body)
})
this.response.body.once('error', (error) => reject(error))
})
})()
continue
}
_response_body = this.response.body
continue
}
if (responseKeys.indexOf(key) !== -1)
_response_headers[key] = this.response[key]
}
if (that.options.debug) that.options._debug('caching', requestKey)
// set new caching entry
let storeRequest = {}
storeRequest[requestKeyHeaders] = JSON.stringify(_response_headers)
storeRequest[requestKeyBody] = JSON.stringify(_response_body)
that.store.setMultiple(requestKey, storeRequest)
}
catch (error) {
if (that.options.debug) console.error(error)
this.throw(error)
}
}
function *setExpires(routeKeysIndex, requestKey) {
let routeExpire = that.routes[that.routeKeys[routeKeysIndex]].timeout
if (routeExpire === false) {
return false
}
// override default timeout
if (typeof routeExpire === 'boolean') routeExpire = that.options.defaultTimeout
else if (routeExpire === 'increasing' && that.options.increasing) {
let count = that.callCount.has(requestKey)
if (count) {
count = that.callCount.get(requestKey) + 1
that.callCount.set(requestKey, count)
let steps = that.cntStep.length
for (let i = 0; i < steps; i++) {
if (count === that.cntStep[i]) {
that.options.expireOpts.set(requestKey, that.options.increasing[that.cntStep[i]])
break
}
}
}
else {
that.callCount.set(requestKey, 1)
that.options.expireOpts.set(requestKey, that.options.increasing[that.cntStep[0]])
}
}
else that.options.expireOpts.set(requestKey, routeExpire)
return true
}
function *setRequestKey(requestKey, routeKey, ctx) {
// append specified http headers to requestKey
if (that.routes[routeKey].cacheKeyArgs.headers instanceof Array) {
requestKey += '#'
for (let name of that.routes[routeKey].cacheKeyArgs.headers) {
requestKey += ctx.header[name]
}
}
// ...or append all http headers to requestKey
else if (that.routes[routeKey].cacheKeyArgs.headers === true) {
requestKey += '#'
for (let name of Object.keys(ctx.headers)) {
requestKey += ctx.headers[name]
}
}
// append specified http url query parameters to requestKey
if (that.routes[routeKey].cacheKeyArgs.query instanceof Array) {
// if the query combination doesn't match, don't cache
let queryParams = Object.keys(ctx.query)
if (that.routes[routeKey].cacheKeyArgs.query.length !== queryParams.length)
return false
queryParams = queryParams.sort()
for (let i in queryParams) {
if (queryParams[i] !== that.routes[routeKey].cacheKeyArgs.query[i])
return false
}
requestKey += '?' + querystring.stringify(ctx.query)
}
// ...or append all http url query parameters to requestKey
else if (that.routes[routeKey].cacheKeyArgs.query === true) {
requestKey += '?' + ctx.querystring
}
return requestKey
}
}
exists(key) {
return this.store.has(key)
}
clear(keys) {
if (!keys) {
if (this.options.cacheKeyPrefix)
this.store.remove(this.options.cacheKeyPrefix + ':*')
else
this.store.remove()
return
}
if (this.options.cacheKeyPrefix)
this.store.remove([
this.options.cacheKeyPrefix + ':' + keys,
this.options.cacheKeyPrefix + ':' + keys + ':*',
this.options.cacheKeyPrefix + ':' + keys + '\\?*',
this.options.cacheKeyPrefix + ':' + keys + '#*' ])
else
this.store.remove([ keys, keys + ':*', keys + '\\?*', keys + '#*' ])
}
currentCacheType() {
return this.store.storeType()
}
}
module.exports = new Cache()