Files
FreeCADWasmDev/tools/partdesign-python-api-facade-smoke-worker.mjs
wangdequan 9a7c4f0335 Save FreeCAD Web workspace sources and user validation evidence
IFC native binding remains incomplete; preserve accurate test results.
2026-09-15 22:01:58 +08:00

35 lines
1.9 KiB
JavaScript

import fs from "node:fs";
import path from "node:path";
import { createRequire } from "node:module";
import { parentPort, workerData } from "node:worker_threads";
import { pathToFileURL } from "node:url";
const facade = await import(workerData.facadePath);
const envelope = await import(workerData.envelopePath);
globalThis.__dirname = `${workerData.buildDirectory}${path.sep}`;
globalThis.require = createRequire(import.meta.url);
const workerJs = path.join(workerData.buildDirectory, "FreeCADWebPartDesignRuntimeProbe.worker.js");
const workerCjs = path.join(workerData.buildDirectory, "FreeCADWebPartDesignRuntimeProbe.worker.cjs");
if (!fs.existsSync(workerCjs) && fs.existsSync(workerJs)) fs.copyFileSync(workerJs, workerCjs);
const loader = await import(pathToFileURL(path.join(workerData.buildDirectory, "FreeCADWebPartDesignRuntimeProbe.js")));
const kernel = await loader.default({
wasmBinary: fs.readFileSync(path.join(workerData.buildDirectory, "FreeCADWebPartDesignRuntimeProbe.wasm")),
locateFile: (file) => file.endsWith(".worker.js") && fs.existsSync(workerCjs) ? workerCjs : path.join(workerData.buildDirectory, file),
});
const queue = new envelope.FacadeSerialQueue();
parentPort.postMessage({ kind: "ready", action: facade.PARTDESIGN_PYTHON_COMMAND });
parentPort.on("message", async (message) => {
if (envelope.isFacadeCancel(message)) {
if (!queue.cancel(message)) parentPort.postMessage(envelope.createFailure(message.requestId, "cancel_rejected", "request is not active"));
return;
}
if (facade.isPartDesignPythonRequest(message)) {
const source = message.requestId === "pdn-python-cancel-1"
? new Promise((resolve) => setTimeout(() => resolve(kernel), 25))
: kernel;
parentPort.postMessage(await facade.dispatchPartDesignPython(queue, message, source));
return;
}
parentPort.postMessage(envelope.createFailure("invalid", "invalid_request", "PartDesign Python envelope invalid"));
});