import { createLinuxCncInterpreterRuntime } from "./linuxcnc-interpreter-runtime.js"; installTextDecoderResizableArrayBufferCompat(); let runtime = null; self.addEventListener("message", async (event) => { const { id, type, payload = {} } = event.data || {}; try { if (type === "init") { runtime = await createLinuxCncInterpreterRuntime({ moduleOptions: payload.moduleOptions, wasmRoot: payload.wasmRoot, sdkModuleUrl: payload.sdkModuleUrl, }); postSuccess(id, runtime.readiness()); return; } if (!runtime?.loaded) { throw new Error("LinuxCNC interpreter worker runtime is not initialized"); } if (type === "readiness") { postSuccess(id, runtime.readiness()); return; } if (type === "runProgram") { postSuccess(id, runtime.runProgram(payload.programText || "")); return; } if (type === "runMachineFileProgram") { postSuccess(id, runtime.runMachineFileProgram(payload)); return; } throw new Error(`unknown LinuxCNC interpreter worker request: ${type}`); } catch (error) { self.postMessage({ id, ok: false, error: error instanceof Error ? error.message : String(error), }); } }); function postSuccess(id, value) { self.postMessage({ id, ok: true, value }); } function installTextDecoderResizableArrayBufferCompat() { const decoderPrototype = globalThis.TextDecoder?.prototype; if (!decoderPrototype || decoderPrototype.__webRtcpResizableArrayBufferCompat) return; const nativeDecode = decoderPrototype.decode; Object.defineProperty(decoderPrototype, "__webRtcpResizableArrayBufferCompat", { value: true, configurable: false, }); decoderPrototype.decode = function decodeResizableArrayBufferCompat(input, options) { try { return nativeDecode.call(this, input, options); } catch (error) { const message = error instanceof Error ? error.message : String(error); if (!message.includes("resizable")) throw error; if (input?.buffer) { return nativeDecode.call(this, Uint8Array.from(input), options); } if (input instanceof ArrayBuffer) { return nativeDecode.call(this, Uint8Array.from(new Uint8Array(input)), options); } throw error; } }; }