Files
wehub-resource-sync 9f97f3abbe
CI - Python Bindings / sdist (push) Failing after 1s
CI - Python Bindings / Build x86_64-unknown-linux-musl (push) Failing after 1s
CI / fmt (push) Failing after 1s
E2E Output Validation / compare-outputs (push) Failing after 1s
Sync Docs to Developer Hub / sync-docs (push) Failing after 1s
CI - Python Bindings / Build x86_64-unknown-linux-gnu (push) Failing after 1s
CI - WASM Bindings / Build WASM (push) Failing after 0s
CI / clippy (push) Failing after 1s
CI / build-and-test (ubuntu-latest) (push) Failing after 0s
CI - WASM Bindings / Edge runtime PDF parse test (push) Has been skipped
CI - WASM Bindings / Browser PDF parse test (push) Has been skipped
Deploy Demo to GitHub Pages / deploy (push) Failing after 1s
CI / build-docker-image (push) Failing after 3s
CI - Node Bindings / Build darwin-x64 (push) Has been cancelled
CI - Node Bindings / Test win32-x64-msvc (push) Has been cancelled
CI - Node Bindings / Build linux-arm64-gnu (push) Has been cancelled
CI - Node Bindings / Build linux-x64-gnu (push) Has been cancelled
CI - Node Bindings / Build linux-x64-musl (push) Has been cancelled
CI - Node Bindings / Build win32-arm64-msvc (push) Has been cancelled
CI - Node Bindings / Build win32-x64-msvc (push) Has been cancelled
CI - Node Bindings / Test darwin-arm64 (push) Has been cancelled
CI - Node Bindings / Test darwin-x64 (push) Has been cancelled
CI - Node Bindings / Test linux-x64-gnu (push) Has been cancelled
CI - Node Bindings / Test linux-x64-musl (push) Has been cancelled
CI - Node Bindings / Test win32-arm64-msvc (push) Has been cancelled
CI - Python Bindings / Build aarch64-pc-windows-msvc (push) Has been cancelled
CI - Python Bindings / Build x86_64-pc-windows-msvc (push) Has been cancelled
CI - Python Bindings / Build x86_64-apple-darwin (push) Has been cancelled
CI - Python Bindings / Build aarch64-apple-darwin (push) Has been cancelled
CI - Python Bindings / Build aarch64-unknown-linux-gnu (push) Has been cancelled
CI - Node Bindings / Build darwin-arm64 (push) Has been cancelled
CI - Python Bindings / Test x86_64-apple-darwin (push) Has been cancelled
CI - Python Bindings / Test aarch64-apple-darwin (push) Has been cancelled
CI - Python Bindings / Test x86_64-unknown-linux-gnu (push) Has been cancelled
CI - Python Bindings / Test x86_64-unknown-linux-musl (push) Has been cancelled
CI - Python Bindings / Test aarch64-pc-windows-msvc (push) Has been cancelled
CI - Python Bindings / Test x86_64-pc-windows-msvc (push) Has been cancelled
CI / build-and-test (macos-26-intel) (push) Has been cancelled
CI / build-and-test (macos-latest) (push) Has been cancelled
CI / build-and-test (windows-11-arm) (push) Has been cancelled
CI / build-and-test (windows-latest) (push) Has been cancelled
E2E Output Validation / upload-dataset (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:23:44 +08:00

127 lines
3.8 KiB
JavaScript

/**
* Edge runtime compatibility test for LiteParse WASM module.
*
* Spins up a Miniflare (Cloudflare Workers) instance that loads the WASM
* module and parses a PDF, verifying it works in an edge runtime environment.
*
* Usage: node scripts/edge-compat/wasm-test.mjs
*
* Requires: miniflare (npm i -D miniflare)
* Expects: packages/wasm/pkg/ to contain the built WASM files
* demo/docs/apple-10k-2024.pdf to exist
*/
import { Miniflare } from "miniflare";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
const ROOT = resolve(fileURLToPath(import.meta.url), "../../..");
const wasmPath = resolve(ROOT, "packages/wasm/pkg/liteparse_wasm_bg.wasm");
const gluePath = resolve(ROOT, "packages/wasm/pkg/liteparse_wasm.js");
const pdfPath = resolve(ROOT, "demo/docs/apple-10k-2024.pdf");
// Build a self-contained worker script.
// The generated glue exports an async `init()` that fetches the .wasm via URL.
// In edge runtimes, we import the WASM as a module and use `initSync` instead.
let glueSource = readFileSync(gluePath, "utf-8");
// Strip default export (the async init / fetch-based loader)
glueSource = glueSource.replace(/export\s*\{[^}]*__wbg_init\s+as\s+default[^}]*\};?/g, "");
glueSource = glueSource.replace(/export\s+default\s+__wbg_init\s*;?/g, "");
glueSource = glueSource.replace(/export\s*\{\s*initSync\s*(?:,\s*__wbg_init\s+as\s+default\s*)?\}\s*;?/g, "");
const workerScript = `
// Import WASM as a module (standard edge runtime pattern)
import __liteparse_wasm_mod from "liteparse.wasm";
// --- LiteParse WASM glue (patched) ---
${glueSource}
// --- Edge worker handler ---
export default {
async fetch(request) {
try {
initSync({ module: __liteparse_wasm_mod });
const parser = new LiteParse({ ocrEnabled: false, outputFormat: "text", quiet: true });
const pdfBytes = new Uint8Array(await request.arrayBuffer());
const result = await parser.parse(pdfBytes);
const pageCount = result.pages.length;
const textLength = result.text.length;
return new Response(JSON.stringify({
ok: true,
pages: pageCount,
textLength: textLength,
}), {
headers: { "Content-Type": "application/json" },
});
} catch (err) {
return new Response(JSON.stringify({
ok: false,
error: err.message || String(err),
stack: err.stack,
}), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
}
};
`;
async function main() {
console.log("Starting Miniflare (Cloudflare Workers runtime)...");
const wasmBytes = readFileSync(wasmPath);
const mf = new Miniflare({
modules: [
{ type: "ESModule", path: "worker.mjs", contents: workerScript },
{ type: "CompiledWasm", path: "liteparse.wasm", contents: wasmBytes },
],
compatibilityDate: "2024-01-01",
});
try {
const pdfBytes = readFileSync(pdfPath);
console.log(`Sending ${(pdfBytes.length / 1024 / 1024).toFixed(1)}MB PDF to edge worker...`);
const response = await mf.dispatchFetch("http://localhost/parse", {
method: "POST",
body: pdfBytes,
});
const result = await response.json();
if (!result.ok) {
console.error(`FAIL: ${result.error}`);
if (result.stack) console.error(result.stack);
process.exit(1);
}
if (result.pages === 0) {
console.error("FAIL: No pages parsed");
process.exit(1);
}
if (result.textLength < 100) {
console.error(`FAIL: Text too short: ${result.textLength} chars`);
process.exit(1);
}
console.log(`PASS: ${result.pages} pages, ${result.textLength} chars`);
} finally {
await mf.dispose();
}
}
main().catch((err) => {
console.error("Test runner failed:", err);
process.exit(1);
});