Monorepo for Aesthetic.Computer aesthetic.computer
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix lith news route resolution

+82 -42
+59
lith/route-resolution.mjs
··· 1 + const ROUTE_ALIASES = { 2 + "verify-password": "verify-builds-password", 3 + "pack-telemetry": "bundle-telemetry", 4 + "pack-telemetry-query": "bundle-telemetry-query", 5 + "track-tape": "track-media", 6 + "logo.png": "logo", 7 + }; 8 + 9 + const NESTED_ROUTES = { 10 + "chat/messages": "chat-messages", 11 + "chat/heart": "chat-heart", 12 + "auth/cli-callback": "auth-cli-callback", 13 + "news/toll": "news-toll", 14 + }; 15 + 16 + const NEWS_API_ROUTES = new Set([ 17 + "posts", 18 + "updates", 19 + "submit", 20 + "comment", 21 + "vote", 22 + "delete", 23 + "unfurl", 24 + ]); 25 + 26 + function normalizeRest(rest) { 27 + if (Array.isArray(rest)) return rest.join("/"); 28 + if (typeof rest === "string") return rest; 29 + return ""; 30 + } 31 + 32 + function resolveFunctionName(fn, rest, functions = {}) { 33 + const normalizedRest = normalizeRest(rest); 34 + 35 + if (normalizedRest) { 36 + const nested = `${fn}/${normalizedRest}`; 37 + for (const [pattern, target] of Object.entries(NESTED_ROUTES)) { 38 + if (nested === pattern || nested.startsWith(`${pattern}/`)) { 39 + return target; 40 + } 41 + } 42 + } 43 + 44 + if (ROUTE_ALIASES[fn]) return ROUTE_ALIASES[fn]; 45 + 46 + if (fn === "news" && normalizedRest) { 47 + const firstSegment = normalizedRest.split("/")[0]; 48 + return NEWS_API_ROUTES.has(firstSegment) ? "news-api" : "news"; 49 + } 50 + 51 + if (fn === "ff1" && normalizedRest) { 52 + const subFn = `ff1-${normalizedRest.split("/")[0]}`; 53 + if (functions[subFn]) return subFn; 54 + } 55 + 56 + return fn; 57 + } 58 + 59 + export { resolveFunctionName };
+2 -42
lith/server.mjs
··· 20 20 import { fileURLToPath } from "url"; 21 21 import { createServer as createHttpsServer } from "https"; 22 22 import { createServer as createHttpServer } from "http"; 23 + import { resolveFunctionName } from "./route-resolution.mjs"; 23 24 24 25 const __dirname = dirname(fileURLToPath(import.meta.url)); 25 26 const SYSTEM = join(__dirname, "..", "system"); ··· 282 283 } 283 284 } 284 285 285 - // --- Route aliases (from netlify.toml where URL path ≠ function name) --- 286 - const ROUTE_ALIASES = { 287 - "verify-password": "verify-builds-password", 288 - "pack-telemetry": "bundle-telemetry", 289 - "pack-telemetry-query": "bundle-telemetry-query", 290 - "track-tape": "track-media", 291 - "logo.png": "logo", 292 - }; 293 - 294 - // --- Nested API routes (e.g. /api/chat/messages → chat-messages) --- 295 - const NESTED_ROUTES = { 296 - "chat/messages": "chat-messages", 297 - "chat/heart": "chat-heart", 298 - "auth/cli-callback": "auth-cli-callback", 299 - "news/toll": "news-toll", 300 - // /api/news/* (posts, updates, submit, etc.) → news-api function 301 - "news/": "news-api", 302 - }; 303 - 304 286 // Resolve function name from URL params 305 287 function resolveFunction(req) { 306 - const fn = req.params.fn; 307 - const rest = req.params.rest; 308 - 309 - // Check nested routes first (e.g., /api/chat/messages) 310 - if (rest) { 311 - const nested = `${fn}/${rest}`; 312 - for (const [pattern, target] of Object.entries(NESTED_ROUTES)) { 313 - if (nested === pattern || nested.startsWith(pattern)) { 314 - return target; 315 - } 316 - } 317 - } 318 - 319 - // Check aliases 320 - if (ROUTE_ALIASES[fn]) return ROUTE_ALIASES[fn]; 321 - 322 - // ff1 dynamic routing: /api/ff1-proxy, /api/ff1-pair, /api/ff1-devices 323 - if (fn === "ff1" && rest) { 324 - const subFn = `ff1-${rest.split("/")[0]}`; 325 - if (functions[subFn]) return subFn; 326 - } 327 - 328 - return fn; 288 + return resolveFunctionName(req.params.fn, req.params.rest, functions); 329 289 } 330 290 331 291 // --- Function handler (updated to use resolveFunction) ---
+21
tests/lith-route-resolution.test.mjs
··· 1 + import assert from "node:assert/strict"; 2 + import { resolveFunctionName } from "../lith/route-resolution.mjs"; 3 + 4 + function run() { 5 + assert.equal(resolveFunctionName("news", undefined), "news"); 6 + assert.equal(resolveFunctionName("news", "posts"), "news-api"); 7 + assert.equal(resolveFunctionName("news", "submit"), "news-api"); 8 + assert.equal(resolveFunctionName("news", "new"), "news"); 9 + assert.equal(resolveFunctionName("news", "report"), "news"); 10 + assert.equal(resolveFunctionName("news", "nwhh"), "news"); 11 + assert.equal(resolveFunctionName("news", ["nwhh"]), "news"); 12 + assert.equal(resolveFunctionName("news", "item/abcd"), "news"); 13 + assert.equal(resolveFunctionName("news", "toll"), "news-toll"); 14 + assert.equal(resolveFunctionName("chat", "messages"), "chat-messages"); 15 + assert.equal(resolveFunctionName("verify-password"), "verify-builds-password"); 16 + assert.equal(resolveFunctionName("ff1", "proxy/status", { "ff1-proxy": true }), "ff1-proxy"); 17 + 18 + console.log("✅ lith route resolution tests passed"); 19 + } 20 + 21 + run();