From 9c0d8b9d6331d87bf44ac534a3678645879b775d Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Sun, 3 Nov 2024 20:49:47 +0100 Subject: [PATCH] Refactor: Simplify `req.fresh` getter with destructuring and streamlined conditions This refactor uses destructuring for `method`, `headers`, `res`, and `status`, consolidating variable declarations into a single line. Combined conditions for method and status validation improve readability and adhere to modern JavaScript practices. --- lib/request.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/request.js b/lib/request.js index 372a9915e9..978e9d2ec9 100644 --- a/lib/request.js +++ b/lib/request.js @@ -454,20 +454,16 @@ defineGetter(req, 'hostname', function hostname(){ * @public */ -defineGetter(req, 'fresh', function(){ - var method = this.method; - var res = this.res - var status = res.statusCode +defineGetter(req, 'fresh', function() { + const { method, headers, res, res: { statusCode: status } } = this; // GET or HEAD for weak freshness validation only - if ('GET' !== method && 'HEAD' !== method) return false; - - // 2xx or 304 as per rfc2616 14.26 - if ((status >= 200 && status < 300) || 304 === status) { - return fresh(this.headers, { + if ((method === 'GET' || method === 'HEAD') && + ((status >= 200 && status < 300) || status === 304)) { + return fresh(headers, { 'etag': res.get('ETag'), 'last-modified': res.get('Last-Modified') - }) + }); } return false;