mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 11:30:22 +08:00
fix(web): boot 前回收 detached 残留 server,修 EADDRINUSE 起不来 (#197)
- restart-web.js 把后台 server 的 pid/url 记到 \/dsh-purge/web-server-<port>.json - killOldServer 增加按端口兜底:清掉旧版本留下、没有 pid 记录的孤儿 - 新增 lib/bin-preflight.js:把回收钩子注入宿主 bin.js 顶部,早于 Cordis 激活与 webserver 绑端口 - 重启时打印『后台运行、关窗不会停』,避免把提示符返回误判成程序已退出 - 新增 test/web-reclaim.test.js(7 例),全量 node --test 91/91 通过
This commit is contained in:
1 parent
53dd6c2f84
commit
5a112b4bdc
7 files changed
+544
-10
No files matched your search
@@ -0,0 +1,85 @@
|
||||
// #197:把「启动前回收残留 web server」的钩子挂到宿主 bin.js 顶部。
|
||||
// 必须在 Cordis 激活之前跑:webserver 插件绑端口失败在 0.2.x 是 fatal,等插件层再处理就晚了。
|
||||
// 注入形态与 hide-console 一致:宿主 lib 下放一个 sidecar + bin.js 顶部加一行 import。
|
||||
// 与 hide-console 的区别:这里跨平台生效(detached 重启在 macOS/Linux 同样会留下孤儿)。
|
||||
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { isAsarSealedPath } from "./desktop.js";
|
||||
import { looksLikeSourceCliRoot, resolveDshBinRoot } from "./hide-console.js";
|
||||
|
||||
const MARK = "dsh-purge-web-preflight";
|
||||
const IMPORT_LINE = `import "./${MARK}.js"; // [dsh-purge] web preflight`;
|
||||
const ENTRY = fileURLToPath(new URL("./web-preflight-entry.js", import.meta.url));
|
||||
|
||||
export function installBinPreflight(aiBase) {
|
||||
const root = resolveDshBinRoot(aiBase);
|
||||
if (!root) return "missing_bin";
|
||||
// 源码部署的 apps/cli/lib 是 tsdown 产物:只准读、不准改(#41)。
|
||||
if (looksLikeSourceCliRoot(root)) return "skipped_source_cli";
|
||||
const binJs = path.join(root, "lib", "bin.js");
|
||||
const side = path.join(root, "lib", `${MARK}.js`);
|
||||
if (isAsarSealedPath(root) || isAsarSealedPath(binJs)) return "sealed_asar";
|
||||
|
||||
let source;
|
||||
try {
|
||||
source = fs.readFileSync(ENTRY, "utf8");
|
||||
fs.writeFileSync(side, source, "utf8");
|
||||
} catch (e) {
|
||||
return `error:${e && e.message ? e.message : e}`;
|
||||
}
|
||||
|
||||
let text;
|
||||
try {
|
||||
text = fs.readFileSync(binJs, "utf8");
|
||||
} catch (e) {
|
||||
return `error:${e && e.message ? e.message : e}`;
|
||||
}
|
||||
if (text.includes(IMPORT_LINE)) return "already_injected";
|
||||
text = text.replace(/^import "\.\/dsh-purge-web-preflight\.js";.*\r?\n/gm, "");
|
||||
if (/^#!/.test(text)) {
|
||||
const nl = text.indexOf("\n");
|
||||
text =
|
||||
nl >= 0
|
||||
? text.slice(0, nl + 1) + `${IMPORT_LINE}\n` + text.slice(nl + 1)
|
||||
: `${text}\n${IMPORT_LINE}\n`;
|
||||
} else {
|
||||
text = `${IMPORT_LINE}\n${text}`;
|
||||
}
|
||||
try {
|
||||
fs.writeFileSync(binJs, text, "utf8");
|
||||
} catch (e) {
|
||||
return `error:${e && e.message ? e.message : e}`;
|
||||
}
|
||||
return "injected";
|
||||
}
|
||||
|
||||
export function revertBinPreflight(aiBase) {
|
||||
const root = resolveDshBinRoot(aiBase);
|
||||
if (!root) return "missing_bin";
|
||||
const binJs = path.join(root, "lib", "bin.js");
|
||||
const side = path.join(root, "lib", `${MARK}.js`);
|
||||
let changed = false;
|
||||
if (!looksLikeSourceCliRoot(root)) {
|
||||
try {
|
||||
const text = fs.readFileSync(binJs, "utf8");
|
||||
const next = text.replace(/^import "\.\/dsh-purge-web-preflight\.js";.*\r?\n/gm, "");
|
||||
if (next !== text) {
|
||||
fs.writeFileSync(binJs, next, "utf8");
|
||||
changed = true;
|
||||
}
|
||||
} catch (e) {
|
||||
return `error:${e && e.message ? e.message : e}`;
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (fs.existsSync(side)) {
|
||||
fs.unlinkSync(side);
|
||||
changed = true;
|
||||
}
|
||||
} catch {
|
||||
/* 重启后再删 */
|
||||
}
|
||||
return changed ? "reverted" : "absent";
|
||||
}
|
||||
Reference in new issue
Block a user