arnaucube
arnaucube/comunicationLeapPublic Self-hosted repository
comunicationLeap/node_modules/fresh/index.js
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
32  // unconditional request
33  if (!modifiedSince && !noneMatch) return false;
34
35  // parse if-none-match
36  if (noneMatch) noneMatch = noneMatch.split(/ *, */);
37
38  // if-none-match
39  if (noneMatch) etagMatches = ~noneMatch.indexOf(etag) || '*' == noneMatch[0];
40
41  // if-modified-since
42  if (modifiedSince) {
43    modifiedSince = new Date(modifiedSince);
44    lastModified = new Date(lastModified);
45    notModified = lastModified <= modifiedSince;
46  }
47
48  return !! (etagMatches && notModified);
49}