/** * 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 { 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 { const source = nodeCoreSource('pkg-proofs') const glue = (await source.importGlue(0)) as ProofModule await glue.default({ module_or_path: await source.compile() }) return glue }