Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

expired

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

expired - npm Package Compare versions

Comparing version 0.1.0 to 1.0.0

README.md

19

package.json
{
"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));
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc