'use strict'; const fs = require('fs'); const path = require('path'); const CONFIG_PATH = path.join(__dirname, '../config.json'); let cachedConfig = null; let cachedConfigMtimeMs = null; const DEFAULT_SCHEDULED_RESTART = { enabled: false, cadence: 'daily', time: '04:00', }; function normalizeScheduledRestart (value) { const raw = value && typeof value === 'object' ? value : {}; const cadence = String(raw.cadence || '').trim().toLowerCase(); const allowedCadences = new Set(['daily', 'every3days', 'weekly', 'biweekly', 'monthly']); const time = String(raw.time || '').trim(); return { enabled: !!raw.enabled, cadence: allowedCadences.has(cadence) ? cadence : DEFAULT_SCHEDULED_RESTART.cadence, time: /^([01]\d|2[0-3]):([0-5]\d)$/.test(time) ? time : DEFAULT_SCHEDULED_RESTART.time, }; } function normalizeLoadedConfig (parsed) { if (!parsed || typeof parsed !== 'object') { return { port: 3000, paths: [], rootAliases: {} }; } if (!parsed.rootAliases || typeof parsed.rootAliases !== 'object') { parsed.rootAliases = {}; } parsed.scheduledRestart = normalizeScheduledRestart(parsed.scheduledRestart); return parsed; } function loadConfig () { try { const stat = fs.statSync(CONFIG_PATH); if (cachedConfig && cachedConfigMtimeMs === stat.mtimeMs) { return cachedConfig; } const raw = fs.readFileSync(CONFIG_PATH, 'utf8'); const parsed = normalizeLoadedConfig(JSON.parse(raw)); cachedConfig = parsed; cachedConfigMtimeMs = stat.mtimeMs; return parsed; } catch { const defaults = { port: 3000, paths: [], rootAliases: {}, scheduledRestart: { ...DEFAULT_SCHEDULED_RESTART } }; fs.writeFileSync(CONFIG_PATH, JSON.stringify(defaults, null, 2), 'utf8'); const stat = fs.statSync(CONFIG_PATH); cachedConfig = defaults; cachedConfigMtimeMs = stat.mtimeMs; return defaults; } } function saveConfig (config) { const dir = path.dirname(CONFIG_PATH); const tmp = path.join(dir, '.config.json.tmp'); const payload = JSON.stringify(config, null, 2); // Atomic replace to avoid partially-written config files. fs.writeFileSync(tmp, payload, 'utf8'); fs.renameSync(tmp, CONFIG_PATH); const normalized = normalizeLoadedConfig(config); cachedConfig = normalized; try { cachedConfigMtimeMs = fs.statSync(CONFIG_PATH).mtimeMs; } catch { cachedConfigMtimeMs = null; } } function invalidateConfigCache () { cachedConfig = null; cachedConfigMtimeMs = null; } function normalizeForCompare (p) { const resolved = path.resolve(p); // Windows paths are case-insensitive for normal filesystems. return process.platform === 'win32' ? resolved.toLowerCase() : resolved; } /** * Returns the de-duplicated, overlap-filtered list of configured paths. * A child path is dropped when a parent path is also configured. * * Works on both Windows and Linux paths. */ function getEffectivePaths (rawPaths) { if (!Array.isArray(rawPaths)) return []; const sep = path.sep; const normalized = rawPaths.map(p => path.resolve(p)); const unique = [...new Set(normalized)]; return unique.filter(p => { const cmpP = normalizeForCompare(p); // Keep p only if no OTHER path in the list is a strict parent of p return !unique.some(other => { if (other === p) return false; const cmpOther = normalizeForCompare(other); return cmpP.startsWith(cmpOther + sep); }); }); } /** * Checks whether `filePath` is safely inside one of the `allowedRoots`. * Returns the matching root or null. */ function getAllowedRoot (filePath, allowedRoots) { const resolved = path.resolve(filePath); const cmpResolved = normalizeForCompare(resolved); const sep = path.sep; for (const root of allowedRoots) { const resolvedRoot = path.resolve(root); const cmpRoot = normalizeForCompare(resolvedRoot); if (cmpResolved === cmpRoot || cmpResolved.startsWith(cmpRoot + sep)) { return resolvedRoot; } } return null; } module.exports = { loadConfig, saveConfig, invalidateConfigCache, getEffectivePaths, getAllowedRoot, };