From 32bd8717f2ec8d0c81d3998ef8d91c0fe821bb34 Mon Sep 17 00:00:00 2001 From: ryanwang Date: Tue, 30 Apr 2024 01:07:12 +0800 Subject: [PATCH 1/5] fix: fix ISSUE 35 --- index.js | 13 +++++++------ test/fresh.js | 4 ++-- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 8c69acc..5f4a23e 100644 --- a/index.js +++ b/index.js @@ -49,26 +49,27 @@ function fresh (reqHeaders, resHeaders) { } // if-none-match - if (noneMatch && noneMatch !== '*') { + if (noneMatch) { + if (noneMatch === '*') { + return true + } var etag = resHeaders.etag if (!etag) { return false } - var etagStale = true + var etagStale = false var matches = parseTokenList(noneMatch) for (var i = 0; i < matches.length; i++) { var match = matches[i] if (match === etag || match === 'W/' + etag || 'W/' + match === etag) { - etagStale = false + etagStale = true break } } - if (etagStale) { - return false - } + return etagStale } // if-modified-since diff --git a/test/fresh.js b/test/fresh.js index f86365e..ebedc03 100644 --- a/test/fresh.js +++ b/test/fresh.js @@ -139,10 +139,10 @@ describe('fresh(reqHeaders, resHeaders)', function () { }) describe('when only ETag matches', function () { - it('should be stale', function () { + it('should be fresh', function () { var reqHeaders = { 'if-none-match': '"foo"', 'if-modified-since': 'Sat, 01 Jan 2000 00:00:00 GMT' } var resHeaders = { etag: '"foo"', 'last-modified': 'Sat, 01 Jan 2000 01:00:00 GMT' } - assert.ok(!fresh(reqHeaders, resHeaders)) + assert.ok(fresh(reqHeaders, resHeaders)) }) }) From 7e27e9322694febfb2c8d08c790276a2b99a52f7 Mon Sep 17 00:00:00 2001 From: ryanwang Date: Tue, 30 Apr 2024 21:20:10 +0800 Subject: [PATCH 2/5] feat: change var's name --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 5f4a23e..424d127 100644 --- a/index.js +++ b/index.js @@ -59,17 +59,17 @@ function fresh (reqHeaders, resHeaders) { return false } - var etagStale = false + var etagFresh = false var matches = parseTokenList(noneMatch) for (var i = 0; i < matches.length; i++) { var match = matches[i] if (match === etag || match === 'W/' + etag || 'W/' + match === etag) { - etagStale = true + etagFresh = true break } } - return etagStale + return etagFresh } // if-modified-since From 295790c1056231ef0a7326bb5488844abc62343a Mon Sep 17 00:00:00 2001 From: Jon Church Date: Wed, 4 Sep 2024 17:32:12 -0400 Subject: [PATCH 3/5] early return from for loop, drop var assignment --- index.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 424d127..38371aa 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,7 @@ module.exports = fresh * @public */ -function fresh (reqHeaders, resHeaders) { +function fresh(reqHeaders, resHeaders) { // fields var modifiedSince = reqHeaders['if-modified-since'] var noneMatch = reqHeaders['if-none-match'] @@ -48,7 +48,7 @@ function fresh (reqHeaders, resHeaders) { return false } - // if-none-match + // if-none-match takes precedent over if-modified-since if (noneMatch) { if (noneMatch === '*') { return true @@ -59,17 +59,15 @@ function fresh (reqHeaders, resHeaders) { return false } - var etagFresh = false var matches = parseTokenList(noneMatch) for (var i = 0; i < matches.length; i++) { var match = matches[i] if (match === etag || match === 'W/' + etag || 'W/' + match === etag) { - etagFresh = true - break + return true } } - return etagFresh + return false } // if-modified-since @@ -92,7 +90,7 @@ function fresh (reqHeaders, resHeaders) { * @private */ -function parseHttpDate (date) { +function parseHttpDate(date) { var timestamp = date && Date.parse(date) // istanbul ignore next: guard against date.js Date.parse patching @@ -108,7 +106,7 @@ function parseHttpDate (date) { * @private */ -function parseTokenList (str) { +function parseTokenList(str) { var end = 0 var list = [] var start = 0 From 9dd4c19b91b3e29883da6628a280afa8b2d4e61a Mon Sep 17 00:00:00 2001 From: Jon Church Date: Wed, 4 Sep 2024 17:36:25 -0400 Subject: [PATCH 4/5] run lint fix --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 38371aa..fc3dea7 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,7 @@ module.exports = fresh * @public */ -function fresh(reqHeaders, resHeaders) { +function fresh (reqHeaders, resHeaders) { // fields var modifiedSince = reqHeaders['if-modified-since'] var noneMatch = reqHeaders['if-none-match'] @@ -90,7 +90,7 @@ function fresh(reqHeaders, resHeaders) { * @private */ -function parseHttpDate(date) { +function parseHttpDate (date) { var timestamp = date && Date.parse(date) // istanbul ignore next: guard against date.js Date.parse patching @@ -106,7 +106,7 @@ function parseHttpDate(date) { * @private */ -function parseTokenList(str) { +function parseTokenList (str) { var end = 0 var list = [] var start = 0 From 846caac7fbb33253a3e44318772de1e8dcc220c1 Mon Sep 17 00:00:00 2001 From: Jon Church Date: Wed, 4 Sep 2024 17:42:00 -0400 Subject: [PATCH 5/5] update history.md --- HISTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.md b/HISTORY.md index 9b70ca2..49a824a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,7 @@ unreleased ========== * Drop support for Node.js below 0.8 + * Fix: Ignore `If-Modified-Since` in the presence of `If-None-Match`, according to [spec](https://www.rfc-editor.org/rfc/rfc9110.html#section-13.1.3-5). Fixes [#35](https://github.com/jshttp/fresh/issues/35) 0.5.2 / 2017-09-13 ==================