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 1.0.0 to 1.1.0

2

package.json
{
"name": "expired",
"version": "1.0.0",
"version": "1.1.0",
"description": "Calculate when HTTP responses expire from the cache headers",

@@ -5,0 +5,0 @@ "main": "src/index.js",

@@ -7,3 +7,3 @@ # expired

`expired` accepts HTTP headers as an argument and will return information on when the resource will expire.
`expired` accepts HTTP headers as an argument and will return information on when the resource will expire. `Cache-Control` and `Expires` headers are supported, if both exist `Cache-Control` takes priority.

@@ -33,5 +33,2 @@ ## Install

new Date()
// Date('2016-12-23T05:50:31.000Z')
expired(headers)

@@ -48,5 +45,2 @@ // false

new Date()
// Date('2016-12-23T05:56:31.000Z')
expired(headers)

@@ -84,13 +78,13 @@ // true

### API
## API
#### expired(headers)
### expired(headers)
Returns a boolean relating to whether the resource has expired or not. `true` means it's expired, `false` means it's fresh.
#### expired.in(headers)
### expired.in(headers)
Returns the amount of milliseconds until the resource will expire. If the resource has already expired it will return a negative integer.
Returns the amount of milliseconds from the current date until the resource will expire. If the resource has already expired it will return a negative integer.
#### expired.on(headers)
### expired.on(headers)

@@ -97,0 +91,0 @@ Returns a JavaScript `Date` object for the date the resource will expire.

@@ -17,18 +17,30 @@ const isPast = require('date-fns/is_past');

// 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);
// Fall back to Expires if it exists
} else if (headers.expires) {
expiredOn = new Date(headers.expires);
}
// Calculate expirey date
return addSeconds(originDate, maxAge);
// Return expirey date
return expiredOn;
};
module.exports = expired;
import test from 'ava';
import tk from 'timekeeper';
import addSeconds from 'date-fns/add_seconds';

@@ -21,5 +20,3 @@ import isEqual from 'date-fns/is_equal';

tk.freeze(date);
t.true(isEqual(expired.on(headers), expiredOn));
tk.reset();
});

@@ -35,5 +32,3 @@

tk.freeze(date);
t.true(isEqual(expired.on(headers), date));
tk.reset();
});

@@ -52,5 +47,3 @@

tk.freeze(date);
t.true(isEqual(expired.on(headers), expiredOn));
tk.reset();
});

@@ -69,5 +62,28 @@

tk.freeze(date);
t.true(isEqual(expired.on(headers), expiredOn));
tk.reset();
});
test('expired.on uses Expires header', t => {
const date = new Date().toUTCString();
const headers = {
date: addSeconds(date, 300),
expires: date
};
t.true(isEqual(expired.on(headers), date));
});
test('expired.on prefers Cache-Control over Expires header', t => {
const date = new Date().toUTCString();
const age = 150;
const maxAge = 300;
const headers = {
date: date,
age: age,
'cache-control': `public, max-age=${maxAge}`,
expires: date
};
const expiredOn = addSeconds(date, (maxAge - age));
t.true(isEqual(expired.on(headers), expiredOn));
});
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