Skip to content

Commit a897f68

Browse files
committed
Heuristic long expiration warning
1 parent 7f18a19 commit a897f68

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,14 @@ module.exports = class CachePolicy {
221221

222222
responseHeaders() {
223223
const headers = this._copyWithoutHopByHopHeaders(this._resHeaders);
224-
headers.age = `${Math.round(this.age())}`;
224+
const age = this.age();
225+
226+
// A cache SHOULD generate 113 warning if it heuristically chose a freshness
227+
// lifetime greater than 24 hours and the response's age is greater than 24 hours.
228+
if (age > 3600*24 && !this._hasExplicitExpiration() && this.maxAge() > 3600*24) {
229+
headers.warning = (headers.warning ? `${headers.warning}, ` : '') + '113 - "rfc7234 5.5.4"';
230+
}
231+
headers.age = `${Math.round(age)}`;
225232
return headers;
226233
}
227234

test/revalidatetest.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,28 @@ describe('Can be revalidated?', function() {
106106
const headers = cache.revalidationHeaders(simpleRequest);
107107
assertHeadersPassed(headers);
108108
assert.equal(headers['if-modified-since'], 'Tue, 15 Nov 1994 12:45:26 GMT');
109+
assert(!/113/.test(headers.warning));
109110
});
110111

111112
it('not without validators', function() {
112113
const cache = new CachePolicy(simpleRequest, cacheableResponse);
113114
const headers = cache.revalidationHeaders(simpleRequest);
114115
assertHeadersPassed(headers);
115116
assertNoValidators(headers);
117+
assert(!/113/.test(headers.warning));
118+
})
119+
120+
it('113 added', function() {
121+
const veryOldResponse = {
122+
headers: {
123+
age: 3600*72,
124+
'last-modified': 'Tue, 15 Nov 1994 12:45:26 GMT',
125+
},
126+
};
127+
128+
const cache = new CachePolicy(simpleRequest, veryOldResponse);
129+
const headers = cache.responseHeaders(simpleRequest);
130+
assert(/113/.test(headers.warning));
116131
})
117132

118133
});

0 commit comments

Comments
 (0)