| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | |
| 6 | const CONFIG_PATH = path.join(__dirname, '../config.json'); |
| 7 | |
| 8 | function loadConfig () |
| 9 | { |
| 10 | try |
| 11 | { |
| 12 | const raw = fs.readFileSync(CONFIG_PATH, 'utf8'); |
| 13 | const parsed = JSON.parse(raw); |
| 14 | if (!parsed || typeof parsed !== 'object') |
| 15 | { |
| 16 | return { port: 3000, paths: [], rootAliases: {} }; |
| 17 | } |
| 18 | if (!parsed.rootAliases || typeof parsed.rootAliases !== 'object') |
| 19 | { |
| 20 | parsed.rootAliases = {}; |
| 21 | } |
| 22 | return parsed; |
| 23 | } catch |
| 24 | { |
| 25 | const defaults = { port: 3000, paths: [], rootAliases: {} }; |
| 26 | fs.writeFileSync(CONFIG_PATH, JSON.stringify(defaults, null, 2), 'utf8'); |
| 27 | return defaults; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | function saveConfig (config) |
| 32 | { |
| 33 | const dir = path.dirname(CONFIG_PATH); |
| 34 | const tmp = path.join(dir, '.config.json.tmp'); |
| 35 | const payload = JSON.stringify(config, null, 2); |
| 36 | |
| 37 | // Atomic replace to avoid partially-written config files. |
| 38 | fs.writeFileSync(tmp, payload, 'utf8'); |
| 39 | fs.renameSync(tmp, CONFIG_PATH); |
| 40 | } |
| 41 | |
| 42 | function normalizeForCompare (p) |
| 43 | { |
| 44 | const resolved = path.resolve(p); |
| 45 | // Windows paths are case-insensitive for normal filesystems. |
| 46 | return process.platform === 'win32' ? resolved.toLowerCase() : resolved; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the de-duplicated, overlap-filtered list of configured paths. |
| 51 | * A child path is dropped when a parent path is also configured. |
| 52 | * |
| 53 | * Works on both Windows and Linux paths. |
| 54 | */ |
| 55 | function getEffectivePaths (rawPaths) |
| 56 | { |
| 57 | if (!Array.isArray(rawPaths)) return []; |
| 58 | |
| 59 | const sep = path.sep; |
| 60 | const normalized = rawPaths.map(p => path.resolve(p)); |
| 61 | const unique = [...new Set(normalized)]; |
| 62 | |
| 63 | return unique.filter(p => |
| 64 | { |
| 65 | const cmpP = normalizeForCompare(p); |
| 66 | // Keep p only if no OTHER path in the list is a strict parent of p |
| 67 | return !unique.some(other => |
| 68 | { |
| 69 | if (other === p) return false; |
| 70 | const cmpOther = normalizeForCompare(other); |
| 71 | return cmpP.startsWith(cmpOther + sep); |
| 72 | }); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Checks whether `filePath` is safely inside one of the `allowedRoots`. |
| 78 | * Returns the matching root or null. |
| 79 | */ |
| 80 | function getAllowedRoot (filePath, allowedRoots) |
| 81 | { |
| 82 | const resolved = path.resolve(filePath); |
| 83 | const cmpResolved = normalizeForCompare(resolved); |
| 84 | const sep = path.sep; |
| 85 | |
| 86 | for (const root of allowedRoots) |
| 87 | { |
| 88 | const resolvedRoot = path.resolve(root); |
| 89 | const cmpRoot = normalizeForCompare(resolvedRoot); |
| 90 | if (cmpResolved === cmpRoot || cmpResolved.startsWith(cmpRoot + sep)) |
| 91 | { |
| 92 | return resolvedRoot; |
| 93 | } |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | module.exports = { loadConfig, saveConfig, getEffectivePaths, getAllowedRoot }; |