Merge pull request #11 from Kilig1058674225/fix/electron-host-hide-console

fix: Electron 宿主(DSH Desktop)跳过 child_process ESM 重定向
This commit is contained in:
叫我小杨同学 authored and GitHub committed 2026-09-10 16:26:26 +08:00
commit e415eb2d19
1 file changed
+14 -3
+14 -3
View File
@@ -73,6 +73,12 @@ function facadeUrl() {
/** Redirect ESM/CJS resolution of child_process to the windowsHide facade. */
export function installChildProcessImportHook() {
if (process.platform !== "win32") return { ok: true, skipped: "not_win32" };
// DSH Desktop runs on Electron. Electron’s overrideChildProcess patches
// child_process by assigning properties (e.g. exec) onto the module object,
// which throws on a frozen ESM namespace. Skip the redirect on Electron hosts.
if (typeof process.versions !== "undefined" && process.versions.electron) {
return { ok: true, skipped: "electron_host" };
}
if (globalThis[HOOK_MARK]) return { ok: true, already: true };
const target = facadeUrl();
registerHooks({
@@ -119,7 +125,6 @@ export function hideConsoleEntrySource() {
// 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 { registerHooks } from "node:module";
import { pathToFileURL } from "node:url";
import { fileURLToPath } from "node:url";
@@ -127,7 +132,13 @@ const HOOK_MARK = "__dshPurgeChildProcessHook";
const MARK = ${JSON.stringify(MARK)};
const facade = pathToFileURL(fileURLToPath(new URL("./dsh-purge-child-process-hide.mjs", import.meta.url))).href;
if (process.platform === "win32" && !globalThis[HOOK_MARK]) {
// DSH Desktop (Electron) must NOT redirect child_process to the ESM facade:
// Electron’s overrideChildProcess assigns e.g. exec onto the module object,
// and a frozen ESM namespace throws TypeError. Only the safe wrapping below runs.
const isElectron = typeof process.versions !== "undefined" && !!process.versions.electron;
if (process.platform === "win32" && !isElectron && !globalThis[HOOK_MARK]) {
const { registerHooks } = await import("node:module");
registerHooks({
resolve(specifier, context, nextResolve) {
if (specifier === "node:child_process" || specifier === "child_process") {
@@ -139,7 +150,7 @@ if (process.platform === "win32" && !globalThis[HOOK_MARK]) {
Object.defineProperty(globalThis, HOOK_MARK, { value: true, enumerable: false });
}
const cp = process.getBuiltinModule("child_process");
const cp = process.getBuiltinModule ? process.getBuiltinModule("child_process") : null;
if (process.platform === "win32" && cp && !cp[MARK]) {
const hide = (options) => {
if (options == null) return { windowsHide: true };