From e63714e9f46f45c3c77827c51bf1f960c3899a54 Mon Sep 17 00:00:00 2001 From: Rim Date: Thu, 17 Apr 2025 08:23:46 -0400 Subject: [PATCH] chore: fix eslint errors --- app.js | 15 ++---- package.json | 4 +- src/js/frontend.js | 13 ++--- src/js/serverUtils.js | 117 ++++++++++++++++++------------------------ src/js/test.js | 2 +- 5 files changed, 63 insertions(+), 88 deletions(-) diff --git a/app.js b/app.js index 3e7d434..a1977a9 100644 --- a/app.js +++ b/app.js @@ -1,12 +1,10 @@ const express = require('express'); -const rateLimit = require('express-rate-limit'); const path = require('path'); const bodyParser = require('body-parser'); const API = require('./src/js/index.js'); const demoTracker = require('./src/js/demoTracker.js'); const { logger } = require('./src/js/logger.js'); const favicon = require('serve-favicon'); -const fs = require('fs'); const app = express(); const port = process.env.PORT || 3512; const { @@ -116,8 +114,7 @@ app.post('/api/stats', async (req, res) => { ); try { - let { username, ssoToken, platform, game, apiCall, sanitize, replaceKeys } = - req.body; + let { username, ssoToken, platform, game, apiCall } = req.body; const defaultToken = demoTracker.getDefaultSsoToken(); if (!ssoToken && defaultToken) { @@ -375,8 +372,7 @@ app.post('/api/matches', async (req, res) => { ); try { - let { username, ssoToken, platform, game, sanitize, replaceKeys } = - req.body; + let { username, ssoToken, platform, game } = req.body; /* logger.debug( @@ -539,7 +535,7 @@ app.post('/api/matchInfo', async (req, res) => { ); try { - let { matchId, ssoToken, platform, game, sanitize, replaceKeys } = req.body; + let { matchId, ssoToken, platform, game } = req.body; /* logger.debug( @@ -688,8 +684,7 @@ app.post('/api/user', async (req, res) => { ); try { - let { username, ssoToken, platform, userCall, sanitize, replaceKeys } = - req.body; + let { username, ssoToken, platform, userCall } = req.body; /* logger.debug( @@ -830,7 +825,7 @@ app.post('/api/search', async (req, res) => { ); try { - let { username, ssoToken, platform, sanitize, replaceKeys } = req.body; + let { username, ssoToken, platform } = req.body; /* logger.debug( diff --git a/package.json b/package.json index e63d2ff..4579733 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,9 @@ "build:win64": "pkg . --targets node18-win-x64 --experimental-modules --icon src/images/favicon.ico --output dist/codtrackerjs-aio-win-amd64.exe", "build:linux64": "pkg . --targets node18-linux-x64 --experimental-modules --output dist/codtrackerjs-aio-linux-amd64", "start": "node app.js", - "lint": "prettier --write .", + "prettify": "prettier --write .", + "lint": "eslint --no-config-lookup .", + "lint:fix": "eslint --no-config-lookup . --fix", "dev": "nodemon app.js" }, "dependencies": { diff --git a/src/js/frontend.js b/src/js/frontend.js index ca8509e..b8ab6f4 100644 --- a/src/js/frontend.js +++ b/src/js/frontend.js @@ -107,7 +107,7 @@ const clientLogger = { timestamp: new Date().toISOString(), ...data, }), - }).catch((e) => logger.error('Logging error:', e)); + }).catch((e) => console.error('Logging error:', e)); } }, }; @@ -315,9 +315,7 @@ function setupProcessingOptions() { if (window.appState.currentData) { // Call window.appState // Re-fetch with new options - const activeTab = document - .querySelector('.tab.active') - .getAttribute('data-tab'); + document.querySelector('.tab.active').getAttribute('data-tab'); triggerActiveTabButton(); } }); @@ -327,9 +325,7 @@ function setupProcessingOptions() { .addEventListener('change', function () { if (window.appState.currentData) { // Re-fetch with new options - const activeTab = document - .querySelector('.tab.active') - .getAttribute('data-tab'); + document.querySelector('.tab.active').getAttribute('data-tab'); triggerActiveTabButton(); } }); @@ -516,6 +512,7 @@ function initializeSessionTracking() { }); }); + /* // Helper function to extract data attributes function getDataAttributes(element) { if (!element.dataset) return {}; @@ -523,7 +520,7 @@ function initializeSessionTracking() { acc[key] = value; return acc; }, {}); - } + } */ // Track tab visibility changes document.addEventListener('visibilitychange', () => { diff --git a/src/js/serverUtils.js b/src/js/serverUtils.js index cf03f43..1fcf51d 100644 --- a/src/js/serverUtils.js +++ b/src/js/serverUtils.js @@ -26,37 +26,42 @@ try { } // Optimized replaceJsonKeys function -const replaceJsonKeys = (obj, replacements) => { - // Handle non-objects early +const replaceJsonKeys = (obj) => { if (!obj || typeof obj !== 'object') return obj; - // Fast path for arrays + // Fast-path for arrays if (Array.isArray(obj)) { - return obj.length === 0 ? - obj - : obj.map((item) => replaceJsonKeys(item, replacements)); + // Only process array if it has items + return obj.length > 0 ? obj.map(replaceJsonKeys) : obj; } - // Fast path for empty objects - const keys = Object.keys(obj); - if (keys.length === 0) return obj; - - // Process normal objects const newObj = {}; - for (let i = 0; i < keys.length; i++) { - const key = keys[i]; - const newKey = replacements[key] || key; + const objKeys = Object.keys(obj); + + // Fast-path for empty objects + if (objKeys.length === 0) return obj; + + // Cache key check + const hasKeyReplacements = Object.keys(keyReplacements).length > 0; + + for (const key of objKeys) { + // Replace key if replacements exist + const newKey = + hasKeyReplacements && keyReplacements[key] ? keyReplacements[key] : key; let value = obj[key]; - // Replace string values if they match a key in replacements - if (typeof value === 'string' && replacements[value]) { - value = replacements[value]; - } else if (value && typeof value === 'object') { - // Only recurse for objects/arrays - value = replaceJsonKeys(value, replacements); + // Replace string values if needed + if ( + hasKeyReplacements && + typeof value === 'string' && + keyReplacements[value] + ) { + value = keyReplacements[value]; } - newObj[newKey] = value; + // Process recursively only if object/array + newObj[newKey] = + value && typeof value === 'object' ? replaceJsonKeys(value) : value; } return newObj; @@ -88,18 +93,27 @@ const sanitizeJsonOutput = (data) => { // Replace the processJsonOutput function with this more efficient version const processJsonOutput = ( data, - options = { - sanitize: true, - replaceKeys: true, - keyReplacements: {}, - } + options = { sanitize: true, replaceKeys: true } ) => { - // Early return for null or non-objects - if (data === null || typeof data !== 'object') { - return data; + // Use a more efficient deep clone approach instead of JSON.parse(JSON.stringify()) + function deepClone(obj) { + if (obj === null || typeof obj !== 'object') { + return obj; + } + + if (Array.isArray(obj)) { + return obj.map((item) => deepClone(item)); + } + + const clone = {}; + Object.keys(obj).forEach((key) => { + clone[key] = deepClone(obj[key]); + }); + + return clone; } - // Copy data first to avoid reference issues + // Create a deep copy of the data to avoid reference issues let processedData = deepClone(data); // Apply sanitization if needed @@ -107,48 +121,14 @@ const processJsonOutput = ( processedData = sanitizeJsonOutput(processedData); } - // Apply key replacement if needed - pass replacements directly - if ( - options.replaceKeys && - Object.keys(options.keyReplacements || {}).length > 0 - ) { - processedData = replaceJsonKeys(processedData, options.keyReplacements); + // Apply key replacement if needed + if (options.replaceKeys) { + processedData = replaceJsonKeys(processedData); } return processedData; }; -/** - * Optimized deep clone function - * @param {any} obj - The object to clone - * @returns {any} - The cloned object - */ -const deepClone = (obj) => { - if (obj === null || typeof obj !== 'object') { - return obj; - } - - // Fast path for arrays - if (Array.isArray(obj)) { - const length = obj.length; - const clone = new Array(length); - for (let i = 0; i < length; i++) { - clone[i] = deepClone(obj[i]); - } - return clone; - } - - // Fast path for objects - const clone = {}; - const keys = Object.keys(obj); - for (let i = 0; i < keys.length; i++) { - const key = keys[i]; - clone[key] = deepClone(obj[key]); - } - - return clone; -}; - // Store active sessions to avoid repeated logins const activeSessions = new Map(); @@ -190,6 +170,7 @@ class ApiTimeoutError extends Error { } } +/* class ApiAuthError extends Error { constructor(message = 'Authentication failed') { super(message); @@ -202,7 +183,7 @@ class ApiDataError extends Error { super(message); this.name = 'ApiDataError'; } -} +} */ // Helper function to handle API errors const handleApiError = (error, res) => { diff --git a/src/js/test.js b/src/js/test.js index 08c9606..55ad27a 100644 --- a/src/js/test.js +++ b/src/js/test.js @@ -3,7 +3,7 @@ import fs from 'fs'; // Login using the SSO token const ssoToken = - 'MTk1NjgyNzA6MTc0NDQ4OTcxNDE4MDpiOGE2MDEwMzY1ZWQ5OTM0NGM3ZjA0MWQxMTFjMTExNA'; + ''; (async () => { try {