Skip to content
This repository has been archived by the owner on Nov 7, 2022. It is now read-only.

Commit

Permalink
perf: skip checking modified time if ETag check failed
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Feb 19, 2017
1 parent b1dddb3 commit a173db8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ unreleased
* 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

0.4.0 / 2017-02-05
Expand Down
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ module.exports = fresh
*/

function fresh (reqHeaders, resHeaders) {
// defaults
var etagMatches = true
var notModified = true

// fields
var modifiedSince = reqHeaders['if-modified-since']
var noneMatch = reqHeaders['if-none-match']
Expand All @@ -62,16 +58,24 @@ function fresh (reqHeaders, resHeaders) {
// if-none-match
if (noneMatch && noneMatch !== '*') {
var etag = resHeaders['etag']
etagMatches = Boolean(etag) && noneMatch.split(TOKEN_LIST_REGEXP).some(function (match) {
return match === etag || match === 'W/' + etag || 'W/' + match === 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
}
}

// if-modified-since
if (modifiedSince) {
var lastModified = resHeaders['last-modified']
notModified = new Date(lastModified) <= new Date(modifiedSince)
var modifiedStale = !lastModified || new Date(lastModified) > new Date(modifiedSince)

if (modifiedStale) {
return false
}
}

return etagMatches && notModified
return true
}

0 comments on commit a173db8

Please sign in to comment.