Files
faunosteo/packages/core-wasm/src/node.ts
T
bchanot 50c8fe0b33 feat(domain): R4.5e wasm proof export, resources per page, N-1 and TS-engine read-back, cost comparison
export_pdf_for_test on the proofs build (ExportInput JSON + base64 rasters): the 14 O2
runs give the native digests through wasm; the frozen O11 scanner reads every R4 PDF and
the N-1 Zod readers accept its envelope; a stripped R4 PDF answers attachment_stripped;
the TypeScript engine reads the R4 PDFs back to the R3 outcome; E2 size ≤ R3 + 5 %. Page
resources list the images the page draws (listing every embed cost 9 MB on CAP). Node,
stub rasters, TS → Rust: demo 198 → 36 ms, L 772 → 57 ms, CAP 5.7 s → 0.22 s; files
236 KB, 386 KB, 1.6 MB against 243 KB, 463 KB, 2.2 MB. Module with the writer linked
(proofs build): 531.6 KB gzip; shipped unchanged at 426 KB until R4.6 links it.
2026-10-18 23:21:05 +02:00

70 lines
2.7 KiB
TypeScript

/**
* Node source of the core module (vitest suites, scripts): the glue and the `.wasm` read
* from `pkg/` (the shipped build) or `pkg-proofs/` (with the R0 trap exports). Never
* imported by the application bundle.
*/
import { readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { CoreRuntime, type CoreSource, type GlueModule } from './runtime'
export type CoreBuild = 'pkg' | 'pkg-proofs'
// Paths from this file's own location: Vite (happy-dom suites) rewrites the
// `new URL(relative, import.meta.url)` form into a dev-server URL, which Node cannot read.
const PACKAGE_DIR = join(dirname(fileURLToPath(import.meta.url)), '..')
const gluePath = (build: CoreBuild) =>
join(PACKAGE_DIR, build, 'faunosteo_wasm.js')
/** Path of a built module on disk (size measurements). */
export function coreWasmPath(build: CoreBuild = 'pkg'): string {
return join(PACKAGE_DIR, build, 'faunosteo_wasm_bg.wasm')
}
/** Each import gets its own query string, hence its own module record and instance. */
let imports = 0
export function nodeCoreSource(build: CoreBuild = 'pkg'): CoreSource {
return {
compile: async () =>
WebAssembly.compile(await readFile(coreWasmPath(build))),
importGlue: async () => {
imports += 1
const url = `${pathToFileURL(gluePath(build)).href}?instance=${imports}`
return (await import(/* @vite-ignore */ url)) as GlueModule
},
}
}
/** A runtime over the Node source, loaded. */
export async function nodeCoreRuntime(
build: CoreBuild = 'pkg',
): Promise<CoreRuntime> {
const runtime = new CoreRuntime(nodeCoreSource(build))
if (!(await runtime.ready())) {
throw new Error(`core module ${build} failed to load`)
}
return runtime
}
/** The functions of the proofs build (R0 trap proofs). */
export interface ProofModule extends GlueModule {
panic_for_test(): void
recurse_for_test(depth: number): number
/** R4.2e: the PDF import with the OD-3 value cap off (the linear ceiling proof). */
import_pdf_unbounded_for_test(bundle: string, bytes: Uint8Array): string
/** R4.2e: allocates `mib` MiB, the linear ceiling proof. */
allocate_for_test(mib: number): number
/** R4.5: one export from an `ExportInput` JSON and base64 rasters (wasm = native proof). */
export_pdf_for_test(input: string, rasters: string): Uint8Array
}
/** A fresh, initialised glue of the proofs build, outside any runtime (R0 proofs). */
export async function loadProofModule(): Promise<ProofModule> {
const source = nodeCoreSource('pkg-proofs')
const glue = (await source.importGlue(0)) as ProofModule
await glue.default({ module_or_path: await source.compile() })
return glue
}