From 96db992b3e82c44081259c8d54ac055dbc36f3b6 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 19:12:43 +0100 Subject: [PATCH 01/14] Refactor compileQueryParser for cleaner return logic and improved readability - Removed unnecessary variable `fn` and directly returned query parser functions - Used template literals for error message for consistency with modern syntax --- lib/utils.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 67eacf274e..66086c6722 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -139,28 +139,24 @@ exports.compileETag = function(val) { * @api private */ -exports.compileQueryParser = function compileQueryParser(val) { - var fn; - +exports.compileQueryParser = function (val) { if (typeof val === 'function') { return val; } + if (val === false) { + return; + } + switch (val) { case true: case 'simple': - fn = querystring.parse; - break; - case false: - break; + return querystring.parse; case 'extended': - fn = parseExtendedQueryString; - break; + return parseExtendedQueryString; default: - throw new TypeError('unknown value for query parser function: ' + val); + throw new TypeError(`unknown value for query parser function: ${val}`); } - - return fn; } /** From ef96e8428b2ede05ecc337a493e1a81820341b85 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 19:31:30 +0100 Subject: [PATCH 02/14] Refactor compileTrust for streamlined type handling and improved readability - Replaced multiple `if` conditions with `switch` for clearer type handling - Simplified boolean function return and direct array handling in `proxyaddr.compile` - Ensured support for empty/default cases with a default return --- lib/utils.js | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 66086c6722..5000dc90bb 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -168,26 +168,22 @@ exports.compileQueryParser = function (val) { */ exports.compileTrust = function(val) { - if (typeof val === 'function') return val; - - if (val === true) { - // Support plain true/false - return function(){ return true }; - } - - if (typeof val === 'number') { - // Support trusting hop count - return function(a, i){ return i < val }; - } - - if (typeof val === 'string') { - // Support comma-separated values - val = val.split(',') - .map(function (v) { return v.trim() }) + switch (typeof val) { + case 'function': + return val; + case 'boolean': + return () => val; + case 'number': + return (a, i) => i < val; + case 'string': + return proxyaddr.compile(val.split(',').map(v => v.trim())); + case 'object': + if (Array.isArray(val)) return proxyaddr.compile(val); + break; + default: + return proxyaddr.compile([]); } - - return proxyaddr.compile(val || []); -} +}; /** * Set the charset in a given Content-Type string. From 4d5b65810c9a5e54b0103c9a4a1fb744e2bbf933 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 19:53:25 +0100 Subject: [PATCH 03/14] Refactor createETagGenerator for improved readability and modern syntax - Replaced function declaration with arrow function - Separated ternary condition into multiple lines for clarity - Changed `var` to `const` for better scoping and modern standards --- lib/utils.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 5000dc90bb..37989f736c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -218,14 +218,14 @@ exports.setCharset = function setCharset(type, charset) { * @private */ -function createETagGenerator (options) { - return function generateETag (body, encoding) { - var buf = !Buffer.isBuffer(body) - ? Buffer.from(body, encoding) - : body - - return etag(buf, options) - } +function createETagGenerator(options) { + return (body, encoding) => { + const buf = Buffer.isBuffer(body) + ? body + : Buffer.from(body, encoding); + + return etag(buf, options); + }; } /** From 1483d77216c7324738df6c0b3d948505d65f4fa9 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 20:09:46 +0100 Subject: [PATCH 04/14] Refactor acceptParams function: - Improved variable declarations using const and let - Simplified parameter parsing with destructuring - Ensured the return structure remains consistent --- lib/utils.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 37989f736c..535f78ede2 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -83,16 +83,16 @@ exports.normalizeTypes = function(types){ * @api private */ -function acceptParams (str) { - var parts = str.split(/ *; */); - var ret = { value: parts[0], quality: 1, params: {} } - - for (var i = 1; i < parts.length; ++i) { - var pms = parts[i].split(/ *= */); - if ('q' === pms[0]) { - ret.quality = parseFloat(pms[1]); +function acceptParams(str) { + const parts = str.split(/ *; */); + const ret = { value: parts[0], quality: 1, params: {} }; + + for (let i = 1; i < parts.length; i++) { + const [key, value] = parts[i].split(/ *= */); + if (key === 'q') { + ret.quality = parseFloat(value); } else { - ret.params[pms[0]] = pms[1]; + ret.params[key] = value; } } @@ -228,6 +228,7 @@ function createETagGenerator(options) { }; } + /** * Parse an extended query string with qs. * From 212aa90439edc5c08a46b794fbf4497c4035a7cf Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 20:13:56 +0100 Subject: [PATCH 05/14] Improve normalizeTypes function: - Use const for length and let for loop variable - Initialize result array with specific length for better performance - Maintain functionality while enhancing code readability --- lib/utils.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 535f78ede2..f9f1bf28f5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -64,15 +64,16 @@ exports.normalizeType = function(type){ * @api private */ -exports.normalizeTypes = function(types){ - var ret = []; +exports.normalizeTypes = function(types) { + const len = types.length; + const ret = new Array(len); - for (var i = 0; i < types.length; ++i) { - ret.push(exports.normalizeType(types[i])); + for (let i = 0; i < len; i++) { + ret[i] = exports.normalizeType(types[i]); } return ret; -}; +} /** * Parse accept params `str` returning an From 3af5baa40eb48d441657b063209bff968e3c298d Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 20:23:15 +0100 Subject: [PATCH 06/14] Optimize normalizeType function: Use includes() for substring check - Replaced indexOf() with includes() for clarity and improved readability. - Maintained original functionality while streamlining the code. --- lib/utils.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index f9f1bf28f5..f0a0ea7aba 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -50,10 +50,10 @@ exports.wetag = createETagGenerator({ weak: true }) * @api private */ -exports.normalizeType = function(type){ - return ~type.indexOf('/') - ? acceptParams(type) - : { value: (mime.lookup(type) || 'application/octet-stream'), params: {} } +exports.normalizeType = function(type) { + return type.includes('/') + ? acceptParams(type) + : { value: mime.lookup(type) || 'application/octet-stream', params: {} }; }; /** From 52205400296dd955e16ac558979c41415b41f29f Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 20:28:56 +0100 Subject: [PATCH 07/14] Refactor compileETag function for clarity and conciseness - Removed unnecessary variable declaration for function assignment. - Simplified return statements for false and valid cases. - Improved error message formatting using template literals. --- lib/utils.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index f0a0ea7aba..282bb1ebbd 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -109,27 +109,23 @@ function acceptParams(str) { */ exports.compileETag = function(val) { - var fn; - if (typeof val === 'function') { return val; } + if (val === false) { + return; + } + switch (val) { case true: case 'weak': - fn = exports.wetag; - break; - case false: - break; + return exports.wetag; case 'strong': - fn = exports.etag; - break; + return exports.etag; default: - throw new TypeError('unknown value for etag function: ' + val); + throw new TypeError(`unknown value for etag function: ${val}`); } - - return fn; } /** From 119dfe61d133861cf4388c394f56db09ce31f5f9 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 20:33:30 +0100 Subject: [PATCH 08/14] Refactor tryStat function to use template literals and simplify error handling - Updated debug message to use template literals for improved readability. - Removed the explicit error variable in the catch statement for cleaner syntax. --- lib/view.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/view.js b/lib/view.js index 6beffca6e2..ea80675fff 100644 --- a/lib/view.js +++ b/lib/view.js @@ -195,11 +195,10 @@ View.prototype.resolve = function resolve(dir, file) { */ function tryStat(path) { - debug('stat "%s"', path); - + debug(`stat "${path}"`); try { return fs.statSync(path); - } catch (e) { + } catch { return undefined; } } From 68ae667b4de03ba2f036da77242a4fe5aa246333 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 20:37:42 +0100 Subject: [PATCH 09/14] Refactor View.prototype.resolve function for improved readability and conciseness - Replaced var with const and let for variable declarations. - Used optional chaining for file existence checks. - Simplified logic by removing intermediate variable assignments. - Added an explicit return of undefined if no valid file is found. --- lib/view.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/view.js b/lib/view.js index ea80675fff..383bd00667 100644 --- a/lib/view.js +++ b/lib/view.js @@ -167,23 +167,17 @@ View.prototype.render = function render(options, callback) { */ View.prototype.resolve = function resolve(dir, file) { - var ext = this.ext; + const ext = this.ext; - // . - var path = join(dir, file); - var stat = tryStat(path); + // Check . + let path = join(dir, file); + if (tryStat(path)?.isFile()) return path; - if (stat && stat.isFile()) { - return path; - } - - // /index. - path = join(dir, basename(file, ext), 'index' + ext); - stat = tryStat(path); + // Check /index. + path = join(dir, basename(file, ext), `index${ext}`); + if (tryStat(path)?.isFile()) return path; - if (stat && stat.isFile()) { - return path; - } + return undefined; }; /** From 8aaf75a0719893eab48b456120331bc3d0102b94 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 20:42:41 +0100 Subject: [PATCH 10/14] Refactor app.engine function for clarity and conciseness - Used const for variable declaration to enhance readability. - Replaced conditional logic with startsWith() for cleaner extension validation. - Added comments for clarity on functionality. --- lib/application.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/application.js b/lib/application.js index b19055ec82..797069d700 100644 --- a/lib/application.js +++ b/lib/application.js @@ -297,12 +297,10 @@ app.engine = function engine(ext, fn) { throw new Error('callback function required'); } - // get file extension - var extension = ext[0] !== '.' - ? '.' + ext - : ext; + // Get file extension + const extension = ext.startsWith('.') ? ext : `.${ext}`; - // store engine + // Store engine function this.engines[extension] = fn; return this; From a205065cc025a0355cacd84ba1f9fad4d8620b10 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Mon, 28 Oct 2024 20:46:17 +0100 Subject: [PATCH 11/14] Refactor req.is function for improved clarity and efficiency - Simplified argument handling by using Array.from() for flattened arguments. - Used const for variable declaration to enhance readability. --- lib/request.js | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/request.js b/lib/request.js index 372a9915e9..063f88a236 100644 --- a/lib/request.js +++ b/lib/request.js @@ -255,16 +255,7 @@ defineGetter(req, 'query', function query(){ */ req.is = function is(types) { - var arr = types; - - // support flattened arguments - if (!Array.isArray(types)) { - arr = new Array(arguments.length); - for (var i = 0; i < arr.length; i++) { - arr[i] = arguments[i]; - } - } - + const arr = Array.isArray(types) ? types : Array.from(arguments); return typeis(this, arr); }; From d1bd9a63ca5b0a30211dfb03e822d9bfdb8f0bf5 Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Tue, 29 Oct 2024 00:05:51 +0100 Subject: [PATCH 12/14] Revert "Refactor compileTrust for streamlined type handling and improved readability" This reverts commit ef96e8428b2ede05ecc337a493e1a81820341b85. --- lib/utils.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 282bb1ebbd..196477a0c7 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -165,22 +165,26 @@ exports.compileQueryParser = function (val) { */ exports.compileTrust = function(val) { - switch (typeof val) { - case 'function': - return val; - case 'boolean': - return () => val; - case 'number': - return (a, i) => i < val; - case 'string': - return proxyaddr.compile(val.split(',').map(v => v.trim())); - case 'object': - if (Array.isArray(val)) return proxyaddr.compile(val); - break; - default: - return proxyaddr.compile([]); + if (typeof val === 'function') return val; + + if (val === true) { + // Support plain true/false + return function(){ return true }; } -}; + + if (typeof val === 'number') { + // Support trusting hop count + return function(a, i){ return i < val }; + } + + if (typeof val === 'string') { + // Support comma-separated values + val = val.split(',') + .map(function (v) { return v.trim() }) + } + + return proxyaddr.compile(val || []); +} /** * Set the charset in a given Content-Type string. From 5b654db16f4204f72103e0c958376c84aecb9fcf Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Tue, 29 Oct 2024 15:40:50 +0100 Subject: [PATCH 13/14] Eslint --- lib/utils.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 196477a0c7..60ae794cc7 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -52,7 +52,7 @@ exports.wetag = createETagGenerator({ weak: true }) exports.normalizeType = function(type) { return type.includes('/') - ? acceptParams(type) + ? acceptParams(type) : { value: mime.lookup(type) || 'application/octet-stream', params: {} }; }; @@ -221,8 +221,8 @@ exports.setCharset = function setCharset(type, charset) { function createETagGenerator(options) { return (body, encoding) => { - const buf = Buffer.isBuffer(body) - ? body + const buf = Buffer.isBuffer(body) + ? body : Buffer.from(body, encoding); return etag(buf, options); From 1a60b353b65827ceb51cd3133fb1be0f7fb4fe64 Mon Sep 17 00:00:00 2001 From: Ayoub Mabrouk Date: Tue, 29 Oct 2024 15:56:41 +0100 Subject: [PATCH 14/14] Eslint --- lib/utils.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 196477a0c7..bb40c78cd7 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -52,7 +52,7 @@ exports.wetag = createETagGenerator({ weak: true }) exports.normalizeType = function(type) { return type.includes('/') - ? acceptParams(type) + ? acceptParams(type) : { value: mime.lookup(type) || 'application/octet-stream', params: {} }; }; @@ -221,10 +221,9 @@ exports.setCharset = function setCharset(type, charset) { function createETagGenerator(options) { return (body, encoding) => { - const buf = Buffer.isBuffer(body) - ? body + const buf = Buffer.isBuffer(body) + ? body : Buffer.from(body, encoding); - return etag(buf, options); }; }