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(