Download worker.js from HelloSun/WFreeCAD: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/HelloSun/WFreeCAD/resolve/main/worker.js
- Command line
-
hf download hf://spaces/HelloSun/WFreeCAD/worker.js
-
curl -L -o worker.js https://huggingface.co/spaces/HelloSun/WFreeCAD/resolve/main/worker.js
1.49 kB
| const PYODIDE_URL = 'https://cdn.jsdelivr.net/pyodide/v0.26.4/full/'; | |
| let pyodide = null; | |
| let shimSource = ''; | |
| let initPromise = null; | |
| function post(message) { | |
| self.postMessage(message); | |
| } | |
| async function init() { | |
| if (pyodide) return pyodide; | |
| if (initPromise) return initPromise; | |
| initPromise = (async () => { | |
| if (!shimSource) throw new Error('No Python shim was supplied.'); | |
| importScripts(`${PYODIDE_URL}pyodide.js`); | |
| if (typeof loadPyodide !== 'function') { | |
| throw new Error('Pyodide did not initialise.'); | |
| } | |
| pyodide = await loadPyodide({ indexURL: PYODIDE_URL }); | |
| pyodide.runPython(shimSource); | |
| post({ type: 'ready' }); | |
| return pyodide; | |
| })(); | |
| return initPromise; | |
| } | |
| self.onmessage = async (event) => { | |
| const message = event.data || {}; | |
| if (message.type === 'init') { | |
| shimSource = message.shim || ''; | |
| try { | |
| await init(); | |
| } catch (error) { | |
| post({ type: 'fatal', error: String((error && error.stack) || error) }); | |
| } | |
| return; | |
| } | |
| if (message.type === 'run') { | |
| try { | |
| await init(); | |
| pyodide.globals.set('__wfcad_code', message.code || ''); | |
| const raw = pyodide.runPython( | |
| 'import json; json.dumps(__wfcad_runtime.run(__wfcad_code))', | |
| ); | |
| post({ type: 'result', id: message.id, result: JSON.parse(raw) }); | |
| } catch (error) { | |
| post({ | |
| type: 'error', | |
| id: message.id, | |
| error: String((error && error.message) || error), | |
| }); | |
| } | |
| } | |
| }; | |