|
| 1 | +import { readdirSync, readFileSync, statSync } from "node:fs"; |
| 2 | +import { join, relative, sep } from "node:path"; |
| 3 | + |
| 4 | +/** |
| 5 | + * F52 — a provider mounted with no `baseUrl` on a project whose `apiPrefix` is not empty. |
| 6 | + * |
| 7 | + * 1.0 moved `$apiPrefix` out of the entity descriptor: a browser's base URL is a |
| 8 | + * DEPLOYMENT fact, so the provider supplies it at runtime. The migration note argues the |
| 9 | + * rename is safe because `<EntityFetcherProvider value={…}>` no longer typechecks — and |
| 10 | + * that is true for the adopter who leaves `value` alone. It says nothing about the |
| 11 | + * adopter who reads the note, renames `value` → `fetcher`, and stops: `baseUrl` is |
| 12 | + * OPTIONAL with default `""`, so that tree compiles clean and every generated hook has |
| 13 | + * quietly lost its `/api` segment. Nothing at the type level can see it, and on the |
| 14 | + * estate where this was found nothing else could either — `vite build` transpiles without |
| 15 | + * typechecking, so `build`, 150 tests and every `meta verify` gate were green over a |
| 16 | + * storefront and an admin app whose entire generated CRUD surface would have 404'd. |
| 17 | + * |
| 18 | + * So this is the only gate that can catch it, and it is a WARNING: `baseUrl` is genuinely |
| 19 | + * optional, a project may serve its API at the origin root, and a prefix can legitimately |
| 20 | + * come from somewhere this scan cannot read. It never touches an exit code. |
| 21 | + * |
| 22 | + * It fires ONLY when `apiPrefix` is non-empty — the `meta init` scaffold sets `""`, which |
| 23 | + * is exactly why the trap is easy to ship: the default-configured project is the one the |
| 24 | + * design was reasoned about. |
| 25 | + */ |
| 26 | +export interface BaseUrlFinding { |
| 27 | + file: string; |
| 28 | + line: number; |
| 29 | + /** The provider construct found without a base. */ |
| 30 | + construct: string; |
| 31 | + message: string; |
| 32 | +} |
| 33 | + |
| 34 | +const IGNORE_SEGMENTS = new Set([ |
| 35 | + "node_modules", ".git", "dist", "build", ".next", ".turbo", ".output", |
| 36 | + "coverage", ".metaobjects", ".wrangler", |
| 37 | +]); |
| 38 | +const SCAN_EXT = /\.(?:ts|tsx|js|jsx|mjs)$/; |
| 39 | +const SKIP_FILE = /(?:\.d\.ts$|\.test\.|\.spec\.)/; |
| 40 | + |
| 41 | +/** |
| 42 | + * The two ways a base is supplied, one per client tier. |
| 43 | + * |
| 44 | + * Matched across the WHOLE opening construct rather than a single line, because a JSX |
| 45 | + * provider is nearly always written multi-line — a per-line rule (which is what the |
| 46 | + * anti-pattern scan uses) would miss the formatting every real codebase has. |
| 47 | + */ |
| 48 | +const PROVIDERS: readonly { construct: string; open: RegExp; kind: "jsx" | "call" }[] = [ |
| 49 | + { construct: "<EntityFetcherProvider>", open: /<EntityFetcherProvider\b/g, kind: "jsx" }, |
| 50 | + { construct: "provideEntityFetcher()", open: /provideEntityFetcher\s*\(/g, kind: "call" }, |
| 51 | +]; |
| 52 | + |
| 53 | +/** Nesting introduced by `c`: +1 for an opener, -1 for a closer, 0 otherwise. */ |
| 54 | +function nesting(c: string): number { |
| 55 | + if (c === "{" || c === "(" || c === "[") return 1; |
| 56 | + if (c === "}" || c === ")" || c === "]") return -1; |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * The text of the opening construct that begins at `start`. |
| 62 | + * |
| 63 | + * JSX: up to the `>` that closes the opening TAG — tracked by nesting depth, because |
| 64 | + * `onSomething={() => x}` puts a `>` inside an attribute and a naive first-`>` search |
| 65 | + * would cut the tag in half and miss a `baseUrl` written after it. |
| 66 | + * |
| 67 | + * Call: to the `)` balancing the argument list, so a nested object or arrow does not end |
| 68 | + * it early. |
| 69 | + * |
| 70 | + * Both bail after a generous cap rather than scanning a whole minified file when a |
| 71 | + * construct is unterminated. |
| 72 | + */ |
| 73 | +function constructText(src: string, start: number, kind: "jsx" | "call"): string { |
| 74 | + const limit = Math.min(src.length, start + 4000); |
| 75 | + let depth = 0; |
| 76 | + for (let i = start; i < limit; i++) { |
| 77 | + const c = src[i]!; |
| 78 | + if (kind === "jsx" && c === ">" && depth === 0) return src.slice(start, i + 1); |
| 79 | + depth += nesting(c); |
| 80 | + if (kind === "call" && depth === 0 && c === ")") return src.slice(start, i + 1); |
| 81 | + } |
| 82 | + return src.slice(start, limit); |
| 83 | +} |
| 84 | + |
| 85 | +function scanFile(rel: string, src: string, apiPrefix: string): BaseUrlFinding[] { |
| 86 | + const out: BaseUrlFinding[] = []; |
| 87 | + for (const { construct, open, kind } of PROVIDERS) { |
| 88 | + open.lastIndex = 0; |
| 89 | + let m: RegExpExecArray | null; |
| 90 | + while ((m = open.exec(src)) !== null) { |
| 91 | + const text = constructText(src, m.index, kind); |
| 92 | + if (/\bbaseUrl\b/.test(text)) continue; |
| 93 | + const line = src.slice(0, m.index).split("\n").length; |
| 94 | + out.push({ |
| 95 | + file: rel, line, construct, |
| 96 | + message: |
| 97 | + `${rel}:${line} — ${construct} is mounted with no baseUrl, but this project's ` + |
| 98 | + `apiPrefix is "${apiPrefix}". Every generated hook fetches a path RELATIVE to ` + |
| 99 | + `baseUrl, which defaults to "", so those requests go to the origin root and miss ` + |
| 100 | + `"${apiPrefix}". Pass baseUrl (a literal, or your build's env var). If the API ` + |
| 101 | + `really is served at the root here, this is a false positive — ignore it.`, |
| 102 | + }); |
| 103 | + } |
| 104 | + } |
| 105 | + return out; |
| 106 | +} |
| 107 | + |
| 108 | +function walk(dir: string, root: string, acc: BaseUrlFinding[], apiPrefix: string): void { |
| 109 | + let entries; |
| 110 | + try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return; } |
| 111 | + for (const e of entries) { |
| 112 | + if (e.name.startsWith(".") && e.name !== ".") { if (IGNORE_SEGMENTS.has(e.name)) continue; } |
| 113 | + if (IGNORE_SEGMENTS.has(e.name)) continue; |
| 114 | + const abs = join(dir, e.name); |
| 115 | + // Follow directories through symlinks the way the anti-pattern scan does not need to, |
| 116 | + // but never re-enter one: statSync on a broken link throws, which is caught here. |
| 117 | + let isDir: boolean; |
| 118 | + try { isDir = e.isDirectory() || (e.isSymbolicLink() && statSync(abs).isDirectory()); } |
| 119 | + catch { continue; } |
| 120 | + if (isDir) { walk(abs, root, acc, apiPrefix); continue; } |
| 121 | + if (!SCAN_EXT.test(e.name) || SKIP_FILE.test(e.name)) continue; |
| 122 | + let src: string; |
| 123 | + try { src = readFileSync(abs, "utf8"); } catch { continue; } |
| 124 | + if (!src.includes("EntityFetcher")) continue; |
| 125 | + acc.push(...scanFile(relative(root, abs).split(sep).join("/"), src, apiPrefix)); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Scan a project tree for providers mounted without a base. |
| 131 | + * |
| 132 | + * Returns [] when `apiPrefix` is empty — there is nothing to lose then, and warning would |
| 133 | + * fire on every scaffolded project. |
| 134 | + */ |
| 135 | +export function scanForMissingBaseUrl(root: string, apiPrefix: string): BaseUrlFinding[] { |
| 136 | + if (apiPrefix === "") return []; |
| 137 | + const acc: BaseUrlFinding[] = []; |
| 138 | + walk(root, root, acc, apiPrefix); |
| 139 | + return acc; |
| 140 | +} |
0 commit comments