From 1fecaf52f74a646dfa87a3989cef3fec7dad60e2 Mon Sep 17 00:00:00 2001 From: "Peter.Morton" Date: Sat, 7 Mar 2026 12:31:23 -0600 Subject: [PATCH] Implement literal function body pattern for global variable functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changed architecture so .js files contain literal function bodies Changes to googleDriveAdapterHelper.js: 1. Changed from object literal (\({...}\)) to return statement (return {...}) 2. File now contains LITERAL BODY of a function 3. Updated header comment to explain this pattern 4. File is NO LONGER valid standalone JavaScript (has bare return) Changes to server.js loadGlobalVariables(): 1. Wrap loaded code in function: `(function() { ${code} })()` 2. Creates IIFE that executes the function body 3. Captures returned object from the function 4. Pattern applies to ALL .js files in globalVariables/ Pattern: ```javascript // File: googleDriveAdapterHelper.js (literal function body) class DocumentCountExceededError extends Error {...} function generateRequestId() {...} return { DocumentCountExceededError, generateRequestId, // ... all exports }; ``` ```javascript // server.js wraps it: const wrappedCode = `(function() { ${code} })()`; const script = new vm.Script(wrappedCode, { filename: file }); const returnedObject = script.runInContext(context); ``` Benefits: ✅ Files represent pure function bodies ✅ Wrapping logic centralized in server.js ✅ Clear separation: content vs. execution wrapper ✅ Explicit return statement in function body ✅ Consistent pattern for all global variable functions ✅ Easy to understand: file = function body, server = wrapper Testing: ✓ Server starts successfully ✓ Module loads: 'Loaded global functions: googleDriveAdapterHelper' ✓ Object captured: type=object, keys=11 ✓ All functions accessible in VM context ✓ proxy.js can call googleDriveAdapterHelper.* functions Note: googleDriveAdapterHelper.js will show syntax error if run standalone (has bare return statement) - this is intentional, it's a function body! Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../googleDriveAdapterHelper.js | 43 ++++++++++--------- src/server.js | 5 ++- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/globalVariables/googleDriveAdapterHelper.js b/src/globalVariables/googleDriveAdapterHelper.js index c706049..15126c9 100644 --- a/src/globalVariables/googleDriveAdapterHelper.js +++ b/src/globalVariables/googleDriveAdapterHelper.js @@ -5,8 +5,9 @@ * to improve code organization while maintaining vm.Script isolation pattern. * * ARCHITECTURE: - * - Loaded by server.js using vm.Script (same as proxy.js) - * - Returns a single object containing all helper functions + * - This file contains the LITERAL BODY of a function + * - server.js wraps this in a function: (function() { })() + * - Function returns a single object containing all helper functions * - Injected into globalVariableContext for access by proxy.js * - NO IMPORTS - All dependencies provided via VM context * @@ -286,27 +287,27 @@ return { route: null, error: "Not found", statusCode: 404 }; // Return helpers object with all functions // ============================================================================= -({ -// Error classes -DocumentCountExceededError, +return { + // Error classes + DocumentCountExceededError, -// Utilities -generateRequestId, -validateDocumentId, -validateDocumentCount, + // Utilities + generateRequestId, + validateDocumentId, + validateDocumentCount, -// XML -escapeXml, + // XML + escapeXml, -// Error mapping -mapDriveErrorToHttp, + // Error mapping + mapDriveErrorToHttp, -// Sitemap -toSitemapEntry, -transformDocumentsToSitemapEntries, -generateSitemapXML, -generateSitemap, + // Sitemap + toSitemapEntry, + transformDocumentsToSitemapEntries, + generateSitemapXML, + generateSitemap, -// Routing -parseRoute, -}); + // Routing + parseRoute, +}; diff --git a/src/server.js b/src/server.js index 6cbab1a..60f5958 100644 --- a/src/server.js +++ b/src/server.js @@ -54,7 +54,10 @@ function loadGlobalVariables() { jsFiles.forEach((file) => { const varName = file.replace(".js", ""); const code = readFileSync(join(globalDir, file), "utf-8"); - const script = new vm.Script(code, { filename: file }); + + // Wrap the literal function body in a function and execute + const wrappedCode = `(function() {\n${code}\n})()`; + const script = new vm.Script(wrappedCode, { filename: file }); const context = vm.createContext({ ...globalVMContext, ...globalVariableContext }); // Execute script and capture returned object