-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
425 lines (381 loc) · 12.8 KB
/
main.js
File metadata and controls
425 lines (381 loc) · 12.8 KB
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
var CrispCache = require('crisp-cache'),
debug = require('debug')('crisp-http-cache'),
onFinished = require('on-finished'),
parseCacheControl = require('parse-cache-control');
/**
*
* @param {{}} [options]
* @param {boolean|undefined} [options.enabled=true] A master switch of if we should cache or not, useful to set this to false while debugging.
* @param {crispHttpCache~contextCallback} [options.shouldCache="Always true"] An async function that should resolve with a boolean
* @param {crispHttpCache~contextCallback} [options.getKey="Use original req URL as key"] An async function that should resolve with a string key based on the request/response.
* @param {crispHttpCache~contextCallback} [options.getTtl="Get from headers"] An async function that resolves with an integer for the TTL of response.
* @param {crispHttpCache~contextCallback} [options.compareCache="Use Headers to make sure this entry applies to request"] An async function that resolves with boolean if the cached version matches the request.
* @param {crispHttpCache~contextCallback} [options.cacheClientMatch="Check ETag"] An async function that resolves with a boolean if the cached version is the exact version the client is requesting.
* @param {crispHttpCache~contextCallback} [options.transformHeaders] Tries to normalize expires headers for expiration.
* @param {{}} [options.cacheOptions] Caching options sent directly to crisp-cache
*
* @see https://github.com/four43/node-crisp-cache For crisp-cache options
*
* @return {crispHttpCache~middleware}
*/
function CrispHttpCache(options) {
if (options === undefined) {
options = {};
}
this.enabled = (options.enabled !== undefined) ? options.enabled : true;
this.shouldCache = options.shouldCache || _shouldCacheAlways;
this.getKey = options.getKey || _getKeyOrigUrl;
this.getTtl = options.getTtl || _getTtlFromHeaders;
this.compareCache = options.compareCache || _compareCacheWithHeaders;
this.cacheClientMatch = options.cacheClientMatch || _matchModifiedOrETag;
this.transformHeaders = options.transformHeaders || _transformHeaders;
this.cacheOptions = options.cacheOptions || {};
if (!this.cacheOptions.fetcher) {
this.cacheOptions.fetcher = function (key, cb) {
cb(new Error("Fetcher not defined yet."));
}
}
this.backend = new CrispCache(this.cacheOptions);
}
CrispHttpCache.prototype.getUsage = function() {
return this.backend.getUsage();
};
CrispHttpCache.prototype.getExpressMiddleware = function () {
return function (req, res, next) {
if (this.enabled) {
this.shouldCache(req, res, function (err, shouldCache) {
if (err) {
return next(new Error("CrispHttpCache - Provided options.shouldCache function returned an error."));
}
if (shouldCache) {
this.getKey(req, res, function (err, key) {
if (err || typeof key !== 'string' || key.length === 0) {
return next(new Error("CrispHttpCache - Provided options.getKey function returned an error. Should return a string."));
}
this.backend.get(key, {skipFetch: true}, function (err, cacheValue) {
if (cacheValue) {
debug("Cache hit for: " + key);
cacheValue.get = _getHeaders.bind(cacheValue);
this.compareCache(req, cacheValue, function (err, cacheOkay) {
if (err) {
next(new Error("CrispHttpCache - Provided options.compareCache returned an error."));
}
if (cacheOkay) {
this.cacheClientMatch(req, cacheValue, function (err, cachedExactMatch) {
if (cachedExactMatch) {
return res.sendStatus(304);
}
res.set.call(res, cacheValue.headers);
return res.send.call(res, cacheValue.body);
});
}
else {
debug("Cache values did not pass compareCache, re-running.");
this._interceptRes(req, res, key, this.getTtl, this.backend, this.transformHeaders);
return next();
}
}.bind(this));
}
else {
debug("Cache miss for: " + key);
if(this.backend._lock(key, function(){})
) {
debug("- Got lock for: " + key);
// We are the first to hit this key, go fetch
this._interceptRes(req, res, key, this.getTtl, this.backend, this.transformHeaders);
return next();
}
else {
debug("- Waiting for lock: " + key);
this.backend._lock(key, function(key, cacheValue) {
debug("- Resolving lock: " + key);
res.set.call(res, cacheValue.headers);
return res.send.call(res, cacheValue.body);
});
return;
}
}
}.bind(this));
}.bind(this));
}
else {
//shouldCache function said we should skip caching.
return next();
}
}.bind(this));
}
else {
//Caching disabled, skip
return next();
}
}.bind(this);
};
CrispHttpCache.prototype._interceptRes = function(req, res, key, getTtl, cache) {
saveBody(res);
preFinish(res, function (res, data, cb) {
this.transformHeaders(res);
cb(null, res);
}.bind(this));
onFinished(res, function (err, res) {
var cachedEntry = {
status: res.statusCode,
headers: res._headers,
body: res.body
};
if (res.statusCode < 200 || res.statusCode >= 300) {
debug("Non 2XX status code, not saving");
cache._resolveLocks(key, cachedEntry);
return;
}
debug("Setting cache: " + key);
// Update cache entry's ttl, set and send.
getTtl(req, res, function (err, ttl) {
debug(" - With TTL: " + ttl);
// If we didn't get a hangup, we can cache it.
if (res.body && res.body.length) {
cache.set(key, cachedEntry, {size: res.body.length, expiresTtl: ttl});
}
});
});
};
module.exports = CrispHttpCache;
function preFinish(res, cb) {
var origSend = res.send;
res.send = function (data) {
var sendArgs = arguments;
cb(res, data, function (err, res) {
origSend.apply(res, sendArgs);
});
}
}
function saveBody(res) {
var oldWrite = res.write,
oldEnd = res.end;
var chunks = [];
// Intercept write
res.write = function (chunk) {
chunks.push(chunk);
oldWrite.apply(res, arguments);
};
// Intercept end
res.end = function (chunk) {
if (chunk)
chunks.push(chunk);
res.body = Buffer.concat(chunks);
oldEnd.apply(res, arguments);
};
}
/**
* Determines if we should cache this request or not, executed before all other steps. Defaults to true.
*
* @param {{}} req The request object
* @param {{}} res The response object
* @param {crispHttpCache~errFirstCallbackBool} callback holding the result if we should cache or not.
* @private
*/
function _shouldCacheAlways(req, res, callback) {
return callback(null, true);
}
/**
* Get our cache key based on our request's original request URL.
*
* @param {{}} req The request object
* @param {string} req.originalUrl The original URL of the request
* @param {string} req.method The HTTP verb used to create this response
* @param {{}} res The response object
* @param {crispHttpCache~errFirstCallbackString} callback will be called with (err, {string})
* @private
*/
function _getKeyOrigUrl(req, res, callback) {
return callback(null, req.method + req.originalUrl);
}
/**
* Parse Headers to get specific TTL for caching
*
* @param {{}} req The request object
* @param {{}} res The response object
* @param {crispHttpCache~errFirstCallbackInt} callback will be called with (err, {int}) (Milliseconds for TTL)
* @private
*/
function _getTtlFromHeaders(req, res, callback) {
//cache-control header always takes precedence over expires: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3
if (res.get('cache-control')) {
var cacheControlInfo = parseCacheControl(res.get('cache-control'));
if (!cacheControlInfo) {
return callback(new Error('Failed to implement HTTP caching: failed to parse cache control headers'));
}
debug(cacheControlInfo);
var isPrivate = cacheControlInfo['private'] || cacheControlInfo['no-cache'] || cacheControlInfo['no-store'];
if (cacheControlInfo['s-maxage'] && !isPrivate) {
return callback(null, parseInt(cacheControlInfo['s-maxage']) * 1000);
}
return callback(null, 0);
}
else if (res.get('expires')) {
var now = new Date();
return callback(null, new Date(res.get('expires')).getTime() - now.getTime());
}
else {
callback(new Error("Could not get TTL via headers"));
}
}
/**
* Compare what we have cached with what the user requested, is this cached request applicable to this request?
*
* @param req
* @param cachedResponse
* @param {crispHttpCache~errFirstCallbackBool} callback holding the result if we should cache or not.
* @private
*/
function _compareCacheWithHeaders(req, cachedResponse, callback) {
//Accept
if (cachedResponse.get('content-type')) {
if (req.get('accept')) {
if (!req.accepts(cachedResponse.get('content-type'))) {
return callback(null, false);
}
}
if (req.get('accept-charset')) {
var contentTypeCharset = _parseContentTypeCharset(cachedResponse.get('content-type'))
if (contentTypeCharset && !req.acceptsCharsets(contentTypeCharset)) {
return callback(null, false);
}
}
}
//Accept-Encoding
if (cachedResponse.get('content-encoding') && req.get('accept-encoding')) {
if (!req.acceptsEncodings(cachedResponse.get('content-encoding'))) {
return callback(null, false);
}
}
//Accept-Language
if (cachedResponse.get('content-language') && req.get('accept-language')) {
if (!req.acceptsLanguages(cachedResponse.get('content-language'))) {
return callback(null, false);
}
}
return callback(null, true);
}
/**
* Strictly compare what we have cached with what the user requested, is this cached request the version the user has already?
*
* @param req
* @param cachedResponse
* @param {crispHttpCache~errFirstCallbackBool} callback holding the result if we should cache or not.
* @private
*/
function _matchModifiedOrETag(req, cachedResponse, callback) {
//If-Modified-Since
if (cachedResponse.get('date') && req.get('if-modified-since')) {
if (new Date(cachedResponse.get('date')) <= new Date(req.get('if-modified-since'))) {
return callback(null, true);
}
}
//If-None-Match (ETag)
if (cachedResponse.get('etag') && req.get('if-none-match')) {
if (cachedResponse.get('etag') === req.get('if-none-match')) {
return callback(null, true);
}
}
//No existing cache information provided, the client doesn't seem to have this content yet.
return callback(null, false);
}
function _transformHeaders(res, estExpiresInterval) {
var responseCacheControl = res.get('cache-control'),
responseExpires = _parseDateString(res.get('expires')),
responseDate = res.get('date'),
expiresDeltaMs = 0;
//Known expiration
if (responseExpires !== undefined) {
if (responseExpires instanceof Date) {
expiresDeltaMs = Math.round((responseExpires.getTime() - Date.now()));
}
else if (responseExpires === Infinity) {
expiresDeltaMs = 31622400000; // 1 year
}
else {
//If it's an integer, assume it's delta ms.
expiresDeltaMs = responseExpires;
}
}
else {
if (responseDate && estExpiresInterval) {
expiresDeltaMs = estExpiresInterval;
}
}
if (responseCacheControl) {
res.set('cache-control', responseCacheControl);
}
else {
var expiresDeltaSeconds = Math.round(expiresDeltaMs / 1000);
res.set('cache-control', 'public, max-age=' + expiresDeltaSeconds + ', s-maxage=' + expiresDeltaSeconds);
}
if (expiresDeltaMs > 0) {
res.set('expires', new Date(Date.now() + expiresDeltaMs).toUTCString());
}
else {
//HTTP Spec specifies if the response should immediately expired, a 0 is allowed.
res.set('expires', 0);
}
if (!responseDate) {
res.set('date', new Date());
}
else {
res.set('date', responseDate);
}
}
function _parseDateString(date) {
if (date === 'Infinity') {
return Infinity;
}
else if (!isNaN(parseInt(date))) {
return parseFloat(date);
} else if (!isNaN(Date.parse(date))) {
return new Date(date);
}
else {
return date;
}
}
function _getHeaders(header) {
return this.headers[header.toLowerCase()];
}
function _parseContentTypeCharset(contentTypeString) {
var matches = contentTypeString.match(/charset=(\S+)/);
if (matches) {
return matches[1];
}
return false;
}
/**
* A function that is provided with the current context of the request
* @callback crispHttpCache~contextCallback
* @param {{}} req
* @param {{}} res
* @param {function} callback
*/
/**
* A typical middleware style function, but options may vary based on implementing library: Express, etc.
* @callback crispHttpCache~middleware
* @param {{}} req
* @param {{}} res
* @param {function} next
*/
/**
* An error first callback, boolean result
* @callback crispHttpCache~errFirstCallbackBool
* @param {Error} err
* @param {boolean} [result]
*/
/**
* An error first callback, string result
* @callback crispHttpCache~errFirstCallbackString
* @param {Error} err
* @param {string} [result]
*/
/**
* An error first callback, int result
* @callback crispHttpCache~errFirstCallbackInt
* @param {Error} err
* @param {int} [result]
*/