Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lukechilds/expired
Calculate when HTTP cache headers expire
https://github.com/lukechilds/expired
cache headers http
Last synced: 10 days ago
JSON representation
Calculate when HTTP cache headers expire
- Host: GitHub
- URL: https://github.com/lukechilds/expired
- Owner: lukechilds
- License: mit
- Created: 2016-12-22T11:51:06.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-03-17T19:17:19.000Z (over 3 years ago)
- Last Synced: 2024-10-24T16:53:49.000Z (17 days ago)
- Topics: cache, headers, http
- Language: JavaScript
- Size: 74.2 KB
- Stars: 74
- Watchers: 6
- Forks: 4
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# expired
> Calculate when HTTP responses expire from the cache headers
[![Build Status](https://travis-ci.org/lukechilds/expired.svg?branch=master)](https://travis-ci.org/lukechilds/expired)
[![Coverage Status](https://coveralls.io/repos/github/lukechilds/expired/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/expired?branch=master)
[![npm](https://img.shields.io/npm/v/expired.svg)](https://www.npmjs.com/package/expired)`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 ([Why?](http://stackoverflow.com/a/7549558/5625059)).
## Install
```shell
npm install --save expired
```## Usage
```js
const expired = require('expired');const headers = `
Age: 0
Cache-Control: public, max-age=300
Content-Encoding: gzip
Content-Type: application/json;charset=utf-8
Date: Fri, 23 Dec 2016 05:50:31 GMT
Last-Modified: Fri, 23 Dec 2016 05:23:23 GMT`;expired(headers);
// falseexpired.in(headers);
// 500000expired.on(headers);
// Date('2016-12-23T05:55:31.000Z')delay(600000).then(() => {
expired(headers);
// trueexpired.in(headers);
// -100000expired.on(headers);
// Date('2016-12-23T05:55:31.000Z')});
```Many HTTP modules will parse response headers into an object for you. `expired` will also accept headers in this format:
```js
const expired = require('expired');const headers = {
'age': '0',
'cache-control': 'public, max-age=300',
'content-encoding': 'gzip',
'content-type': 'application/json;charset=utf-8',
'date': 'Fri, 23 Dec 2016 05:50:31 GMT',
'last-modified': 'Fri, 23 Dec 2016 05:23:23 GMT'
};expired(headers);
// false
```## Pure Usage
You can make the functions pure by passing in a JavaScript `Date` object to compare to instead of depending on `new Date()`. This isn't necessary for `expired.on` as it doesn't compare dates and is already pure.
The following are all pure functions:
```js
const headers = `...`;
const date = new Date();expired(headers, date);
expired.in(headers, date);
expired.on(headers);
```## API
### expired(headers, [date])
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, [date])
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)
Returns a JavaScript `Date` object for the date the resource will expire.
## License
MIT © Luke Childs