From c73ce5d9f792d5e9fcfcb5ba02cbf35b1b2bb792 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Tue, 27 Aug 2024 22:49:05 +0200 Subject: [PATCH] Optimize deepClone function and lint out the codebase --- src/handlers/clientErrorHandler.js | 24 ++++++------- src/handlers/requestHandler.js | 30 +++++++--------- src/utils/deepClone.js | 58 ++++++++++++++++-------------- 3 files changed, 55 insertions(+), 57 deletions(-) diff --git a/src/handlers/clientErrorHandler.js b/src/handlers/clientErrorHandler.js index 2a44649..ff7e5ca 100644 --- a/src/handlers/clientErrorHandler.js +++ b/src/handlers/clientErrorHandler.js @@ -204,21 +204,17 @@ function clientErrorHandler(err, socket) { } }); } else { - fs.access( - "." + errorCode.toString(), - fs.constants.F_OK, - (err) => { - try { - if (err) { - callback(errorCode.toString() + ".html"); - } else { - callback("." + errorCode.toString()); - } - } catch (err2) { - callServerError(500, err2); + fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => { + try { + if (err) { + callback(errorCode.toString() + ".html"); + } else { + callback("." + errorCode.toString()); } - }, - ); + } catch (err2) { + callServerError(500, err2); + } + }); } } }; diff --git a/src/handlers/requestHandler.js b/src/handlers/requestHandler.js index e947d80..ffef689 100644 --- a/src/handlers/requestHandler.js +++ b/src/handlers/requestHandler.js @@ -54,7 +54,7 @@ function requestHandler(req, res) { return true; } }); - if (vhostP && vhostP.headers) ph = {...ph, ...vhostP.headers}; + if (vhostP && vhostP.headers) ph = { ...ph, ...vhostP.headers }; } Object.keys(ph).forEach((phk) => { if (typeof ph[phk] == "string") @@ -369,24 +369,20 @@ function requestHandler(req, res) { } }); } else { - fs.access( - "." + errorCode.toString(), - fs.constants.F_OK, - (err) => { - try { - if (err) { - callback(errorCode.toString() + ".html"); - } else { - callback("." + errorCode.toString()); - } - } catch (err2) { - res.error(500, err2); + fs.access("." + errorCode.toString(), fs.constants.F_OK, (err) => { + try { + if (err) { + callback(errorCode.toString() + ".html"); + } else { + callback("." + errorCode.toString()); } - }, - ); + } catch (err2) { + res.error(500, err2); + } + }); } } - } + }; if (!_i) _i = 0; if (_i >= list.length) { @@ -412,7 +408,7 @@ function requestHandler(req, res) { } }); } - } + }; getErrorFileName(config.errorPages, function (errorFile) { // Generate error stack if not provided diff --git a/src/utils/deepClone.js b/src/utils/deepClone.js index dbaa979..4f21e63 100644 --- a/src/utils/deepClone.js +++ b/src/utils/deepClone.js @@ -1,37 +1,43 @@ // Function to deep clone an object or array -function deepClone(obj, _objectsArray, _clonesArray) { - if (!_objectsArray) _objectsArray = []; - if (!_clonesArray) _clonesArray = []; +function deepClone(obj) { if (typeof obj !== "object" || obj === null) { return obj; } - let objectsArrayIndex = _objectsArray.indexOf(obj); - if (objectsArrayIndex != -1) { - return _clonesArray[objectsArrayIndex]; - } + const recurse = (obj, _objectsArray, _clonesArray) => { + if (!_objectsArray) _objectsArray = []; + if (!_clonesArray) _clonesArray = []; - let clone; + let objectsArrayIndex = _objectsArray.indexOf(obj); + if (objectsArrayIndex != -1) { + return _clonesArray[objectsArrayIndex]; + } - if (Array.isArray(obj)) { - clone = []; - _objectsArray.push(obj); - _clonesArray.push(clone); - obj.forEach((item, index) => { - clone[index] = deepClone(item, _objectsArray, _clonesArray); - }); - } else { - clone = {}; - _objectsArray.push(obj); - _clonesArray.push(clone); - Object.keys(obj).forEach((key) => { - if (Object.prototype.hasOwnProperty.call(obj, key)) { - clone[key] = deepClone(obj[key], _objectsArray, _clonesArray); - } - }); - } + let clone; - return clone; + if (Array.isArray(obj)) { + clone = []; + _objectsArray.push(obj); + _clonesArray.push(clone); + obj.forEach((item, index) => { + clone[index] = recurse(item, _objectsArray, _clonesArray); + }); + } else { + clone = {}; + _objectsArray.push(obj); + _clonesArray.push(clone); + Object.keys(obj).forEach((key) => { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + clone[key] = + typeof obj[key] !== "object" || obj[key] === null + ? obj[key] + : recurse(obj[key], _objectsArray, _clonesArray); + } + }); + } + return clone; + }; + return recurse(obj); } module.exports = deepClone;