node-wasm.ts

1.1 KB · raw

1/**2 * Node-only WASM byte loader for Vitest.3 *4 * Browser extension builds must not see direct Node built-in imports here;5 * Vite/Rolldown rewrites those to browser external shims that fail in6 * extension pages. The split specifier plus @vite-ignore keeps this fallback7 * out of the browser module graph while still letting Node tests read from8 * node_modules.9 */1011const WASM_NODE_MODULE_PATH =12  "node_modules/@subspace/reed-solomon-erasure.wasm/dist/reed_solomon_erasure_bg.wasm";1314type NodeFs = {15  readFileSync(path: string): Uint8Array;16};1718type NodePath = {19  resolve(...parts: string[]): string;20};2122async function importNodeModule<T>(name: string): Promise<T> {23  return import(/* @vite-ignore */ (`node:${name}`)) as Promise<T>;24}2526export async function readNodeReedSolomonWasmBytes(): Promise<Uint8Array<ArrayBuffer>> {27  const [fs, path] = await Promise.all([28    importNodeModule<NodeFs>("fs"),29    importNodeModule<NodePath>("path"),30  ]);31  const fileBytes = fs.readFileSync(path.resolve(WASM_NODE_MODULE_PATH));32  const bytes: Uint8Array<ArrayBuffer> = new Uint8Array(fileBytes.byteLength);33  bytes.set(fileBytes);34  return bytes;35}