|
| 1 | +import { readFileSync, writeFileSync, existsSync } from 'fs' |
| 2 | + |
| 3 | +export type RouteConfig = { |
| 4 | + path: string |
| 5 | + listing: boolean | string[] |
| 6 | + download: boolean |
| 7 | + recursive: boolean |
| 8 | +} |
| 9 | + |
| 10 | +export type ServeConfig = { |
| 11 | + routes: RouteConfig[] |
| 12 | +} |
| 13 | + |
| 14 | +const defaultConfig: ServeConfig = { routes: [] } |
| 15 | + |
| 16 | +export function loadServeConfig(configPath: string): ServeConfig { |
| 17 | + if (!existsSync(configPath)) return defaultConfig |
| 18 | + const raw = readFileSync(configPath, 'utf-8') |
| 19 | + const parsed = JSON.parse(raw) |
| 20 | + if (!parsed || !Array.isArray(parsed.routes)) return defaultConfig |
| 21 | + return { |
| 22 | + routes: parsed.routes.map((r: Record<string, unknown>) => ({ |
| 23 | + path: normalizePath(String(r.path ?? '')), |
| 24 | + listing: Array.isArray(r.listing) ? r.listing.map(String) : r.listing === true, |
| 25 | + download: r.download !== false, |
| 26 | + recursive: r.recursive === true, |
| 27 | + })), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +export function saveServeConfig(configPath: string, config: ServeConfig): void { |
| 32 | + writeFileSync(configPath, JSON.stringify(config, null, 2) + '\n') |
| 33 | +} |
| 34 | + |
| 35 | +function normalizePath(p: string): string { |
| 36 | + return p.replace(/^\/+/, '').replace(/\/+$/, '') |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Find the most specific route matching the given path. |
| 41 | + * |
| 42 | + * Non-recursive routes cover the exact path and one level of children: |
| 43 | + * route "s" matches "s", "s/file.txt" — but NOT "s/sub/" or "s/sub/file.txt" |
| 44 | + * |
| 45 | + * Recursive routes cover the path and all descendants: |
| 46 | + * route "public" (recursive) matches "public", "public/a", "public/a/b/c" |
| 47 | + * |
| 48 | + * Most specific (longest path) match always wins. |
| 49 | + */ |
| 50 | +export function findRoute(path: string, config: ServeConfig): RouteConfig | null { |
| 51 | + const normalized = normalizePath(path) |
| 52 | + let best: RouteConfig | null = null |
| 53 | + let bestLen = -1 |
| 54 | + |
| 55 | + for (const route of config.routes) { |
| 56 | + const rp = route.path |
| 57 | + |
| 58 | + if (rp === '') { |
| 59 | + if (normalized === '') { |
| 60 | + if (bestLen < 0) { |
| 61 | + best = route |
| 62 | + bestLen = 0 |
| 63 | + } |
| 64 | + } else if (route.recursive) { |
| 65 | + if (bestLen < 0) { |
| 66 | + best = route |
| 67 | + bestLen = 0 |
| 68 | + } |
| 69 | + } else { |
| 70 | + if (!normalized.includes('/') && bestLen < 0) { |
| 71 | + best = route |
| 72 | + bestLen = 0 |
| 73 | + } |
| 74 | + } |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + if (normalized === rp) { |
| 79 | + if (rp.length > bestLen) { |
| 80 | + best = route |
| 81 | + bestLen = rp.length |
| 82 | + } |
| 83 | + } else if (normalized.startsWith(rp + '/')) { |
| 84 | + const remainder = normalized.substring(rp.length + 1) |
| 85 | + if (route.recursive) { |
| 86 | + if (rp.length > bestLen) { |
| 87 | + best = route |
| 88 | + bestLen = rp.length |
| 89 | + } |
| 90 | + } else { |
| 91 | + if (!remainder.includes('/') && rp.length > bestLen) { |
| 92 | + best = route |
| 93 | + bestLen = rp.length |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return best |
| 100 | +} |
| 101 | + |
| 102 | +/** Check if a path has any matching route (is served at all). */ |
| 103 | +export function isPathServed(path: string, config: ServeConfig): boolean { |
| 104 | + return findRoute(path, config) !== null |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Check if directory listing is allowed for this path. |
| 109 | + * - true: all items shown |
| 110 | + * - false: no listing |
| 111 | + * - string[]: only items with matching names shown (use isNameListed to filter) |
| 112 | + */ |
| 113 | +export function canList(path: string, config: ServeConfig): boolean { |
| 114 | + const route = findRoute(path, config) |
| 115 | + if (!route) return false |
| 116 | + return route.listing !== false |
| 117 | +} |
| 118 | + |
| 119 | +/** Check if a specific name is included in the listing filter. */ |
| 120 | +export function isNameListed(name: string, route: RouteConfig): boolean { |
| 121 | + if (route.listing === true) return true |
| 122 | + if (route.listing === false) return false |
| 123 | + return route.listing.includes(name) |
| 124 | +} |
| 125 | + |
| 126 | +/** Check if file downloads are allowed for this path. */ |
| 127 | +export function canDownload(path: string, config: ServeConfig): boolean { |
| 128 | + const route = findRoute(path, config) |
| 129 | + if (!route) return false |
| 130 | + return route.download |
| 131 | +} |
0 commit comments