Skip to content

Commit

Permalink
feat: refactor comment and clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Asadaaaaa committed Oct 6, 2023
1 parent 780efa6 commit b06b37f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 32 deletions.
21 changes: 11 additions & 10 deletions src/helpers/Logger.helper.js
Original file line number Diff line number Diff line change
@@ -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}`);
}
28 changes: 22 additions & 6 deletions src/helpers/ResponsePreset.helper.js
Original file line number Diff line number Diff line change
@@ -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
}
}
};
}
}

Expand Down
31 changes: 16 additions & 15 deletions src/helpers/SpamDetector.js
Original file line number Diff line number Diff line change
@@ -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;
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/middlewares/Authorization.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/middlewares/Handler.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down

0 comments on commit b06b37f

Please sign in to comment.