Public: anyone can read this; the crew can change it.
A test names every synchronous file or process call on a request path, and the list only shrinks #17895
Merged 22h 42m ago, live in production
mason/forge:mason/17885-a-test-names-every-synchronous-file-or-p → main 0 commits fork
1 file
changed +126
−0
test/request-path-sync.test.ts added +126
| @@ -0,0 +1,126 @@ | ||
| 1 | /** | |
| 2 | * Every synchronous file or process call on a request path is named here, with | |
| 3 | * its reason (#17885). A stall comes back one cause at a time: a `readFileSync` | |
| 4 | * in a handler blocks every request the process holds while the disk answers. | |
| 5 | * This test reads src/http, src/web/pages and src/apps/proxy.ts for the calls | |
| 6 | * in SYNC and fails on a file whose count is not exactly its row in ALLOWED: | |
| 7 | * a new call fails until it is cached, sliced, moved off the thread or named | |
| 8 | * with a reason; a call that went away fails until its row shrinks, so the list | |
| 9 | * only shrinks. It reads the source as text and never runs it, as | |
| 10 | * src/core/review_scan.ts does. | |
| 11 | */ | |
| 12 | import { expect, test } from "bun:test"; | |
| 13 | import { readdirSync, readFileSync, statSync } from "node:fs"; | |
| 14 | import { join, relative } from "node:path"; | |
| 15 | ||
| 16 | const ROOT = join(import.meta.dir, ".."); | |
| 17 | const SCANNED = ["src/http", "src/web/pages", "src/apps/proxy.ts"]; | |
| 18 | const SYNC = ["spawnSync", "execSync", "readFileSync", "readdirSync", "statSync", "writeFileSync", "appendFileSync"] as const; | |
| 19 | type SyncCall = (typeof SYNC)[number]; | |
| 20 | ||
| 21 | interface Allowed { | |
| 22 | readonly count: number; | |
| 23 | /** | |
| 24 | * Why this call does not stall a request: boot-only, cached after first read, off-thread, bounded | |
| 25 | * and rare. | |
| 26 | */ | |
| 27 | readonly reason: string; | |
| 28 | } | |
| 29 | ||
| 30 | /** By repository path, then by call: how many there are and why each is allowed. */ | |
| 31 | const ALLOWED: Record<string, Partial<Record<SyncCall, Allowed>>> = { | |
| 32 | "src/http/plugin_routes.ts": { | |
| 33 | readdirSync: { count: 1, reason: "boot-only: marketplaceManifest lists the commands once, when pluginRoutes registers it" }, | |
| 34 | readFileSync: { count: 1, reason: "cached after first read: plugin() keeps the bytes and ETag for the life of the process" }, | |
| 35 | }, | |
| 36 | "src/http/access_log.ts": { | |
| 37 | readdirSync: { | |
| 38 | count: 2, | |
| 39 | reason: "prune runs once a day per directory inside the batched flush; readAccess lists at most 14 day files for an owner's settings page", | |
| 40 | }, | |
| 41 | statSync: { | |
| 42 | count: 4, | |
| 43 | reason: "batched: flushAccess runs once a second on a timer (or on the 200th waiting line), once per day file, and prune at most once a day", | |
| 44 | }, | |
| 45 | writeFileSync: { count: 1, reason: "rare: shedOlder runs only when a day's file reaches its cap, writing half of it once" }, | |
| 46 | appendFileSync: { count: 1, reason: "batched: one append per day file a flush, once a second on a timer, never one per request" }, | |
| 47 | }, | |
| 48 | "src/web/pages/security.ts": { | |
| 49 | statSync: { count: 1, reason: "admin-only security page: one stat of security-report.json before it is read" }, | |
| 50 | readFileSync: { count: 1, reason: "admin-only security page: one read of security-report.json, refused past 64 KiB by the stat before it" }, | |
| 51 | }, | |
| 52 | }; | |
| 53 | ||
| 54 | const FIX = "cache it (read once, keep it), slice it (read a bounded tail, in batches), or move it off the thread (Bun.file, node:fs/promises, a worker)"; | |
| 55 | ||
| 56 | /** The .ts files under each scanned path, repository-relative. */ | |
| 57 | function scannedFiles(): string[] { | |
| 58 | const out: string[] = []; | |
| 59 | for (const path of SCANNED) { | |
| 60 | const abs = join(ROOT, path); | |
| 61 | if (statSync(abs).isFile()) out.push(path); | |
| 62 | else | |
| 63 | for (const f of readdirSync(abs, { recursive: true }) as string[]) | |
| 64 | if (f.endsWith(".ts")) out.push(relative(ROOT, join(abs, f))); | |
| 65 | } | |
| 66 | return out.sort(); | |
| 67 | } | |
| 68 | ||
| 69 | /** Source without its comments: block comments and lines that are only a line comment. */ | |
| 70 | function code(text: string): string { | |
| 71 | return text | |
| 72 | .replace(/\/\*[\s\S]*?\*\//g, "") | |
| 73 | .split("\n") | |
| 74 | .filter((l) => !l.trimStart().startsWith("//")) | |
| 75 | .join("\n"); | |
| 76 | } | |
| 77 | ||
| 78 | /** Each sync call a file makes, by name, with its count; imports are not calls. */ | |
| 79 | function syncCalls(text: string): Partial<Record<SyncCall, number>> { | |
| 80 | const out: Partial<Record<SyncCall, number>> = {}; | |
| 81 | const body = code(text); | |
| 82 | for (const name of SYNC) { | |
| 83 | const n = body.match(new RegExp(`\\b${name}\\s*\\(`, "g"))?.length ?? 0; | |
| 84 | if (n > 0) out[name] = n; | |
| 85 | } | |
| 86 | return out; | |
| 87 | } | |
| 88 | ||
| 89 | test("the scan counts calls, not imports or comments", () => { | |
| 90 | const text = [ | |
| 91 | 'import { readFileSync, statSync } from "node:fs";', | |
| 92 | "// readFileSync(x) in a comment", | |
| 93 | "/** statSync(x) in a doc */", | |
| 94 | "const a = readFileSync(p); const b = fs.readFileSync (q);", | |
| 95 | "Bun.spawnSync(['git']);", | |
| 96 | ].join("\n"); | |
| 97 | expect(syncCalls(text)).toEqual({ readFileSync: 2, spawnSync: 1 }); | |
| 98 | }); | |
| 99 | ||
| 100 | test("every synchronous file or process call on a request path is named with its reason, and the list only shrinks", () => { | |
| 101 | const problems: string[] = []; | |
| 102 | const files = scannedFiles(); | |
| 103 | for (const file of files) { | |
| 104 | const found = syncCalls(readFileSync(join(ROOT, file), "utf8")); | |
| 105 | const allowed = ALLOWED[file] ?? {}; | |
| 106 | for (const name of SYNC) { | |
| 107 | const n = found[name] ?? 0; | |
| 108 | const row = allowed[name]; | |
| 109 | const want = row?.count ?? 0; | |
| 110 | if (n > want) | |
| 111 | problems.push( | |
| 112 | `${file}: ${n} ${name} call(s), ${want} allowed. A request path must not block on the disk or a process: ${FIX}. ` + | |
| 113 | `Only if it truly never runs per request, add it to ALLOWED in test/request-path-sync.test.ts with its reason.`, | |
| 114 | ); | |
| 115 | else if (n < want) | |
| 116 | problems.push(`${file}: ${n} ${name} call(s), ALLOWED says ${want}. The list only shrinks: lower its count (or drop the row) in test/request-path-sync.test.ts.`); | |
| 117 | } | |
| 118 | } | |
| 119 | for (const [file, rows] of Object.entries(ALLOWED)) { | |
| 120 | if (!files.includes(file)) problems.push(`${file}: in ALLOWED but not scanned (moved or deleted): drop its rows.`); | |
| 121 | for (const [name, row] of Object.entries(rows)) | |
| 122 | if (row.reason.trim().length < 10) | |
| 123 | problems.push(`${file}: ${name} is allowed without a reason. Say why it does not stall a request (boot-only, cached after first read, off-thread), or ${FIX}.`); | |
| 124 | } | |
| 125 | expect(problems).toEqual([]); | |
| 126 | }); |
new above
offline