r4-import.mjs runs the 226 baseline files and the hostile classes through a disposable module worker calling SheetHandle.import_pdf in Chromium 156, Firefox 157 and WebKit 27.2 under the RSS watchdog (not a guaranteed limit); compare-imports.mjs prints TS vs Rust; serve.mjs gains opt-in mounts (/r4/, /pkg/). Results 2026-10-18-r4-import.json: 666/678 agreements, the 12 others are the 4 declared files; W4 met (max 68 MiB linear); the 1 GiB ceiling traps in every engine.
154 lines
4.9 KiB
JavaScript
154 lines
4.9 KiB
JavaScript
import {
|
|
copyFileSync,
|
|
existsSync,
|
|
mkdirSync,
|
|
readFileSync,
|
|
rmdirSync,
|
|
unlinkSync,
|
|
} from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { pathToFileURL } from 'node:url'
|
|
import { wasmStamp } from '../../wasm-stamp.mjs'
|
|
import { sha256 } from './inputs.mjs'
|
|
import { REPO_ROOT } from './paths.mjs'
|
|
|
|
/**
|
|
* The core module of packages/core-wasm as the R4 import page loads it:
|
|
* the sheet bundle the application embeds, a snapshot of both builds for
|
|
* the run (the module can be rebuilt while a run lasts), the module's facts
|
|
* for the results file, and a Node instance of the shipped build (to build
|
|
* the hostile envelopes exactly as pdf-import.budget.test.ts does).
|
|
*/
|
|
|
|
export const PKG_DIR = join(REPO_ROOT, 'packages/core-wasm/pkg')
|
|
export const PROOFS_DIR = join(REPO_ROOT, 'packages/core-wasm/pkg-proofs')
|
|
const SEED = join(REPO_ROOT, 'supabase/seed/anatomical-sheets/equine/draft')
|
|
const WASM = 'faunosteo_wasm_bg.wasm'
|
|
const GLUE = 'faunosteo_wasm.js'
|
|
const STAMP = 'BUILD_STAMP'
|
|
const SNAPSHOT_FILES = [GLUE, WASM, STAMP]
|
|
const PAGE_BYTES = 64 * 1024
|
|
|
|
const readText = (path) => readFileSync(path, 'utf8')
|
|
const readJson = (path) => JSON.parse(readText(path))
|
|
|
|
/** The bundled draft as apps/app/vite/sheet-bundle.ts embeds it. */
|
|
export function draftBundle() {
|
|
const assets = {}
|
|
const assetHashes = {}
|
|
for (const asset of readJson(join(SEED, 'assets.json'))) {
|
|
assets[asset.svg_asset_id] = readText(join(SEED, 'assets', asset.file))
|
|
assetHashes[asset.svg_asset_id] = asset.sha256
|
|
}
|
|
return JSON.stringify({
|
|
status: 'draft',
|
|
ref: readJson(join(SEED, 'sheet-ref.json')),
|
|
document: readJson(join(SEED, 'document.json')),
|
|
migrationMap: readJson(join(SEED, 'report-migration-map.json')),
|
|
assets,
|
|
assetHashes,
|
|
})
|
|
}
|
|
|
|
/** An unsigned LEB128 at `at`: `[value, next offset]`. */
|
|
function leb128(bytes, at) {
|
|
let value = 0
|
|
let shift = 0
|
|
for (;;) {
|
|
const byte = bytes[at]
|
|
at += 1
|
|
value += (byte & 0x7f) * 2 ** shift
|
|
if ((byte & 0x80) === 0) return [value, at]
|
|
shift += 7
|
|
}
|
|
}
|
|
|
|
/** The limits of the module's own memory (section 5), in 64 KiB pages. */
|
|
export function memoryLimits(bytes) {
|
|
let at = 8
|
|
while (at < bytes.length) {
|
|
const id = bytes[at]
|
|
const [size, body] = leb128(bytes, at + 1)
|
|
if (id === 5) {
|
|
const [, flagsAt] = leb128(bytes, body)
|
|
const flags = bytes[flagsAt]
|
|
const [initial, maxAt] = leb128(bytes, flagsAt + 1)
|
|
const maximum = flags & 1 ? leb128(bytes, maxAt)[0] : null
|
|
return { initialPages: initial, maximumPages: maximum }
|
|
}
|
|
at = body + size
|
|
}
|
|
return null
|
|
}
|
|
|
|
/** A copy of one build's served files under `dir`; null when not built. */
|
|
function copyBuild(source, dir) {
|
|
if (!SNAPSHOT_FILES.every((f) => existsSync(join(source, f)))) return null
|
|
mkdirSync(dir)
|
|
for (const file of SNAPSHOT_FILES) {
|
|
copyFileSync(join(source, file), join(dir, file))
|
|
}
|
|
return dir
|
|
}
|
|
|
|
/**
|
|
* The served copies of pkg/ and pkg-proofs/ under `parent`; `remove()`
|
|
* deletes the copied files one by one, then the two directories.
|
|
*/
|
|
export function snapshotBuilds(parent) {
|
|
const pkg = copyBuild(PKG_DIR, join(parent, 'pkg'))
|
|
const proofs = copyBuild(PROOFS_DIR, join(parent, 'pkg-proofs'))
|
|
const remove = () => {
|
|
for (const dir of [pkg, proofs].filter(Boolean)) {
|
|
for (const file of SNAPSHOT_FILES) unlinkSync(join(dir, file))
|
|
rmdirSync(dir)
|
|
}
|
|
}
|
|
return { pkg, proofs, remove }
|
|
}
|
|
|
|
function buildFacts(source, dir) {
|
|
if (!dir) return null
|
|
const wasm = readFileSync(join(dir, WASM))
|
|
const limits = memoryLimits(wasm)
|
|
const stampFile = join(dir, STAMP)
|
|
return {
|
|
dir: source.slice(REPO_ROOT.length + 1),
|
|
servedFrom: 'a copy made at the start of the run',
|
|
wasmBytes: wasm.length,
|
|
wasmSha256: sha256(wasm),
|
|
glueSha256: sha256(readFileSync(join(dir, GLUE))),
|
|
buildStamp: existsSync(stampFile) ? readText(stampFile).trim() : null,
|
|
memory: limits && {
|
|
...limits,
|
|
maximumBytes:
|
|
limits.maximumPages === null ? null : limits.maximumPages * PAGE_BYTES,
|
|
},
|
|
}
|
|
}
|
|
|
|
/** What the results file states about the modules the pages loaded. */
|
|
export function moduleFacts(snapshot) {
|
|
const stamp = wasmStamp()
|
|
const pkg = buildFacts(PKG_DIR, snapshot.pkg)
|
|
const proofs = buildFacts(PROOFS_DIR, snapshot.proofs)
|
|
return {
|
|
pkg,
|
|
proofs,
|
|
sourcesStamp: stamp,
|
|
pkgStampCurrent: pkg?.buildStamp === stamp,
|
|
proofsStampCurrent: proofs?.buildStamp === stamp,
|
|
note:
|
|
'sourcesStamp is scripts/wasm-stamp.mjs on the working tree; a stamp ' +
|
|
'that differs means the sources changed after the module was built',
|
|
}
|
|
}
|
|
|
|
/** A Node instance of the shipped build (`dir`), the draft sheet loaded. */
|
|
export async function nodeSheet(bundle, dir) {
|
|
const glue = await import(pathToFileURL(join(dir, GLUE)).href)
|
|
const module = await WebAssembly.compile(readFileSync(join(dir, WASM)))
|
|
await glue.default({ module_or_path: module })
|
|
return new glue.SheetHandle(bundle)
|
|
}
|