Comparing version 0.1.0 to 1.0.0
{ | ||
"name": "expired", | ||
"version": "0.1.0", | ||
"description": "Calculate when HTTP cache headers expire", | ||
"version": "1.0.0", | ||
"description": "Calculate when HTTP responses expire from the cache headers", | ||
"main": "src/index.js", | ||
@@ -16,8 +16,10 @@ "scripts": { | ||
"keywords": [ | ||
"calculate", | ||
"HTTP", | ||
"response", | ||
"expires", | ||
"expire", | ||
"expirey", | ||
"cache", | ||
"headers", | ||
"expire", | ||
"expires", | ||
"expirey" | ||
"headers" | ||
], | ||
@@ -31,3 +33,4 @@ "author": "Luke Childs <lukechilds123@gmail.com> (http://lukechilds.co.uk)", | ||
"dependencies": { | ||
"date-fns": "^1.21.1" | ||
"date-fns": "1.21.1", | ||
"parse-headers": "2.0.1" | ||
}, | ||
@@ -37,5 +40,7 @@ "devDependencies": { | ||
"coveralls": "^2.11.15", | ||
"date-fns": "^1.21.1", | ||
"nyc": "^10.0.0", | ||
"timekeeper": "^1.0.0", | ||
"xo": "^0.17.1" | ||
} | ||
} |
const isPast = require('date-fns/is_past'); | ||
const differenceInMilliseconds = require('date-fns/difference_in_milliseconds'); | ||
const addSeconds = require('date-fns/add_seconds'); | ||
const parse = require('parse-headers'); | ||
// Returns boolean for whether or not the cache has expired | ||
const expired = headers => isPast(expired.on(headers)); | ||
// Return ms until cache expires | ||
expired.in = headers => differenceInMilliseconds(expired.on(headers), new Date()); | ||
// Returns date when cache will expire | ||
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); | ||
// Get max age ms | ||
// Get max age ms | ||
let maxAge = headers['cache-control'] && headers['cache-control'].match(/max-age=(\d+)/); | ||
maxAge = parseInt(maxAge ? maxAge[1] : 0, 10); | ||
// Take current age into account | ||
// Take current age into account | ||
if (headers.age) { | ||
@@ -18,6 +29,6 @@ maxAge -= headers.age; | ||
// Calculate expirey date | ||
return addSeconds(new Date(originDate), maxAge); | ||
// Calculate expirey date | ||
return addSeconds(originDate, maxAge); | ||
}; | ||
module.exports = expired; |
import test from 'ava'; | ||
import subSeconds from 'date-fns/sub_seconds'; | ||
import expired from '../'; | ||
@@ -7,1 +8,21 @@ | ||
}); | ||
test('expired returns false for valid cache', t => { | ||
const headers = { | ||
date: new Date().toUTCString(), | ||
age: 0, | ||
'cache-control': 'public, max-age=300' | ||
}; | ||
t.false(expired(headers)); | ||
}); | ||
test('expired returns true for stale cache', t => { | ||
const headers = { | ||
date: subSeconds(new Date().toUTCString(), 500), | ||
age: 0, | ||
'cache-control': 'public, max-age=300' | ||
}; | ||
t.true(expired(headers)); | ||
}); |
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
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10609
10
178
0
1
98
2
6
+ Addedparse-headers@2.0.1
+ Addeddate-fns@1.21.1(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedparse-headers@2.0.1(transitive)
+ Addedtrim@0.0.1(transitive)
- Removeddate-fns@1.30.1(transitive)
Updateddate-fns@1.21.1