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