chore(workspace): Rust workspace R0 — builder image, core crate (JS-compatible JSON, report contracts, facade), wasm facade, core-wasm package with proofs
This commit is contained in:
1 parent
5fec0654d3
commit
2a38fa37b5
39 files changed
+1913
-1
No files matched your search
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Loader of the Faunosteo core module (wasm-bindgen `--target web` output under `pkg/`,
|
||||
* built by `pnpm wasm:build`, git-ignored).
|
||||
*
|
||||
* The module is never imported statically: `pkg/` may be absent on a host without Docker,
|
||||
* and the application loads the core lazily behind the paths that need it (review III.3).
|
||||
* No file of this package is reachable from the application before R2.
|
||||
*/
|
||||
import { existsSync } from 'node:fs'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
/** The functions the module exports (mirror of `pkg/faunosteo_wasm.d.ts`). */
|
||||
export interface CoreModule {
|
||||
core_version(): string
|
||||
call(op: string, input: string): string
|
||||
panic_for_test(): void
|
||||
recurse_for_test(depth: number): number
|
||||
}
|
||||
|
||||
/** Outcome envelope of every `call` (closed kinds, never an exception for content). */
|
||||
export type CoreOutcome<T = unknown> =
|
||||
| { ok: true; value: T }
|
||||
| { ok: false; kind: string; detail?: string }
|
||||
|
||||
const PKG_JS = new URL('../pkg/faunosteo_wasm.js', import.meta.url)
|
||||
const PKG_WASM = new URL('../pkg/faunosteo_wasm_bg.wasm', import.meta.url)
|
||||
|
||||
interface PkgModule extends CoreModule {
|
||||
default(input: { module_or_path: BufferSource }): Promise<unknown>
|
||||
}
|
||||
|
||||
/** True when `pnpm wasm:build` has produced the module. */
|
||||
export function isCoreBuilt(): boolean {
|
||||
return existsSync(fileURLToPath(PKG_WASM))
|
||||
}
|
||||
|
||||
let instances = 0
|
||||
|
||||
/**
|
||||
* Instantiates a fresh module (Node: bytes from disk). Each call is a new instance.
|
||||
*
|
||||
* The wasm-bindgen glue keeps its instance in a module-level variable and the ESM cache
|
||||
* returns the same module for the same URL, so a second plain `import()` would hand back
|
||||
* the previous (possibly trapped) instance: R0 proved that an instance is unusable after a
|
||||
* trap (`memory access out of bounds` in the allocator on the next call). A cache-busting
|
||||
* query gives a new module record, hence a new instance. The browser loader of R2 must do
|
||||
* the same (static glue URL imported with a query, or a Worker recreated on trap).
|
||||
*/
|
||||
export async function loadCore(): Promise<CoreModule> {
|
||||
instances += 1
|
||||
const pkg = (await import(
|
||||
`${PKG_JS.href}?instance=${instances}`
|
||||
)) as PkgModule
|
||||
const bytes = await readFile(PKG_WASM)
|
||||
await pkg.default({ module_or_path: bytes })
|
||||
return pkg
|
||||
}
|
||||
|
||||
/** Runs one operation and parses its envelope. */
|
||||
export function callCore<T = unknown>(
|
||||
core: CoreModule,
|
||||
op: string,
|
||||
input: string,
|
||||
): CoreOutcome<T> {
|
||||
return JSON.parse(core.call(op, input)) as CoreOutcome<T>
|
||||
}
|
||||
|
||||
/** Path of the built module on disk (size measurements). */
|
||||
export function coreWasmPath(): string {
|
||||
return fileURLToPath(PKG_WASM)
|
||||
}
|
||||
Reference in new issue
Block a user