Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

13.6 KB
Raw Blame
import assert from "node:assert/strict";
import { test } from "node:test";
import fs from "node:fs";
import path from "node:path";
import { builtinModules } from "node:module";
import { fileURLToPath } from "node:url";

/**
 * #10692: a `"use client"` page reached the SQLite driver through
 * `serviceKindIndex → mediaServiceKinds → imageRegistry → aihorde/imageModels →
 * aihordeImageCatalog → safeOutboundFetch → proxyFetch → featureFlags → db/core`, so the
 * production build tried to bundle `fs`/`net`/`tls` for the browser and failed with 28
 * `Module not found` errors (`Build App` red for 60 consecutive runs).
 *
 * `serviceKindIndex.ts` had stated the invariant in a comment — *"Client-safe:
 * `mediaServiceKinds` only pulls in the pure-data media registries (no server-only deps)"* —
 * and a comment cannot fail a build, so #10542 broke it unnoticed.
 *
 * This walks the real static-import graph, the same edges the bundler follows, from EVERY
 * `"use client"` file in the repo rather than a hand-picked pair.
 *
 * First-party dynamic `import()` with a static string specifier IS followed. The
 * bundler resolves those edges into the client chunk just like static imports
 * (a `"use client"` page reaching a server-only module through `await import()`
 * fails the production build with `Module not found`), so a guard that ignores
 * them stays green on a broken build. Only specifiers that resolve through the
 * same first-party resolver as static imports are followed — npm packages and
 * non-literal specifiers are ignored exactly like their static counterparts.
 *
 * Two deliberate exclusions, both load-bearing:
 *
 *  - **`import type` is not an edge.** TypeScript erases it before the bundler sees it. A scan
 *    that counts type imports reports 26 phantom leaks against 2 real ones here — a guard that
 *    cries wolf gets switched off.
 *
 * A reached module counts as server-only when it statically imports a Node builtin the
 * production bundler cannot resolve for the browser. The pinned list below (the original
 * #10692 chain) stays explicit so it keeps failing loudly even if the discovery logic
 * changes; everything else is found by walking the graph and checking each visited file
 * for a builtin import.
 *
 * Builtins Next ships a browser polyfill for are tolerated when imported by their BARE name
 * (`path`, `os`, `crypto`, `buffer`, …): Next's client build maps exactly those names to
 * `next/dist/compiled/*` shims (`resolve.fallback` for the client compiler in
 * `node_modules/next/dist/build/webpack-config.js`), so flagging them would cry wolf the
 * same way counting `import type` did. The `node:` scheme is never tolerated — the client
 * build has no fallback for it (`UnhandledSchemeError` on `node:fs` / `node:os` / `node:path`
 * is what broke the build this guard was widened for).
 */
const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");

/** Modules that pull in Node builtins (fs/net/tls) and must never be statically reachable. */
const SERVER_ONLY = new Set([
  "src/lib/db/core.ts",
  "src/lib/db/adapters/driverFactory.ts",
  "src/lib/db/adapters/sqljsAdapter.ts",
  "src/lib/db/migrationRunner.ts",
  "open-sse/utils/proxyFetch.ts",
  "open-sse/utils/tlsClient.ts",
]);

/**
 * Bare builtin names Next's client build polyfills (the client `resolve.fallback` map in
 * `next/dist/build/webpack-config.js`). Kept in sync by the drift test at the bottom.
 */
const NEXT_CLIENT_POLYFILLED_BUILTINS = new Set([
  "assert",
  "buffer",
  "constants",
  "crypto",
  "domain",
  "events",
  "http",
  "https",
  "os",
  "path",
  "process",
  "punycode",
  "querystring",
  "stream",
  "string_decoder",
  "sys",
  "timers",
  "tty",
  "util",
  "vm",
  "zlib",
]);

const NODE_BUILTINS = new Set(
  builtinModules.map((name) => name.replace(/^node:/, "")).filter((bare) => !bare.startsWith("_"))
);

/** True when `specifier` names a Node builtin the browser bundle cannot resolve. */
function isBrowserForbiddenBuiltin(specifier: string): boolean {
  if (specifier.startsWith("node:")) return true; // no client fallback for the scheme
  const root = specifier.split("/")[0]; // `fs/promises` → `fs`
  return NODE_BUILTINS.has(root) && !NEXT_CLIENT_POLYFILLED_BUILTINS.has(root);
}

/**
 * Non-`"use client"` entry points that still end up in a client bundle because client
 * components import them. Kept explicit so the original #10692 chain stays pinned even if the
 * page that exposed it is refactored.
 */
const EXTRA_ENTRIES = [
  "src/lib/providers/serviceKindIndex.ts",
  "open-sse/config/mediaServiceKinds.ts",
];

const EXTENSIONS = [".ts", ".tsx", ".mts", ".js"];
const SKIP_DIRS = new Set(["node_modules", ".git", ".build", "dist", ".next", ".claude"]);

/** Resolve an import specifier to a repo-relative file, or null when it leaves the repo. */
function resolveSpecifier(fromFile: string, specifier: string): string | null {
  let base: string;
  if (specifier.startsWith(".")) {
    base = path.resolve(path.dirname(path.join(REPO_ROOT, fromFile)), specifier);
  } else if (specifier.startsWith("@omniroute/open-sse")) {
    const rest = specifier.slice("@omniroute/open-sse".length).replace(/^\//, "");
    base = path.join(REPO_ROOT, "open-sse", rest);
  } else if (specifier.startsWith("@omniroute/browser-pool")) {
    const rest = specifier.slice("@omniroute/browser-pool".length).replace(/^\//, "");
    base = path.join(REPO_ROOT, "packages/browser-pool/src", rest);
  } else if (specifier.startsWith("@/")) {
    base = path.join(REPO_ROOT, "src", specifier.slice(2));
  } else {
    return null; // npm package — not our graph
  }

  const candidates = [
    base,
    ...EXTENSIONS.map((ext) => base + ext),
    ...EXTENSIONS.map((ext) => path.join(base, `index${ext}`)),
  ];
  // A `.js` specifier on a first-party module means the sibling `.ts` (see #10674).
  if (base.endsWith(".js")) candidates.push(base.replace(/\.js$/, ".ts"));

  for (const candidate of candidates) {
    if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
      return path.relative(REPO_ROOT, candidate);
    }
  }
  return null;
}

/** True when the import clause contributes no runtime binding (pure `import type`). */
function isTypeOnlyClause(clause: string): boolean {
  if (/^\s*type\s/.test(clause)) return true;
  const named = /\{([^}]*)\}/.exec(clause);
  if (!named) return false;
  // `import Default, { type A }` still emits an edge for the default binding.
  const outsideBraces = clause.replace(/\{[^}]*\}/, "").trim();
  if (/[A-Za-z_$*]/.test(outsideBraces)) return false;
  const bindings = named[1]
    .split(",")
    .map((binding) => binding.trim())
    .filter(Boolean);
  return bindings.length > 0 && bindings.every((binding) => /^type\s/.test(binding));
}

/** Value-carrying static specifiers only. */
function staticSpecifiers(source: string): string[] {
  const withoutDynamic = source.replace(/\bimport\s*\(/g, "__dynamic_import__");
  const out: string[] = [];
  for (const pattern of [
    /(?:^|\n)\s*import\s+([^;'"]*)from\s*["']([^"']+)["']/g,
    /(?:^|\n)\s*export\s+([^;'"]*)from\s*["']([^"']+)["']/g,
  ]) {
    for (const match of withoutDynamic.matchAll(pattern)) {
      if (isTypeOnlyClause(match[1])) continue;
      out.push(match[2]);
    }
  }
  // Side-effect imports (`import "./x"`) always emit an edge.
  for (const match of withoutDynamic.matchAll(/(?:^|\n)\s*import\s*["']([^"']+)["']/g)) {
    out.push(match[1]);
  }
  return out;
}

/**
 * First-party dynamic-import targets (`import("…")` / `await import(`…`)`) with a
 * resolvable static specifier. The bundler follows exactly these edges into the
 * client chunk, so the guard must too. Non-literal specifiers (`import(variable)`)
 * cannot be resolved statically by the bundler either and are ignored; npm-package
 * specifiers resolve to null downstream, like their static counterparts.
 */
function dynamicSpecifiers(source: string): string[] {
  const out: string[] = [];
  for (const match of source.matchAll(/\bimport\s*\(\s*["']([^"']+)["']\s*\)/g)) {
    out.push(match[1]);
  }
  for (const match of source.matchAll(/\bimport\s*\(\s*`([^`$]+)`\s*\)/g)) {
    out.push(match[1]);
  }
  return out;
}

const specifierCache = new Map<string, string[]>();
function specifiersOf(file: string): string[] {
  const cached = specifierCache.get(file);
  if (cached) return cached;
  const absolute = path.join(REPO_ROOT, file);
  let specs: string[] = [];
  if (fs.existsSync(absolute)) {
    const source = fs.readFileSync(absolute, "utf8");
    specs = [...staticSpecifiers(source), ...dynamicSpecifiers(source)];
  }
  specifierCache.set(file, specs);
  return specs;
}
// Resolved edges and verdicts are cached per file: the BFS runs once per client entry and
// re-visits the same shared modules thousands of times, so re-resolving specifiers
// (fs.existsSync/statSync per candidate) on every visit made the guard ~6x slower.
const edgeCache = new Map<string, string[]>();
function edgesOf(file: string): string[] {
  const cached = edgeCache.get(file);
  if (cached) return cached;
  const edges = specifiersOf(file)
    .map((specifier) => resolveSpecifier(file, specifier))
    .filter((resolved): resolved is string => resolved !== null);
  edgeCache.set(file, edges);
  return edges;
}

const serverOnlyVerdictCache = new Map<string, boolean>();
/** True when the file is pinned server-only or itself imports a browser-forbidden builtin. */
function isServerOnly(file: string): boolean {
  const cached = serverOnlyVerdictCache.get(file);
  if (cached !== undefined) return cached;
  const verdict = SERVER_ONLY.has(file) || specifiersOf(file).some(isBrowserForbiddenBuiltin);
  serverOnlyVerdictCache.set(file, verdict);
  return verdict;
}

/** BFS over static imports; returns the first path reaching a server-only module. */
function findServerOnlyPath(entry: string): string[] | null {
  const seen = new Set<string>([entry]);
  if (isServerOnly(entry)) return [entry];
  const queue: Array<string[]> = [[entry]];
  while (queue.length > 0) {
    const trail = queue.shift()!;
    for (const resolved of edgesOf(trail[trail.length - 1])) {
      if (seen.has(resolved)) continue;
      if (isServerOnly(resolved)) {
        return [...trail, resolved];
      }
      seen.add(resolved);
      queue.push([...trail, resolved]);
    }
  }
  return null;
}

function walk(dir: string, acc: string[] = []): string[] {
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
    const full = path.join(dir, entry.name);
    if (entry.isDirectory()) {
      if (SKIP_DIRS.has(entry.name)) continue;
      walk(full, acc);
    } else if (/\.tsx?$/.test(entry.name)) {
      acc.push(path.relative(REPO_ROOT, full));
    }
  }
  return acc;
}

function clientEntryPoints(): string[] {
  return walk(path.join(REPO_ROOT, "src")).filter((file) =>
    /^\s*["']use client["']/m.test(
      fs.readFileSync(path.join(REPO_ROOT, file), "utf8").slice(0, 200)
    )
  );
}

test("no client entry point statically reaches server-only code", () => {
  const entries = [...clientEntryPoints(), ...EXTRA_ENTRIES];
  assert.ok(entries.length > 100, `expected the repo's client components, found ${entries.length}`);

  const offenders = entries
    .map((entry) => ({ entry, trail: findServerOnlyPath(entry) }))
    .filter((row): row is { entry: string; trail: string[] } => row.trail !== null);

  assert.deepEqual(
    offenders.map((o) => o.entry),
    [],
    "A client bundle would have to include server-only modules:\n" +
      offenders.map((o) => `  ${o.trail.join("\n    → ")}`).join("\n\n") +
      "\nBreak the chain — or, when the binding is only a type, mark it `import type` so it " +
      "carries no runtime edge."
  );
});

test("dynamic import specifiers: literals are followed, variables and npm packages are not", () => {
  assert.deepEqual(
    dynamicSpecifiers(
      `const a = await import("@/lib/db/readCache");\nconst b = await import("./local");`
    ),
    ["@/lib/db/readCache", "./local"]
  );
  // Non-literal specifiers cannot be resolved statically — by the bundler or the guard.
  assert.deepEqual(dynamicSpecifiers("const m = await import(specifier);"), []);
  assert.deepEqual(dynamicSpecifiers("const m = await import(`./${name}`);"), []);
  // npm packages leave the repo graph: they are extracted here but resolve to null.
  assert.equal(resolveSpecifier("open-sse/services/model.ts", "zod"), null);
  assert.equal(
    resolveSpecifier("open-sse/services/model.ts", "@/lib/db/readCache"),
    "src/lib/db/readCache.ts"
  );
});
test("builtin classification: node: scheme always forbidden, bare polyfilled names tolerated", () => {
  for (const specifier of ["node:fs", "node:path", "node:os", "node:crypto", "fs", "fs/promises"]) {
    assert.equal(isBrowserForbiddenBuiltin(specifier), true, specifier);
  }
  for (const specifier of ["child_process", "net", "tls", "module", "worker_threads"]) {
    assert.equal(isBrowserForbiddenBuiltin(specifier), true, specifier);
  }
  for (const specifier of ["path", "os", "crypto", "buffer", "events", "util", "stream"]) {
    assert.equal(isBrowserForbiddenBuiltin(specifier), false, specifier);
  }
  for (const specifier of ["react", "@/lib/db/core", "./local", "zod"]) {
    assert.equal(isBrowserForbiddenBuiltin(specifier), false, specifier);
  }
});

test("the polyfilled-builtin allowlist matches Next's client resolve.fallback", () => {
  const webpackConfig = fs.readFileSync(
    path.join(REPO_ROOT, "node_modules/next/dist/build/webpack-config.js"),
    "utf8"
  );
  for (const name of NEXT_CLIENT_POLYFILLED_BUILTINS) {
    assert.match(
      webpackConfig,
      new RegExp(`\\b${name}: require\\.resolve\\(`),
      `Next no longer polyfills "${name}" for the client — drop it from the allowlist`
    );
  }
});