feat(micron-wasm): integrate Micron-Parser-Go WASM support and configuration

This commit is contained in:
Ivan
2026-05-02 09:02:39 -05:00
parent e6647415e8
commit 5097fb632d
17 changed files with 893 additions and 74 deletions
+72
View File
@@ -0,0 +1,72 @@
#!/usr/bin/env node
/**
* Downloads micron-parser-go WASM release assets and matching wasm_exec.js for Vite public/.
* Safe to run offline: exits 0 without files when MICRON_WASM_SKIP=1 or network fails.
*
* Override URLs:
* MICRON_PARSER_GO_WASM_URL
* MICRON_GO_WASM_EXEC_URL
*/
import fs from "fs";
import path from "path";
import { MICRON_PARSER_GO_RELEASE_TAG } from "./micron-parser-go-version.mjs";
import { micronWasmVendorPaths, micronWasmRepoRoot } from "./micron-wasm-resolve-bundled.mjs";
const DEFAULT_WASM_URL = `https://github.com/Quad4-Software/Micron-Parser-Go/releases/download/${MICRON_PARSER_GO_RELEASE_TAG}/micron-parser-go.wasm`;
const DEFAULT_WASM_EXEC_URL = "https://raw.githubusercontent.com/golang/go/go1.26.2/lib/wasm/wasm_exec.js";
const TIMEOUT_MS = Number(process.env.MICRON_WASM_FETCH_TIMEOUT_MS || 120000);
function rmQuiet(p) {
try {
fs.rmSync(p, { force: true });
} catch {
/* ignore */
}
}
async function fetchBinary(url, destFile) {
const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), TIMEOUT_MS);
try {
const res = await fetch(url, { signal: ctrl.signal });
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
const buf = Buffer.from(await res.arrayBuffer());
fs.mkdirSync(path.dirname(destFile), { recursive: true });
fs.writeFileSync(destFile, buf);
return buf.length;
} finally {
clearTimeout(t);
}
}
async function main() {
if (process.env.MICRON_WASM_SKIP === "1") {
console.log("fetch-micron-wasm: MICRON_WASM_SKIP=1, skipping.");
process.exit(0);
}
const root = micronWasmRepoRoot();
const { dir, wasm, wasmExec } = micronWasmVendorPaths(root);
const wasmUrl = process.env.MICRON_PARSER_GO_WASM_URL || DEFAULT_WASM_URL;
const execUrl = process.env.MICRON_GO_WASM_EXEC_URL || DEFAULT_WASM_EXEC_URL;
fs.mkdirSync(dir, { recursive: true });
try {
console.log("fetch-micron-wasm: downloading wasm_exec.js...");
await fetchBinary(execUrl, wasmExec);
console.log("fetch-micron-wasm: downloading micron-parser-go.wasm...");
const n = await fetchBinary(wasmUrl, wasm);
console.log(`fetch-micron-wasm: OK (${n} bytes WASM)`);
} catch (e) {
console.warn("fetch-micron-wasm: failed:", e?.message || e);
rmQuiet(wasm);
rmQuiet(wasmExec);
process.exit(0);
}
}
main();
+5
View File
@@ -0,0 +1,5 @@
/**
* Micron-Parser-Go release tag for default WASM URL (scripts/fetch-micron-wasm.mjs)
* and build-time tooltip copy (VITE_MICRON_PARSER_GO_RELEASE).
*/
export const MICRON_PARSER_GO_RELEASE_TAG = "v1.0.2";
+39
View File
@@ -0,0 +1,39 @@
/**
* Resolve paths and detect whether micron-parser-go WASM artifacts are present for Vite/Vitest.
*/
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/** Repository root (parent of scripts/). */
export function micronWasmRepoRoot() {
return path.join(__dirname, "..");
}
export function micronWasmVendorPaths(repoRoot = micronWasmRepoRoot()) {
const dir = path.join(repoRoot, "meshchatx", "src", "frontend", "public", "vendor", "micron-parser-go");
return {
dir,
wasm: path.join(dir, "micron-parser-go.wasm"),
wasmExec: path.join(dir, "wasm_exec.js"),
};
}
/**
* True when both wasm_exec.js and a minimally-sized WASM binary exist (build-time fetch succeeded).
*/
export function isMicronWasmBundled(repoRoot = micronWasmRepoRoot()) {
const { wasm, wasmExec } = micronWasmVendorPaths(repoRoot);
try {
if (!fs.existsSync(wasm) || !fs.existsSync(wasmExec)) {
return false;
}
const wasmStat = fs.statSync(wasm);
const execStat = fs.statSync(wasmExec);
return wasmStat.size >= 8192 && execStat.size >= 1024;
} catch {
return false;
}
}