Skip to content

Commit

Permalink
Wrap Cache-Control calculations in if check for headers
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechilds committed Dec 29, 2016
1 parent db3964e commit 47550d4
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,28 @@ expired.on = headers => {
// Parse headers if we got a raw string
headers = (typeof headers === 'string') ? parse(headers) : headers;

// Date from headers
const originDate = new Date(headers.date);
let expiredOn = new Date();

// Get max age ms
let maxAge = headers['cache-control'] && headers['cache-control'].match(/max-age=(\d+)/);
maxAge = parseInt(maxAge ? maxAge[1] : 0, 10);
// Prefer Cache-Control
if (headers['cache-control']) {
// Date from headers
const originDate = new Date(headers.date);

// Take current age into account
if (headers.age) {
maxAge -= headers.age;
// Get max age ms
let maxAge = headers['cache-control'].match(/max-age=(\d+)/);
maxAge = parseInt(maxAge ? maxAge[1] : 0, 10);

// Take current age into account
if (headers.age) {
maxAge -= headers.age;
}

// Calculate expirey date
expiredOn = addSeconds(originDate, maxAge);
}

// Calculate expirey date
return addSeconds(originDate, maxAge);
// Return expirey date
return expiredOn;
};

module.exports = expired;

0 comments on commit 47550d4

Please sign in to comment.