arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/express/node_modules/fresh/index.js
node_modules/express/node_modules/fresh/index.jsRaw
 1
 2/**
 3 * Expose `fresh()`.
 4 */
 5
 6module.exports = fresh;
 7
 8/**
 9 * Check freshness of `req` and `res` headers.
10 *
11 * When the cache is "fresh" __true__ is returned,
12 * otherwise __false__ is returned to indicate that
13 * the cache is now stale.
14 *
15 * @param {Object} req
16 * @param {Object} res
17 * @return {Boolean}
18 * @api public
19 */
20
21function fresh(req, res) {
22  // defaults
23  var etagMatches = true;
24  var notModified = true;
25
26  // fields
27  var modifiedSince = req['if-modified-since'];
28  var noneMatch = req['if-none-match'];
29  var lastModified = res['last-modified'];
30  var etag = res['etag'];
31  var cc = req['cache-control'];
32
33  // unconditional request
34  if (!modifiedSince && !noneMatch) return false;
35
36  // check for no-cache cache request directive
37  if (cc && cc.indexOf('no-cache') !== -1) return false;  
38
39  // parse if-none-match
40  if (noneMatch) noneMatch = noneMatch.split(/ *, */);
41
42  // if-none-match
43  if (noneMatch) {
44    etagMatches = noneMatch.some(function (match) {
45      return match === '*' || match === etag || match === 'W/' + etag;
46    });
47  }
48
49  // if-modified-since
50  if (modifiedSince) {
51    modifiedSince = new Date(modifiedSince);
52    lastModified = new Date(lastModified);
53    notModified = lastModified <= modifiedSince;
54  }
55
56  return !! (etagMatches && notModified);
57}