From b06b37fd8f13381faa32c7f8fb62ded87aeb6971 Mon Sep 17 00:00:00 2001 From: Mikail Asada Date: Sat, 7 Oct 2023 04:22:58 +0700 Subject: [PATCH] feat: refactor comment and clean code --- src/helpers/Logger.helper.js | 21 +++++++------- src/helpers/ResponsePreset.helper.js | 28 +++++++++++++++---- src/helpers/SpamDetector.js | 31 +++++++++++---------- src/middlewares/Authorization.middleware.js | 5 ++++ src/middlewares/Handler.middleware.js | 8 +++++- 5 files changed, 61 insertions(+), 32 deletions(-) diff --git a/src/helpers/Logger.helper.js b/src/helpers/Logger.helper.js index cee01ec..ce1ece4 100644 --- a/src/helpers/Logger.helper.js +++ b/src/helpers/Logger.helper.js @@ -1,12 +1,13 @@ -export default (text) => { - const date = new Date(new Date().toLocaleString('en-US', {timeZone: 'Asia/Jakarta'})); - const currentDate = '[' + - date.getDate() + '/' + - (date.getMonth() + 1) + '/' + - date.getHours() + ':' + - date.getMinutes() + ':' + - date.getSeconds() + - ']'; +/** + * Logs the given text along with the current date and process ID. + * @param {string} text - The text to be logged. + */ +export default function Logger(text) { + if (typeof text !== 'string') { + console.error('Error: text must be a string'); + return; + } - console.log('\n' + currentDate + ' (' + process.pid + '): ' + text); + const date = new Date().toLocaleString('en-US', { timeZone: 'Asia/Jakarta' }); + console.log(`[${date}] (${process.pid}): ${text}`); } \ No newline at end of file diff --git a/src/helpers/ResponsePreset.helper.js b/src/helpers/ResponsePreset.helper.js index fb5b753..ac8624e 100644 --- a/src/helpers/ResponsePreset.helper.js +++ b/src/helpers/ResponsePreset.helper.js @@ -1,22 +1,38 @@ +/** + * A helper class for creating response presets. + */ class ResponsePreset { - + /** + * Returns a successful response object. + * @param {string} message - The message to include in the response. + * @param {object} [data=null] - The data to include in the response. + * @returns {object} - The response object. + */ resOK(message, data) { return { status: 200, message, - ...(data === null ? {} : {data}) - } + data: data || null + }; } - + + /** + * Returns an error response object. + * @param {number} status - The HTTP status code to include in the response. + * @param {string} message - The message to include in the response. + * @param {string} type - The type of error. + * @param {object} [data=null] - The data to include in the response. + * @returns {object} - The response object. + */ resErr(status, message, type, data) { return { status, message, err: { type, - ...(data === null ? {} : {data}) + data: data || null } - } + }; } } diff --git a/src/helpers/SpamDetector.js b/src/helpers/SpamDetector.js index 6ccda4b..b868dd0 100644 --- a/src/helpers/SpamDetector.js +++ b/src/helpers/SpamDetector.js @@ -1,22 +1,23 @@ +/** + * Determines if a list of dates indicates spam activity. + * @param {string[]} dateList - An array of date strings in ISO format. + * @returns {boolean} - True if the date list indicates spam activity, false otherwise. + */ export default (dateList) => { - const currentDate = new Date(); - const threeHoursAgo = new Date(); - threeHoursAgo.setHours(threeHoursAgo.getHours() - 3); + const threeHoursAgo = new Date(Date.now() - 3 * 60 * 60 * 1000); + const recentDates = dateList.filter(dateString => new Date(dateString) >= threeHoursAgo); - const recentDates = dateList.filter((dateString) => { - const date = new Date(dateString); - return date >= threeHoursAgo && date <= currentDate; - }); + if (recentDates.length < 3) { + return false; + } + + const sortedDates = recentDates.sort((a, b) => new Date(a) - new Date(b)); - if (recentDates.length >= 3) { - const sortedDates = recentDates.sort((a, b) => new Date(a) - new Date(b)); + for (let i = 0; i <= sortedDates.length - 3; i++) { + const timeDifference = (new Date(sortedDates[i + 2]) - new Date(sortedDates[i])) / 1000; // in seconds - for (let i = 0; i <= sortedDates.length - 3; i++) { - const timeDifference = (new Date(sortedDates[i + 2]) - new Date(sortedDates[i])) / 1000; // in seconds - - if (timeDifference < 120) { - return true; - } + if (timeDifference < 120) { + return true; } } diff --git a/src/middlewares/Authorization.middleware.js b/src/middlewares/Authorization.middleware.js index 64d4e7b..7c53246 100644 --- a/src/middlewares/Authorization.middleware.js +++ b/src/middlewares/Authorization.middleware.js @@ -69,6 +69,11 @@ class Authorization { } } + /** + * Determines if the requested route is optional and does not require authorization. + * @param {Object} req - The request object. + * @returns {boolean} - Returns true if the requested route is optional, false otherwise. + */ optionalRoutes(req) { switch(true) { // Profile diff --git a/src/middlewares/Handler.middleware.js b/src/middlewares/Handler.middleware.js index 765d460..40970e4 100644 --- a/src/middlewares/Handler.middleware.js +++ b/src/middlewares/Handler.middleware.js @@ -67,7 +67,13 @@ class Handler { res.end = function (chunk) { if (chunk) chunks.push(chunk); - const responseBody = JSON.parse(chunks); + let responseBody = null; + + try { + responseBody = JSON.parse(chunks) + } catch(err) { + responseBody = chunks + } server.sendLogs('New Request: ' + req.originalUrl + '\n- Header: ' + JSON.stringify(req.headers, null, 2) + '\n- Body: ' + JSON.stringify(req.body, null, 2) + '\n- Response: ' + JSON.stringify(responseBody, null, 2));