Files
scriptc/packages/compiler/test/node-pin.test.ts
T
Chris Tate 9abf35f6a2 Build the large compiler tables on first use
The native compiler ran four large module initializers at every startup:
the builtin call signatures (LIB_FN_SIGS), the Node 24 fetch
compatibility profile, and the builtin module function and ambient
surface tables. Together their init functions are about 1.3 MB of code,
and --version and warm cache-hit builds paged them in and built the
tables without reading them.

Each table is now built by a function on its first call and kept in a
module variable:

- libFnSignatures() replaces LIB_FN_SIGS
- node24FetchCompatProfile() and staticFetchMembers() replace
  NODE24_FETCH_COMPAT_PROFILE and the STATIC_* member sets
- builtinModuleFns() and ambientSurfaceFns() replace BUILTIN_MODULE_FNS
  and AMBIENT_SURFACE_FNS

The @scriptc/compiler entry point keeps LIB_FN_SIGS and
NODE24_FETCH_COMPAT_PROFILE as deprecated constants built from the new
functions when the entry module loads. The native compiler does not load
that module.

The surface manifest's coverage notes read the fetch target through the
profile function, so building the manifest module no longer builds the
profile. The table contents are unchanged.
2026-10-25 05:23:35 -05:00

60 lines
2.5 KiB
TypeScript

/* Every Node pin names the same release: .node-version (the
* differential oracle), Dockerfile.sandbox, the compatibility pin, the fetch
* profile, the docs matrix fallback, and the version the compiled binary
* reports. */
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { expect, test } from "vitest";
import { node24FetchCompatProfile } from "../src/compat/fetch-profile.js";
import { NODE_COMPAT_VERSION } from "../src/frontend/lowering/builtins/process.js";
import { CRYPTO_CONSTANTS } from "../src/frontend/lowering/crypto-tables.js";
import { ZLIB_CONSTANTS } from "../src/frontend/lowering/zlib-constants.js";
const repoRoot = join(import.meta.dirname, "../../..");
const read = (path: string): string => readFileSync(join(repoRoot, path), "utf8");
test("the Node pins agree", () => {
const pinned = read(".node-version").trim();
const compat = JSON.parse(read("internal/compatibility/node-v24.json")) as {
version: string;
tag: string;
};
expect(compat.version).toBe(pinned);
expect(compat.tag).toBe(`v${pinned}`);
expect(read("Dockerfile.sandbox")).toMatch(new RegExp(`^ARG NODE_VERSION=${pinned}$`, "m"));
expect(NODE_COMPAT_VERSION).toBe(pinned);
expect(node24FetchCompatProfile().target.node).toBe(pinned);
expect(read("packages/runtime/src/scr_lib.c")).toContain(
`#define SCR_NODE_COMPAT_VERSION "${pinned}"`,
);
expect(read("docs/src/components/node-compatibility-matrix.tsx")).toContain(
`snapshot?.nodeVersion ?? "${pinned}"`,
);
});
test("the crypto.constants declaration matches the baked table", () => {
const dts = read("packages/compiler/ambient/scriptc-node-fallback.d.ts");
const start = dts.indexOf("export interface CryptoConstants {");
const body = dts.slice(start, dts.indexOf("\n }\n", start));
const declared = Object.fromEntries(
[...body.matchAll(/readonly (\w+): (-?\d+|"[^"]*");/g)].map(([, name, value]) => [
name!,
JSON.parse(value!) as number | string,
]),
);
expect(declared).toEqual(CRYPTO_CONSTANTS);
});
test("the zlib.constants declaration matches the baked table", () => {
const dts = read("packages/compiler/ambient/scriptc-node-fallback.d.ts");
const start = dts.indexOf("export interface ZlibConstants {");
const body = dts.slice(start, dts.indexOf("\n }\n", start));
const declared = Object.fromEntries(
[...body.matchAll(/readonly (\w+): (-?\d+|number);/g)].map(([, name, value]) => [
name!,
value === "number" ? Infinity : Number(value),
]),
);
expect(declared).toEqual(ZLIB_CONSTANTS);
});