Skip to content

Commit

Permalink
Drop trustServerDate
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Feb 13, 2020
1 parent eb7028f commit 2c2fac2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
27 changes: 4 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ module.exports = class CachePolicy {
cacheHeuristic,
immutableMinTimeToLive,
ignoreCargoCult,
trustServerDate,
_fromObject,
} = {}
) {
Expand All @@ -103,8 +102,6 @@ module.exports = class CachePolicy {

this._responseTime = this.now();
this._isShared = shared !== false;
this._trustServerDate =
undefined !== trustServerDate ? trustServerDate : true;
this._cacheHeuristic =
undefined !== cacheHeuristic ? cacheHeuristic : 0.1; // 10% matches IE
this._immutableMinTtl =
Expand Down Expand Up @@ -331,24 +328,13 @@ module.exports = class CachePolicy {
}

/**
* 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 invalid
* @return timestamp
*/
date() {
if (this._trustServerDate) {
return this._serverDate();
}
return this._responseTime;
}

_serverDate() {
const serverDate = Date.parse(this._resHeaders.date);
if (isFinite(serverDate)) {
const maxClockDrift = 8 * 3600 * 1000;
const clockDrift = Math.abs(this._responseTime - serverDate);
if (clockDrift < maxClockDrift) {
return serverDate;
}
return serverDate;
}
return this._responseTime;
}
Expand All @@ -360,11 +346,7 @@ module.exports = class CachePolicy {
* @return Number
*/
age() {
let age = Math.max(0, (this._responseTime - this.date()) / 1000);
if (this._resHeaders.age) {
let ageValue = this._ageValue();
if (ageValue > age) age = ageValue;
}
let age = this._ageValue();

const residentTime = (this.now() - this._responseTime) / 1000;
return age + residentTime;
Expand Down Expand Up @@ -419,7 +401,7 @@ module.exports = class CachePolicy {

const defaultMinTtl = this._rescc.immutable ? this._immutableMinTtl : 0;

const serverDate = this._serverDate();
const serverDate = this.date();
if (this._resHeaders.expires) {
const expires = Date.parse(this._resHeaders.expires);
// A cache recipient MUST interpret invalid date formats, especially the value "0", as representing a time in the past (i.e., "already expired").
Expand Down Expand Up @@ -644,7 +626,6 @@ module.exports = class CachePolicy {
shared: this._isShared,
cacheHeuristic: this._cacheHeuristic,
immutableMinTimeToLive: this._immutableMinTtl,
trustServerDate: this._trustServerDate,
}),
modified: false,
matches: true,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "http-cache-semantics",
"version": "4.0.4",
"version": "4.1.0",
"description": "Parses Cache-Control and other headers. Helps building correct HTTP caches and proxies",
"repository": "https://github.com/kornelski/http-cache-semantics.git",
"main": "index.js",
Expand Down
10 changes: 5 additions & 5 deletions test/okhttptest.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('okhttp tests', function() {
{ shared: false }
);

assert(cache.stale());
assert(!cache.stale());
});

it('max age preferred over lower shared max age', function() {
Expand All @@ -180,7 +180,7 @@ describe('okhttp tests', function() {
{ headers: {} },
{
headers: {
date: formatDate(-3, 60),
age: 360,
'cache-control': 's-maxage=60, max-age=180',
},
},
Expand Down Expand Up @@ -262,7 +262,7 @@ describe('okhttp tests', function() {
{
headers: {
'last-modified': formatDate(-2, 3600),
date: formatDate(-1, 60),
age: 60,
expires: formatDate(1, 3600),
},
},
Expand Down Expand Up @@ -325,7 +325,7 @@ describe('okhttp tests', function() {
{
headers: {
'cache-control': 'max-age=120',
date: formatDate(-4, 60),
age: 4*60,
},
},
{ shared: false }
Expand Down Expand Up @@ -364,7 +364,7 @@ describe('okhttp tests', function() {
{
headers: {
'cache-control': 'max-age=120, must-revalidate',
date: formatDate(-4, 60),
age: 360,
},
},
{ shared: false }
Expand Down

0 comments on commit 2c2fac2

Please sign in to comment.