Documentation: utils / baseContext
Purpose:
- Asynchronously build base context object with site-wide data for rendering views.
- Construct rendering context and helpers for templates.
Lifecycle Role:
Prepare shared context before rendering templates.
Dependencies:
Upstream:
- postMenuService
- utilityFunctions (formatMonths, filterSecureLinks)
- environmentVariables
- jsonContentFiles
- getBaseContext
- qualifyLink
- generateToken
Downstream:
- routeHandlers
- controllers rendering pages with standard site context
- view renderers
Data Flow:
Inputs:
- isAuthenticated boolean
- optional context overrides
- req.isAuthenticated
Outputs:
- context object with UI state, navigation, menus, environment-configured values
- res.locals.baseContext
- custom render functions
Side Effects:
- Token generation
- async file reads
Performance and Scalability:
Bottlenecks:
- async file reads (getPostsMenu) delay on slow IO
- reliance on correct environment variable settings
- possible navLinks JSON file read failures or malformed data
- Token generation per request
Concurrency:
None
Security and Stability:
Validation:
- Filters secure links based on authentication
- Requires validation of dynamic environment variables
- Dynamic content used in views must be escaped
Vulnerabilities:
- Risk of environment variable injection if unvalidated
- Token misuse via URLs
Architecture Assessment:
Coupling:
Moderate coupling to post menu service, utilities, environment, token logic
Abstraction:
- Centralizes context building to promote DRY templates
- Rendering context injection
Recommendations:
- Cache menu and navLinks to reduce IO per request
- Validate environment variables at startup
- Memoize within request lifecycle to avoid repeated calls
- Cache static context
- Sanitize dynamic content