fresh 0.3.0 → 0.5.2

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.
Files changed (5) hide show
  1. package/HISTORY.md +32 -0
  2. package/LICENSE +1 -0
  3. package/README.md +73 -12
  4. package/index.js +114 -34
  5. package/package.json +13 -2
package/HISTORY.md CHANGED
@@ -1,3 +1,34 @@
1
+ 0.5.2 / 2017-09-13
2
+ ==================
3
+
4
+ * Fix regression matching multiple ETags in `If-None-Match`
5
+ * perf: improve `If-None-Match` token parsing
6
+
7
+ 0.5.1 / 2017-09-11
8
+ ==================
9
+
10
+ * Fix handling of modified headers with invalid dates
11
+ * perf: improve ETag match loop
12
+
13
+ 0.5.0 / 2017-02-21
14
+ ==================
15
+
16
+ * Fix incorrect result when `If-None-Match` has both `*` and ETags
17
+ * Fix weak `ETag` matching to match spec
18
+ * perf: delay reading header values until needed
19
+ * perf: skip checking modified time if ETag check failed
20
+ * perf: skip parsing `If-None-Match` when no `ETag` header
21
+ * perf: use `Date.parse` instead of `new Date`
22
+
23
+ 0.4.0 / 2017-02-05
24
+ ==================
25
+
26
+ * Fix false detection of `no-cache` request directive
27
+ * perf: enable strict mode
28
+ * perf: hoist regular expressions
29
+ * perf: remove duplicate conditional
30
+ * perf: remove unnecessary boolean coercions
31
+
1
32
  0.3.0 / 2015-05-12
2
33
  ==================
3
34
 
@@ -30,6 +61,7 @@
30
61
 
31
62
  0.1.0 / 2012-06-15
32
63
  ==================
64
+
33
65
  * Add `If-None-Match: *` support
34
66
 
35
67
  0.0.1 / 2012-06-10
package/LICENSE CHANGED
@@ -1,6 +1,7 @@
1
1
  (The MIT License)
2
2
 
3
3
  Copyright (c) 2012 TJ Holowaychuk <tj@vision-media.ca>
4
+ Copyright (c) 2016-2017 Douglas Christopher Wilson <doug@somethingdoug.com>
4
5
 
5
6
  Permission is hereby granted, free of charge, to any person obtaining
6
7
  a copy of this software and associated documentation files (the
package/README.md CHANGED
@@ -10,38 +10,99 @@ HTTP response freshness testing
10
10
 
11
11
  ## Installation
12
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
+
13
17
  ```
14
18
  $ npm install fresh
15
19
  ```
16
20
 
17
21
  ## API
18
22
 
23
+ <!-- eslint-disable no-unused-vars -->
24
+
19
25
  ```js
20
26
  var fresh = require('fresh')
21
27
  ```
22
28
 
23
- ### fresh(req, res)
29
+ ### fresh(reqHeaders, resHeaders)
30
+
31
+ Check freshness of the response using request and response headers.
32
+
33
+ When the response is still "fresh" in the client's cache `true` is
34
+ returned, otherwise `false` is returned to indicate that the client
35
+ cache is now stale and the full response should be sent.
36
+
37
+ When a client sends the `Cache-Control: no-cache` request header to
38
+ indicate an end-to-end reload request, this module will return `false`
39
+ to make handling these requests transparent.
24
40
 
25
- Check freshness of `req` and `res` headers.
41
+ ## Known Issues
26
42
 
27
- When the cache is "fresh" __true__ is returned,
28
- otherwise __false__ is returned to indicate that
29
- the cache is now stale.
43
+ This module is designed to only follow the HTTP specifications, not
44
+ to work-around all kinda of client bugs (especially since this module
45
+ typically does not recieve enough information to understand what the
46
+ client actually is).
47
+
48
+ There is a known issue that in certain versions of Safari, Safari
49
+ will incorrectly make a request that allows this module to validate
50
+ freshness of the resource even when Safari does not have a
51
+ representation of the resource in the cache. The module
52
+ [jumanji](https://www.npmjs.com/package/jumanji) can be used in
53
+ an Express application to work-around this issue and also provides
54
+ links to further reading on this Safari bug.
30
55
 
31
56
  ## Example
32
57
 
58
+ ### API usage
59
+
60
+ <!-- eslint-disable no-redeclare, no-undef -->
61
+
33
62
  ```js
34
- var req = { 'if-none-match': 'tobi' };
35
- var res = { 'etag': 'luna' };
36
- fresh(req, res);
63
+ var reqHeaders = { 'if-none-match': '"foo"' }
64
+ var resHeaders = { 'etag': '"bar"' }
65
+ fresh(reqHeaders, resHeaders)
37
66
  // => false
38
67
 
39
- var req = { 'if-none-match': 'tobi' };
40
- var res = { 'etag': 'tobi' };
41
- fresh(req, res);
68
+ var reqHeaders = { 'if-none-match': '"foo"' }
69
+ var resHeaders = { 'etag': '"foo"' }
70
+ fresh(reqHeaders, resHeaders)
42
71
  // => true
43
72
  ```
44
73
 
74
+ ### Using with Node.js http server
75
+
76
+ ```js
77
+ var fresh = require('fresh')
78
+ var http = require('http')
79
+
80
+ var server = http.createServer(function (req, res) {
81
+ // perform server logic
82
+ // ... including adding ETag / Last-Modified response headers
83
+
84
+ if (isFresh(req, res)) {
85
+ // client has a fresh copy of resource
86
+ res.statusCode = 304
87
+ res.end()
88
+ return
89
+ }
90
+
91
+ // send the resource
92
+ res.statusCode = 200
93
+ res.end('hello, world!')
94
+ })
95
+
96
+ function isFresh (req, res) {
97
+ return fresh(req.headers, {
98
+ 'etag': res.getHeader('ETag'),
99
+ 'last-modified': res.getHeader('Last-Modified')
100
+ })
101
+ }
102
+
103
+ server.listen(3000)
104
+ ```
105
+
45
106
  ## License
46
107
 
47
108
  [MIT](LICENSE)
@@ -49,7 +110,7 @@ fresh(req, res);
49
110
  [npm-image]: https://img.shields.io/npm/v/fresh.svg
50
111
  [npm-url]: https://npmjs.org/package/fresh
51
112
  [node-version-image]: https://img.shields.io/node/v/fresh.svg
52
- [node-version-url]: http://nodejs.org/download/
113
+ [node-version-url]: https://nodejs.org/en/
53
114
  [travis-image]: https://img.shields.io/travis/jshttp/fresh/master.svg
54
115
  [travis-url]: https://travis-ci.org/jshttp/fresh
55
116
  [coveralls-image]: https://img.shields.io/coveralls/jshttp/fresh/master.svg
package/index.js CHANGED
@@ -1,57 +1,137 @@
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
- * Expose `fresh()`.
11
+ * RegExp to check for no-cache token in Cache-Control.
12
+ * @private
4
13
  */
5
14
 
6
- module.exports = fresh;
15
+ var CACHE_CONTROL_NO_CACHE_REGEXP = /(?:^|,)\s*?no-cache\s*?(?:,|$)/
7
16
 
8
17
  /**
9
- * Check freshness of `req` and `res` headers.
10
- *
11
- * When the cache is "fresh" __true__ is returned,
12
- * otherwise __false__ is returned to indicate that
13
- * the cache is now stale.
18
+ * Module exports.
19
+ * @public
20
+ */
21
+
22
+ module.exports = fresh
23
+
24
+ /**
25
+ * Check freshness of the response using request and response headers.
14
26
  *
15
- * @param {Object} req
16
- * @param {Object} res
27
+ * @param {Object} reqHeaders
28
+ * @param {Object} resHeaders
17
29
  * @return {Boolean}
18
- * @api public
30
+ * @public
19
31
  */
20
32
 
21
- function fresh(req, res) {
22
- // defaults
23
- var etagMatches = true;
24
- var notModified = true;
25
-
33
+ function fresh (reqHeaders, resHeaders) {
26
34
  // fields
27
- var modifiedSince = req['if-modified-since'];
28
- var noneMatch = req['if-none-match'];
29
- var lastModified = res['last-modified'];
30
- var etag = res['etag'];
31
- var cc = req['cache-control'];
35
+ var modifiedSince = reqHeaders['if-modified-since']
36
+ var noneMatch = reqHeaders['if-none-match']
32
37
 
33
38
  // unconditional request
34
- if (!modifiedSince && !noneMatch) return false;
35
-
36
- // check for no-cache cache request directive
37
- if (cc && cc.indexOf('no-cache') !== -1) return false;
39
+ if (!modifiedSince && !noneMatch) {
40
+ return false
41
+ }
38
42
 
39
- // parse if-none-match
40
- if (noneMatch) noneMatch = noneMatch.split(/ *, */);
43
+ // Always return stale when Cache-Control: no-cache
44
+ // to support end-to-end reload requests
45
+ // https://tools.ietf.org/html/rfc2616#section-14.9.4
46
+ var cacheControl = reqHeaders['cache-control']
47
+ if (cacheControl && CACHE_CONTROL_NO_CACHE_REGEXP.test(cacheControl)) {
48
+ return false
49
+ }
41
50
 
42
51
  // if-none-match
43
- if (noneMatch) {
44
- etagMatches = noneMatch.some(function (match) {
45
- return match === '*' || match === etag || match === 'W/' + etag;
46
- });
52
+ if (noneMatch && noneMatch !== '*') {
53
+ var etag = resHeaders['etag']
54
+
55
+ if (!etag) {
56
+ return false
57
+ }
58
+
59
+ var etagStale = true
60
+ var matches = parseTokenList(noneMatch)
61
+ for (var i = 0; i < matches.length; i++) {
62
+ var match = matches[i]
63
+ if (match === etag || match === 'W/' + etag || 'W/' + match === etag) {
64
+ etagStale = false
65
+ break
66
+ }
67
+ }
68
+
69
+ if (etagStale) {
70
+ return false
71
+ }
47
72
  }
48
73
 
49
74
  // if-modified-since
50
75
  if (modifiedSince) {
51
- modifiedSince = new Date(modifiedSince);
52
- lastModified = new Date(lastModified);
53
- notModified = lastModified <= modifiedSince;
76
+ var lastModified = resHeaders['last-modified']
77
+ var modifiedStale = !lastModified || !(parseHttpDate(lastModified) <= parseHttpDate(modifiedSince))
78
+
79
+ if (modifiedStale) {
80
+ return false
81
+ }
82
+ }
83
+
84
+ return true
85
+ }
86
+
87
+ /**
88
+ * Parse an HTTP Date into a number.
89
+ *
90
+ * @param {string} date
91
+ * @private
92
+ */
93
+
94
+ function parseHttpDate (date) {
95
+ var timestamp = date && Date.parse(date)
96
+
97
+ // istanbul ignore next: guard against date.js Date.parse patching
98
+ return typeof timestamp === 'number'
99
+ ? timestamp
100
+ : NaN
101
+ }
102
+
103
+ /**
104
+ * Parse a HTTP token list.
105
+ *
106
+ * @param {string} str
107
+ * @private
108
+ */
109
+
110
+ function parseTokenList (str) {
111
+ var end = 0
112
+ var list = []
113
+ var start = 0
114
+
115
+ // gather tokens
116
+ for (var i = 0, len = str.length; i < len; i++) {
117
+ switch (str.charCodeAt(i)) {
118
+ case 0x20: /* */
119
+ if (start === end) {
120
+ start = end = i + 1
121
+ }
122
+ break
123
+ case 0x2c: /* , */
124
+ list.push(str.substring(start, end))
125
+ start = end = i + 1
126
+ break
127
+ default:
128
+ end = i + 1
129
+ break
130
+ }
54
131
  }
55
132
 
56
- return !! (etagMatches && notModified);
133
+ // final token
134
+ list.push(str.substring(start, end))
135
+
136
+ return list
57
137
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "fresh",
3
3
  "description": "HTTP response freshness testing",
4
- "version": "0.3.0",
4
+ "version": "0.5.2",
5
5
  "author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",
6
6
  "contributors": [
7
7
  "Douglas Christopher Wilson <doug@somethingdoug.com>",
@@ -16,7 +16,16 @@
16
16
  ],
17
17
  "repository": "jshttp/fresh",
18
18
  "devDependencies": {
19
- "istanbul": "0.3.9",
19
+ "beautify-benchmark": "0.2.4",
20
+ "benchmark": "2.1.4",
21
+ "eslint": "3.19.0",
22
+ "eslint-config-standard": "10.2.1",
23
+ "eslint-plugin-import": "2.7.0",
24
+ "eslint-plugin-markdown": "1.0.0-beta.6",
25
+ "eslint-plugin-node": "5.1.1",
26
+ "eslint-plugin-promise": "3.5.0",
27
+ "eslint-plugin-standard": "3.0.1",
28
+ "istanbul": "0.4.5",
20
29
  "mocha": "1.21.5"
21
30
  },
22
31
  "files": [
@@ -28,6 +37,8 @@
28
37
  "node": ">= 0.6"
29
38
  },
30
39
  "scripts": {
40
+ "bench": "node benchmark/index.js",
41
+ "lint": "eslint --plugin markdown --ext js,md .",
31
42
  "test": "mocha --reporter spec --bail --check-leaks test/",
32
43
  "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
33
44
  "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"