release: 1.3.5 适配 dsh 0.1.2-rc.1 / Node 24 Windows 无感与首轮注入

修复 Node 24 ESM spawn 补丁无效导致的 CMD 闪窗;梁神 phase-1 保留完整 system-prompt;README 写明适配版本与变更说明。
This commit is contained in:
yang committed 2026-09-05 19:57:42 +08:00
1 parent 905383165b
commit 9d139bf474
10 files changed
+1097 -171

No files matched your search

+101 -46
View File
@@ -1,11 +1,21 @@
// Force every child_process launch on Windows to use CREATE_NO_WINDOW.
// Must load before other plugins spawn work — import this module for side effects,
// or call installHideConsole() explicitly from apply().
//
// Node 24: ESM `import { spawn } from "node:child_process"` is NOT a live
// binding to module.exports.spawn. Monkey-patching require("child_process")
// alone does nothing for DSH packages. We register an import hook that
// redirects node:child_process → ./child-process-hide.mjs (which wraps the
// builtin via process.getBuiltinModule).
import { createRequire } from "node:module";
import { createRequire, registerHooks } from "node:module";
import { pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
import path from "node:path";
const MARK = "__dshPurgeHideConsole";
const IMPORT_MARK = "dsh-purge-hide-console";
const HOOK_MARK = "__dshPurgeChildProcessHook";
function forceHideOptions(options) {
if (options == null) return { windowsHide: true };
@@ -17,7 +27,6 @@ function forceHideOptions(options) {
function wrapSpawn(orig) {
return function patchedSpawn(file, args, options) {
if (args != null && !Array.isArray(args)) {
// spawn(file, options)
return orig.call(this, file, forceHideOptions(args));
}
return orig.call(this, file, args ?? [], forceHideOptions(options));
@@ -39,7 +48,6 @@ function wrapExecFile(orig) {
return orig.call(this, file, forceHideOptions(undefined), args);
}
if (args != null && !Array.isArray(args)) {
// execFile(file, options, callback)
return orig.call(this, file, forceHideOptions(args), options);
}
if (typeof options === "function") {
@@ -58,12 +66,40 @@ function wrapFork(orig) {
};
}
/** Patch node:child_process in this process. Idempotent. */
function facadeUrl() {
return pathToFileURL(fileURLToPath(new URL("./child-process-hide.mjs", import.meta.url))).href;
}
/** Redirect ESM/CJS resolution of child_process to the windowsHide facade. */
export function installChildProcessImportHook() {
if (process.platform !== "win32") return { ok: true, skipped: "not_win32" };
if (globalThis[HOOK_MARK]) return { ok: true, already: true };
const target = facadeUrl();
registerHooks({
resolve(specifier, context, nextResolve) {
if (specifier === "node:child_process" || specifier === "child_process") {
// Do not redirect the facade's own getBuiltinModule path (it never
// resolves via this specifier). Short-circuit every importer.
return { shortCircuit: true, url: target, format: "module" };
}
return nextResolve(specifier, context);
},
});
Object.defineProperty(globalThis, HOOK_MARK, { value: true, enumerable: false });
return { ok: true, hooked: true };
}
/** Patch node:child_process in this process (CJS require path). Idempotent. */
export function installHideConsole() {
if (process.platform !== "win32") return { ok: true, skipped: "not_win32" };
const hook = installChildProcessImportHook();
const require = createRequire(import.meta.url);
const cp = require("node:child_process");
if (cp[MARK]) return { ok: true, already: true };
// Prefer builtin so we patch the real object the facade also wraps.
const cp =
typeof process.getBuiltinModule === "function"
? process.getBuiltinModule("child_process")
: require("node:child_process");
if (cp[MARK]) return { ok: true, already: true, hook };
cp.spawn = wrapSpawn(cp.spawn);
cp.spawnSync = wrapSpawn(cp.spawnSync);
@@ -74,17 +110,37 @@ export function installHideConsole() {
if (typeof cp.fork === "function") cp.fork = wrapFork(cp.fork);
Object.defineProperty(cp, MARK, { value: true, enumerable: false });
return { ok: true, patched: true };
return { ok: true, patched: true, hook };
}
/** Source written next to DSH `lib/bin.js` so the entry imports it first. */
export function hideConsoleEntrySource() {
// Keep the side file self-contained: registerHooks + wrap builtin.
// The facade lives beside this file as dsh-purge-child-process-hide.mjs
// (copied by installHideConsoleIntoBin).
return `// ${IMPORT_MARK} — auto-installed by dsh-purge; do not edit
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const cp = require("node:child_process");
import { registerHooks } from "node:module";
import { pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
const HOOK_MARK = "__dshPurgeChildProcessHook";
const MARK = ${JSON.stringify(MARK)};
if (process.platform === "win32" && !cp[MARK]) {
const facade = pathToFileURL(fileURLToPath(new URL("./dsh-purge-child-process-hide.mjs", import.meta.url))).href;
if (process.platform === "win32" && !globalThis[HOOK_MARK]) {
registerHooks({
resolve(specifier, context, nextResolve) {
if (specifier === "node:child_process" || specifier === "child_process") {
return { shortCircuit: true, url: facade, format: "module" };
}
return nextResolve(specifier, context);
},
});
Object.defineProperty(globalThis, HOOK_MARK, { value: true, enumerable: false });
}
const cp = process.getBuiltinModule("child_process");
if (process.platform === "win32" && cp && !cp[MARK]) {
const hide = (options) => {
if (options == null) return { windowsHide: true };
if (typeof options !== "object") return options;
@@ -121,67 +177,73 @@ if (process.platform === "win32" && !cp[MARK]) {
`;
}
const IMPORT_LINE = `import "./${IMPORT_MARK}.js"; // [dsh-purge] hide-console`;
function requireFs() {
return createRequire(import.meta.url)("node:fs");
}
function requirePath() {
return createRequire(import.meta.url)("node:path");
}
/**
* Resolve the @deepseek-ai/dsh package root that actually owns lib/bin.js.
* `ai_base` is often `.../dsh/node_modules/@deepseek-ai` (packages tree), which
* does NOT contain bin.js — walking up / sideways is required.
*/
export function resolveDshBinRoot(hint, { fs, path } = {}) {
export function resolveDshBinRoot(hint, { fs, path: nodePath } = {}) {
const nodeFs = fs ?? requireFs();
const nodePath = path ?? requirePath();
const p = nodePath ?? requirePath();
const tries = [];
const push = (p) => {
if (p && !tries.includes(p)) tries.push(p);
const push = (x) => {
if (x && !tries.includes(x)) tries.push(x);
};
if (hint) {
push(hint);
push(nodePath.join(hint, "dsh"));
push(nodePath.join(hint, "@deepseek-ai", "dsh"));
if (nodePath.basename(hint) === "@deepseek-ai") {
push(nodePath.dirname(hint)); // .../node_modules
push(nodePath.join(nodePath.dirname(hint), "dsh"));
push(nodePath.dirname(nodePath.dirname(hint))); // .../dsh when hint is .../dsh/node_modules/@deepseek-ai
push(p.join(hint, "dsh"));
push(p.join(hint, "@deepseek-ai", "dsh"));
if (p.basename(hint) === "@deepseek-ai") {
push(p.dirname(hint));
push(p.join(p.dirname(hint), "dsh"));
push(p.dirname(p.dirname(hint)));
}
if (nodePath.basename(hint) === "dsh") push(hint);
if (p.basename(hint) === "dsh") push(hint);
}
if (typeof process.argv[1] === "string" && /[\\/]bin\.js$/i.test(process.argv[1])) {
push(nodePath.dirname(nodePath.dirname(process.argv[1])));
push(p.dirname(p.dirname(process.argv[1])));
}
for (const root of tries) {
try {
if (nodeFs.existsSync(nodePath.join(root, "lib", "bin.js"))) return nodePath.normalize(root);
if (nodeFs.existsSync(p.join(root, "lib", "bin.js"))) return p.normalize(root);
} catch {}
}
return null;
}
const IMPORT_LINE = `import "./${IMPORT_MARK}.js"; // [dsh-purge] hide-console`;
/**
* Drop hide-console next to bin.js and ensure it is the first import.
* Returns status string for reports.
*/
export function installHideConsoleIntoBin(aiBase, { fs, path } = {}) {
export function installHideConsoleIntoBin(aiBase, { fs, path: nodePath } = {}) {
if (process.platform !== "win32") return "skipped_not_win32";
const nodeFs = fs ?? requireFs();
const nodePath = path ?? requirePath();
const root = resolveDshBinRoot(aiBase, { fs: nodeFs, path: nodePath });
const p = nodePath ?? requirePath();
const root = resolveDshBinRoot(aiBase, { fs: nodeFs, path: p });
if (!root) return "missing_bin";
const binJs = nodePath.join(root, "lib", "bin.js");
const side = nodePath.join(root, "lib", `${IMPORT_MARK}.js`);
const binJs = p.join(root, "lib", "bin.js");
const side = p.join(root, "lib", `${IMPORT_MARK}.js`);
const facadeSide = p.join(root, "lib", "dsh-purge-child-process-hide.mjs");
const facadeSrc = nodeFs.readFileSync(
fileURLToPath(new URL("./child-process-hide.mjs", import.meta.url)),
"utf8",
);
nodeFs.writeFileSync(facadeSide, facadeSrc, "utf8");
nodeFs.writeFileSync(side, hideConsoleEntrySource(), "utf8");
let text = nodeFs.readFileSync(binJs, "utf8");
if (text.includes(IMPORT_MARK) && text.includes(IMPORT_LINE.split(" //")[0])) {
// Refresh side file only
// Side + facade refreshed above; bin.js import already present.
return "already_injected";
}
// Remove stale markers then inject after shebang
text = text.replace(
/^import "\.\/dsh-purge-hide-console\.js";.*\r?\n/m,
"",
);
text = text.replace(/^import "\.\/dsh-purge-hide-console\.js";.*\r?\n/m, "");
if (/^#!/.test(text)) {
const nl = text.indexOf("\n");
text =
@@ -195,12 +257,5 @@ export function installHideConsoleIntoBin(aiBase, { fs, path } = {}) {
return "injected";
}
function requireFs() {
return createRequire(import.meta.url)("node:fs");
}
function requirePath() {
return createRequire(import.meta.url)("node:path");
}
// Side-effect install when this module is imported on win32
installHideConsole();