remove globalThis and added URL to global
This commit is contained in:
@@ -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`:
|
The `server.js` file MUST inject the following objects into VM context for use by `proxy.js`:
|
||||||
|
|
||||||
**VM Context Injection Pattern:**
|
**VM Context Injection Pattern:**
|
||||||
|
|
||||||
|
server.js uses a spread operator pattern for cleaner context creation:
|
||||||
|
|
||||||
```javascript
|
```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({
|
const context = vm.createContext({
|
||||||
...globalVMContext,
|
...globalVMContext, // Spread static dependencies
|
||||||
...globalVariableContext,
|
...globalVariableContext, // Spread dynamic data (e.g., google_drive_settings)
|
||||||
req,
|
req, // Fresh request object
|
||||||
res,
|
res // Fresh response object
|
||||||
});
|
});
|
||||||
script.runInContext(context);
|
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:**
|
**Core Infrastructure Context Variables:**
|
||||||
|
|
||||||
1. **console** - Custom logger from `logger.js`
|
1. **console** - Custom logger from `logger.js`
|
||||||
- Purpose: Structured JSON logging
|
- Purpose: Structured JSON logging
|
||||||
- Usage: `console.info()`, `console.debug()`, `console.error()`
|
- 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)
|
2. **crypto** - Web Crypto API (built-in)
|
||||||
- Purpose: UUID generation, cryptographic operations
|
- Purpose: UUID generation, cryptographic operations
|
||||||
@@ -129,14 +148,7 @@ script.runInContext(context);
|
|||||||
- Injected from: `globalVMContext.crypto`
|
- Injected from: `globalVMContext.crypto`
|
||||||
- Note: Web Crypto API available by default in Node.js
|
- Note: Web Crypto API available by default in Node.js
|
||||||
|
|
||||||
3. **config** - Configuration object
|
3. **axios** - HTTP client library
|
||||||
- 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
|
|
||||||
- Purpose: Making HTTP requests to external APIs
|
- Purpose: Making HTTP requests to external APIs
|
||||||
- Usage: `axios.get(url)`, `axios.post(url, data)`
|
- Usage: `axios.get(url)`, `axios.post(url, data)`
|
||||||
- Package: `axios`
|
- Package: `axios`
|
||||||
|
|||||||
@@ -16,7 +16,7 @@
|
|||||||
* - uuidv4: UUID generator
|
* - uuidv4: UUID generator
|
||||||
* - jwt: JSON Web Token library
|
* - jwt: JSON Web Token library
|
||||||
* - xmlBuilder: XML document builder
|
* - 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
|
* - serviceAccount: Service account credentials
|
||||||
* - scopes: OAuth2 scopes array
|
* - scopes: OAuth2 scopes array
|
||||||
* - driveQuery: Drive API query filter
|
* - driveQuery: Drive API query filter
|
||||||
@@ -100,11 +100,11 @@ async function getAccessToken(jwtToken) {
|
|||||||
async function initializeServiceAccount() {
|
async function initializeServiceAccount() {
|
||||||
try {
|
try {
|
||||||
// Load settings from consolidated global object
|
// Load settings from consolidated global object
|
||||||
const settings = globalThis["google_drive_settings"];
|
const settings = google_drive_settings;
|
||||||
|
|
||||||
if (!settings) {
|
if (!settings) {
|
||||||
throw new Error(
|
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) {
|
async function handleSitemapRequest(res, requestId) {
|
||||||
try {
|
try {
|
||||||
// Get configuration from consolidated global settings
|
// Get configuration from consolidated global settings
|
||||||
const settings = globalThis["google_drive_settings"] || {};
|
const settings = google_drive_settings || {};
|
||||||
const maxUrls = settings.sitemap?.maxUrls || 50000;
|
const maxUrls = settings.sitemap?.maxUrls || 50000;
|
||||||
const query = settings.driveQuery || "trashed = false";
|
const query = settings.driveQuery || "trashed = false";
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ const __dirname = dirname(__filename);
|
|||||||
|
|
||||||
const globalVMContext = {
|
const globalVMContext = {
|
||||||
URLSearchParams,
|
URLSearchParams,
|
||||||
|
URL,
|
||||||
console: logger,
|
console: logger,
|
||||||
crypto,
|
crypto,
|
||||||
axios,
|
axios,
|
||||||
|
|||||||
Reference in New Issue
Block a user