mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-27 17:20:23 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b46c952584 | ||
|
|
d9a8b0164c | ||
|
|
61ecf7977d | ||
|
|
c236e689f3 | ||
|
|
9118bf61d6 | ||
|
|
7b3f8ede49 |
No files matched your search
@@ -1145,6 +1145,23 @@ body[data-ds-dark-theme] .dshp-launch{background:color-mix(in srgb,var(--dsw-ali
|
||||
.dshp-dock[data-theme="white"] .dshp-dock-body .rt-err{color:#9a3434!important}
|
||||
.dshp-dock[data-theme="dusk"] .dshp-dock-body .rt-err{color:#f0b4b4!important}
|
||||
.dshp-dock-body .rt-rep-http,.dshp-dock-body pre{background:var(--rt-well)!important;color:var(--dshp-ink)!important;border:1px solid var(--dshp-line)!important}
|
||||
/* #196:暗色宿主里 --dsw-alias-brand-primary 解析为近白色(neutral-bluish-50 #f9fafb),
|
||||
任何“品牌色作底 + 白字”的按钮都会变成白底白字。暗色下主按钮统一改用绿色实底。 */
|
||||
body[data-ds-dark-theme] .dshp-btn-primary,
|
||||
.dshp-root[data-theme="dusk"] .dshp-btn-primary,
|
||||
.dshp-dock[data-theme="dusk"] .dshp-btn-primary,
|
||||
.dshp-restart-mask[data-theme="dusk"] .dshp-btn-primary{background:#3d8f68!important;border-color:#3d8f68!important;color:#fff!important;-webkit-text-fill-color:#fff!important}
|
||||
body[data-ds-dark-theme] .dshp-btn-primary:hover:not(:disabled),
|
||||
.dshp-root[data-theme="dusk"] .dshp-btn-primary:hover:not(:disabled),
|
||||
.dshp-dock[data-theme="dusk"] .dshp-btn-primary:hover:not(:disabled),
|
||||
.dshp-restart-mask[data-theme="dusk"] .dshp-btn-primary:hover:not(:disabled){background:#347a59!important;border-color:#347a59!important;color:#fff!important;-webkit-text-fill-color:#fff!important}
|
||||
body[data-ds-dark-theme] .rt-btn-primary,
|
||||
body[data-ds-dark-theme] .rt-btn-primary:hover:not(:disabled),
|
||||
.dshp-dock[data-theme="dusk"] .rt-btn-primary,
|
||||
.dshp-dock[data-theme="dusk"] .rt-btn-primary:hover:not(:disabled),
|
||||
body[data-ds-dark-theme] .dshp-dock-body .rt-btn-primary,
|
||||
.dshp-dock[data-theme="dusk"] .dshp-dock-body .rt-btn-primary{background:#3d8f68!important;border-color:#3d8f68!important;color:#fff!important;-webkit-text-fill-color:#fff!important;backdrop-filter:none}
|
||||
body[data-ds-dark-theme] .rt-rep-step-no,.dshp-dock[data-theme="dusk"] .rt-rep-step-no{background:#3d8f68;color:#fff}
|
||||
`;
|
||||
|
||||
function formatSize(bytes) {
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
+20
-3
@@ -11,6 +11,7 @@ import {
|
||||
resolveDshBinRoot,
|
||||
revertHideConsoleFromBin,
|
||||
} from "./hide-console.js";
|
||||
import { installBinPreflight, revertBinPreflight } from "./bin-preflight.js";
|
||||
import * as desktop from "./desktop.js";
|
||||
import {
|
||||
asarArchiveIsFile,
|
||||
@@ -400,7 +401,14 @@ export function ensureHiddenConsole(aiBase = findAiBase()) {
|
||||
} catch (e) {
|
||||
entry = `error:${e && e.message ? e.message : e}`;
|
||||
}
|
||||
return { live, entry, entryRoot };
|
||||
// #197:web 重启后的孤儿 server 要在 Cordis 激活前回收,钩子必须早于 webserver 绑端口。
|
||||
let preflight = "unknown";
|
||||
try {
|
||||
preflight = installBinPreflight(entryRoot || aiBase);
|
||||
} catch (e) {
|
||||
preflight = `error:${e && e.message ? e.message : e}`;
|
||||
}
|
||||
return { live, entry, preflight, entryRoot };
|
||||
}
|
||||
|
||||
export function silenceCmdFlash(aiBase = findAiBase()) {
|
||||
@@ -429,6 +437,9 @@ export function silenceCmdFlash(aiBase = findAiBase()) {
|
||||
liangshenPrompt === "already" ||
|
||||
liangshenPrompt === "patched" ||
|
||||
liangshenPrompt === "pattern_miss";
|
||||
const preflightOk =
|
||||
["injected", "already_injected", "skipped_source_cli", "missing_bin", "sealed_asar"].includes(hide.preflight) ||
|
||||
(typeof hide.preflight === "string" && hide.preflight.startsWith("error:"));
|
||||
return {
|
||||
...hide,
|
||||
doctor,
|
||||
@@ -440,7 +451,7 @@ export function silenceCmdFlash(aiBase = findAiBase()) {
|
||||
officialPhase1,
|
||||
liangshenPrompt,
|
||||
mnemon,
|
||||
ok: hideOk && liangshenOk,
|
||||
ok: hideOk && liangshenOk && preflightOk,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -582,7 +593,13 @@ export function revertCmdFlash(aiBase = findAiBase(), dshHome = findDshHome()) {
|
||||
if (next.includes(importReplacement)) next = next.replace(importReplacement, importNeedle);
|
||||
return next;
|
||||
});
|
||||
return { hide, subprocess, customBash, phase1, doctorSpawn, market };
|
||||
let preflight = "absent";
|
||||
try {
|
||||
preflight = revertBinPreflight(aiBase);
|
||||
} catch (e) {
|
||||
preflight = `error:${e && e.message ? e.message : e}`;
|
||||
}
|
||||
return { hide, preflight, subprocess, customBash, phase1, doctorSpawn, market };
|
||||
}
|
||||
|
||||
export function silenceSubprocessLocalFlash(dshHome = findDshHome()) {
|
||||
|
||||
+20
-1
@@ -1010,7 +1010,7 @@ function desktopRuntimeOf(ctx) {
|
||||
return runtime && typeof runtime.requestRestart === "function" ? runtime : null;
|
||||
}
|
||||
|
||||
const ASAR_SWAP_PS1 = [
|
||||
export const ASAR_SWAP_PS1 = [
|
||||
"param(",
|
||||
" [Parameter(Mandatory = $true)][int]$WaitPid,",
|
||||
" [Parameter(Mandatory = $true)][string]$AsarPath,",
|
||||
@@ -1037,6 +1037,16 @@ const ASAR_SWAP_PS1 = [
|
||||
"# 来得及处理 host 退出事件就被 kill,UI 根本不存在,无法弹 crash dialog。",
|
||||
"# PowerShell 本身是 detached 独立进程(spawn 时 detached:true, stdio:ignore),",
|
||||
"# 杀 Electron 不会连带杀 PowerShell。",
|
||||
"# #197b: 先杀 Electron main。main 是唯一会弹「host stopped」对话框的进程:",
|
||||
"# 若先杀 host,main 还活着就会弹框,然后脚本若在挪 asar 前失败,用户就卡在",
|
||||
"# 「host 死了、main 半死、asar 没动」的状态(2026-10-31 实测)。先杀 main 就没框。",
|
||||
"$mains = @(Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {",
|
||||
" $ep = $_.ExecutablePath",
|
||||
" if (-not $ep) { return $false }",
|
||||
" ($ep.ToLower() -eq $exeNorm) -and ($_.CommandLine -notmatch '--type=')",
|
||||
"})",
|
||||
"foreach ($p in $mains) { Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue }",
|
||||
"Start-Sleep -Milliseconds 400",
|
||||
"Stop-HarnessInstall",
|
||||
"Start-Sleep -Milliseconds 500",
|
||||
"Stop-HarnessInstall",
|
||||
@@ -1080,7 +1090,15 @@ const ASAR_SWAP_PS1 = [
|
||||
" Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue",
|
||||
" Start-Process -FilePath $ExePath",
|
||||
"} else {",
|
||||
" # #197b: 挪不动(asar 仍被占用)时不要把用户扔在空桌面上——把原版拉起来,",
|
||||
" # 留下 rename-pending 让下一次空闲重试;失败原因写进 dsh-purge 日志。",
|
||||
" Set-Content -LiteralPath $lock -Value ((Get-Date).ToString('o')) -Encoding ascii",
|
||||
" try {",
|
||||
" $log = Join-Path $env:TEMP 'dsh-purge-desktop.log'",
|
||||
" Add-Content -LiteralPath $log -Value ((Get-Date).ToUniversalTime().ToString('o') + ' [asar-swap] move failed; relaunching original app (asar still sealed)')",
|
||||
" } catch {}",
|
||||
" Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue",
|
||||
" Start-Process -FilePath $ExePath",
|
||||
"}",
|
||||
"",
|
||||
].join("\r\n");
|
||||
@@ -1940,3 +1958,4 @@ export function repairOfficialCliAfterUnpack(exe = findDesktopAppExecutable()) {
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
+33
-7
@@ -1,6 +1,14 @@
|
||||
import net from "node:net";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { spawn } from "node:child_process";
|
||||
import { envForDetachedSpawn } from "./core.js";
|
||||
import {
|
||||
cmdlineOf,
|
||||
dshHomeDir,
|
||||
killPid,
|
||||
listenerPids,
|
||||
looksLikeDshWeb,
|
||||
writeRecord,
|
||||
} from "./web-reclaim.js";
|
||||
|
||||
const encoded = process.env.DSH_PURGE_RESTART;
|
||||
if (!encoded) process.exit(1);
|
||||
@@ -25,14 +33,18 @@ function portFree() {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 杀掉「发起重启的那个进程」;再按端口兜底一次,清掉旧版本留下、没有 pid 记录的孤儿
|
||||
* (#197:只按 info.pid 杀时,上一轮 detached 起来的 server 永远没人管)。
|
||||
*/
|
||||
function killOldServer() {
|
||||
const pid = Number(info.pid);
|
||||
if (!pid || pid === process.pid) return;
|
||||
if (process.platform === "win32") {
|
||||
spawnSync("taskkill", ["/PID", String(pid), "/F"], { windowsHide: true, stdio: "ignore" });
|
||||
return;
|
||||
if (pid && pid !== process.pid) killPid(pid);
|
||||
for (const other of listenerPids(port)) {
|
||||
if (other === process.pid || other === pid) continue;
|
||||
if (!looksLikeDshWeb(cmdlineOf(other))) continue;
|
||||
killPid(other);
|
||||
}
|
||||
try { process.kill(pid, "SIGTERM"); } catch { /* already gone */ }
|
||||
}
|
||||
|
||||
function openBrowser(url) {
|
||||
@@ -77,6 +89,15 @@ const child = spawn(info.execPath, info.argv, {
|
||||
shell: false,
|
||||
});
|
||||
child.on("error", () => process.exit(1));
|
||||
// #197:记下这个后台 server 的 pid。下次 `dsh web` 启动时,注入到宿主 bin.js 的
|
||||
// preflight 会先把它收掉,不会再撞 EADDRINUSE。
|
||||
child.on("spawn", () => {
|
||||
try {
|
||||
writeRecord(dshHomeDir(), port, child.pid, { host, url: "" });
|
||||
} catch {
|
||||
/* 记录失败不影响重启 */
|
||||
}
|
||||
});
|
||||
const wantBrowser = info.openBrowser !== false;
|
||||
const takeUrl = (chunk) => {
|
||||
const text = String(chunk || "");
|
||||
@@ -84,8 +105,13 @@ const takeUrl = (chunk) => {
|
||||
const match = text.match(/dsh web:\s+(https?:\/\/\S+)/);
|
||||
if (!match) return;
|
||||
opened = true;
|
||||
if (!wantBrowser) return;
|
||||
const url = match[1].replace(/[),.;]+$/, "");
|
||||
try {
|
||||
writeRecord(dshHomeDir(), port, child.pid, { host, url });
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
if (!wantBrowser) return;
|
||||
openBrowser(url);
|
||||
};
|
||||
child.stdout?.on("data", takeUrl);
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
// [dsh-purge] web preflight — 由 lib/bin-preflight.js 复制到宿主 lib 并 import 到 bin.js 顶部。
|
||||
//
|
||||
// 目的:dsh-purge 的「应用 → 自动重启」用 detached 子进程接管 server,父进程随即退出,
|
||||
// 关窗/关终端都杀不到那个子进程;下一次 `dsh web` 会在 webserver 插件绑端口时撞
|
||||
// EADDRINUSE,而 0.2.x 把 required 插件激活失败当 fatal,宿主直接起不来(issue #197)。
|
||||
//
|
||||
// 这里在 Cordis 激活之前做一次回收:先查 $DSH_HOME/dsh-purge/web-server-<port>.json,
|
||||
// 记录失效时再按端口兜底;只杀命令行命中 `@deepseek-ai/dsh/lib/bin.js … web` 的进程。
|
||||
// 只处理 `dsh web`,其它子命令零开销。
|
||||
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
const DSH_BIN = /@deepseek-ai[\\/]dsh[\\/]lib[\\/]bin\.js/;
|
||||
const WEB_ARG = /(^|[\s"'])web([\s"']|$)/;
|
||||
|
||||
function run(file, args) {
|
||||
try {
|
||||
const r = spawnSync(file, args, { encoding: "utf8", windowsHide: true, timeout: 8000 });
|
||||
return r && r.status === 0 ? String(r.stdout || "") : "";
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function cmdlineOf(pid) {
|
||||
if (process.platform === "win32") {
|
||||
return run("powershell.exe", [
|
||||
"-NoLogo", "-NoProfile", "-NonInteractive", "-Command",
|
||||
`(Get-CimInstance Win32_Process -Filter "ProcessId=${pid}" -ErrorAction SilentlyContinue).CommandLine`,
|
||||
]).trim();
|
||||
}
|
||||
return run("ps", ["-p", String(pid), "-o", "command="]).trim();
|
||||
}
|
||||
|
||||
function isDshWeb(cmd) {
|
||||
return DSH_BIN.test(cmd) && WEB_ARG.test(cmd);
|
||||
}
|
||||
|
||||
function alive(pid) {
|
||||
try {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function kill(pid) {
|
||||
if (!pid || pid === process.pid) return false;
|
||||
if (process.platform === "win32") {
|
||||
const r = spawnSync("taskkill", ["/PID", String(pid), "/F"], { windowsHide: true, stdio: "ignore" });
|
||||
return !r.error && r.status === 0;
|
||||
}
|
||||
try {
|
||||
process.kill(pid, "SIGKILL");
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function listeningPids(port) {
|
||||
if (process.platform === "win32") {
|
||||
const out = run("netstat", ["-ano"]);
|
||||
const pids = [];
|
||||
for (const line of out.split(/\r?\n/)) {
|
||||
const m = line.match(/^\s*TCP\s+(\S+):(\d+)\s+\S+\s+LISTENING\s+(\d+)\s*$/i);
|
||||
if (m && Number(m[2]) === port) pids.push(Number(m[3]));
|
||||
}
|
||||
return [...new Set(pids)];
|
||||
}
|
||||
const out = run("lsof", ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-t"]);
|
||||
return [...new Set(out.split(/\s+/).map(Number).filter((n) => Number.isInteger(n) && n > 0))];
|
||||
}
|
||||
|
||||
function main() {
|
||||
if (!process.argv.slice(2).some((a) => a === "web")) return;
|
||||
const i = process.argv.indexOf("--port");
|
||||
const port = Number(i > -1 ? process.argv[i + 1] : process.env.PORT) || 3080;
|
||||
const home = String(process.env.DSH_HOME || "").trim() || path.join(os.homedir(), ".dsh");
|
||||
const record = path.join(home, "dsh-purge", `web-server-${port}.json`);
|
||||
|
||||
try {
|
||||
let killed = 0;
|
||||
const killedPids = [];
|
||||
let rec = null;
|
||||
try {
|
||||
rec = JSON.parse(fs.readFileSync(record, "utf8"));
|
||||
} catch {
|
||||
rec = null;
|
||||
}
|
||||
const listeners = listeningPids(port);
|
||||
const recPid = Number(rec && rec.pid) || 0;
|
||||
let recStillAlive = false;
|
||||
if (recPid && alive(recPid)) {
|
||||
const cmd = cmdlineOf(recPid);
|
||||
// 命令行拿不到时,必须证明这个 pid 仍占着端口才杀:记录可能早于进程退出,
|
||||
// pid 被系统复用后盲目 taskkill 会杀掉无关进程。
|
||||
const ownsPort = listeners.includes(recPid);
|
||||
if (isDshWeb(cmd) || (!cmd && ownsPort)) {
|
||||
if (kill(recPid)) {
|
||||
killed += 1;
|
||||
killedPids.push(recPid);
|
||||
}
|
||||
}
|
||||
recStillAlive = alive(recPid);
|
||||
}
|
||||
// 杀不掉就留着记录,下一次启动再试;只有确认进程已经不在才清记录。
|
||||
if (rec && !recStillAlive) {
|
||||
try {
|
||||
fs.unlinkSync(record);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
if (!killed) {
|
||||
for (const pid of listeners) {
|
||||
if (pid === process.pid) continue;
|
||||
if (!isDshWeb(cmdlineOf(pid))) continue;
|
||||
if (kill(pid)) {
|
||||
killed += 1;
|
||||
killedPids.push(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// taskkill 返回不代表端口已经释放。等被杀进程真正退出再让 Cordis 绑端口,
|
||||
// 否则还是会撞 EADDRINUSE。
|
||||
waitGone(killedPids);
|
||||
|
||||
if (killed) {
|
||||
process.stderr.write(`[dsh-purge] reclaimed stale dsh web on :${port}, retrying boot\n`);
|
||||
}
|
||||
} catch {
|
||||
/* 回收失败不阻断启动 */
|
||||
}
|
||||
}
|
||||
|
||||
function waitGone(pids, timeoutMs = 1500) {
|
||||
if (!pids.length) return;
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
const sleeper = new Int32Array(new SharedArrayBuffer(4));
|
||||
while (Date.now() < deadline && pids.some((pid) => alive(pid))) {
|
||||
try {
|
||||
Atomics.wait(sleeper, 0, 0, 80);
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -0,0 +1,215 @@
|
||||
// #197:Web「应用 → 自动重启」用 detached 子进程接管 server,父进程 250ms 后退出,
|
||||
// 关窗/关终端都杀不到它;下一次 `dsh web` 撞 EADDRINUSE,0.2.x 把 required 插件
|
||||
// 激活失败当 fatal,宿主直接起不来。
|
||||
//
|
||||
// 这个模块提供三件事,供 restart-web.js(记录 pid)和注入到宿主 bin.js 的启动前
|
||||
// 回收脚本(清理残留)共用:
|
||||
// 1. pid 记录文件 $DSH_HOME/dsh-purge/web-server-<port>.json
|
||||
// 2. 按端口反查监听 pid(win32: netstat;darwin/linux: lsof)
|
||||
// 3. 判定某个 pid 是不是 `dsh web`,只杀自己人
|
||||
//
|
||||
// 本模块刻意不 import core.js(core 会 import 本模块的方向,避免循环依赖)。
|
||||
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
export const RECORD_DIR = "dsh-purge";
|
||||
export const RECORD_PREFIX = "web-server-";
|
||||
/** 宿主入口的稳定特征:@deepseek-ai/dsh/lib/bin.js */
|
||||
export const DSH_BIN_PATTERN = /@deepseek-ai[\\/]dsh[\\/]lib[\\/]bin\.js/;
|
||||
|
||||
export function dshHomeDir(env = process.env) {
|
||||
const home = typeof env?.DSH_HOME === "string" ? env.DSH_HOME.trim() : "";
|
||||
return home || path.join(os.homedir(), ".dsh");
|
||||
}
|
||||
|
||||
export function recordPath(dshHome, port) {
|
||||
return path.join(dshHome, RECORD_DIR, `${RECORD_PREFIX}${Number(port) || 3080}.json`);
|
||||
}
|
||||
|
||||
export function writeRecord(dshHome, port, pid, extra = {}) {
|
||||
const fp = recordPath(dshHome, port);
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(fp), { recursive: true });
|
||||
const payload = { pid: Number(pid) || 0, port: Number(port) || 3080, ...extra, at: new Date().toISOString() };
|
||||
fs.writeFileSync(fp, `${JSON.stringify(payload, null, 2)}\n`, "utf8");
|
||||
return fp;
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
export function readRecord(dshHome, port) {
|
||||
try {
|
||||
const raw = JSON.parse(fs.readFileSync(recordPath(dshHome, port), "utf8"));
|
||||
if (!raw || typeof raw !== "object") return null;
|
||||
const pid = Number(raw.pid) || 0;
|
||||
if (!pid) return null;
|
||||
return { pid, port: Number(raw.port) || Number(port) || 3080, at: String(raw.at || "") };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function clearRecord(dshHome, port) {
|
||||
try {
|
||||
fs.unlinkSync(recordPath(dshHome, port));
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 命令行长什么样才算「我们自己起的 dsh web」。
|
||||
* 只认 bin.js 入口 + 带 web 子命令,避免误杀同机其它 node 进程。
|
||||
*/
|
||||
export function looksLikeDshWeb(cmdline) {
|
||||
const text = String(cmdline || "");
|
||||
if (!text || !DSH_BIN_PATTERN.test(text)) return false;
|
||||
return /(^|[\s"'])web([\s"']|$)/.test(text) || /(^|[\s"'])web\s+--/.test(text);
|
||||
}
|
||||
|
||||
/** win32 netstat -ano 的 LISTENING 行 → [{ address, port, pid }] */
|
||||
export function parseNetstatListeners(output) {
|
||||
const out = [];
|
||||
for (const line of String(output || "").split(/\r?\n/)) {
|
||||
const m = line.match(/^\s*TCP\s+(\S+):(\d+)\s+\S+\s+LISTENING\s+(\d+)\s*$/i);
|
||||
if (!m) continue;
|
||||
out.push({ address: m[1], port: Number(m[2]), pid: Number(m[3]) });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** lsof -nP -iTCP:PORT -sTCP:LISTEN -t 的输出 → pid 数组 */
|
||||
export function parseLsofPids(output) {
|
||||
return String(output || "")
|
||||
.split(/\s+/)
|
||||
.map((x) => Number(x))
|
||||
.filter((n) => Number.isInteger(n) && n > 0);
|
||||
}
|
||||
|
||||
function run(file, args, options = {}) {
|
||||
try {
|
||||
const r = spawnSync(file, args, {
|
||||
encoding: "utf8",
|
||||
windowsHide: true,
|
||||
timeout: 8000,
|
||||
...options,
|
||||
});
|
||||
if (r.error || r.status !== 0) return "";
|
||||
return String(r.stdout || "");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/** 当前监听某端口的 pid(跨平台) */
|
||||
export function listenerPids(port) {
|
||||
const p = Number(port) || 3080;
|
||||
if (process.platform === "win32") {
|
||||
const rows = parseNetstatListeners(run("netstat", ["-ano"]));
|
||||
return [...new Set(rows.filter((r) => r.port === p).map((r) => r.pid))];
|
||||
}
|
||||
return parseLsofPids(run("lsof", ["-nP", `-iTCP:${p}`, "-sTCP:LISTEN", "-t"]));
|
||||
}
|
||||
|
||||
/** 取某个 pid 的完整命令行(拿不到就返回空串) */
|
||||
export function cmdlineOf(pid) {
|
||||
const n = Number(pid) || 0;
|
||||
if (!n) return "";
|
||||
if (process.platform === "win32") {
|
||||
const out = run("powershell.exe", [
|
||||
"-NoLogo", "-NoProfile", "-NonInteractive", "-Command",
|
||||
`(Get-CimInstance Win32_Process -Filter "ProcessId=${n}" -ErrorAction SilentlyContinue).CommandLine`,
|
||||
]);
|
||||
return out.trim();
|
||||
}
|
||||
return run("ps", ["-p", String(n), "-o", "command="]).trim();
|
||||
}
|
||||
|
||||
export function pidAlive(pid) {
|
||||
const n = Number(pid) || 0;
|
||||
if (!n) return false;
|
||||
try {
|
||||
process.kill(n, 0);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function killPid(pid) {
|
||||
const n = Number(pid) || 0;
|
||||
if (!n || n === process.pid) return false;
|
||||
if (process.platform === "win32") {
|
||||
const r = spawnSync("taskkill", ["/PID", String(n), "/F"], { windowsHide: true, stdio: "ignore" });
|
||||
return !r.error && r.status === 0;
|
||||
}
|
||||
try {
|
||||
process.kill(n, "SIGKILL");
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动前回收:先按记录杀,记录缺失/失效时再按端口兜底。
|
||||
* @returns {{ killed: number[], source: "record" | "port" | "" }}
|
||||
*/
|
||||
export function reclaimStaleWebServer({ port, dshHome, logger } = {}) {
|
||||
const p = Number(port) || 3080;
|
||||
const home = dshHome || dshHomeDir();
|
||||
const log = typeof logger === "function" ? logger : () => {};
|
||||
const killed = [];
|
||||
|
||||
const rec = readRecord(home, p);
|
||||
const listeners = listenerPids(p);
|
||||
let recStillAlive = false;
|
||||
if (rec && pidAlive(rec.pid)) {
|
||||
const cmd = cmdlineOf(rec.pid);
|
||||
// 命令行拿不到时,必须证明这个 pid 仍占着端口才杀:记录早于进程退出时,
|
||||
// pid 被系统复用后盲目 kill 会打到无关进程。
|
||||
const ownsPort = listeners.includes(rec.pid);
|
||||
if (looksLikeDshWeb(cmd) || (!cmd && ownsPort)) {
|
||||
if (killPid(rec.pid)) killed.push(rec.pid);
|
||||
}
|
||||
recStillAlive = pidAlive(rec.pid);
|
||||
}
|
||||
// 杀不掉就保留记录,下一次启动再试。
|
||||
if (rec && !recStillAlive) clearRecord(home, p);
|
||||
if (killed.length) {
|
||||
waitGone(killed);
|
||||
return { killed, source: "record" };
|
||||
}
|
||||
|
||||
for (const pid of listeners) {
|
||||
if (pid === process.pid) continue;
|
||||
const cmd = cmdlineOf(pid);
|
||||
if (!looksLikeDshWeb(cmd)) continue;
|
||||
if (killPid(pid)) killed.push(pid);
|
||||
}
|
||||
if (killed.length) {
|
||||
waitGone(killed);
|
||||
log(`[dsh-purge] reclaimed stale web server on :${p} (pid ${killed.join(",")})`);
|
||||
}
|
||||
return { killed, source: killed.length ? "port" : "" };
|
||||
}
|
||||
|
||||
/** taskkill / SIGKILL 返回不代表端口已释放;绑端口前等一下进程真正退出。 */
|
||||
export function waitGone(pids, timeoutMs = 1500) {
|
||||
const list = Array.isArray(pids) ? pids.filter((pid) => Number(pid) > 0) : [];
|
||||
if (!list.length) return;
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
const sleeper = new Int32Array(new SharedArrayBuffer(4));
|
||||
while (Date.now() < deadline && list.some((pid) => pidAlive(pid))) {
|
||||
try {
|
||||
Atomics.wait(sleeper, 0, 0, 80);
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
@@ -77,6 +77,16 @@ function spawnRestartHelper(helper, payload, exitAfter = 250) {
|
||||
export function scheduleRestart(request, _ctx, options = {}) {
|
||||
const openBrowser = options.openBrowser ?? (request != null);
|
||||
const helper = fileURLToPath(new URL("./restart-web.js", import.meta.url));
|
||||
// #197:前台进程 250ms 后就会退出,终端回到提示符,但 server 在后台跑。
|
||||
// 不把这句话打出来,用户会以为程序已经关掉,然后立刻再开一个 → EADDRINUSE。
|
||||
try {
|
||||
console.log(
|
||||
"[dsh-purge] restarting dsh web in the background; closing this window will NOT stop it. " +
|
||||
"The next `dsh web` reclaims it automatically.",
|
||||
);
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
spawnRestartHelper(helper, {
|
||||
kind: "web",
|
||||
execPath: process.execPath,
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dsh-purge",
|
||||
"version": "1.1.84",
|
||||
"version": "1.1.86",
|
||||
"description": "DeepSeek Harness 破甲:让所有模型都能破甲,不同模型可换不同提示词;默认提示词面向国模「小码酱」。Jailbreak for every model — swap prompts per model. 求 Star 收藏 ⭐ 如要二开,请查看署名要求部分",
|
||||
"type": "module",
|
||||
"main": "lib/index.js",
|
||||
|
||||
@@ -1,3 +1,53 @@
|
||||
# 1.1.86
|
||||
|
||||
## 中文
|
||||
|
||||
- 版本 **1.1.86**。
|
||||
- **Web「应用 → 自动重启」后残留 server 导致 EADDRINUSE 起不来(#197,合并 PR #197)**:
|
||||
- `restart-web.js` 把后台 server 的 pid/url 记到 `$DSH_HOME/dsh-purge/web-server-<port>.json`;`killOldServer` 增加按端口兜底,清掉旧版本留下、没有记录的孤儿。
|
||||
- 新增 `lib/bin-preflight.js`:把回收钩子注入宿主 `bin.js` 顶部(跨平台),在 Cordis 激活、webserver 绑端口之前先回收残留;只杀命令行命中 `@deepseek-ai/dsh/lib/bin.js … web` 的进程。
|
||||
- 重启时打印「后台运行、关窗不会停」,避免把提示符返回误判成程序已退出。
|
||||
- 加固:记录里的 pid 拿不到命令行时,必须仍占着该端口才允许杀,防 PID 复用误杀;杀不掉就保留记录下次再试;杀完等待进程真正退出再绑端口。
|
||||
- **桌面端点「应用」换包顺序(#198,合并 PR #198)**:
|
||||
- 先按安装根路径且不含 `--type=` 识别并杀掉 Electron main,再扫安装目录其余进程,避免 host 先死、main 还活着弹「host stopped」。
|
||||
- 挪包失败(asar 仍被占用)时补上 `Start-Process`,把原版应用拉起来并写入 `%TEMP%\dsh-purge-desktop.log`,不再留空桌面。
|
||||
- 测试:96/96 通过。
|
||||
|
||||
## English
|
||||
|
||||
- **1.1.86**.
|
||||
- **Stale web server after "Apply → auto restart" caused EADDRINUSE on boot (#197, merged PR #197)**:
|
||||
- `restart-web.js` records the background server pid/url in `$DSH_HOME/dsh-purge/web-server-<port>.json`; `killOldServer` now also reclaims by port, catching orphans left by older versions with no record.
|
||||
- New `lib/bin-preflight.js` injects a reclaim hook at the top of the host `bin.js` (cross-platform) so it runs before Cordis activates and before the webserver binds; only processes whose command line matches `@deepseek-ai/dsh/lib/bin.js … web` are killed.
|
||||
- The restart prints "running in the background; closing this window will NOT stop it" so the returned prompt is not mistaken for an exit.
|
||||
- Hardening: when the recorded pid's command line cannot be read, it is killed only if it still listens on that port (guards against PID reuse); a failed kill keeps the record for the next boot; boot waits for the killed process to actually exit before binding.
|
||||
- **Desktop asar swap order (#198, merged PR #198)**:
|
||||
- Electron main is identified by install root plus "no `--type=`" and killed first, then the rest of the install tree is swept, so the host no longer dies while main stays alive showing a "host stopped" dialog.
|
||||
- If the asar move fails (still locked), the original app is relaunched and the reason is appended to `%TEMP%\dsh-purge-desktop.log` instead of leaving an empty desktop.
|
||||
- Tests: 96/96 pass.
|
||||
|
||||
# 1.1.85
|
||||
|
||||
## 中文
|
||||
|
||||
- 版本 **1.1.85**。
|
||||
- **#196**:修复深色模式下部分按钮白底白字、完全看不清的问题。
|
||||
- 根因:宿主暗色主题把 `--dsw-alias-brand-primary` 解析成近白色(`neutral-bluish-50` `#f9fafb`)。插件里凡是"品牌色作底 + 白字"的按钮(设置页的「应用」「保存注入」、演练台的「保存」「检索」「检测连通性」以及各类主按钮)在暗色下就变成一整块白。
|
||||
- 现在暗色(`body[data-ds-dark-theme]`、`[data-theme="dusk"]`)下主按钮统一用绿色实底 `#3d8f68` + 白字,悬停用更深的 `#347a59`;`-webkit-text-fill-color` 一并显式指定,避免宿主强制白字穿透。
|
||||
- 报告里的步骤序号圆点(`.rt-rep-step-no`)同样改为绿底白字。
|
||||
- 浅色模式表现不变;新增 `test/client-dark-theme.test.js` 静态守护这几条暗色规则。
|
||||
- 测试:84/84 通过。
|
||||
|
||||
## English
|
||||
|
||||
- **1.1.85**.
|
||||
- **#196**: fix buttons in dark mode rendering as a solid white block (white text on white background).
|
||||
- Root cause: the host dark theme resolves `--dsw-alias-brand-primary` to a near-white token (`neutral-bluish-50`, `#f9fafb`). Every plugin button that used "brand color as background + white text" (settings **Apply**, **Save injection**, drill console **Save**, **Search**, **Test connectivity**, and other primary buttons) turned into a white block in dark mode.
|
||||
- Primary buttons under dark themes (`body[data-ds-dark-theme]`, `[data-theme="dusk"]`) now use a solid green `#3d8f68` with white text, with `#347a59` on hover; `-webkit-text-fill-color` is set explicitly so host-forced white text cannot leak through.
|
||||
- The report step-number dots (`.rt-rep-step-no`) are switched to the same green/white pair.
|
||||
- Light mode is unchanged; added `test/client-dark-theme.test.js` to guard these dark-mode rules.
|
||||
- Tests: 84/84 pass.
|
||||
|
||||
# 1.1.84
|
||||
|
||||
## 中文
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const root = join(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const client = readFileSync(join(root, "client.js"), "utf8");
|
||||
|
||||
function rule(selector) {
|
||||
const at = client.indexOf(selector);
|
||||
if (at < 0) return "";
|
||||
const brace = client.indexOf("{", at);
|
||||
const end = client.indexOf("}", brace);
|
||||
return brace < 0 || end < 0 ? "" : client.slice(brace + 1, end);
|
||||
}
|
||||
|
||||
test("#196 暗色宿主主按钮不再使用近白色品牌底", () => {
|
||||
// 宿主暗色下 --dsw-alias-brand-primary = #f9fafb,直接拿它作底会白底白字。
|
||||
for (const selector of [
|
||||
'body[data-ds-dark-theme] .dshp-btn-primary',
|
||||
'.dshp-root[data-theme="dusk"] .dshp-btn-primary',
|
||||
'.dshp-dock[data-theme="dusk"] .dshp-btn-primary',
|
||||
'body[data-ds-dark-theme] .rt-btn-primary',
|
||||
'.dshp-dock[data-theme="dusk"] .rt-btn-primary',
|
||||
'.dshp-dock[data-theme="dusk"] .dshp-dock-body .rt-btn-primary',
|
||||
]) {
|
||||
const body = rule(selector);
|
||||
assert.match(body, /background:#3d8f68!important/, selector);
|
||||
assert.match(body, /color:#fff!important/, selector);
|
||||
}
|
||||
});
|
||||
|
||||
test("#196 暗色主按钮悬停仍有可读底色", () => {
|
||||
const body = rule('body[data-ds-dark-theme] .dshp-btn-primary:hover:not(:disabled)');
|
||||
assert.match(body, /background:#347a59!important/);
|
||||
});
|
||||
|
||||
test("#196 报告步骤序号圆点暗色下保持绿底白字", () => {
|
||||
const at = client.indexOf('body[data-ds-dark-theme] .rt-rep-step-no,.dshp-dock[data-theme="dusk"] .rt-rep-step-no');
|
||||
assert.notEqual(at, -1);
|
||||
const body = client.slice(at, client.indexOf("}", at));
|
||||
assert.match(body, /background:#3d8f68/);
|
||||
assert.match(body, /color:#fff/);
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { ASAR_SWAP_PS1 } from "../lib/desktop.js";
|
||||
|
||||
// #197b:桌面端点「应用」时 host 被杀、main 还活着 → 弹「host stopped」,
|
||||
// 且 asar 没挪动 → 应用变成半死状态。这里守住两条顺序性约束。
|
||||
|
||||
test("先杀 Electron main,再杀安装目录其余进程", () => {
|
||||
const mainKill = ASAR_SWAP_PS1.indexOf("Stop-Process -Id $p.ProcessId");
|
||||
// 只看「作为独立一行的调用」,不要命中函数定义 `function Stop-HarnessInstall` 与缩进的内部调用
|
||||
const sweep = ASAR_SWAP_PS1.indexOf("\r\nStop-HarnessInstall\r\n");
|
||||
assert.ok(mainKill > 0, "缺少 main 进程 kill");
|
||||
assert.ok(sweep > 0, "缺少 Stop-HarnessInstall 调用");
|
||||
assert.ok(sweep > mainKill, "Stop-HarnessInstall 必须在 main kill 之后");
|
||||
});
|
||||
|
||||
test("main 进程的识别排除渲染/GPU 子进程(--type=)", () => {
|
||||
assert.match(ASAR_SWAP_PS1, /CommandLine -notmatch '--type='/);
|
||||
});
|
||||
|
||||
test("挪包失败时也要把原版拉起来,不留空桌面", () => {
|
||||
const relaunch = ASAR_SWAP_PS1.split("Start-Process -FilePath $ExePath").length - 1;
|
||||
assert.equal(relaunch, 2, "成功/失败两个分支都要重启 exe");
|
||||
assert.match(ASAR_SWAP_PS1, /move failed; relaunching original app/);
|
||||
});
|
||||
@@ -0,0 +1,100 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import {
|
||||
clearRecord,
|
||||
dshHomeDir,
|
||||
looksLikeDshWeb,
|
||||
parseLsofPids,
|
||||
parseNetstatListeners,
|
||||
readRecord,
|
||||
recordPath,
|
||||
waitGone,
|
||||
writeRecord,
|
||||
} from "../lib/web-reclaim.js";
|
||||
|
||||
test("recordPath 落在 $DSH_HOME/dsh-purge 下,按端口区分", () => {
|
||||
const home = path.join(os.tmpdir(), "dsh-home-x");
|
||||
assert.equal(recordPath(home, 3080), path.join(home, "dsh-purge", "web-server-3080.json"));
|
||||
assert.equal(recordPath(home, 4000), path.join(home, "dsh-purge", "web-server-4000.json"));
|
||||
});
|
||||
|
||||
test("write/read/clear 记录往返", () => {
|
||||
const home = fs.mkdtempSync(path.join(os.tmpdir(), "dsh-purge-rec-"));
|
||||
try {
|
||||
writeRecord(home, 3080, 4321, { url: "http://127.0.0.1:3080/?token=x" });
|
||||
const rec = readRecord(home, 3080);
|
||||
assert.equal(rec.pid, 4321);
|
||||
assert.equal(rec.port, 3080);
|
||||
assert.ok(rec.at);
|
||||
assert.ok(clearRecord(home, 3080));
|
||||
assert.equal(readRecord(home, 3080), null);
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("readRecord 对坏文件返回 null 而不抛", () => {
|
||||
const home = fs.mkdtempSync(path.join(os.tmpdir(), "dsh-purge-bad-"));
|
||||
try {
|
||||
fs.mkdirSync(path.join(home, "dsh-purge"), { recursive: true });
|
||||
fs.writeFileSync(path.join(home, "dsh-purge", "web-server-3080.json"), "{not json", "utf8");
|
||||
assert.equal(readRecord(home, 3080), null);
|
||||
} finally {
|
||||
fs.rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("dshHomeDir 优先 DSH_HOME,其次 ~/.dsh", () => {
|
||||
assert.equal(dshHomeDir({ DSH_HOME: " D:\\dsh-home " }), "D:\\dsh-home");
|
||||
assert.equal(dshHomeDir({}), path.join(os.homedir(), ".dsh"));
|
||||
});
|
||||
|
||||
test("looksLikeDshWeb 只认 dsh bin.js + web 子命令", () => {
|
||||
const win =
|
||||
'"node" "C:\\Users\\X\\AppData\\Roaming\\npm\\\\node_modules\\@deepseek-ai\\dsh\\lib\\bin.js" web';
|
||||
const posix = "/usr/bin/node /usr/lib/node_modules/@deepseek-ai/dsh/lib/bin.js web --port 3080";
|
||||
assert.equal(looksLikeDshWeb(win), true);
|
||||
assert.equal(looksLikeDshWeb(posix), true);
|
||||
|
||||
// 同机其它 node 进程不能被误杀
|
||||
assert.equal(looksLikeDshWeb('"node" "C:\\app\\server.js" web'), false);
|
||||
assert.equal(looksLikeDshWeb('"node" "…\\@deepseek-ai\\dsh\\lib\\bin.js" desktop'), false);
|
||||
assert.equal(looksLikeDshWeb(""), false);
|
||||
assert.equal(looksLikeDshWeb(undefined), false);
|
||||
});
|
||||
|
||||
test("parseNetstatListeners 抽出 LISTENING 行", () => {
|
||||
const out = [
|
||||
" TCP 127.0.0.1:3080 0.0.0.0:0 LISTENING 28276",
|
||||
" TCP 127.0.0.1:3080 127.0.0.1:50511 TIME_WAIT 0",
|
||||
" TCP [::1]:4000 [::]:0 LISTENING 999",
|
||||
].join("\r\n");
|
||||
const rows = parseNetstatListeners(out);
|
||||
assert.equal(rows.length, 2);
|
||||
assert.deepEqual(rows[0], { address: "127.0.0.1", port: 3080, pid: 28276 });
|
||||
assert.equal(rows[1].pid, 999);
|
||||
});
|
||||
|
||||
test("parseLsofPids 处理多行/空输出", () => {
|
||||
assert.deepEqual(parseLsofPids("1234\n5678\n"), [1234, 5678]);
|
||||
assert.deepEqual(parseLsofPids(""), []);
|
||||
assert.deepEqual(parseLsofPids(" \n"), []);
|
||||
});
|
||||
|
||||
test("waitGone 对空列表/已退出 pid 立即返回", () => {
|
||||
const t0 = Date.now();
|
||||
waitGone([]);
|
||||
waitGone([0, -1, 999999]);
|
||||
assert.ok(Date.now() - t0 < 1000, "不应为已退出的进程等到超时");
|
||||
});
|
||||
|
||||
test("preflight 侧车:记录 pid 只有仍在监听端口时才允许盲杀", () => {
|
||||
const side = fs.readFileSync(new URL("../lib/web-preflight-entry.js", import.meta.url), "utf8");
|
||||
assert.match(side, /const ownsPort = listeners\.includes\(recPid\)/);
|
||||
assert.match(side, /isDshWeb\(cmd\) \|\| \(!cmd && ownsPort\)/);
|
||||
assert.match(side, /if \(rec && !recStillAlive\)/);
|
||||
assert.match(side, /waitGone\(killedPids\)/);
|
||||
});
|
||||
Reference in new issue
Block a user