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>
This commit is contained in:
2026-03-07 12:14:45 -06:00
parent c094d4d472
commit 07968927fc

View File

@@ -6,7 +6,7 @@
*
* ARCHITECTURE:
* - Loaded by server.js using vm.Script (same as proxy.js)
* - Returns a single object containing all helper functions
* - Function that returns a single object containing all helper functions
* - Injected into globalVariableContext for access by proxy.js
* - NO IMPORTS - All dependencies provided via VM context
*
@@ -17,41 +17,42 @@
* @returns {Object} Helpers object with all utility functions
*/
/**
(function() {
/**
* Custom error for document count exceeding limit
*/
class DocumentCountExceededError extends Error {
constructor(count, limit) {
class DocumentCountExceededError extends Error {
constructor(count, limit) {
super(`Document count ${count} exceeds limit of ${limit}`);
this.name = "DocumentCountExceededError";
this.count = count;
this.limit = limit;
this.statusCode = 413;
}
}
}
}
// =============================================================================
// Utility Functions
// =============================================================================
// =============================================================================
// Utility Functions
// =============================================================================
/**
/**
* Generate a unique request ID for tracing
* Uses UUID v4 for uniqueness
*
* @returns {string} Request ID in format: req_<uuid>
*/
function generateRequestId() {
return `req_${crypto.randomUUID()}`;
}
function generateRequestId() {
return `req_${crypto.randomUUID()}`;
}
/**
/**
* Validate document ID format
* Google Drive IDs are alphanumeric with hyphens and underscores
*
* @param {string} id - Document ID to validate
* @returns {boolean} True if valid
*/
function validateDocumentId(id) {
function validateDocumentId(id) {
if (!id || typeof id !== "string") {
return false;
}
@@ -60,33 +61,33 @@ function validateDocumentId(id) {
// Characters: a-z, A-Z, 0-9, -, _
const pattern = /^[a-zA-Z0-9_-]{8,128}$/;
return pattern.test(id);
}
}
/**
/**
* Validate document count against limit
*
* @param {number} count - Document count
* @param {number} limit - Maximum allowed (default: 50000)
* @throws {DocumentCountExceededError} If count > limit
*/
function validateDocumentCount(count, limit = 50000) {
function validateDocumentCount(count, limit = 50000) {
if (count > limit) {
throw new DocumentCountExceededError(count, limit);
}
}
}
// =============================================================================
// XML Utilities
// =============================================================================
// =============================================================================
// XML Utilities
// =============================================================================
/**
/**
* Escape special XML characters
* Prevents XML injection and ensures valid XML output
*
* @param {string} str - String to escape
* @returns {string} Escaped string safe for XML
*/
function escapeXml(str) {
function escapeXml(str) {
if (!str) return "";
return str
@@ -95,13 +96,13 @@ function escapeXml(str) {
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&apos;");
}
}
// =============================================================================
// Error Mapping
// =============================================================================
// =============================================================================
// Error Mapping
// =============================================================================
/**
/**
* Map Drive API error to HTTP status code and retry info
*
* Per specification:
@@ -113,7 +114,7 @@ function escapeXml(str) {
* @param {Error} error - Drive API error
* @returns {Object} { statusCode, retryAfter? }
*/
function mapDriveErrorToHttp(error) {
function mapDriveErrorToHttp(error) {
// Handle DocumentCountExceededError
if (error instanceof DocumentCountExceededError) {
return { statusCode: 413 };
@@ -146,13 +147,13 @@ function mapDriveErrorToHttp(error) {
// All other errors map to 500
return { statusCode: 500 };
}
}
// =============================================================================
// Sitemap Functions
// =============================================================================
// =============================================================================
// Sitemap Functions
// =============================================================================
/**
/**
* Transform Drive document to sitemap entry
*
* Creates RESTful URL in format: {baseUrl}/documents/{documentId}
@@ -164,7 +165,7 @@ function mapDriveErrorToHttp(error) {
* @param {string} baseUrl - Base URL for the adapter
* @returns {Object} Sitemap entry { loc, lastmod }
*/
function toSitemapEntry(document, baseUrl) {
function toSitemapEntry(document, baseUrl) {
if (!document || !document.id) {
console.error("Invalid document for sitemap entry", { document });
return null;
@@ -191,16 +192,16 @@ function toSitemapEntry(document, baseUrl) {
}
return { loc, lastmod };
}
}
/**
/**
* Transform array of Drive documents to sitemap entries
*
* @param {Array<Object>} documents - Array of Drive API documents
* @param {string} baseUrl - Base URL for the adapter
* @returns {Array<Object>} Array of sitemap entries
*/
function transformDocumentsToSitemapEntries(documents, baseUrl) {
function transformDocumentsToSitemapEntries(documents, baseUrl) {
if (!Array.isArray(documents)) {
console.error("Documents must be an array", { documents });
return [];
@@ -209,9 +210,9 @@ function transformDocumentsToSitemapEntries(documents, baseUrl) {
return documents
.map((doc) => toSitemapEntry(doc, baseUrl))
.filter((entry) => entry !== null);
}
}
/**
/**
* Generate XML sitemap from sitemap entries
*
* Handles empty sitemap (0 documents) case - returns valid XML with empty urlset.
@@ -219,7 +220,7 @@ function transformDocumentsToSitemapEntries(documents, baseUrl) {
* @param {Array<Object>} sitemapEntries - Array of { loc, lastmod } objects
* @returns {string} Complete XML sitemap string
*/
function generateSitemapXML(sitemapEntries) {
function generateSitemapXML(sitemapEntries) {
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';
@@ -239,9 +240,9 @@ function generateSitemapXML(sitemapEntries) {
xml += "</urlset>";
return xml;
}
}
/**
/**
* Main sitemap generation function
*
* Combines document transformation and XML generation.
@@ -250,22 +251,22 @@ function generateSitemapXML(sitemapEntries) {
* @param {string} baseUrl - Base URL for the adapter
* @returns {string} Complete XML sitemap
*/
function generateSitemap(documents, baseUrl) {
function generateSitemap(documents, baseUrl) {
const entries = transformDocumentsToSitemapEntries(documents, baseUrl);
return generateSitemapXML(entries);
}
}
// =============================================================================
// Route Parsing
// =============================================================================
// =============================================================================
// Route Parsing
// =============================================================================
/**
/**
* Parse route from request
* @param {string} method - HTTP method
* @param {string} url - Request URL
* @returns {Object} Route info or error
*/
function parseRoute(method, url) {
function parseRoute(method, url) {
if (method !== "GET") {
return { route: null, error: "Method not allowed", statusCode: 405 };
}
@@ -278,35 +279,35 @@ function parseRoute(method, url) {
return { route: "sitemap" };
}
// All other paths return 404
return { route: null, error: "Not found", statusCode: 404 };
}
// All other paths return 404
return { route: null, error: "Not found", statusCode: 404 };
}
// =============================================================================
// Return helpers object with all functions
// =============================================================================
// =============================================================================
// Return helpers object with all functions
// =============================================================================
return {
// Error classes
DocumentCountExceededError,
({
// 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,
};
})();