Sha256: 86749509a0f818dfa58c1df9e7ad48cea83107b23bcb39d22d8b0a2ed84e5cbd

Contents?: true

Size: 1.27 KB

Versions: 16

Compression:

Stored size: 1.27 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.some(function (match) {
      return match === '*' || match === etag || match === 'W/' + etag;
    });
  }

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

  return !! (etagMatches && notModified);
}

Version data entries

16 entries across 16 versions & 4 rubygems

Version Path
lanes-0.8.0 node_modules/fresh/index.js
blackboard-3.1.9 lib/site_template/node_modules/fresh/index.js
webpack_rails-2.0.1 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-2.0.0 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-1.3.1 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-1.3.0 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-1.2.2 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-1.2.1 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-1.2.0 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-1.1.1 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-1.1.0 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-1.0.1 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
webpack_rails-1.0.0 lib/webpack_rails/node_modules/express/node_modules/fresh/index.js
gulp_assets-1.0.0.pre.5 template/node_modules/webpack-dev-server/node_modules/express/node_modules/fresh/index.js
gulp_assets-1.0.0.pre.4 template/node_modules/webpack-dev-server/node_modules/express/node_modules/fresh/index.js
gulp_assets-1.0.0.pre.3 template/node_modules/webpack-dev-server/node_modules/express/node_modules/fresh/index.js