http-cache-semantics
Advanced tools
Comparing version 3.3.3 to 3.4.0
57
index.js
@@ -6,3 +6,3 @@ 'use strict'; | ||
// This implementation does not understand partial responses (206) | ||
const understoodStatuses = [200, 204, 301, 302, 303, 404, 410, 501]; | ||
const understoodStatuses = [200, 203, 204, 300, 301, 302, 303, 307, 308, 404, 405, 410, 414, 501]; | ||
@@ -75,4 +75,4 @@ const hopByHopHeaders = {'connection':true, 'keep-alive':true, 'proxy-authenticate':true, 'proxy-authorization':true, 'te':true, 'trailers':true, 'transfer-encoding':true, 'upgrade':true}; | ||
this._resHeaders = Object.assign({}, this._resHeaders, {'cache-control': formatCacheControl(this._rescc)}); | ||
delete this._resHeaders['expires']; | ||
delete this._resHeaders['pragma']; | ||
delete this._resHeaders.expires; | ||
delete this._resHeaders.pragma; | ||
} | ||
@@ -93,3 +93,3 @@ | ||
// The "no-store" request directive indicates that a cache MUST NOT store any part of either this request or any response to it. | ||
return !this._reqcc['no-store'] && | ||
return !!(!this._reqcc['no-store'] && | ||
// A cache MUST NOT store a response to any request, unless: | ||
@@ -116,3 +116,3 @@ // The request method is understood by the cache and defined as being cacheable, and | ||
statusCodeCacheableByDefault.includes(this._status) | ||
); | ||
)); | ||
} | ||
@@ -140,2 +140,19 @@ | ||
if (requestCC['max-age'] && this.age() > requestCC['max-age']) { | ||
return false; | ||
} | ||
if (requestCC['min-fresh'] && this.timeToLive() < 1000*requestCC['min-fresh']) { | ||
return false; | ||
} | ||
// the stored response is either: | ||
// fresh, or allowed to be served stale | ||
if (this.stale()) { | ||
const allowsStale = requestCC['max-stale'] && !this._rescc['must-revalidate'] && (true === requestCC['max-stale'] || requestCC['max-stale'] > this.age() - this.maxAge()); | ||
if (!allowsStale) { | ||
return false; | ||
} | ||
} | ||
// The presented effective request URI and that of the stored response match, and | ||
@@ -147,6 +164,3 @@ return (!this._url || this._url === req.url) && | ||
// selecting header fields nominated by the stored response (if any) match those presented, and | ||
this._varyMatches(req) && | ||
// the stored response is either: | ||
// fresh, or allowed to be served stale | ||
!this.stale() // TODO: allow stale | ||
this._varyMatches(req); | ||
} | ||
@@ -189,2 +203,12 @@ | ||
} | ||
if (headers.warning) { | ||
const warnings = headers.warning.split(/,/).filter(warning => { | ||
return !/^\s*1[0-9][0-9]/.test(warning); | ||
}); | ||
if (!warnings.length) { | ||
delete headers.warning; | ||
} else { | ||
headers.warning = warnings.join(',').trim(); | ||
} | ||
} | ||
headers.age = `${Math.round(this.age())}`; | ||
@@ -208,3 +232,5 @@ return headers; | ||
/** | ||
* Value of the Age header, in seconds, updated for the current time | ||
* Value of the Age header, in seconds, updated for the current time. | ||
* May be fractional. | ||
* | ||
* @return Number | ||
@@ -215,6 +241,4 @@ */ | ||
if (this._resHeaders.age) { | ||
let ageValue = parseInt(this._resHeaders.age); | ||
if (isFinite(ageValue)) { | ||
if (ageValue > age) age = ageValue; | ||
} | ||
let ageValue = this._ageValue(); | ||
if (ageValue > age) age = ageValue; | ||
} | ||
@@ -226,2 +250,7 @@ | ||
_ageValue() { | ||
const ageValue = parseInt(this._resHeaders.age); | ||
return isFinite(ageValue) ? ageValue : 0; | ||
} | ||
maxAge() { | ||
@@ -228,0 +257,0 @@ if (!this.storable() || this._rescc['no-cache']) { |
{ | ||
"name": "http-cache-semantics", | ||
"version": "3.3.3", | ||
"version": "3.4.0", | ||
"description": "Parses Cache-Control headers and friends", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,6 +14,18 @@ # Can I cache this? | ||
// throw the response away, it's not usable at all | ||
return; | ||
} | ||
if (policy.satisfiesWithoutRevalidation(newRequest)) { | ||
// the previous `response` can be used to respond to the `newRequest` | ||
// Cache the data AND the policy object in your cache | ||
// (this is pseudocode, roll your own cache (lru-cache package works)) | ||
letsPretendThisIsSomeCache.set(request.url, {policy, response}, policy.timeToLive()); | ||
``` | ||
```js | ||
// And later, when you receive a new request: | ||
const {policy, response} = letsPretendThisIsSomeCache.get(newRequest.url); | ||
// It's not enough that it exists in the cache, it has to match the new request, too: | ||
if (policy && policy.satisfiesWithoutRevalidation(newRequest)) { | ||
// OK, the previous `response` can be used to respond to the `newRequest` | ||
return response; | ||
} | ||
@@ -101,2 +113,2 @@ ``` | ||
* No support for revalidation and stale responses | ||
* No support for revalidation |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
88602
10
999
113
1