001-drive-proxy-adapter #1
@@ -27,18 +27,25 @@ const globalVMContext = {
|
|||||||
let globalVariableContext = {};
|
let globalVariableContext = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load all JSON files from globalVariables/ directory and make them available as global objects
|
* Load all files from globalVariables/ directory into globalVariableContext
|
||||||
* Pattern: globalVariables/filename.json -> globalVariableContext['filename']
|
* Pattern: globalVariables/filename.{json|js} -> globalVariableContext['filename']
|
||||||
|
* - .json files: Parsed as JSON data
|
||||||
|
* - .js files: Executed via vm.Script, must return object via IIFE
|
||||||
*/
|
*/
|
||||||
function loadGlobalObjects() {
|
function loadGlobalVariables() {
|
||||||
const globalDir = join(__dirname, "globalVariables");
|
const globalDir = join(__dirname, "globalVariables");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const files = readdirSync(globalDir).filter(
|
const allFiles = readdirSync(globalDir).filter(
|
||||||
(f) => f.endsWith(".json") && !f.endsWith(".example"),
|
(f) => (f.endsWith(".json") || f.endsWith(".js")) && !f.endsWith(".example.json") && !f.endsWith(".example.js")
|
||||||
);
|
);
|
||||||
|
|
||||||
files.forEach((file) => {
|
// Separate files by type
|
||||||
|
const jsonFiles = allFiles.filter((f) => f.endsWith(".json"));
|
||||||
|
const jsFiles = allFiles.filter((f) => f.endsWith(".js"));
|
||||||
|
|
||||||
|
// Load JSON files first (data)
|
||||||
|
jsonFiles.forEach((file) => {
|
||||||
const objectName = file.replace(".json", "");
|
const objectName = file.replace(".json", "");
|
||||||
const filePath = join(globalDir, file);
|
const filePath = join(globalDir, file);
|
||||||
|
|
||||||
@@ -46,42 +53,18 @@ function loadGlobalObjects() {
|
|||||||
const content = readFileSync(filePath, "utf-8");
|
const content = readFileSync(filePath, "utf-8");
|
||||||
const data = JSON.parse(content);
|
const data = JSON.parse(content);
|
||||||
globalVariableContext[objectName] = data;
|
globalVariableContext[objectName] = data;
|
||||||
logger.info(`Loaded global object: ${objectName}`, {
|
logger.info(`Loaded global data: ${objectName}`, {
|
||||||
file: file,
|
file: file,
|
||||||
keys: Object.keys(data),
|
keys: Object.keys(data),
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Failed to load global object from ${file}`, {
|
logger.error(`Failed to load ${file}`, { error: error.message });
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(`Loaded ${files.length} global objects from ${globalDir}`);
|
// Load JS files second (functions) - can reference JSON data
|
||||||
} catch (error) {
|
jsFiles.forEach((file) => {
|
||||||
logger.error("Failed to load global objects", {
|
|
||||||
directory: globalDir,
|
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load all .js files from globalVariables/ directory using vm.Script
|
|
||||||
* Pattern: globalVariables/filename.js -> globalVariableContext['filename']
|
|
||||||
* Each .js file must return an object via IIFE
|
|
||||||
*/
|
|
||||||
function loadGlobalVariableFunctions() {
|
|
||||||
const globalDir = join(__dirname, "globalVariables");
|
|
||||||
|
|
||||||
try {
|
|
||||||
const files = readdirSync(globalDir).filter(
|
|
||||||
(f) => f.endsWith(".js") && !f.endsWith(".example.js"),
|
|
||||||
);
|
|
||||||
|
|
||||||
files.forEach((file) => {
|
|
||||||
const functionName = file.replace(".js", "");
|
const functionName = file.replace(".js", "");
|
||||||
const filePath = join(globalDir, file);
|
const filePath = join(globalDir, file);
|
||||||
|
|
||||||
@@ -90,32 +73,29 @@ function loadGlobalVariableFunctions() {
|
|||||||
const script = new vm.Script(code, { filename: file });
|
const script = new vm.Script(code, { filename: file });
|
||||||
|
|
||||||
// Execute in context with all VM globals and previously loaded variables
|
// Execute in context with all VM globals and previously loaded variables
|
||||||
// Allows function modules to access same dependencies as proxy.js
|
|
||||||
const tempContext = vm.createContext({
|
const tempContext = vm.createContext({
|
||||||
...globalVMContext,
|
...globalVMContext,
|
||||||
...globalVariableContext,
|
...globalVariableContext,
|
||||||
});
|
});
|
||||||
|
|
||||||
const exportedObject = script.runInContext(tempContext);
|
const exportedObject = script.runInContext(tempContext);
|
||||||
|
|
||||||
globalVariableContext[functionName] = exportedObject;
|
globalVariableContext[functionName] = exportedObject;
|
||||||
logger.info(`Loaded global function module: ${functionName}`, {
|
logger.info(`Loaded global functions: ${functionName}`, {
|
||||||
file: file,
|
file: file,
|
||||||
exports: Object.keys(exportedObject || {}).length,
|
exports: Object.keys(exportedObject || {}).length,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Failed to load global function module from ${file}`, {
|
logger.error(`Failed to load ${file}`, { error: error.message });
|
||||||
error: error.message,
|
|
||||||
});
|
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`Loaded ${files.length} global function modules from ${globalDir}`,
|
`Loaded ${allFiles.length} global variables from ${globalDir}`,
|
||||||
|
{ json: jsonFiles.length, js: jsFiles.length }
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Failed to load global function modules", {
|
logger.error("Failed to load global variables", {
|
||||||
directory: globalDir,
|
directory: globalDir,
|
||||||
error: error.message,
|
error: error.message,
|
||||||
});
|
});
|
||||||
@@ -174,11 +154,8 @@ async function startServer() {
|
|||||||
// Load configuration into global.config
|
// Load configuration into global.config
|
||||||
global.config = loadConfig();
|
global.config = loadConfig();
|
||||||
|
|
||||||
// Load global objects from globalVariables/ directory (e.g., service account keys)
|
// Load all global variables (JSON data + JS function modules)
|
||||||
loadGlobalObjects();
|
loadGlobalVariables();
|
||||||
|
|
||||||
// Load global function modules from globalVariables/ directory (e.g., helpers.js)
|
|
||||||
loadGlobalVariableFunctions();
|
|
||||||
|
|
||||||
logger.info("Starting Proxy Script Server...");
|
logger.info("Starting Proxy Script Server...");
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|||||||
Reference in New Issue
Block a user