import { parentPort } from 'node:worker_threads'; import fs from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; // libraw-wasm targets browsers: emulate the worker globals its build expects. globalThis.self = globalThis; globalThis.WorkerGlobalScope = globalThis; globalThis.postMessage = (msg, transfer) => parentPort.postMessage(msg, transfer); globalThis.addEventListener = () => {}; // Emscripten loads libraw.wasm through fetch(); Node's fetch rejects file:// URLs. const realFetch = globalThis.fetch; globalThis.fetch = async (url, init) => { const href = typeof url === 'string' ? url : url instanceof URL ? url.href : String(url); if (href.startsWith('file:')) { const buf = await fs.readFile(fileURLToPath(href)); return new Response(buf, { status: 200, headers: { 'content-type': 'application/wasm' } }); } return realFetch(url, init); }; // Buffer anything that arrives before the module registers `onmessage`. const queue = []; parentPort.on('message', (data) => { if (typeof globalThis.onmessage === 'function') globalThis.onmessage({ data }); else queue.push(data); }); await import('libraw-wasm/dist/worker.js'); for (const data of queue.splice(0)) globalThis.onmessage({ data });