Files
luminary/server/libraw-worker.mjs
T
bhursey cd52a2f82f Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw).
Local database, uploaded photos, build output and dependencies are gitignored.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-23 15:19:25 +00:00

32 lines
1.2 KiB
JavaScript

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 });