diff --git a/.specify/memory/constitution.md b/.specify/memory/constitution.md index 721d7f0..a50e6f4 100644 --- a/.specify/memory/constitution.md +++ b/.specify/memory/constitution.md @@ -105,23 +105,42 @@ During code review and planning: The `server.js` file MUST inject the following objects into VM context for use by `proxy.js`: **VM Context Injection Pattern:** + +server.js uses a spread operator pattern for cleaner context creation: + ```javascript -// Create a context with all globals that proxy.js needs +// Define static VM context (libraries and built-ins) +const globalVMContext = { + URLSearchParams, + console: logger, + crypto, + axios, + uuidv4, + jwt, + xmlBuilder, +}; + +// Load dynamic data from global/ directory into globalVariableContext +let globalVariableContext = {}; // Populated by loadGlobalObjects() + +// Per-request: Create fresh context with all dependencies const context = vm.createContext({ - ...globalVMContext, - ...globalVariableContext, - req, - res, + ...globalVMContext, // Spread static dependencies + ...globalVariableContext, // Spread dynamic data (e.g., google_drive_settings) + req, // Fresh request object + res // Fresh response object }); script.runInContext(context); ``` +**Note:** proxy.js accesses these as direct variables (e.g., `google_drive_settings`, not `globalThis["google_drive_settings"]`). The VM context makes all properties available as top-level variables. + **Core Infrastructure Context Variables:** 1. **console** - Custom logger from `logger.js` - Purpose: Structured JSON logging - Usage: `console.info()`, `console.debug()`, `console.error()` - - Injected from: `globalVMContext.console` + - Injected from: `globalVMContext.console` (set to `logger`) 2. **crypto** - Web Crypto API (built-in) - Purpose: UUID generation, cryptographic operations @@ -129,14 +148,7 @@ script.runInContext(context); - Injected from: `globalVMContext.crypto` - Note: Web Crypto API available by default in Node.js -3. **config** - Configuration object - - Purpose: Infrastructure settings ONLY (server host/port, logging level) - - Usage: `config.server.port`, `config.logging.level` - - Injected from: `global.config` - - Loaded from: `config/default.json` merged with ENV vars - - **DOES NOT contain**: Authentication, secrets, API keys, behavioral config (use global/ instead) - -4. **axios** - HTTP client library +3. **axios** - HTTP client library - Purpose: Making HTTP requests to external APIs - Usage: `axios.get(url)`, `axios.post(url, data)` - Package: `axios` diff --git a/src/proxy.js b/src/proxy.js index 9fc3dc0..5beda5a 100644 --- a/src/proxy.js +++ b/src/proxy.js @@ -16,7 +16,7 @@ * - uuidv4: UUID generator * - jwt: JSON Web Token library * - xmlBuilder: XML document builder - * - globalThis['google_drive_settings']: Consolidated settings (from global/google_drive_settings.json) + * - google_drive_settings: Consolidated settings (from global/google_drive_settings.json) * - serviceAccount: Service account credentials * - scopes: OAuth2 scopes array * - driveQuery: Drive API query filter @@ -100,11 +100,11 @@ async function getAccessToken(jwtToken) { async function initializeServiceAccount() { try { // Load settings from consolidated global object - const settings = globalThis["google_drive_settings"]; + const settings = google_drive_settings; if (!settings) { throw new Error( - 'Google Drive settings not found in globalThis["google_drive_settings"]. Ensure server.js loaded global/google_drive_settings.json', + 'Google Drive settings not found in `google_drive_settings`. Ensure server.js loaded global/google_drive_settings.json', ); } @@ -641,7 +641,7 @@ function parseRoute(method, url) { async function handleSitemapRequest(res, requestId) { try { // Get configuration from consolidated global settings - const settings = globalThis["google_drive_settings"] || {}; + const settings = google_drive_settings || {}; const maxUrls = settings.sitemap?.maxUrls || 50000; const query = settings.driveQuery || "trashed = false"; diff --git a/src/server.js b/src/server.js index 35a3c43..251b731 100644 --- a/src/server.js +++ b/src/server.js @@ -15,6 +15,7 @@ const __dirname = dirname(__filename); const globalVMContext = { URLSearchParams, + URL, console: logger, crypto, axios,