// 环境包解压工作子进程(utilityProcess.fork 启动)。 // AdmZip.extractAllTo 是同步 API,294MB 环境包若在主进程解压会阻塞事件循环几十秒, // 期间窗口点击必现"未响应"。故将【解压+校验+原子切换】整体下沉到子进程,主进程保持响应。 "use strict"; const fs = require("fs"); const path = require("path"); const AdmZip = require("adm-zip"); const { parentPort } = process; function rm(p) { try { fs.rmSync(p, { recursive: true, force: true }); } catch (e) { /* 忽略 */ } } function sleep(ms) { const end = Date.now() + ms; while (Date.now() < end) { /* 忙等(子进程内允许,避免阻塞主进程) */ } } // 入参:process.argv = [modulePath, zip, tmp, target] async function main() { const zip = process.argv[2]; const tmp = process.argv[3]; const target = process.argv[4]; try { rm(tmp); rm(target); fs.mkdirSync(tmp, { recursive: true }); // 防路径穿越 const az = new AdmZip(zip); for (const en of az.getEntries()) { const name = en.entryName.replace(/\\/g, "/"); if (name.startsWith("/") || name.split("/").includes("..")) { throw new Error(`环境包含非法路径: ${name}`); } } // 同步解压(本进程阻塞无所谓) az.extractAllTo(tmp, true); // 冒烟校验:必须含 python.exe + plugin_runner.py if (!fs.existsSync(path.join(tmp, "python.exe"))) throw new Error("环境包缺少 python.exe"); if (!fs.existsSync(path.join(tmp, "webhost", "backend", "core", "plugin_runner.py"))) { throw new Error("环境包缺少 plugin_runner.py"); } // 原子切换(带重试:杀软可能短暂占用新解压文件导致 rename EPERM) let lastErr = null; for (let i = 0; i < 3; i++) { try { fs.renameSync(tmp, target); parentPort.postMessage({ ok: true, target }); return; } catch (e) { lastErr = e; sleep(500 * (i + 1)); } } throw lastErr || new Error("环境包切换失败"); } catch (e) { parentPort.postMessage({ ok: false, error: (e && e.message) || String(e) }); } } main();