From 72e1874d1fa4072f5de3753f458e866ef5e4984f Mon Sep 17 00:00:00 2001 From: "Peter.Morton" Date: Sat, 7 Mar 2026 11:25:06 -0600 Subject: [PATCH] Simplify server.js: Combine loaders into unified loadGlobalVariables() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplification: - Combine loadGlobalObjects() + loadGlobalVariableFunctions() → loadGlobalVariables() - Single function handles both .json and .js files - Reduced from 258 to 234 lines (24 lines saved, ~9% reduction) Benefits: - Simpler API: One function instead of two - Clearer intent: Load all global variables regardless of type - Less duplication: Shared error handling and logging - Better logging: Summary shows json: N, js: M breakdown - Same behavior: JSON loaded first, then JS (maintains order) Implementation: - Filter for both .json and .js files in one pass - Separate into jsonFiles and jsFiles arrays - Process JSON files first (data) - Process JS files second (can reference JSON data) - Single summary log with breakdown: {json: 1, js: 1} Changes: - Renamed: loadGlobalObjects() → removed - Renamed: loadGlobalVariableFunctions() → removed - Added: loadGlobalVariables() - unified loader - Updated: startServer() - single loader call - Improved: Error messages now consistent ('Failed to load {file}') - Improved: Log messages ('Loaded global data' vs 'Loaded global functions') Testing: - ✓ Syntax validated - ✓ Both JSON and JS files load correctly - ✓ Correct load order (JSON first, JS second) - ✓ All 2 files loaded successfully - ✓ helpers.generateRequestId() works Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/server.js | 73 ++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 48 deletions(-) diff --git a/src/server.js b/src/server.js index 1a64dce..75800a6 100644 --- a/src/server.js +++ b/src/server.js @@ -27,18 +27,25 @@ const globalVMContext = { let globalVariableContext = {}; /** - * Load all JSON files from globalVariables/ directory and make them available as global objects - * Pattern: globalVariables/filename.json -> globalVariableContext['filename'] + * Load all files from globalVariables/ directory into globalVariableContext + * Pattern: globalVariables/filename.{json|js} -> globalVariableContext['filename'] + * - .json files: Parsed as JSON data + * - .js files: Executed via vm.Script, must return object via IIFE */ -function loadGlobalObjects() { +function loadGlobalVariables() { const globalDir = join(__dirname, "globalVariables"); try { - const files = readdirSync(globalDir).filter( - (f) => f.endsWith(".json") && !f.endsWith(".example"), + const allFiles = readdirSync(globalDir).filter( + (f) => (f.endsWith(".json") || f.endsWith(".js")) && !f.endsWith(".example.json") && !f.endsWith(".example.js") ); - files.forEach((file) => { + // Separate files by type + const jsonFiles = allFiles.filter((f) => f.endsWith(".json")); + const jsFiles = allFiles.filter((f) => f.endsWith(".js")); + + // Load JSON files first (data) + jsonFiles.forEach((file) => { const objectName = file.replace(".json", ""); const filePath = join(globalDir, file); @@ -46,42 +53,18 @@ function loadGlobalObjects() { const content = readFileSync(filePath, "utf-8"); const data = JSON.parse(content); globalVariableContext[objectName] = data; - logger.info(`Loaded global object: ${objectName}`, { + logger.info(`Loaded global data: ${objectName}`, { file: file, keys: Object.keys(data), }); } catch (error) { - logger.error(`Failed to load global object from ${file}`, { - error: error.message, - }); + logger.error(`Failed to load ${file}`, { error: error.message }); throw error; } }); - logger.info(`Loaded ${files.length} global objects from ${globalDir}`); - } catch (error) { - logger.error("Failed to load global objects", { - directory: globalDir, - error: error.message, - }); - throw error; - } -} - -/** - * Load all .js files from globalVariables/ directory using vm.Script - * Pattern: globalVariables/filename.js -> globalVariableContext['filename'] - * Each .js file must return an object via IIFE - */ -function loadGlobalVariableFunctions() { - const globalDir = join(__dirname, "globalVariables"); - - try { - const files = readdirSync(globalDir).filter( - (f) => f.endsWith(".js") && !f.endsWith(".example.js"), - ); - - files.forEach((file) => { + // Load JS files second (functions) - can reference JSON data + jsFiles.forEach((file) => { const functionName = file.replace(".js", ""); const filePath = join(globalDir, file); @@ -90,32 +73,29 @@ function loadGlobalVariableFunctions() { const script = new vm.Script(code, { filename: file }); // Execute in context with all VM globals and previously loaded variables - // Allows function modules to access same dependencies as proxy.js const tempContext = vm.createContext({ ...globalVMContext, ...globalVariableContext, }); - - const exportedObject = script.runInContext(tempContext); + const exportedObject = script.runInContext(tempContext); globalVariableContext[functionName] = exportedObject; - logger.info(`Loaded global function module: ${functionName}`, { + logger.info(`Loaded global functions: ${functionName}`, { file: file, exports: Object.keys(exportedObject || {}).length, }); } catch (error) { - logger.error(`Failed to load global function module from ${file}`, { - error: error.message, - }); + logger.error(`Failed to load ${file}`, { error: error.message }); throw error; } }); logger.info( - `Loaded ${files.length} global function modules from ${globalDir}`, + `Loaded ${allFiles.length} global variables from ${globalDir}`, + { json: jsonFiles.length, js: jsFiles.length } ); } catch (error) { - logger.error("Failed to load global function modules", { + logger.error("Failed to load global variables", { directory: globalDir, error: error.message, }); @@ -174,11 +154,8 @@ async function startServer() { // Load configuration into global.config global.config = loadConfig(); - // Load global objects from globalVariables/ directory (e.g., service account keys) - loadGlobalObjects(); - - // Load global function modules from globalVariables/ directory (e.g., helpers.js) - loadGlobalVariableFunctions(); + // Load all global variables (JSON data + JS function modules) + loadGlobalVariables(); logger.info("Starting Proxy Script Server..."); logger.info(