Comparing version 0.4.0 to 0.5.0
@@ -0,1 +1,11 @@ | ||
0.5.0 / 2017-02-21 | ||
================== | ||
* Fix incorrect result when `If-None-Match` has both `*` and ETags | ||
* Fix weak `ETag` matching to match spec | ||
* perf: delay reading header values until needed | ||
* perf: skip checking modified time if ETag check failed | ||
* perf: skip parsing `If-None-Match` when no `ETag` header | ||
* perf: use `Date.parse` instead of `new Date` | ||
0.4.0 / 2017-02-05 | ||
@@ -42,2 +52,3 @@ ================== | ||
================== | ||
* Add `If-None-Match: *` support | ||
@@ -44,0 +55,0 @@ |
31
index.js
@@ -41,12 +41,5 @@ /*! | ||
function fresh (reqHeaders, resHeaders) { | ||
// defaults | ||
var etagMatches = true | ||
var notModified = true | ||
// fields | ||
var cacheControl = reqHeaders['cache-control'] | ||
var modifiedSince = reqHeaders['if-modified-since'] | ||
var noneMatch = reqHeaders['if-none-match'] | ||
var lastModified = resHeaders['last-modified'] | ||
var etag = resHeaders['etag'] | ||
@@ -61,2 +54,3 @@ // unconditional request | ||
// https://tools.ietf.org/html/rfc2616#section-14.9.4 | ||
var cacheControl = reqHeaders['cache-control'] | ||
if (cacheControl && CACHE_CONTROL_NO_CACHE_REGEXP.test(cacheControl)) { | ||
@@ -67,7 +61,11 @@ return false | ||
// if-none-match | ||
if (noneMatch) { | ||
noneMatch = noneMatch.split(TOKEN_LIST_REGEXP) | ||
etagMatches = noneMatch.some(function (match) { | ||
return match === '*' || match === etag || match === 'W/' + etag | ||
if (noneMatch && noneMatch !== '*') { | ||
var etag = resHeaders['etag'] | ||
var etagStale = !etag || noneMatch.split(TOKEN_LIST_REGEXP).every(function (match) { | ||
return match !== etag && match !== 'W/' + etag && 'W/' + match !== etag | ||
}) | ||
if (etagStale) { | ||
return false | ||
} | ||
} | ||
@@ -77,8 +75,11 @@ | ||
if (modifiedSince) { | ||
modifiedSince = new Date(modifiedSince) | ||
lastModified = new Date(lastModified) | ||
notModified = lastModified <= modifiedSince | ||
var lastModified = resHeaders['last-modified'] | ||
var modifiedStale = !lastModified || Date.parse(lastModified) > Date.parse(modifiedSince) | ||
if (modifiedStale) { | ||
return false | ||
} | ||
} | ||
return etagMatches && notModified | ||
return true | ||
} |
{ | ||
"name": "fresh", | ||
"description": "HTTP response freshness testing", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"author": "TJ Holowaychuk <tj@vision-media.ca> (http://tjholowaychuk.com)", | ||
@@ -19,5 +19,5 @@ "contributors": [ | ||
"devDependencies": { | ||
"eslint": "3.15.0", | ||
"eslint": "3.16.0", | ||
"eslint-config-standard": "6.2.1", | ||
"eslint-plugin-promise": "3.4.0", | ||
"eslint-plugin-promise": "3.4.2", | ||
"eslint-plugin-standard": "2.0.1", | ||
@@ -24,0 +24,0 @@ "istanbul": "0.4.5", |
@@ -36,3 +36,3 @@ # fresh | ||
When a client sends the `Cache-Control: no-cache` request header to | ||
indicate an end-to-end reoad request, this module will return `false` | ||
indicate an end-to-end reload request, this module will return `false` | ||
to make handling these requests transparent. | ||
@@ -57,2 +57,4 @@ | ||
### API usage | ||
```js | ||
@@ -70,2 +72,32 @@ var reqHeaders = { 'if-none-match': '"foo"' } | ||
### Using with Node.js http server | ||
```js | ||
var fresh = require('fresh') | ||
var http = require('http') | ||
var server = http.createServer(function (req, res) { | ||
// perform server logic | ||
// ... including adding ETag / Last-Modified response headers | ||
if (isFresh(req, res)) { | ||
// client has a fresh copy of resource | ||
res.statusCode = 304 | ||
res.end() | ||
return | ||
} | ||
// send the resource | ||
}) | ||
function isFresh (req, res) { | ||
return fresh(req.headers, { | ||
'etag': res.getHeader('ETag'), | ||
'last-modified': res.getHeader('Last-Modified') | ||
}) | ||
} | ||
server.listen(3000) | ||
``` | ||
## License | ||
@@ -72,0 +104,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8477
114