Simplify server.js: Combine loaders into unified loadGlobalVariables()
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>
This commit is contained in:
@@ -27,18 +27,25 @@ const globalVMContext = {
|
|||||||
let globalVariableContext = {};
|
let globalVariableContext = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load all JSON files from globalVariables/ directory and make them available as global objects
|
* Load all files from globalVariables/ directory into globalVariableContext
|
||||||
* Pattern: globalVariables/filename.json -> globalVariableContext['filename']
|
* 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");
|
const globalDir = join(__dirname, "globalVariables");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const files = readdirSync(globalDir).filter(
|
const allFiles = readdirSync(globalDir).filter(
|
||||||
(f) => f.endsWith(".json") && !f.endsWith(".example"),
|
(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 objectName = file.replace(".json", "");
|
||||||
const filePath = join(globalDir, file);
|
const filePath = join(globalDir, file);
|
||||||
|
|
||||||
@@ -46,42 +53,18 @@ function loadGlobalObjects() {
|
|||||||
const content = readFileSync(filePath, "utf-8");
|
const content = readFileSync(filePath, "utf-8");
|
||||||
const data = JSON.parse(content);
|
const data = JSON.parse(content);
|
||||||
globalVariableContext[objectName] = data;
|
globalVariableContext[objectName] = data;
|
||||||
logger.info(`Loaded global object: ${objectName}`, {
|
logger.info(`Loaded global data: ${objectName}`, {
|
||||||
file: file,
|
file: file,
|
||||||
keys: Object.keys(data),
|
keys: Object.keys(data),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Failed to load global object from ${file}`, {
|
logger.error(`Failed to load ${file}`, { error: error.message });
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`Loaded ${files.length} global objects from ${globalDir}`);
|
// Load JS files second (functions) - can reference JSON data
|
||||||
} catch (error) {
|
jsFiles.forEach((file) => {
|
||||||
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) => {
|
|
||||||
const functionName = file.replace(".js", "");
|
const functionName = file.replace(".js", "");
|
||||||
const filePath = join(globalDir, file);
|
const filePath = join(globalDir, file);
|
||||||
|
|
||||||
@@ -90,32 +73,29 @@ function loadGlobalVariableFunctions() {
|
|||||||
const script = new vm.Script(code, { filename: file });
|
const script = new vm.Script(code, { filename: file });
|
||||||
|
|
||||||
// Execute in context with all VM globals and previously loaded variables
|
// 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({
|
const tempContext = vm.createContext({
|
||||||
...globalVMContext,
|
...globalVMContext,
|
||||||
...globalVariableContext,
|
...globalVariableContext,
|
||||||
});
|
});
|
||||||
|
|
||||||
const exportedObject = script.runInContext(tempContext);
|
|
||||||
|
|
||||||
|
const exportedObject = script.runInContext(tempContext);
|
||||||
globalVariableContext[functionName] = exportedObject;
|
globalVariableContext[functionName] = exportedObject;
|
||||||
logger.info(`Loaded global function module: ${functionName}`, {
|
logger.info(`Loaded global functions: ${functionName}`, {
|
||||||
file: file,
|
file: file,
|
||||||
exports: Object.keys(exportedObject || {}).length,
|
exports: Object.keys(exportedObject || {}).length,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Failed to load global function module from ${file}`, {
|
logger.error(`Failed to load ${file}`, { error: error.message });
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(
|
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) {
|
} catch (error) {
|
||||||
logger.error("Failed to load global function modules", {
|
logger.error("Failed to load global variables", {
|
||||||
directory: globalDir,
|
directory: globalDir,
|
||||||
error: error.message,
|
error: error.message,
|
||||||
});
|
});
|
||||||
@@ -174,11 +154,8 @@ async function startServer() {
|
|||||||
// Load configuration into global.config
|
// Load configuration into global.config
|
||||||
global.config = loadConfig();
|
global.config = loadConfig();
|
||||||
|
|
||||||
// Load global objects from globalVariables/ directory (e.g., service account keys)
|
// Load all global variables (JSON data + JS function modules)
|
||||||
loadGlobalObjects();
|
loadGlobalVariables();
|
||||||
|
|
||||||
// Load global function modules from globalVariables/ directory (e.g., helpers.js)
|
|
||||||
loadGlobalVariableFunctions();
|
|
||||||
|
|
||||||
logger.info("Starting Proxy Script Server...");
|
logger.info("Starting Proxy Script Server...");
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|||||||
Reference in New Issue
Block a user