import path from 'path';
import os from 'os';
export const CONFIG = {
defaultLimit: 100,
maxLimit: 1000,
maxDepth: 256,
maxFilesScanned: 200000,
maxDirectoriesScanned: 50000,
timeoutMs: 10000,
followSymlinks: false,
};
let allowedRoots: string[] = [];
let defaultRoot: string | undefined;
export function initializeConfig() {
const envRoots = process.env.ALLOW_ROOTS;
if (!envRoots) {
console.error("Warning: ALLOW_ROOTS not set. Defaulting to current working directory for development.");
allowedRoots = [path.resolve(process.cwd())];
defaultRoot = allowedRoots[0];
return;
}
allowedRoots = envRoots.split(/[,;]/).map(r => path.resolve(r.trim())).filter(r => r.length > 0);
if (allowedRoots.length === 0) {
throw new Error("ALLOW_ROOTS must contain at least one path.");
}
const envDefault = process.env.DEFAULT_ROOT;
if (envDefault) {
const resolvedDefault = path.resolve(envDefault.trim());
const isWindows = os.platform() === 'win32';
const found = allowedRoots.find(r => {
if (isWindows) return r.toLowerCase() === resolvedDefault.toLowerCase();
return r === resolvedDefault;
});
if (!found) {
throw new Error(`DEFAULT_ROOT (${resolvedDefault}) is not in ALLOW_ROOTS.`);
}
defaultRoot = found;
} else {
defaultRoot = allowedRoots[0];
}
}
export function getAllowedRoots(): string[] {
if (allowedRoots.length === 0) initializeConfig();
return allowedRoots;
}
export function getDefaultRoot(): string {
if (!defaultRoot) initializeConfig();
return defaultRoot!;
}