fresh 0.4.0 → 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.
Files changed (4) hide show
  1. package/HISTORY.md +11 -0
  2. package/README.md +33 -1
  3. package/index.js +16 -15
  4. package/package.json +3 -3
package/HISTORY.md CHANGED
@@ -1,3 +1,13 @@
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
+
1
11
  0.4.0 / 2017-02-05
2
12
  ==================
3
13
 
@@ -39,6 +49,7 @@
39
49
 
40
50
  0.1.0 / 2012-06-15
41
51
  ==================
52
+
42
53
  * Add `If-None-Match: *` support
43
54
 
44
55
  0.0.1 / 2012-06-10
package/README.md CHANGED
@@ -33,7 +33,7 @@ returned, otherwise `false` is returned to indicate that the client
33
33
  cache is now stale and the full response should be sent.
34
34
 
35
35
  When a client sends the `Cache-Control: no-cache` request header to
36
- indicate an end-to-end reoad request, this module will return `false`
36
+ indicate an end-to-end reload request, this module will return `false`
37
37
  to make handling these requests transparent.
38
38
 
39
39
  ## Known Issues
@@ -53,6 +53,8 @@ links to further reading on this Safari bug.
53
53
 
54
54
  ## Example
55
55
 
56
+ ### API usage
57
+
56
58
  ```js
57
59
  var reqHeaders = { 'if-none-match': '"foo"' }
58
60
  var resHeaders = { 'etag': '"bar"' }
@@ -65,6 +67,36 @@ fresh(reqHeaders, resHeaders)
65
67
  // => true
66
68
  ```
67
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
+
68
100
  ## License
69
101
 
70
102
  [MIT](LICENSE)
package/index.js CHANGED
@@ -38,16 +38,9 @@ module.exports = fresh
38
38
  */
39
39
 
40
40
  function fresh (reqHeaders, resHeaders) {
41
- // defaults
42
- var etagMatches = true
43
- var notModified = true
44
-
45
41
  // fields
46
- var cacheControl = reqHeaders['cache-control']
47
42
  var modifiedSince = reqHeaders['if-modified-since']
48
43
  var noneMatch = reqHeaders['if-none-match']
49
- var lastModified = resHeaders['last-modified']
50
- var etag = resHeaders['etag']
51
44
 
52
45
  // unconditional request
53
46
  if (!modifiedSince && !noneMatch) {
@@ -57,24 +50,32 @@ function fresh (reqHeaders, resHeaders) {
57
50
  // Always return stale when Cache-Control: no-cache
58
51
  // to support end-to-end reload requests
59
52
  // https://tools.ietf.org/html/rfc2616#section-14.9.4
53
+ var cacheControl = reqHeaders['cache-control']
60
54
  if (cacheControl && CACHE_CONTROL_NO_CACHE_REGEXP.test(cacheControl)) {
61
55
  return false
62
56
  }
63
57
 
64
58
  // if-none-match
65
- if (noneMatch) {
66
- noneMatch = noneMatch.split(TOKEN_LIST_REGEXP)
67
- etagMatches = noneMatch.some(function (match) {
68
- return match === '*' || match === etag || match === 'W/' + etag
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
69
63
  })
64
+
65
+ if (etagStale) {
66
+ return false
67
+ }
70
68
  }
71
69
 
72
70
  // if-modified-since
73
71
  if (modifiedSince) {
74
- modifiedSince = new Date(modifiedSince)
75
- lastModified = new Date(lastModified)
76
- notModified = lastModified <= modifiedSince
72
+ var lastModified = resHeaders['last-modified']
73
+ var modifiedStale = !lastModified || Date.parse(lastModified) > Date.parse(modifiedSince)
74
+
75
+ if (modifiedStale) {
76
+ return false
77
+ }
77
78
  }
78
79
 
79
- return etagMatches && notModified
80
+ return true
80
81
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "fresh",
3
3
  "description": "HTTP response freshness testing",
4
- "version": "0.4.0",
4
+ "version": "0.5.0",
5
5
  "author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)",
6
6
  "contributors": [
7
7
  "Douglas Christopher Wilson <doug@somethingdoug.com>",
@@ -16,9 +16,9 @@
16
16
  ],
17
17
  "repository": "jshttp/fresh",
18
18
  "devDependencies": {
19
- "eslint": "3.15.0",
19
+ "eslint": "3.16.0",
20
20
  "eslint-config-standard": "6.2.1",
21
- "eslint-plugin-promise": "3.4.0",
21
+ "eslint-plugin-promise": "3.4.2",
22
22
  "eslint-plugin-standard": "2.0.1",
23
23
  "istanbul": "0.4.5",
24
24
  "mocha": "1.21.5"