返回 CodeWhale
check-runtime-routes.mjs
根目录 / web / scripts / check-runtime-routes.mjs
1 import { readFileSync, writeFileSync } from "node:fs";
2 import { fileURLToPath } from "node:url";
3
4 export function runtimeRouteInventory(source) {
5 const rows = new Map();
6 const pattern = /\.route\(\s*"([^"\n]+)"\s*,/g;
7 for (const match of source.matchAll(pattern)) {
8 let depth = 1;
9 let quoted = false;
10 let escaped = false;
11 let lineComment = false;
12 let blockComment = false;
13 let end = match.index + match[0].length;
14 for (; end < source.length && depth; end++) {
15 const char = source[end], next = source[end + 1];
16 if (lineComment) { if (char === "\n") lineComment = false; continue; }
17 if (blockComment) { if (char === "*" && next === "/") { blockComment = false; end++; } continue; }
18 if (quoted) {
19 if (escaped) escaped = false;
20 else if (char === "\\") escaped = true;
21 else if (char === '"') quoted = false;
22 continue;
23 }
24 if (char === "/" && next === "/") { lineComment = true; end++; continue; }
25 if (char === "/" && next === "*") { blockComment = true; end++; continue; }
26 if (char === '"') quoted = true;
27 else if (char === "(") depth++;
28 else if (char === ")") depth--;
29 }
30 if (depth) throw new Error(`Unclosed route registration: ${match[1]}`);
31 const body = source.slice(match.index + match[0].length, end);
32 const methods = [...body.matchAll(/\b(get|post|put|patch|delete|options|head|any)\s*\(/g)].map((method) => method[1].toUpperCase());
33 if (!methods.length) throw new Error(`Unknown HTTP method registration: ${match[1]}`);
34 const registered = rows.get(match[1]) ?? new Set();
35 for (const method of methods) registered.add(method);
36 rows.set(match[1], registered);
37 }
38 return [...rows].sort(([a], [b]) => a.localeCompare(b)).map(([path, methods]) => ({ path, methods: [...methods].sort() }));
39 }
40
41 export function renderRuntimeRouteInventory(source) {
42 const rows = runtimeRouteInventory(source);
43 return `# Runtime HTTP route inventory\n\nThis table is generated from literal route registrations in\n\`crates/app-server/src/runtime_api.rs\`. It names the paths and methods that\nthe current source registers, including native-client and compatibility\nsurfaces. A registered path is not a grant of permission or a promise that\na feature is enabled in your installed version.\n\nThe [Runtime API contract](RUNTIME_API.md) explains request fields, events,\nauthorization and compatibility. This inventory is a navigation index, not\na replacement for those schemas; routes used by the native client can have\nclient-specific contracts. Provider calls, file mutations and connected-service\nactions remain subject to their documented authority boundaries.\n\nRun \`node web/scripts/check-runtime-routes.mjs --write\` after changing a\nregistration; \`npm run check:docs\` refuses a stale inventory. Dynamic nested\nrouters, fallback handlers and static-asset mounts are outside this literal\nregistration index.\n\n| HTTP method | Path |\n| --- | --- |\n${rows.map((row) => `| ${row.methods.join(" / ")} | \`${row.path}\` |`).join("\n")}\n\nThe source contains ${rows.length} distinct literal paths in this table.\n`;
44 }
45
46 const sourcePath = new URL("../../crates/app-server/src/runtime_api.rs", import.meta.url);
47 const documentPath = new URL("../../docs/RUNTIME_ROUTES.md", import.meta.url);
48 if (process.argv[1] === fileURLToPath(import.meta.url)) {
49 const source = readFileSync(sourcePath, "utf8");
50 const expected = renderRuntimeRouteInventory(source);
51 if (process.argv.includes("--write")) writeFileSync(documentPath, expected);
52 else if (readFileSync(documentPath, "utf8") !== expected) {
53 console.error("[runtime-routes] FAIL: docs/RUNTIME_ROUTES.md differs from registered source paths; regenerate it.");
54 process.exit(1);
55 }
56 console.log(`[runtime-routes] PASS: ${runtimeRouteInventory(source).length} literal HTTP paths match source.`);
57 }
58
58 lines Plain Text