Sha256: 70dd2be77f311f2b1467be3a4e13dfc7a10e5b8a2e54dd270646262607b94621

Contents?: true

Size: 1.2 KB

Versions: 17

Compression:

Stored size: 1.2 KB

Contents

/**
 * Expose `fresh()`.
 */

module.exports = fresh;

/**
 * Check freshness of `req` and `res` headers.
 *
 * When the cache is "fresh" __true__ is returned,
 * otherwise __false__ is returned to indicate that
 * the cache is now stale.
 *
 * @param {Object} req
 * @param {Object} res
 * @return {Boolean}
 * @api public
 */

function fresh(req, res) {
  // defaults
  var etagMatches = true;
  var notModified = true;

  // fields
  var modifiedSince = req['if-modified-since'];
  var noneMatch = req['if-none-match'];
  var lastModified = res['last-modified'];
  var etag = res['etag'];
  var cc = req['cache-control'];

  // unconditional request
  if (!modifiedSince && !noneMatch) return false;

  // check for no-cache cache request directive
  if (cc && cc.indexOf('no-cache') !== -1) return false;  

  // parse if-none-match
  if (noneMatch) noneMatch = noneMatch.split(/ *, */);

  // if-none-match
  if (noneMatch) etagMatches = ~noneMatch.indexOf(etag) || '*' == noneMatch[0];

  // if-modified-since
  if (modifiedSince) {
    modifiedSince = new Date(modifiedSince);
    lastModified = new Date(lastModified);
    notModified = lastModified <= modifiedSince;
  }

  return !! (etagMatches && notModified);
}

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
hooch-0.4.2 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
hooch-0.4.1 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
hooch-0.4.0 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
hooch-0.3.0 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
hooch-0.2.1 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
hooch-0.2.0 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
hooch-0.1.0 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
hooch-0.0.8 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
hooch-0.0.7 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
hooch-0.0.6 jasmine/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
entangled-0.0.16 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
entangled-0.0.15 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
entangled-0.0.14 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
entangled-0.0.13 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
entangled-0.0.12 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
entangled-0.0.11 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/fresh/index.js
entangled-0.0.10 spec/dummy/public/node_modules/karma/node_modules/connect/node_modules/fresh/index.js