Commit Graph

24 Commits

Author SHA1 Message Date
a32e84ef55 Add explicit logging for JS module loading in server.js
Enhanced logging to verify object capture from globalVariables/*.js files

Changes to server.js loadGlobalVariables():
1. Extract returnedObject explicitly before assignment
2. Add detailed logging showing:
   - type: typeof the returned value
   - isObject: boolean check if it's an object
   - keys: number of properties in the object

Benefits:
 Verifies that vm.Script returns the object correctly
 Shows object has expected number of keys (11 functions)
 Confirms globalVariableContext[varName] receives the object
 Helpful for debugging module loading issues
 Makes the capture step more explicit and clear

Logging output shows:
  "Loaded global functions: googleDriveAdapterHelper",
  "type":"object",
  "isObject":true,
  "keys":11

This confirms the return object from googleDriveAdapterHelper.js
IS being added to globalVariableContext correctly!

Pattern verification:
- Line 60: const returnedObject = script.runInContext(context);
- Line 61: globalVariableContext[varName] = returnedObject;
- Result: globalVariableContext.googleDriveAdapterHelper = {11 functions}

Testing:
✓ Server starts successfully
✓ Module loads with correct object type
✓ All 11 functions captured
✓ Object available in VM context
✓ proxy.js can call googleDriveAdapterHelper.generateRequestId() etc.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 12:25:33 -06:00
ab8e43e4ab Revert to simple object literal pattern (no IIFE, no return)
Simplified to pure object literal expression

Rationale:
- User requested to forget about IIFE wrapper
- Simpler pattern: file evaluates directly to an object
- No function wrapper needed
- No return statement needed
- server.js already handles this correctly at line 59

Pattern:
```javascript
// Define functions at top level
class DocumentCountExceededError extends Error {...}
function generateRequestId() {...}
function parseRoute() {...}

// Final expression evaluates to object
({
  DocumentCountExceededError,
  generateRequestId,
  parseRoute,
  // ... all functions
});
```

How server.js handles it (already correct):
```javascript
const script = new vm.Script(code, { filename: file });
const context = vm.createContext({ ...globalVMContext, ...globalVariableContext });
globalVariableContext[varName] = script.runInContext(context);
// script.runInContext() returns the evaluated expression (the object)
```

Changes:
- Reverted from IIFE with return: (function() { return {...}; })()
- Back to object literal: ({...})
- Removed 2-space indentation throughout
- No function wrapper
- No return statement

Benefits:
 Simplest possible pattern
 Direct object evaluation
 No syntax complexity
 Server.js already handles it correctly
 Clean, minimal code

Testing:
✓ Syntax validated
✓ Server starts successfully
✓ Module loads: 'Loaded global functions: googleDriveAdapterHelper'
✓ All function calls work correctly
✓ Request handling functional

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 12:22:15 -06:00
07968927fc Change googleDriveAdapterHelper.js to use return statement
Reverted from object literal to function with return statement

Rationale:
- User requested explicit 'return' statement for clarity
- Function wrapper with 'return' is more conventional and readable
- Makes it clear the code returns an object when executed
- Matches common module pattern expectations

Structure:
```javascript
(function() {
  // Define all classes and functions
  class DocumentCountExceededError extends Error {...}
  function generateRequestId() {...}
  // ... all other functions

  // Return object with all exports
  return {
    DocumentCountExceededError,
    generateRequestId,
    // ... all other functions
  };
})();
```

Changes:
- Wrapped entire file in IIFE: (function() { ... })()
- Changed final expression from ({ ... }) to return { ... };
- Re-added 2-space indentation for content inside function
- Updated header comment: 'Returns' → 'Function that returns'

Benefits:
 Explicit return statement (clearer intent)
 Standard IIFE module pattern
 More conventional JavaScript style
 Easier to understand for new developers
 Same functionality as before

Testing:
✓ Syntax validated
✓ Server starts successfully
✓ Module loads: 'Loaded global functions: googleDriveAdapterHelper'
✓ All function calls work correctly
✓ Request handling functional

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 12:14:45 -06:00
c094d4d472 Remove IIFE wrapper from googleDriveAdapterHelper.js
Changed from IIFE pattern to direct object literal evaluation

Rationale:
- Global variable functions should evaluate to an object directly
- IIFE wrapper was unnecessary complexity
- Simpler pattern: code evaluates to object, not function that returns object
- Matches intended architecture for vm.Script module loading

Changes:
1. Removed IIFE wrapper:
   - Before: (function createHelpers() { ... return {...} })()
   - After: ({ ... })

2. Removed function indentation (2 spaces throughout file)

3. Updated constitution.md:
   - Pattern description: 'IIFE returning object' → 'evaluates to a single object'
   - Clarified: 'not wrapped in IIFE'
   - Updated all pattern references

Structure:
- File defines classes and functions at top level
- Final expression is object literal referencing all functions
- When executed via vm.Script, evaluates to the object
- Object is assigned to globalVariableContext.googleDriveAdapterHelper

Benefits:
 Simpler pattern (no function wrapper)
 Clearer intent (direct object evaluation)
 Matches architecture description
 Easier to understand and maintain
 Same functionality, cleaner implementation

Before (IIFE):

After (Object Literal):

Testing:
✓ Syntax validated
✓ Server starts successfully
✓ Module loads: 'Loaded global functions: googleDriveAdapterHelper'
✓ All function calls work correctly
✓ Request handling works as expected

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 12:06:13 -06:00
470f760b9b Rename helpers.js to googleDriveAdapterHelper.js
Rationale:
- More descriptive name indicates purpose (Google Drive adapter utilities)
- Distinguishes from generic 'helpers' (could apply to anything)
- Clearer intent when reading code and logs
- Follows convention of naming modules by their domain

Changes:
1. File renamed:
   - src/globalVariables/helpers.js → src/globalVariables/googleDriveAdapterHelper.js

2. Updated all references in proxy.js (6 occurrences):
   - helpers.generateRequestId() → googleDriveAdapterHelper.generateRequestId()
   - helpers.parseRoute() → googleDriveAdapterHelper.parseRoute()
   - helpers.DocumentCountExceededError → googleDriveAdapterHelper.DocumentCountExceededError()
   - helpers.generateSitemap() → googleDriveAdapterHelper.generateSitemap()
   - helpers.mapDriveErrorToHttp() → googleDriveAdapterHelper.mapDriveErrorToHttp()
   - Header comment updated

3. Updated constitution.md documentation:
   - All references to helpers.js → googleDriveAdapterHelper.js
   - globalVariableContext.helpers → globalVariableContext.googleDriveAdapterHelper
   - Function call examples updated throughout

Benefits:
 Self-documenting name (clear it's for Google Drive adapter)
 Better intellisense/autocomplete (shows domain)
 Clearer logs (googleDriveAdapterHelper vs generic helpers)
 Future-proof (can add other helper modules without confusion)

Generic Loading Pattern:
- server.js automatically loads all .js files from globalVariables/
- Filename determines key: googleDriveAdapterHelper.js → globalVariableContext.googleDriveAdapterHelper
- No server.js changes needed (generic loader handles it)

Testing:
✓ Syntax validated
✓ Server starts successfully
✓ Module loads: 'Loaded global functions: googleDriveAdapterHelper'
✓ All function calls work correctly

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 11:46:06 -06:00
dd19104b70 Remove unnecessary request queue from proxy.js
Reduced from 298 to 246 lines (52 lines saved, 17% reduction)

Rationale:
- Proxy.js receives only 1 request at a time per execution context
- Each request runs in isolated VM context via server.js
- FIFO queue was unnecessary complexity for single-request execution
- Node.js HTTP server already handles request queueing at TCP level

Changes:
- Removed: RequestQueue class (48 lines including getters)
- Removed: requestQueue singleton instance
- Removed: Queue wrapping in main handler
- Updated: Direct call to handleSitemapRequest()
- Updated: Section numbering (2→3, 3→4 removed)
- Updated: File header structure documentation

Benefits:
 Simpler code (17% reduction)
 Less cognitive overhead (no queue state to track)
 Faster execution (no queue overhead)
 Clearer intent (direct async call)
 Same functionality (still sequential per VM context)

Architecture Note:
- server.js creates fresh VM context per request
- Each proxy.js execution is inherently isolated
- No concurrent access possible within same context
- HTTP server manages request ordering at network layer

Testing:
✓ Syntax validated
✓ Server starts successfully
✓ Request handling works correctly
✓ No queue-related functionality needed

Progressive Simplification:
- Start: 752 lines (monolithic with utilities)
- After helper extraction: 493 lines (-35%)
- After moderate cleanup: 298 lines (-60%)
- After queue removal: 246 lines (-67% total)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 11:41:30 -06:00
74ef2761cc Update constitution: Document simplification and optimization (v1.19.0)
Version: 1.18.0 → 1.19.0 (MINOR: Code simplification and optimization)

Documented Changes:
1. **Unified Loader Function**
   - Combined loadGlobalObjects() + loadGlobalVariableFunctions() → loadGlobalVariables()
   - Single-pass file scanning with immediate categorization
   - More efficient, simpler API

2. **Code Simplification Results**
   - server.js: 258 → 183 lines (29% reduction)
   - proxy.js: 752 → 298 lines (60% reduction)
   - Combined: 596 lines removed (47% overall reduction)

3. **Key Improvements**
   - Named constants for timeouts (TOKEN_EXPIRY_MS, TOKEN_BUFFER_MS)
   - Immutable config merging pattern
   - Natural error bubbling (better stack traces)
   - Production-ready logging (info/error only)
   - Simplified validation logic

Architecture Changes:
- server.js: Unified loader, single-pass scanning, immutable patterns
- proxy.js: Inlined helpers, added constants, removed debug logging

Modified Sections:
- Sync Impact Report: Added v1.19.0 entry with complete change summary
- Section I.V: Updated to reference loadGlobalVariables() instead of separate functions
- Section I.II: Updated helpers.js pattern documentation
- Throughout: Replaced loadGlobalVariableFunctions references

Benefits Documented:
 47% fewer lines to maintain
 Faster execution (single-pass scanning)
 Better debuggability (natural error propagation)
 Improved onboarding (less code to understand)
 Production-ready (appropriate logging)

Rationale:
- Unified loader is simpler and more efficient
- Named constants improve code clarity
- Natural error bubbling provides better diagnostics
- Production logging reduces noise while maintaining observability
- Cleaner code accelerates development and reduces bugs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 11:37:31 -06:00
81dd86c09b Simplify proxy.js: Moderate cleanup for better maintainability
Section 1: Authentication (135 → 62 lines, 54% reduction)
- Inlined createServiceAccountJWT into initializeServiceAccount
- Inlined getAccessToken axios call (simple wrapper removed)
- Removed clearAuthCache (3-line test helper)
- Added constants: TOKEN_EXPIRY_MS, TOKEN_BUFFER_MS (no magic numbers)
- Simplified error handling (removed redundant try-catch)
- Condensed JWT signing to single jwt.sign() call
- Optional chaining: settings?.serviceAccount?.client_email

Section 2: Request Queue (85 → 39 lines, 54% reduction)
- Removed verbose debug logging (enqueue, _processNext)
- Simplified enqueue: inline if statement
- Simplified _processNext: direct await in try block
- Removed redundant JSDoc comments
- Kept essential structure and error handling

Section 3: Drive API Client (109 → 52 lines, 52% reduction)
- Removed debug logging (query start, each page)
- Removed verbose error logging (already bubbles with stack)
- Removed unnecessary try-catch (let errors bubble naturally)
- Moved accessToken outside loop for clarity
- Removed DocumentCountExceededError re-throw check (unnecessary)
- Inline params.append when pageToken exists

Section 4: Request Handling (124 → 88 lines, 29% reduction)
- Removed redundant JSDoc @param tags
- Simplified handleSitemapRequest comments
- Removed 'successfully' from log messages (redundant)
- Removed duplicate comment about empty body
- Inline async wrapper in requestQueue.enqueue
- Condensed route not found logic

Benefits:
 40% fewer lines to read and maintain
 Named constants instead of magic numbers
 Cleaner error handling (natural bubbling)
 Less noise from debug logging (info/error only)
 Same functionality, clearer code
 Easier to understand core business logic

Testing:
✓ Syntax validated
✓ Server starts successfully
✓ Request handling works
✓ Route parsing functional
✓ All core logic preserved

Philosophy:
- Remove verbosity that obscures intent
- Inline simple wrappers (1-2 line functions)
- Let errors bubble naturally (fewer try-catch)
- Use constants for magic numbers
- Keep essential structure and logic

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 11:35:23 -06:00
6450b93439 Simplify server.js: Conservative cleanup for better readability
Reduced from 234 to 183 lines (51 lines saved, 22% reduction)

Changes:
1. **Simplified loadGlobalVariables()** (69 lines → 33 lines)
   - Single-pass file scan with immediate categorization
   - Removed nested try-catch blocks (let errors bubble up naturally)
   - Inline file processing (no separate temp variables)
   - Cleaner: file.includes('.example') check
   - More concise logging

2. **Streamlined loadConfig()** (14 lines → 13 lines)
   - Immutable object spreading instead of mutation
   - Clearer structure shows ENV precedence
   - Removed redundant comments

3. **Simplified validateConfig()** (17 lines → 5 lines)
   - Direct error throw instead of error array accumulation
   - Single validation check (can expand as needed)
   - Removed JSDoc params (types obvious from code)

4. **Reduced redundant comments**
   - Removed comments that restate what code clearly shows
   - Kept essential pattern documentation
   - Removed JSDoc for obvious parameters

Benefits:
-  Same functionality, cleaner code
-  Fewer lines to maintain
-  Faster file scanning (single pass)
-  Simpler error handling (natural bubbling)
-  More readable (less nesting)
-  Immutable config merging (safer)

Testing:
- ✓ Syntax validated
- ✓ Server starts successfully
- ✓ Both JSON and JS files load correctly
- ✓ All 2 global variables loaded
- ✓ Configuration merging works

Philosophy:
- Conservative approach: Maintain clarity while removing redundancy
- Follow DRY principle: Eliminate duplicate filtering/processing
- Let code speak: Remove comments that restate obvious logic
- Simpler error handling: Let errors bubble naturally

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 11:29:56 -06:00
72e1874d1f Simplify server.js: Combine loaders into unified loadGlobalVariables()
Simplification:
- Combine loadGlobalObjects() + loadGlobalVariableFunctions() → loadGlobalVariables()
- Single function handles both .json and .js files
- Reduced from 258 to 234 lines (24 lines saved, ~9% reduction)

Benefits:
- Simpler API: One function instead of two
- Clearer intent: Load all global variables regardless of type
- Less duplication: Shared error handling and logging
- Better logging: Summary shows json: N, js: M breakdown
- Same behavior: JSON loaded first, then JS (maintains order)

Implementation:
- Filter for both .json and .js files in one pass
- Separate into jsonFiles and jsFiles arrays
- Process JSON files first (data)
- Process JS files second (can reference JSON data)
- Single summary log with breakdown: {json: 1, js: 1}

Changes:
- Renamed: loadGlobalObjects() → removed
- Renamed: loadGlobalVariableFunctions() → removed
- Added: loadGlobalVariables() - unified loader
- Updated: startServer() - single loader call
- Improved: Error messages now consistent ('Failed to load {file}')
- Improved: Log messages ('Loaded global data' vs 'Loaded global functions')

Testing:
- ✓ Syntax validated
- ✓ Both JSON and JS files load correctly
- ✓ Correct load order (JSON first, JS second)
- ✓ All 2 files loaded successfully
- ✓ helpers.generateRequestId() works

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 11:25:06 -06:00
b55e503cbc Update constitution to document loadGlobalVariableFunctions pattern
Version: 1.17.0 → 1.18.0 (MINOR: Function loading pattern documentation)

Changes:
- Document loadGlobalVariableFunctions() generic pattern
- Update VM context injection code example
- Update helpers.js loading documentation
- Document tempContext with full globalVMContext + globalVariableContext
- Fix duplicate req entry and renumber to 11 and 12

Key Documentation Updates:

1. Section I.II - helpers.js Pattern:
   - Loaded by loadGlobalVariableFunctions() which scans ALL .js files
   - Filename determines key: helpers.js → globalVariableContext.helpers
   - Executed in tempContext with full VM globals access

2. Section I.V - VM Context Injection Pattern:
   - Replaced specific helpers loading code with generic pattern
   - Documents loadGlobalVariableFunctions() scanning all .js files
   - Shows tempContext creation with {...globalVMContext, ...globalVariableContext}
   - Documents 4-step loading process with filename-to-key mapping

3. Section I.V - Helper Functions Module:
   - Added Generic Loading Pattern section
   - Documents full access to VM globals (axios, jwt, crypto, etc.)
   - Documents access to previously loaded JSON data and functions
   - Added note about extensibility (utils.js, validators.js, etc.)

Architecture Notes:
- Generic pattern: any .js file in globalVariables/ auto-loaded
- tempContext includes all dependencies from both contexts
- Function modules can access same globals as proxy.js
- Supports multiple function modules without code changes

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 11:22:07 -06:00
6df1d49e9a Update tempContext to use full globalVMContext and globalVariableContext
Changes:
- Change tempContext from minimal {crypto, console: logger}
- Now uses {...globalVMContext, ...globalVariableContext}
- Provides function modules access to all VM globals
- Matches the context pattern used for proxy.js

Benefits:
- Function modules can access all dependencies (axios, jwt, etc.)
- Consistent with per-request context pattern
- Previously loaded JSON data available to function modules
- More flexible for complex function modules

Context now includes:
- From globalVMContext: URLSearchParams, URL, console, crypto, axios, uuidv4, jwt, xmlBuilder
- From globalVariableContext: Previously loaded JSON files and function modules
- Allows function modules to depend on other globals if needed

Testing:
- ✓ Syntax validated
- ✓ helpers.js loads correctly with full context
- ✓ helpers.generateRequestId() uses crypto from context
- ✓ All 11 exports available

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 11:17:19 -06:00
6e2e385d6a Refactor: Rename loadHelpers to loadGlobalVariableFunctions with generic pattern
Changes:
- Rename loadHelpers() → loadGlobalVariableFunctions()
- Match pattern of loadGlobalObjects() for consistency
- Load ALL .js files from globalVariables/ directory (not just helpers.js)
- Use filename as key in globalVariableContext (e.g., helpers.js → 'helpers')
- Generic implementation supports multiple function modules

Pattern:
- globalVariables/filename.js → globalVariableContext['filename']
- Each .js file must return an object via IIFE
- Files filtered: *.js (excluding *.example.js)
- Executed in temp context with crypto and console

Benefits:
- Consistent with loadGlobalObjects() pattern
- Generic: can add more function modules without code changes
- Filename determines the global variable name
- Clear separation: .json for data, .js for functions

Example:
- helpers.js → globalVariableContext.helpers
- utils.js → globalVariableContext.utils (future)
- validators.js → globalVariableContext.validators (future)

Testing:
- ✓ Syntax validated
- ✓ helpers.js loads correctly with 11 exports
- ✓ Available in context as 'helpers' object
- ✓ Pattern matches loadGlobalObjects() structure

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 11:03:35 -06:00
d6fa0b1f97 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>
2026-03-07 11:00:17 -06:00
ab5aa83b6f 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>
2026-03-07 10:55:17 -06:00
b263311a43 Extract helper functions from proxy.js into helpers.js module
- Create src/globalVariables/helpers.js (315 lines)
- Extract 11 pure utility functions from proxy.js
- Reduce proxy.js from 752 to 493 lines (35% reduction)
- Load helpers via vm.Script with same isolation pattern
- Update constitution to document helper extraction pattern

Extracted functions:
- generateRequestId, validateDocumentId, validateDocumentCount
- escapeXml, mapDriveErrorToHttp
- toSitemapEntry, transformDocumentsToSitemapEntries
- generateSitemapXML, generateSitemap
- parseRoute, DocumentCountExceededError class

Architecture:
- helpers.js loaded via vm.Script (IIFE returning object)
- Injected as 'helpers' global object into VM context
- proxy.js accesses via helpers.functionName() pattern
- Maintains zero-import isolation pattern

Constitution version: 1.16.0 → 1.17.0

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 10:52:49 -06:00
f6710203c7 Reorganize project structure: relocate proxy.js and global directory
- Move src/proxy.js → src/proxyScripts/proxy.js
- Move global/ → src/globalVariables/
- Update constitution.md to reflect new file locations
- Update all documentation references to new paths
- Consolidate all source code under src/ directory

Constitution version: 1.15.0 → 1.16.0

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-03-07 10:35:03 -06:00
c2ecc72775 removed .old files 2026-03-07 01:36:41 -06:00
b35a5c82ce bringing inline with code 2026-03-07 01:35:07 -06:00
a9406d4292 remove globalThis and added URL to global 2026-03-07 01:29:07 -06:00
1a6bd09b7b Now working as a vm.Script passing in all the Globals the proxy script needs 2026-03-07 01:20:45 -06:00
67b36f97ce trying to stop proxy.js from exporting 2026-03-07 00:02:46 -06:00
e9495f65b5 Initial Version of sitemap.xml spec 2026-03-06 23:34:00 -06:00
fec5bfa5c7 Initial commit from Specify template 2026-03-05 23:44:06 -06:00