Update server.js to load and inject helpers module

Changes:
- Add loadHelpers() function to load helpers.js via vm.Script
- Load helpers module at startup using same isolation pattern as proxy.js
- Inject helpers object into VM context for proxy.js access
- Update paths: global/ → globalVariables/, proxy.js → proxyScripts/proxy.js
- Create isolated context for helpers with crypto and console

Implementation:
- helpers.js loaded via vm.Script with isolated context
- helpers context includes crypto and console (logger)
- helpers object returned by IIFE execution
- Injected into per-request VM context alongside other globals
- Follows constitution pattern for vm.Script module loading

Testing:
- ✓ All JavaScript syntax validated
- ✓ Helpers module loads successfully
- ✓ All 11 expected functions present
- ✓ generateRequestId() returns valid format

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
2026-03-07 10:55:17 -06:00
parent b263311a43
commit ab5aa83b6f

View File

@@ -27,11 +27,11 @@ const globalVMContext = {
let globalVariableContext = {}; let globalVariableContext = {};
/** /**
* Load all JSON files from global/ directory and make them available as global objects * Load all JSON files from globalVariables/ directory and make them available as global objects
* Pattern: global/filename.json -> globalVariableContext['filename'] * Pattern: globalVariables/filename.json -> globalVariableContext['filename']
*/ */
function loadGlobalObjects() { function loadGlobalObjects() {
const globalDir = join(__dirname, "..", "global"); const globalDir = join(__dirname, "globalVariables");
try { try {
const files = readdirSync(globalDir).filter( const files = readdirSync(globalDir).filter(
@@ -68,6 +68,44 @@ function loadGlobalObjects() {
} }
} }
/**
* Load helpers module using vm.Script
* Pattern: helpers.js returns object with utility functions via IIFE
*
* @returns {Object} Helpers object with all utility functions
*/
function loadHelpers() {
const helpersPath = join(__dirname, "globalVariables", "helpers.js");
try {
const helpersCode = readFileSync(helpersPath, "utf-8");
const helpersScript = new vm.Script(helpersCode, { filename: "helpers.js" });
// Create isolated context for helpers (needs crypto and console)
const helpersContext = vm.createContext({
crypto,
console: logger,
});
// Execute helpers.js and get the returned object
const helpers = helpersScript.runInContext(helpersContext);
logger.info("Loaded helpers module", {
functions: Object.keys(helpers).length,
exports: Object.keys(helpers),
});
return helpers;
} catch (error) {
logger.error("Failed to load helpers module", {
path: helpersPath,
error: error.message,
stack: error.stack,
});
throw error;
}
}
/** /**
* Load configuration from config/default.json * Load configuration from config/default.json
* Merges with environment variables (ENV takes precedence) * Merges with environment variables (ENV takes precedence)
@@ -119,9 +157,12 @@ async function startServer() {
// Load configuration into global.config // Load configuration into global.config
global.config = loadConfig(); global.config = loadConfig();
// Load global objects from global/ 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
const helpers = loadHelpers();
logger.info("Starting Proxy Script Server..."); logger.info("Starting Proxy Script Server...");
logger.info( logger.info(
`Configuration loaded: ${JSON.stringify({ `Configuration loaded: ${JSON.stringify({
@@ -135,7 +176,7 @@ async function startServer() {
validateConfig(global.config); validateConfig(global.config);
logger.info("Configuration validated successfully"); logger.info("Configuration validated successfully");
const proxyPath = join(__dirname, "proxy.js"); const proxyPath = join(__dirname, "proxyScripts", "proxy.js");
const proxyCode = readFileSync(proxyPath, "utf-8"); const proxyCode = readFileSync(proxyPath, "utf-8");
const script = new vm.Script(proxyCode, { filename: "proxy.js" }); const script = new vm.Script(proxyCode, { filename: "proxy.js" });
@@ -146,6 +187,7 @@ async function startServer() {
const context = vm.createContext({ const context = vm.createContext({
...globalVMContext, ...globalVMContext,
...globalVariableContext, ...globalVariableContext,
helpers,
req, req,
res, res,
}); });