Skip to content

Commit 2c2fac2

Browse files
committed
Drop trustServerDate
1 parent eb7028f commit 2c2fac2

File tree

3 files changed

+10
-29
lines changed

3 files changed

+10
-29
lines changed

index.js

+4-23
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ module.exports = class CachePolicy {
8787
cacheHeuristic,
8888
immutableMinTimeToLive,
8989
ignoreCargoCult,
90-
trustServerDate,
9190
_fromObject,
9291
} = {}
9392
) {
@@ -103,8 +102,6 @@ module.exports = class CachePolicy {
103102

104103
this._responseTime = this.now();
105104
this._isShared = shared !== false;
106-
this._trustServerDate =
107-
undefined !== trustServerDate ? trustServerDate : true;
108105
this._cacheHeuristic =
109106
undefined !== cacheHeuristic ? cacheHeuristic : 0.1; // 10% matches IE
110107
this._immutableMinTtl =
@@ -331,24 +328,13 @@ module.exports = class CachePolicy {
331328
}
332329

333330
/**
334-
* Value of the Date response header or current time if Date was demed invalid
331+
* Value of the Date response header or current time if Date was invalid
335332
* @return timestamp
336333
*/
337334
date() {
338-
if (this._trustServerDate) {
339-
return this._serverDate();
340-
}
341-
return this._responseTime;
342-
}
343-
344-
_serverDate() {
345335
const serverDate = Date.parse(this._resHeaders.date);
346336
if (isFinite(serverDate)) {
347-
const maxClockDrift = 8 * 3600 * 1000;
348-
const clockDrift = Math.abs(this._responseTime - serverDate);
349-
if (clockDrift < maxClockDrift) {
350-
return serverDate;
351-
}
337+
return serverDate;
352338
}
353339
return this._responseTime;
354340
}
@@ -360,11 +346,7 @@ module.exports = class CachePolicy {
360346
* @return Number
361347
*/
362348
age() {
363-
let age = Math.max(0, (this._responseTime - this.date()) / 1000);
364-
if (this._resHeaders.age) {
365-
let ageValue = this._ageValue();
366-
if (ageValue > age) age = ageValue;
367-
}
349+
let age = this._ageValue();
368350

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

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

422-
const serverDate = this._serverDate();
404+
const serverDate = this.date();
423405
if (this._resHeaders.expires) {
424406
const expires = Date.parse(this._resHeaders.expires);
425407
// A cache recipient MUST interpret invalid date formats, especially the value "0", as representing a time in the past (i.e., "already expired").
@@ -644,7 +626,6 @@ module.exports = class CachePolicy {
644626
shared: this._isShared,
645627
cacheHeuristic: this._cacheHeuristic,
646628
immutableMinTimeToLive: this._immutableMinTtl,
647-
trustServerDate: this._trustServerDate,
648629
}),
649630
modified: false,
650631
matches: true,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-cache-semantics",
3-
"version": "4.0.4",
3+
"version": "4.1.0",
44
"description": "Parses Cache-Control and other headers. Helps building correct HTTP caches and proxies",
55
"repository": "https://github.com/kornelski/http-cache-semantics.git",
66
"main": "index.js",

test/okhttptest.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ describe('okhttp tests', function() {
157157
{ shared: false }
158158
);
159159

160-
assert(cache.stale());
160+
assert(!cache.stale());
161161
});
162162

163163
it('max age preferred over lower shared max age', function() {
@@ -180,7 +180,7 @@ describe('okhttp tests', function() {
180180
{ headers: {} },
181181
{
182182
headers: {
183-
date: formatDate(-3, 60),
183+
age: 360,
184184
'cache-control': 's-maxage=60, max-age=180',
185185
},
186186
},
@@ -262,7 +262,7 @@ describe('okhttp tests', function() {
262262
{
263263
headers: {
264264
'last-modified': formatDate(-2, 3600),
265-
date: formatDate(-1, 60),
265+
age: 60,
266266
expires: formatDate(1, 3600),
267267
},
268268
},
@@ -325,7 +325,7 @@ describe('okhttp tests', function() {
325325
{
326326
headers: {
327327
'cache-control': 'max-age=120',
328-
date: formatDate(-4, 60),
328+
age: 4*60,
329329
},
330330
},
331331
{ shared: false }
@@ -364,7 +364,7 @@ describe('okhttp tests', function() {
364364
{
365365
headers: {
366366
'cache-control': 'max-age=120, must-revalidate',
367-
date: formatDate(-4, 60),
367+
age: 360,
368368
},
369369
},
370370
{ shared: false }

0 commit comments

Comments
 (0)