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:
@@ -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
|
||||
*
|
||||
* @returns {Object} Helpers object with all utility functions
|
||||
*/
|
||||
function loadHelpers() {
|
||||
const helpersPath = join(__dirname, "globalVariables", "helpers.js");
|
||||
@@ -81,21 +79,21 @@ function loadHelpers() {
|
||||
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({
|
||||
// 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);
|
||||
|
||||
// Execute helpers.js and get the returned object
|
||||
const helpers = helpersScript.runInContext(helpersContext);
|
||||
// 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),
|
||||
});
|
||||
|
||||
return helpers;
|
||||
} catch (error) {
|
||||
logger.error("Failed to load helpers module", {
|
||||
path: helpersPath,
|
||||
@@ -160,8 +158,8 @@ async function startServer() {
|
||||
// Load global objects from globalVariables/ directory (e.g., service account keys)
|
||||
loadGlobalObjects();
|
||||
|
||||
// Load helpers module via vm.Script
|
||||
const helpers = loadHelpers();
|
||||
// Load helpers module via vm.Script and add to globalVariableContext
|
||||
loadHelpers();
|
||||
|
||||
logger.info("Starting Proxy Script Server...");
|
||||
logger.info(
|
||||
@@ -184,10 +182,10 @@ async function startServer() {
|
||||
const server = http.createServer((req, res) => {
|
||||
try {
|
||||
// Create a context with all globals that proxy.js needs
|
||||
// globalVariableContext includes both JSON data and helpers object
|
||||
const context = vm.createContext({
|
||||
...globalVMContext,
|
||||
...globalVariableContext,
|
||||
helpers,
|
||||
req,
|
||||
res,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user