Files
ipx/web/concat-map.mts
rays f8408a3d26 Tests, tooling and the served scripts are TypeScript (#392)
The page and the service worker were strict TypeScript; the tests, the
build, the home page's generator, the load tests, boot.js and the
directory's site.js were plain JavaScript that nothing checked. All of
it is TypeScript now, in five programs, each with its own globals, all
checked by npm run typecheck: the page (tsconfig.json, with boot.ts and
the directory's site.ts), the worker, Node's (tsconfig.tools.json: the
Node tests, the browser suite, the build, the site generator, the load
harness), and k6's (tsconfig.k6.json, with @types/k6).

Node 24 runs the .ts files by stripping their types, so they hold only
TypeScript that erases (erasableSyntaxOnly) and import each other by
.ts name; package.json says "type": "module" for it, and Dockerfile.web
moves to Node 24 to run build.mts. k6 2.3 strips its scripts' types
itself.

The browser suite's page.evaluate calls reach the page's globals; the
page's tsc run now also writes its declarations to target/page-types,
which the suite's program includes, so those calls are typed by the
page's own code rather than by hand-written copies.

boot.ts is compiled by build.mts with swc, as the worker is, and named
by the hash of what is served. The directory's build has no Node, so
site.js is compiled from site.ts and committed (node web/build.mts
--directory-script), and tests/directory-script.ts fails if they drift.

Checked: the site generator's 139 pages are byte for byte what the
JavaScript made; the page build's app.js is identical and only boot.js
(now minified) and its hash differ; contrast, page-smoke, page-unit,
native-bridge, the browser suite (96 passed, 2 skipped) and the
directory's and ipx's Rust tests pass; k6 parses each load script. The
load tests themselves did not run: they build a release binary outside
the capped builder. .agents/skills/security-audit is an installed
skill's, not ours, and is left as it is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 02:23:22 +00:00

50 lines
1.6 KiB
TypeScript

// A source map for the page's script as web/build.mts makes it, files joined one after another
// (#390): each line of the joined script maps to its line in the file it came from. Given to swc
// as the input map, the built script then maps to web/src/*.ts file by file, which is what the
// coverage report needs. Only a coverage build uses it.
export interface SourceMap {
version: 3;
sources: string[];
sourcesContent: string[];
names: string[];
mappings: string;
}
const B64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
/// One number in a source map's base64 VLQ: sign in the lowest bit, five bits a digit.
export function vlq(n: number): string {
let v = n < 0 ? (-n << 1) | 1 : n << 1;
let out = '';
do {
let digit = v & 31;
v >>>= 5;
if (v) digit |= 32;
out += B64[digit];
} while (v);
return out;
}
/// The map of `files` joined with '\n' between them, as build.mts joins them.
export function concatMap(files: { name: string; text: string }[]): SourceMap {
const lines: string[] = [];
// Each segment's fields are relative to the one before: source, line in it, column.
let lastSource = 0, lastLine = 0;
files.forEach((f, source) => {
const count = f.text.split('\n').length;
for (let line = 0; line < count; line++) {
lines.push(vlq(0) + vlq(source - lastSource) + vlq(line - lastLine) + vlq(0));
lastSource = source;
lastLine = line;
}
});
return {
version: 3,
sources: files.map((f) => f.name),
sourcesContent: files.map((f) => f.text),
names: [],
mappings: lines.join(';'),
};
}