/** * Runs helpdeco for johnmcm.com's /hlp-viewer, one help file per message, * each in a fresh instance. * * Messages in: { id, name: string (file name inside the decompiler), bytes: Uint8Array } * Messages out: { id, code, files: { name, data }[], log } or { id, error }. * * Written for johnmcm.com (2026); part of the module published at /source/helpdeco/. */ import createHelpdeco from './helpdeco.mjs'; import { decompile } from './run.mjs'; let compiled = null; function wasmModule() { // A failed fetch or compile is not kept, so the next file tries again. compiled ??= WebAssembly.compileStreaming(fetch(new URL('./helpdeco.wasm', import.meta.url))).catch((err) => { compiled = null; throw err; }); return compiled; } self.onmessage = async (e) => { const { id, name, bytes } = e.data; try { const module = await wasmModule(); // If instantiating fails, the factory never settles; this lets the failure through. let failed; const instantiateFailed = new Promise((_, reject) => (failed = reject)); const out = await Promise.race([ decompile(createHelpdeco, name, bytes, { instantiateWasm(imports, done) { WebAssembly.instantiate(module, imports).then((i) => done(i)).catch(failed); return {}; }, }), instantiateFailed, ]); self.postMessage({ id, ...out }, out.files.map((f) => f.data.buffer)); } catch (err) { self.postMessage({ id, error: String(err?.message || err) }); } };