Files
bhursey 0cb315ea88
CI / build (push) Has been cancelled
feat(editor): bridge the advanced photo editor into the event workflow
Event photos can now be opened in Lumenroom, a WebGL2 develop module, from the
photo editor's Advanced button. The dashboard signs a short-lived token scoped
to one user and event; Lumenroom exchanges it, imports the event's photo list
and proxies bytes on demand, so nothing is copied and the temporary catalog
expires with the bridge session. Saving in Lumenroom renders the photo and
writes it back as the event's edited JPEG, with the advanced recipe kept in
event_photos.advanced_edit_json.

Lumenroom is vendored under lumenroom/ and built as a second compose service,
so one clone and `docker compose up -d --build` deploys both apps.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-21 01:34:20 +00:00

121 lines
3.0 KiB
JavaScript

import { RAW_IDLE_MS, RAW_WORKERS } from './config.mjs';
import { installLibrawRuntime } from './libraw-runtime.mjs';
let ctorPromise = null;
/** Load the decoder class once; installs the Node worker shim on first use. */
export function loadLibRaw() {
if (!ctorPromise) {
ctorPromise = (async () => {
await installLibrawRuntime();
const mod = await import('libraw-wasm');
const ctor = mod.default?.default ?? mod.default;
if (typeof ctor !== 'function') throw new Error('libraw-wasm did not export a constructor');
return ctor;
})();
}
return ctorPromise;
}
/**
* Bounded pool of decoder instances. Each instance owns one worker thread, so
* this caps how much demosaicing runs at the same time; extra jobs queue.
* Idle instances are disposed after RAW_IDLE_MS to give the memory back.
*/
class LibRawPool {
constructor(size = RAW_WORKERS) {
this.size = Math.max(1, Math.floor(size));
this.free = [];
this.total = 0;
this.waiters = [];
this.closed = false;
}
async acquire() {
const entry = this.free.pop();
if (entry) {
if (entry.timer) clearTimeout(entry.timer);
return entry.lib;
}
if (this.total < this.size) {
this.total++;
try {
const Ctor = await loadLibRaw();
return new Ctor();
} catch (err) {
this.total--;
throw err;
}
}
return new Promise((resolve, reject) => this.waiters.push({ resolve, reject }));
}
release(lib, healthy = true) {
if (this.closed || !healthy) {
this.discard(lib);
return;
}
const waiter = this.waiters.shift();
if (waiter) {
waiter.resolve(lib);
return;
}
const entry = { lib, timer: null };
entry.timer = setTimeout(() => {
const idx = this.free.indexOf(entry);
if (idx >= 0) {
this.free.splice(idx, 1);
this.discard(lib);
}
}, RAW_IDLE_MS);
entry.timer.unref?.();
this.free.push(entry);
}
discard(lib) {
try {
lib.dispose();
} catch {
/* worker already gone */
}
this.total = Math.max(0, this.total - 1);
const waiter = this.waiters.shift();
if (waiter) {
this.total++;
loadLibRaw()
.then((Ctor) => new Ctor())
.then(waiter.resolve, waiter.reject);
}
}
/** Run `fn` with a decoder instance; the instance returns to the pool after. */
async use(fn) {
const lib = await this.acquire();
let healthy = true;
try {
return await fn(lib);
} catch (err) {
// A dead worker keeps failing, so recycle it instead of handing it back.
if (/worker|terminated|disposed|memory/i.test(String(err?.message ?? ''))) healthy = false;
throw err;
} finally {
this.release(lib, healthy);
}
}
get stats() {
return {
size: this.size,
busy: this.total - this.free.length,
idle: this.free.length,
queued: this.waiters.length,
};
}
}
export const rawPool = new LibRawPool();
export function poolSupport() {
return { size: rawPool.size, workers: RAW_WORKERS };
}