fresh 0.2.3 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HISTORY.md +58 -0
- package/LICENSE +23 -0
- package/README.md +113 -0
- package/index.js +60 -32
- package/package.json +27 -14
- package/Readme.md +0 -56
package/HISTORY.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
0.5.0 / 2017-02-21
|
|
2
|
+
==================
|
|
3
|
+
|
|
4
|
+
* Fix incorrect result when `If-None-Match` has both `*` and ETags
|
|
5
|
+
* Fix weak `ETag` matching to match spec
|
|
6
|
+
* perf: delay reading header values until needed
|
|
7
|
+
* perf: skip checking modified time if ETag check failed
|
|
8
|
+
* perf: skip parsing `If-None-Match` when no `ETag` header
|
|
9
|
+
* perf: use `Date.parse` instead of `new Date`
|
|
10
|
+
|
|
11
|
+
0.4.0 / 2017-02-05
|
|
12
|
+
==================
|
|
13
|
+
|
|
14
|
+
* Fix false detection of `no-cache` request directive
|
|
15
|
+
* perf: enable strict mode
|
|
16
|
+
* perf: hoist regular expressions
|
|
17
|
+
* perf: remove duplicate conditional
|
|
18
|
+
* perf: remove unnecessary boolean coercions
|
|
19
|
+
|
|
20
|
+
0.3.0 / 2015-05-12
|
|
21
|
+
==================
|
|
22
|
+
|
|
23
|
+
* Add weak `ETag` matching support
|
|
24
|
+
|
|
25
|
+
0.2.4 / 2014-09-07
|
|
26
|
+
==================
|
|
27
|
+
|
|
28
|
+
* Support Node.js 0.6
|
|
29
|
+
|
|
30
|
+
0.2.3 / 2014-09-07
|
|
31
|
+
==================
|
|
32
|
+
|
|
33
|
+
* Move repository to jshttp
|
|
34
|
+
|
|
35
|
+
0.2.2 / 2014-02-19
|
|
36
|
+
==================
|
|
37
|
+
|
|
38
|
+
* Revert "Fix for blank page on Safari reload"
|
|
39
|
+
|
|
40
|
+
0.2.1 / 2014-01-29
|
|
41
|
+
==================
|
|
42
|
+
|
|
43
|
+
* Fix for blank page on Safari reload
|
|
44
|
+
|
|
45
|
+
0.2.0 / 2013-08-11
|
|
46
|
+
==================
|
|
47
|
+
|
|
48
|
+
* Return stale for `Cache-Control: no-cache`
|
|
49
|
+
|
|
50
|
+
0.1.0 / 2012-06-15
|
|
51
|
+
==================
|
|
52
|
+
|
|
53
|
+
* Add `If-None-Match: *` support
|
|
54
|
+
|
|
55
|
+
0.0.1 / 2012-06-10
|
|
56
|
+
==================
|
|
57
|
+
|
|
58
|
+
* Initial release
|
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
(The MIT License)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
|
|
4
|
+
Copyright (c) 2016-2017 Douglas Christopher Wilson <doug@somethingdoug.com>
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
7
|
+
a copy of this software and associated documentation files (the
|
|
8
|
+
'Software'), to deal in the Software without restriction, including
|
|
9
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
10
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
11
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
12
|
+
the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be
|
|
15
|
+
included in all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
18
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
20
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
21
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
22
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
23
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# fresh
|
|
2
|
+
|
|
3
|
+
[![NPM Version][npm-image]][npm-url]
|
|
4
|
+
[![NPM Downloads][downloads-image]][downloads-url]
|
|
5
|
+
[![Node.js Version][node-version-image]][node-version-url]
|
|
6
|
+
[![Build Status][travis-image]][travis-url]
|
|
7
|
+
[![Test Coverage][coveralls-image]][coveralls-url]
|
|
8
|
+
|
|
9
|
+
HTTP response freshness testing
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
This is a [Node.js](https://nodejs.org/en/) module available through the
|
|
14
|
+
[npm registry](https://www.npmjs.com/). Installation is done using the
|
|
15
|
+
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
$ npm install fresh
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## API
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
var fresh = require('fresh')
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### fresh(reqHeaders, resHeaders)
|
|
28
|
+
|
|
29
|
+
Check freshness of the response using request and response headers.
|
|
30
|
+
|
|
31
|
+
When the response is still "fresh" in the client's cache `true` is
|
|
32
|
+
returned, otherwise `false` is returned to indicate that the client
|
|
33
|
+
cache is now stale and the full response should be sent.
|
|
34
|
+
|
|
35
|
+
When a client sends the `Cache-Control: no-cache` request header to
|
|
36
|
+
indicate an end-to-end reload request, this module will return `false`
|
|
37
|
+
to make handling these requests transparent.
|
|
38
|
+
|
|
39
|
+
## Known Issues
|
|
40
|
+
|
|
41
|
+
This module is designed to only follow the HTTP specifications, not
|
|
42
|
+
to work-around all kinda of client bugs (especially since this module
|
|
43
|
+
typically does not recieve enough information to understand what the
|
|
44
|
+
client actually is).
|
|
45
|
+
|
|
46
|
+
There is a known issue that in certain versions of Safari, Safari
|
|
47
|
+
will incorrectly make a request that allows this module to validate
|
|
48
|
+
freshness of the resource even when Safari does not have a
|
|
49
|
+
representation of the resource in the cache. The module
|
|
50
|
+
[jumanji](https://www.npmjs.com/package/jumanji) can be used in
|
|
51
|
+
an Express application to work-around this issue and also provides
|
|
52
|
+
links to further reading on this Safari bug.
|
|
53
|
+
|
|
54
|
+
## Example
|
|
55
|
+
|
|
56
|
+
### API usage
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
var reqHeaders = { 'if-none-match': '"foo"' }
|
|
60
|
+
var resHeaders = { 'etag': '"bar"' }
|
|
61
|
+
fresh(reqHeaders, resHeaders)
|
|
62
|
+
// => false
|
|
63
|
+
|
|
64
|
+
var reqHeaders = { 'if-none-match': '"foo"' }
|
|
65
|
+
var resHeaders = { 'etag': '"foo"' }
|
|
66
|
+
fresh(reqHeaders, resHeaders)
|
|
67
|
+
// => true
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Using with Node.js http server
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
var fresh = require('fresh')
|
|
74
|
+
var http = require('http')
|
|
75
|
+
|
|
76
|
+
var server = http.createServer(function (req, res) {
|
|
77
|
+
// perform server logic
|
|
78
|
+
// ... including adding ETag / Last-Modified response headers
|
|
79
|
+
|
|
80
|
+
if (isFresh(req, res)) {
|
|
81
|
+
// client has a fresh copy of resource
|
|
82
|
+
res.statusCode = 304
|
|
83
|
+
res.end()
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// send the resource
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
function isFresh (req, res) {
|
|
91
|
+
return fresh(req.headers, {
|
|
92
|
+
'etag': res.getHeader('ETag'),
|
|
93
|
+
'last-modified': res.getHeader('Last-Modified')
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
server.listen(3000)
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
[MIT](LICENSE)
|
|
103
|
+
|
|
104
|
+
[npm-image]: https://img.shields.io/npm/v/fresh.svg
|
|
105
|
+
[npm-url]: https://npmjs.org/package/fresh
|
|
106
|
+
[node-version-image]: https://img.shields.io/node/v/fresh.svg
|
|
107
|
+
[node-version-url]: https://nodejs.org/en/
|
|
108
|
+
[travis-image]: https://img.shields.io/travis/jshttp/fresh/master.svg
|
|
109
|
+
[travis-url]: https://travis-ci.org/jshttp/fresh
|
|
110
|
+
[coveralls-image]: https://img.shields.io/coveralls/jshttp/fresh/master.svg
|
|
111
|
+
[coveralls-url]: https://coveralls.io/r/jshttp/fresh?branch=master
|
|
112
|
+
[downloads-image]: https://img.shields.io/npm/dm/fresh.svg
|
|
113
|
+
[downloads-url]: https://npmjs.org/package/fresh
|
package/index.js
CHANGED
|
@@ -1,53 +1,81 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* fresh
|
|
3
|
+
* Copyright(c) 2012 TJ Holowaychuk
|
|
4
|
+
* Copyright(c) 2016-2017 Douglas Christopher Wilson
|
|
5
|
+
* MIT Licensed
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict'
|
|
1
9
|
|
|
2
10
|
/**
|
|
3
|
-
*
|
|
11
|
+
* RegExp to check for no-cache token in Cache-Control.
|
|
12
|
+
* @private
|
|
4
13
|
*/
|
|
5
14
|
|
|
6
|
-
|
|
15
|
+
var CACHE_CONTROL_NO_CACHE_REGEXP = /(?:^|,)\s*?no-cache\s*?(?:,|$)/
|
|
7
16
|
|
|
8
17
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
18
|
+
* Simple expression to split token list.
|
|
19
|
+
* @private
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
var TOKEN_LIST_REGEXP = / *, */
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Module exports.
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
module.exports = fresh
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Check freshness of the response using request and response headers.
|
|
14
33
|
*
|
|
15
|
-
* @param {Object}
|
|
16
|
-
* @param {Object}
|
|
34
|
+
* @param {Object} reqHeaders
|
|
35
|
+
* @param {Object} resHeaders
|
|
17
36
|
* @return {Boolean}
|
|
18
|
-
* @
|
|
37
|
+
* @public
|
|
19
38
|
*/
|
|
20
39
|
|
|
21
|
-
function fresh(
|
|
22
|
-
// defaults
|
|
23
|
-
var etagMatches = true;
|
|
24
|
-
var notModified = true;
|
|
25
|
-
|
|
40
|
+
function fresh (reqHeaders, resHeaders) {
|
|
26
41
|
// fields
|
|
27
|
-
var modifiedSince =
|
|
28
|
-
var noneMatch =
|
|
29
|
-
var lastModified = res['last-modified'];
|
|
30
|
-
var etag = res['etag'];
|
|
31
|
-
var cc = req['cache-control'];
|
|
42
|
+
var modifiedSince = reqHeaders['if-modified-since']
|
|
43
|
+
var noneMatch = reqHeaders['if-none-match']
|
|
32
44
|
|
|
33
45
|
// unconditional request
|
|
34
|
-
if (!modifiedSince && !noneMatch)
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (cc && cc.indexOf('no-cache') !== -1) return false;
|
|
46
|
+
if (!modifiedSince && !noneMatch) {
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
38
49
|
|
|
39
|
-
//
|
|
40
|
-
|
|
50
|
+
// Always return stale when Cache-Control: no-cache
|
|
51
|
+
// to support end-to-end reload requests
|
|
52
|
+
// https://tools.ietf.org/html/rfc2616#section-14.9.4
|
|
53
|
+
var cacheControl = reqHeaders['cache-control']
|
|
54
|
+
if (cacheControl && CACHE_CONTROL_NO_CACHE_REGEXP.test(cacheControl)) {
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
41
57
|
|
|
42
58
|
// if-none-match
|
|
43
|
-
if (noneMatch
|
|
59
|
+
if (noneMatch && noneMatch !== '*') {
|
|
60
|
+
var etag = resHeaders['etag']
|
|
61
|
+
var etagStale = !etag || noneMatch.split(TOKEN_LIST_REGEXP).every(function (match) {
|
|
62
|
+
return match !== etag && match !== 'W/' + etag && 'W/' + match !== etag
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
if (etagStale) {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
}
|
|
44
69
|
|
|
45
70
|
// if-modified-since
|
|
46
71
|
if (modifiedSince) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
72
|
+
var lastModified = resHeaders['last-modified']
|
|
73
|
+
var modifiedStale = !lastModified || Date.parse(lastModified) > Date.parse(modifiedSince)
|
|
74
|
+
|
|
75
|
+
if (modifiedStale) {
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
50
78
|
}
|
|
51
79
|
|
|
52
|
-
return
|
|
53
|
-
}
|
|
80
|
+
return true
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,27 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fresh",
|
|
3
|
-
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",
|
|
4
3
|
"description": "HTTP response freshness testing",
|
|
5
|
-
"version": "0.
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
},
|
|
4
|
+
"version": "0.5.0",
|
|
5
|
+
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Douglas Christopher Wilson <doug@somethingdoug.com>",
|
|
8
|
+
"Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)"
|
|
9
|
+
],
|
|
12
10
|
"license": "MIT",
|
|
13
|
-
"scripts": {
|
|
14
|
-
"test": "mocha --reporter spec --require should",
|
|
15
|
-
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --require should",
|
|
16
|
-
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot --require should"
|
|
17
|
-
},
|
|
18
11
|
"keywords": [
|
|
19
12
|
"fresh",
|
|
20
13
|
"http",
|
|
21
14
|
"conditional",
|
|
22
15
|
"cache"
|
|
23
16
|
],
|
|
17
|
+
"repository": "jshttp/fresh",
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"eslint": "3.16.0",
|
|
20
|
+
"eslint-config-standard": "6.2.1",
|
|
21
|
+
"eslint-plugin-promise": "3.4.2",
|
|
22
|
+
"eslint-plugin-standard": "2.0.1",
|
|
23
|
+
"istanbul": "0.4.5",
|
|
24
|
+
"mocha": "1.21.5"
|
|
25
|
+
},
|
|
24
26
|
"files": [
|
|
27
|
+
"HISTORY.md",
|
|
28
|
+
"LICENSE",
|
|
25
29
|
"index.js"
|
|
26
|
-
]
|
|
30
|
+
],
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">= 0.6"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"lint": "eslint .",
|
|
36
|
+
"test": "mocha --reporter spec --bail --check-leaks test/",
|
|
37
|
+
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
|
|
38
|
+
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
|
|
39
|
+
}
|
|
27
40
|
}
|
package/Readme.md
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
# fresh
|
|
3
|
-
|
|
4
|
-
[![NPM version][npm-image]][npm-url]
|
|
5
|
-
[![Build status][travis-image]][travis-url]
|
|
6
|
-
[![Test coverage][coveralls-image]][coveralls-url]
|
|
7
|
-
[![Dependency Status][david-image]][david-url]
|
|
8
|
-
[![License][license-image]][license-url]
|
|
9
|
-
[![Downloads][downloads-image]][downloads-url]
|
|
10
|
-
|
|
11
|
-
HTTP response freshness testing
|
|
12
|
-
|
|
13
|
-
## fresh(req, res)
|
|
14
|
-
|
|
15
|
-
Check freshness of `req` and `res` headers.
|
|
16
|
-
|
|
17
|
-
When the cache is "fresh" __true__ is returned,
|
|
18
|
-
otherwise __false__ is returned to indicate that
|
|
19
|
-
the cache is now stale.
|
|
20
|
-
|
|
21
|
-
## Example:
|
|
22
|
-
|
|
23
|
-
```js
|
|
24
|
-
var req = { 'if-none-match': 'tobi' };
|
|
25
|
-
var res = { 'etag': 'luna' };
|
|
26
|
-
fresh(req, res);
|
|
27
|
-
// => false
|
|
28
|
-
|
|
29
|
-
var req = { 'if-none-match': 'tobi' };
|
|
30
|
-
var res = { 'etag': 'tobi' };
|
|
31
|
-
fresh(req, res);
|
|
32
|
-
// => true
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## Installation
|
|
36
|
-
|
|
37
|
-
```
|
|
38
|
-
$ npm install fresh
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
[npm-image]: https://img.shields.io/npm/v/fresh.svg?style=flat-square
|
|
42
|
-
[npm-url]: https://npmjs.org/package/fresh
|
|
43
|
-
[github-tag]: http://img.shields.io/github/tag/jshttp/fresh.svg?style=flat-square
|
|
44
|
-
[github-url]: https://github.com/jshttp/fresh/tags
|
|
45
|
-
[travis-image]: https://img.shields.io/travis/jshttp/fresh.svg?style=flat-square
|
|
46
|
-
[travis-url]: https://travis-ci.org/jshttp/fresh
|
|
47
|
-
[coveralls-image]: https://img.shields.io/coveralls/jshttp/fresh.svg?style=flat-square
|
|
48
|
-
[coveralls-url]: https://coveralls.io/r/jshttp/fresh?branch=master
|
|
49
|
-
[david-image]: http://img.shields.io/david/jshttp/fresh.svg?style=flat-square
|
|
50
|
-
[david-url]: https://david-dm.org/jshttp/fresh
|
|
51
|
-
[license-image]: http://img.shields.io/npm/l/fresh.svg?style=flat-square
|
|
52
|
-
[license-url]: LICENSE
|
|
53
|
-
[downloads-image]: http://img.shields.io/npm/dm/fresh.svg?style=flat-square
|
|
54
|
-
[downloads-url]: https://npmjs.org/package/fresh
|
|
55
|
-
[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square
|
|
56
|
-
[gittip-url]: https://www.gittip.com/jonathanong/
|