/// /** * Opens a drawing off the page - converting a DWG, reading the DXF and * building the model and layouts - so a large file does not freeze the tab, * and Cancel can end it at once (the page ends this worker). * * The page starts it from a blob: URL that imports this module, so it runs * under the page's own Content Security Policy, which allows the converter's * WebAssembly bindings; a worker loaded from its own URL would get the * stricter policy of a script file. Its first message says where it is. */ import { DrawingError, openDrawing } from './drawing-file'; export type WorkerRequest = { type: 'open'; bytes: Uint8Array; name: string }; export type WorkerReply = | { type: 'where'; url: string } | { type: 'progress'; stage: string } | { type: 'done'; opened: Awaited> } | { type: 'error'; message: string; drawing: boolean; converter: boolean }; const post = (m: WorkerReply, transfer: Transferable[] = []) => (self as unknown as DedicatedWorkerGlobalScope).postMessage(m, transfer); post({ type: 'where', url: import.meta.url }); self.onmessage = async (e: MessageEvent) => { if (e.data?.type !== 'open') return; try { const opened = await openDrawing(e.data.bytes, e.data.name, (stage) => post({ type: 'progress', stage })); post({ type: 'done', opened }, opened.dxf ? [opened.dxf.buffer as ArrayBuffer] : []); } catch (err) { post({ type: 'error', message: (err as Error)?.message ?? String(err), drawing: err instanceof DrawingError, converter: err instanceof DrawingError && err.converter }); } };