Refactor: Store helpers in globalVariableContext instead of separate variable

Changes:
- Store helpers object in globalVariableContext.helpers
- Remove separate 'helpers' variable and helpersContext
- Helpers now spread into context via ...globalVariableContext
- Simplifies context injection - all globals in one place

Benefits:
- More consistent with JSON data loading pattern
- Single source of truth (globalVariableContext) for all VM globals
- Cleaner context creation (no separate helpers variable)
- helpers treated same as other global objects

Implementation:
- loadHelpers() now mutates globalVariableContext instead of returning
- Use tempContext for helpers execution (discarded after use)
- helpers accessible as 'helpers' in proxy.js via spread operator

Testing:
- ✓ Syntax validated
- ✓ helpers accessible in VM context
- ✓ Spread operator includes both JSON data and helpers
- ✓ All 11 helper functions available

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

View File

@@ -69,10 +69,8 @@ function loadGlobalObjects() {
} }
/** /**
* Load helpers module using vm.Script * Load helpers module using vm.Script and store in globalVariableContext
* Pattern: helpers.js returns object with utility functions via IIFE * Pattern: helpers.js returns object with utility functions via IIFE
*
* @returns {Object} Helpers object with all utility functions
*/ */
function loadHelpers() { function loadHelpers() {
const helpersPath = join(__dirname, "globalVariables", "helpers.js"); const helpersPath = join(__dirname, "globalVariables", "helpers.js");
@@ -81,21 +79,21 @@ function loadHelpers() {
const helpersCode = readFileSync(helpersPath, "utf-8"); const helpersCode = readFileSync(helpersPath, "utf-8");
const helpersScript = new vm.Script(helpersCode, { filename: "helpers.js" }); const helpersScript = new vm.Script(helpersCode, { filename: "helpers.js" });
// Create isolated context for helpers (needs crypto and console) // Execute helpers.js in context with crypto and console
const helpersContext = vm.createContext({ // Returns object with all helper functions
const tempContext = vm.createContext({
crypto, crypto,
console: logger, console: logger,
}); });
const helpers = helpersScript.runInContext(tempContext);
// Execute helpers.js and get the returned object // Store in globalVariableContext to be spread into per-request context
const helpers = helpersScript.runInContext(helpersContext); globalVariableContext.helpers = helpers;
logger.info("Loaded helpers module", { logger.info("Loaded helpers module", {
functions: Object.keys(helpers).length, functions: Object.keys(helpers).length,
exports: Object.keys(helpers), exports: Object.keys(helpers),
}); });
return helpers;
} catch (error) { } catch (error) {
logger.error("Failed to load helpers module", { logger.error("Failed to load helpers module", {
path: helpersPath, path: helpersPath,
@@ -160,8 +158,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 // Load helpers module via vm.Script and add to globalVariableContext
const helpers = loadHelpers(); loadHelpers();
logger.info("Starting Proxy Script Server..."); logger.info("Starting Proxy Script Server...");
logger.info( logger.info(
@@ -184,10 +182,10 @@ async function startServer() {
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
try { try {
// Create a context with all globals that proxy.js needs // Create a context with all globals that proxy.js needs
// globalVariableContext includes both JSON data and helpers object
const context = vm.createContext({ const context = vm.createContext({
...globalVMContext, ...globalVMContext,
...globalVariableContext, ...globalVariableContext,
helpers,
req, req,
res, res,
}); });