Refactor: Rename loadHelpers to loadGlobalVariableFunctions with generic pattern

Changes:
- Rename loadHelpers() → loadGlobalVariableFunctions()
- Match pattern of loadGlobalObjects() for consistency
- Load ALL .js files from globalVariables/ directory (not just helpers.js)
- Use filename as key in globalVariableContext (e.g., helpers.js → 'helpers')
- Generic implementation supports multiple function modules

Pattern:
- globalVariables/filename.js → globalVariableContext['filename']
- Each .js file must return an object via IIFE
- Files filtered: *.js (excluding *.example.js)
- Executed in temp context with crypto and console

Benefits:
- Consistent with loadGlobalObjects() pattern
- Generic: can add more function modules without code changes
- Filename determines the global variable name
- Clear separation: .json for data, .js for functions

Example:
- helpers.js → globalVariableContext.helpers
- utils.js → globalVariableContext.utils (future)
- validators.js → globalVariableContext.validators (future)

Testing:
- ✓ Syntax validated
- ✓ helpers.js loads correctly with 11 exports
- ✓ Available in context as 'helpers' object
- ✓ Pattern matches loadGlobalObjects() structure

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-03-07 11:03:35 -06:00
parent d6fa0b1f97
commit 6e2e385d6a

View File

@@ -69,36 +69,54 @@ function loadGlobalObjects() {
}
/**
* Load helpers module using vm.Script and store in globalVariableContext
* Pattern: helpers.js returns object with utility functions via IIFE
* 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 loadHelpers() {
const helpersPath = join(__dirname, "globalVariables", "helpers.js");
function loadGlobalVariableFunctions() {
const globalDir = join(__dirname, "globalVariables");
try {
const helpersCode = readFileSync(helpersPath, "utf-8");
const helpersScript = new vm.Script(helpersCode, { filename: "helpers.js" });
// Execute helpers.js in context with crypto and console
// Returns object with all helper functions
const tempContext = vm.createContext({
crypto,
console: logger,
});
const helpers = helpersScript.runInContext(tempContext);
// Store in globalVariableContext to be spread into per-request context
globalVariableContext.helpers = helpers;
logger.info("Loaded helpers module", {
functions: Object.keys(helpers).length,
exports: Object.keys(helpers),
const files = readdirSync(globalDir).filter(
(f) => f.endsWith(".js") && !f.endsWith(".example.js"),
);
files.forEach((file) => {
const functionName = file.replace(".js", "");
const filePath = join(globalDir, file);
try {
const code = readFileSync(filePath, "utf-8");
const script = new vm.Script(code, { filename: file });
// Execute in context with crypto and console
// Expects file to return an object via IIFE
const tempContext = vm.createContext({
crypto,
console: logger,
});
const exportedObject = script.runInContext(tempContext);
globalVariableContext[functionName] = exportedObject;
logger.info(`Loaded global function module: ${functionName}`, {
file: file,
exports: Object.keys(exportedObject || {}).length,
});
} catch (error) {
logger.error(`Failed to load global function module from ${file}`, {
error: error.message,
});
throw error;
}
});
logger.info(
`Loaded ${files.length} global function modules from ${globalDir}`,
);
} catch (error) {
logger.error("Failed to load helpers module", {
path: helpersPath,
logger.error("Failed to load global function modules", {
directory: globalDir,
error: error.message,
stack: error.stack,
});
throw error;
}
@@ -158,8 +176,8 @@ async function startServer() {
// Load global objects from globalVariables/ directory (e.g., service account keys)
loadGlobalObjects();
// Load helpers module via vm.Script and add to globalVariableContext
loadHelpers();
// Load global function modules from globalVariables/ directory (e.g., helpers.js)
loadGlobalVariableFunctions();
logger.info("Starting Proxy Script Server...");
logger.info(