Browse Source

Class syntax

master
Kornel Lesiński 9 years ago
parent
commit
4508bd4b1d
  1. 30
      index.js

30
index.js

@ -22,7 +22,8 @@ function parseCacheControl(header) {
return cc; return cc;
} }
function CachePolicy(req, res, {shared, cacheHeuristic} = {}) { module.exports = class CachePolicy {
constructor(req, res, {shared, cacheHeuristic} = {}) {
if (!res || !res.headers) { if (!res || !res.headers) {
throw Error("Response headers missing"); throw Error("Response headers missing");
} }
@ -51,10 +52,9 @@ function CachePolicy(req, res, {shared, cacheHeuristic} = {}) {
} }
} }
CachePolicy.prototype = {
now() { now() {
return Date.now(); return Date.now();
}, }
storable() { storable() {
// The "no-store" request directive indicates that a cache MUST NOT store any part of either this request or any response to it. // The "no-store" request directive indicates that a cache MUST NOT store any part of either this request or any response to it.
@ -81,14 +81,14 @@ CachePolicy.prototype = {
// has a status code that is defined as cacheable by default // has a status code that is defined as cacheable by default
statusCodeCacheableByDefault.includes(this._status) statusCodeCacheableByDefault.includes(this._status)
); );
}, }
_hasExplicitExpiration() { _hasExplicitExpiration() {
// 4.2.1 Calculating Freshness Lifetime // 4.2.1 Calculating Freshness Lifetime
return (this._isShared && this._rescc['s-maxage']) || return (this._isShared && this._rescc['s-maxage']) ||
this._rescc['max-age'] || this._rescc['max-age'] ||
this._resHeaders.expires; this._resHeaders.expires;
}, }
satisfiesWithoutRevalidation(req) { satisfiesWithoutRevalidation(req) {
if (!req || !req.headers) { if (!req || !req.headers) {
@ -113,12 +113,12 @@ CachePolicy.prototype = {
// the stored response is either: // the stored response is either:
// fresh, or allowed to be served stale // fresh, or allowed to be served stale
!this.stale() // TODO: allow stale !this.stale() // TODO: allow stale
}, }
_allowsStoringAuthenticated() { _allowsStoringAuthenticated() {
// following Cache-Control response directives (Section 5.2.2) have such an effect: must-revalidate, public, and s-maxage. // following Cache-Control response directives (Section 5.2.2) have such an effect: must-revalidate, public, and s-maxage.
return this._rescc['must-revalidate'] || this._rescc.public || this._rescc['s-maxage']; return this._rescc['must-revalidate'] || this._rescc.public || this._rescc['s-maxage'];
}, }
_varyMatches(req) { _varyMatches(req) {
if (!this._resHeaders.vary) { if (!this._resHeaders.vary) {
@ -135,7 +135,7 @@ CachePolicy.prototype = {
if (req.headers[name] !== this._reqHeaders[name]) return false; if (req.headers[name] !== this._reqHeaders[name]) return false;
} }
return true; return true;
}, }
responseHeaders() { responseHeaders() {
const headers = {}; const headers = {};
@ -152,7 +152,7 @@ CachePolicy.prototype = {
} }
headers.age = `${Math.round(this.age())}`; headers.age = `${Math.round(this.age())}`;
return headers; return headers;
}, }
/** /**
* Value of the Date response header or current time if Date was demed invalid * Value of the Date response header or current time if Date was demed invalid
@ -165,7 +165,7 @@ CachePolicy.prototype = {
return this._responseTime; return this._responseTime;
} }
return dateValue; return dateValue;
}, }
/** /**
* Value of the Age header, in seconds, updated for the current time * Value of the Age header, in seconds, updated for the current time
@ -182,7 +182,7 @@ CachePolicy.prototype = {
const residentTime = (this.now() - this._responseTime)/1000; const residentTime = (this.now() - this._responseTime)/1000;
return age + residentTime; return age + residentTime;
}, }
maxAge() { maxAge() {
if (!this.storable() || this._rescc['no-cache']) { if (!this.storable() || this._rescc['no-cache']) {
@ -231,15 +231,13 @@ CachePolicy.prototype = {
} }
} }
return 0; return 0;
}, }
timeToLive() { timeToLive() {
return Math.max(0, this.maxAge() - this.age())*1000; return Math.max(0, this.maxAge() - this.age())*1000;
}, }
stale() { stale() {
return this.maxAge() <= this.age(); return this.maxAge() <= this.age();
}, }
}; };
module.exports = CachePolicy;

Loading…
Cancel
Save