// SPDX-License-Identifier: GPL-3.0-or-later /** * The decoder-only worker of the save import – the GPL boundary. It holds the vendored ooz-wasm (GPL-3.0-or-later, * lib/save/licenses.ts) and this small wrapper, nothing else: compressed bytes in, raw bytes out. This file is the ONLY * importer of the decoder on the site (tests/save-license.test.ts checks), it imports no other code of ours (types * only), and it is published as part of the decoder's corresponding source (`SAVE_DECODER_LICENSE.wrapperUrl`, * GPL-3.0-or-later like the decoder). * * ./client.ts hands it one end of a MessageChannel; the save reader (./worker.ts) holds the other. The decoder module * is loaded on the first `load` request, not when the worker starts. No network besides loading its own module; * nothing is stored. */ import type { OodleReply, OodleRequest } from '@/lib/save/types'; type Decompress = (payload: Uint8Array, rawLength: number) => Uint8Array; const scope = self as unknown as { onmessage: ((event: MessageEvent) => void) | null }; let decoder: Promise | undefined; const message = (error: unknown): string => (error instanceof Error ? error.message : String(error)); function serve(port: MessagePort): void { port.onmessage = async (event: MessageEvent) => { const request = event.data; try { decoder ??= import('ooz-wasm').then((m) => m.decompress); const decompress = await decoder; if (request.type === 'load') return port.postMessage({ id: request.id, ok: true } satisfies OodleReply); // `decompress` returns a copy with its own buffer (not a view of the WASM memory), so it can be transferred const raw = decompress(request.payload, request.rawLength); port.postMessage({ id: request.id, ok: true, raw } satisfies OodleReply, [raw.buffer]); } catch (error) { if (request.type === 'load') decoder = undefined; port.postMessage({ id: request.id, ok: false, detail: message(error) } satisfies OodleReply); } }; } scope.onmessage = (event) => { const port = event.ports[0]; if (port) serve(port); };