diff --git a/components/lazyLoadedSurfaces.test.ts b/components/lazyLoadedSurfaces.test.ts index 00a7d9dca..db416894c 100644 --- a/components/lazyLoadedSurfaces.test.ts +++ b/components/lazyLoadedSurfaces.test.ts @@ -37,7 +37,7 @@ test("Windows settings close uses a plain button without a primary tooltip", () const source = readFileSync(new URL("./SettingsPage.tsx", import.meta.url), "utf8"); const closeIndex = source.indexOf('aria-label={t("common.close")}'); assert.notEqual(closeIndex, -1); - // The old Tooltip + TooltipContent("关闭") rendered as a stuck blue chip over the chrome. + // The old Tooltip + TooltipContent("Close") rendered as a stuck blue chip over the chrome. assert.doesNotMatch( source.slice(Math.max(0, closeIndex - 200), closeIndex + 400), /TooltipContent|TooltipTrigger/, diff --git a/electron/bridges/terminalBridge.moshHandshakeSession.test.cjs b/electron/bridges/terminalBridge.moshHandshakeSession.test.cjs index e860e08a7..eaa8012e3 100644 --- a/electron/bridges/terminalBridge.moshHandshakeSession.test.cjs +++ b/electron/bridges/terminalBridge.moshHandshakeSession.test.cjs @@ -108,6 +108,8 @@ function makeHarness(t) { return { bridge, + tmp, + sshPath, sessions, sent, spawns, @@ -134,6 +136,39 @@ test("startMoshSession handshake path returns the same shape as the legacy path" assert.deepEqual(result, { sessionId: "mosh-test-session" }); }); +test("Mosh PTYs explicitly enable bundled ConPTY clear support only on Windows", async (t) => { + const platformDescriptor = Object.getOwnPropertyDescriptor(process, "platform"); + + const startAndSwap = async (platform) => { + const h = makeHarness(t); + Object.defineProperty(process, "platform", { ...platformDescriptor, value: platform }); + + await h.bridge.startMoshSession(h.event, h.options, { + moshClientLookup: h.lookupOpts, + findExecutable: () => h.sshPath, + }); + h.spawns[0].emitData( + "MOSH IP 203.0.113.8\r\nMOSH CONNECT 60002 ABCDEFGHIJKLMNOPQRSTUV==\r\n", + ); + h.spawns[0].emitExit({ exitCode: 0, signal: 0 }); + return h.spawns; + }; + + try { + const windowsSpawns = await startAndSwap("win32"); + assert.equal(windowsSpawns.length, 2); + assert.equal(windowsSpawns[0].opts.useConptyDll, true); + assert.equal(windowsSpawns[1].opts.useConptyDll, true); + + const linuxSpawns = await startAndSwap("linux"); + assert.equal(linuxSpawns.length, 2); + assert.equal(linuxSpawns[0].opts.useConptyDll, false); + assert.equal(linuxSpawns[1].opts.useConptyDll, false); + } finally { + Object.defineProperty(process, "platform", platformDescriptor); + } +}); + test("startMoshSession offers all locale settings to mosh-server without exporting them through SSH", async (t) => { const h = makeHarness(t); h.options.env = { diff --git a/electron/bridges/terminalBridge/etSession.cjs b/electron/bridges/terminalBridge/etSession.cjs index 7cc9d2ed6..527874122 100644 --- a/electron/bridges/terminalBridge/etSession.cjs +++ b/electron/bridges/terminalBridge/etSession.cjs @@ -963,6 +963,7 @@ main(); env, cwd: os.homedir(), encoding: null, // Return Buffer for ZMODEM binary support + useConptyDll: process.platform === "win32", }); const session = { diff --git a/electron/bridges/terminalBridge/etSession.test.cjs b/electron/bridges/terminalBridge/etSession.test.cjs index 8fb382475..7e01c2f3d 100644 --- a/electron/bridges/terminalBridge/etSession.test.cjs +++ b/electron/bridges/terminalBridge/etSession.test.cjs @@ -110,6 +110,43 @@ test("startEtSession preserves discovered automatic identities for host informat assert.equal(sessions.get("sess-auto-stats").etStatsAuth.authMethod, "auto"); }); +test("ET PTY explicitly enables bundled ConPTY clear support only on Windows", async (t) => { + const spawnForPlatform = async (platform) => { + let spawnOptions = null; + const processMock = Object.create(process); + Object.defineProperty(processMock, "platform", { value: platform }); + const proc = { + onData() {}, + onExit() {}, + write() {}, + }; + const { api } = makeApi(t, { + process: processMock, + bundledEtClient: () => "/fake/et", + pty: { + spawn: (_command, _args, options) => { + spawnOptions = options; + return proc; + }, + }, + electronModule: { webContents: { fromId: () => null } }, + openTerminalOutputSession: () => {}, + selectZmodemUploadFiles: null, + selectZmodemDownloadDirectory: null, + }); + + await api.startEtSession({ sender: { id: 7 } }, { + sessionId: `sess-conpty-clear-${platform}`, + hostname: "host.example", + username: "alice", + }); + return spawnOptions; + }; + + assert.equal((await spawnForPlatform("win32")).useConptyDll, true); + assert.equal((await spawnForPlatform("linux")).useConptyDll, false); +}); + test("explicitly closed ET sessions do not emit a second exit event", async (t) => { let onExit = null; const sent = []; diff --git a/electron/bridges/terminalBridge/moshSession.cjs b/electron/bridges/terminalBridge/moshSession.cjs index 09eb81e15..6d8c29809 100644 --- a/electron/bridges/terminalBridge/moshSession.cjs +++ b/electron/bridges/terminalBridge/moshSession.cjs @@ -427,6 +427,7 @@ function createMoshSessionApi(ctx) { env: sshEnv, cwd: os.homedir(), encoding: null, + useConptyDll: process.platform === "win32", }); } catch (err) { cleanupMoshAuthTempFiles(moshAuth.tempFiles); @@ -674,6 +675,7 @@ function createMoshSessionApi(ctx) { env, cwd: os.homedir(), encoding: null, + useConptyDll: process.platform === "win32", }); // Atomic swap — writeToSession / resizeSession both read @@ -843,7 +845,7 @@ function createMoshSessionApi(ctx) { const sshExe = moshHandshake.resolveSshExecutable({ findExecutable: (name) => ( process.platform === "win32" - ? findExecutable(name, { pathOverride: mergedPathForResolution }) + ? (opts.findExecutable || findExecutable)(name, { pathOverride: mergedPathForResolution }) : resolvePosixExecutable(name, { pathOverride: mergedPathForResolution }) ), fileExists: (p) => isExecutableFile(p) || fs.existsSync(p), diff --git a/package.json b/package.json index 5883d3ba9..45177140c 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "pack:linux": "npm run build && cross-env NODE_OPTIONS=--disable-warning=DEP0190 electron-builder --config electron-builder.config.cjs --linux --publish=never", "pack:linux-x64": "npm run build && cross-env npm_config_arch=x64 NODE_OPTIONS=--disable-warning=DEP0190 electron-builder --config electron-builder.config.cjs --linux --x64 --publish=never", "pack:linux-arm64": "npm run build && cross-env npm_config_arch=arm64 NODE_OPTIONS=--disable-warning=DEP0190 electron-builder --config electron-builder.config.cjs --linux --arm64 --publish=never", - "postinstall": "electron-builder install-app-deps && patch-package && node scripts/patch-xterm-webgl-atlas.cjs", + "postinstall": "patch-package && electron-builder install-app-deps && node scripts/rebuildPatchedNodePty.cjs && node scripts/patch-xterm-webgl-atlas.cjs", "rebuild": "electron-builder install-app-deps", "tool:cli": "node electron/cli/netcatty-tool-cli.cjs", "generate:capability-tools": "node scripts/generate-capability-tools.cjs", diff --git a/patches/node-pty+1.1.0.patch b/patches/node-pty+1.1.0.patch new file mode 100644 index 000000000..3e95413cf --- /dev/null +++ b/patches/node-pty+1.1.0.patch @@ -0,0 +1,17 @@ +diff --git a/node_modules/node-pty/src/win/conpty.cc b/node_modules/node-pty/src/win/conpty.cc +index 7b286d3..5423c8e 100644 +--- a/node_modules/node-pty/src/win/conpty.cc ++++ b/node_modules/node-pty/src/win/conpty.cc +@@ -35 +35 @@ typedef HRESULT (__stdcall *PFNRESIZEPSEUDOCONSOLE)(HPCON hpc, COORD newSize); +-typedef HRESULT (__stdcall *PFNCLEARPSEUDOCONSOLE)(HPCON hpc); ++typedef HRESULT (__stdcall *PFNCLEARPSEUDOCONSOLE)(HPCON hpc, BOOL keepCursorRow); +@@ -526 +526 @@ static Napi::Value PtyClear(const Napi::CallbackInfo& info) { +- pfnClearPseudoConsole(handle->hpc); ++ pfnClearPseudoConsole(handle->hpc, TRUE); +diff --git a/node_modules/node-pty/src/win/conpty.h b/node_modules/node-pty/src/win/conpty.h +index 4cef31c..4f03a0c 100644 +--- a/node_modules/node-pty/src/win/conpty.h ++++ b/node_modules/node-pty/src/win/conpty.h +@@ -33 +33 @@ CONPTY_EXPORT HRESULT WINAPI ConptyResizePseudoConsole(HPCON hPC, COORD size); +-CONPTY_EXPORT HRESULT WINAPI ConptyClearPseudoConsole(HPCON hPC); ++CONPTY_EXPORT HRESULT WINAPI ConptyClearPseudoConsole(HPCON hPC, BOOL keepCursorRow); diff --git a/scripts/afterPackMacUuid.cjs b/scripts/afterPackMacUuid.cjs index ef1801497..4050a2b23 100644 --- a/scripts/afterPackMacUuid.cjs +++ b/scripts/afterPackMacUuid.cjs @@ -20,6 +20,10 @@ const fs = require("node:fs"); const path = require("node:path"); const crypto = require("node:crypto"); const { execFileSync } = require("node:child_process"); +const { + copyPatchedNodePtyToPackagedApp, + rebuildPatchedNodePty, +} = require("./nodePtyConptyPatch.cjs"); const LC_UUID = 0x1b; const MH_MAGIC_64 = 0xfeedfacf; // thin 64-bit, little-endian on disk @@ -299,6 +303,17 @@ async function afterPack(context) { ); } + if (context.electronPlatformName === "win32") { + const projectDir = context.packager.appDir || context.packager.projectDir; + rebuildPatchedNodePty({ projectDir, platform: "win32", arch: context.arch }); + copyPatchedNodePtyToPackagedApp({ + projectDir, + resourcesDir: appResourcesDir(context), + }); + console.log("[afterPack] Installed patched node-pty ConPTY runtime into packaged app"); + return; + } + if (context.electronPlatformName !== "darwin") return; const appId = context.packager.appInfo.id || "com.netcatty.app"; diff --git a/scripts/beforePackCursorSdk.test.cjs b/scripts/beforePackCursorSdk.test.cjs index db8355837..b32754470 100644 --- a/scripts/beforePackCursorSdk.test.cjs +++ b/scripts/beforePackCursorSdk.test.cjs @@ -8,6 +8,10 @@ const { CURSOR_PLATFORM_PACKAGES, ensureCursorSdkPlatformPackages, } = require("./beforePackCursorSdk.cjs"); +const { + copyPatchedNodePtyToPackagedApp, + rebuildPatchedNodePty, +} = require("./nodePtyConptyPatch.cjs"); function writeJson(filePath, value) { fs.mkdirSync(path.dirname(filePath), { recursive: true }); @@ -60,3 +64,93 @@ test("ensureCursorSdkPlatformPackages is a no-op when target packages exist", (t assert.deepEqual(installed, []); assert.deepEqual(calls, []); }); + +test("Windows packaging rebuilds patched node-pty from source for the target architecture", () => { + const calls = []; + const rebuilt = rebuildPatchedNodePty({ + projectDir: "/workspace/netcatty", + platform: "win32", + arch: 3, + run: (...args) => calls.push(args), + exists: () => true, + logger: { log() {} }, + }); + + assert.equal(rebuilt, true); + assert.equal(calls.length, 2); + assert.equal(calls[0][0], process.execPath); + assert.deepEqual(calls[0][1], [ + path.join("/workspace/netcatty", "node_modules", "@electron", "rebuild", "lib", "cli.js"), + "--force", + "--build-from-source", + "--only", + "node-pty", + "--arch", + "arm64", + ]); + assert.equal(calls[0][2].cwd, "/workspace/netcatty"); + assert.equal(calls[1][0], process.execPath); + assert.equal( + calls[1][1][0], + path.join("/workspace/netcatty", "node_modules", "node-pty", "scripts", "post-install.js"), + ); + assert.equal(calls[1][2].env.npm_config_arch, "arm64"); +}); + +test("Windows packaging fails when rebuilt node-pty runtime files are incomplete", () => { + assert.throws(() => rebuildPatchedNodePty({ + projectDir: "/workspace/netcatty", + platform: "win32", + arch: 1, + run() {}, + exists: (filePath) => !filePath.endsWith("conpty.dll"), + logger: { log() {} }, + }), /Patched node-pty artifacts missing: .*conpty\.dll/); +}); + +test("non-Windows packaging keeps the prebuilt node-pty path", () => { + const calls = []; + const rebuilt = rebuildPatchedNodePty({ + projectDir: "/workspace/netcatty", + platform: "linux", + arch: 1, + run: (...args) => calls.push(args), + logger: { log() {} }, + }); + + assert.equal(rebuilt, false); + assert.deepEqual(calls, []); +}); + +test("Windows afterPack copies rebuilt ConPTY files over packaged prebuilds", () => { + const copied = []; + const made = []; + const destinations = copyPatchedNodePtyToPackagedApp({ + projectDir: "/workspace/netcatty", + resourcesDir: "/workspace/release/resources", + copy: (...args) => copied.push(args), + mkdir: (...args) => made.push(args), + }); + + assert.equal(copied.length, 3); + assert.equal(made.length, 3); + assert.equal(copied[0][0], path.join( + "/workspace/netcatty", "node_modules", "node-pty", "build", "Release", "conpty.node", + )); + assert.equal(copied[0][1], path.join( + "/workspace/release/resources", "app.asar.unpacked", "node_modules", "node-pty", "build", "Release", "conpty.node", + )); + assert.deepEqual(destinations, copied.map(([, destination]) => destination)); +}); + +test("node-pty patch matches bundled ConPTY clear ABI and preserves the cursor row", () => { + const patch = fs.readFileSync( + path.join(__dirname, "..", "patches", "node-pty+1.1.0.patch"), + "utf8", + ); + + assert.match(patch, /ConptyClearPseudoConsole\(HPCON hPC, BOOL keepCursorRow\)/); + assert.match(patch, /PFNCLEARPSEUDOCONSOLE\)\(HPCON hpc, BOOL keepCursorRow\)/); + assert.match(patch, /pfnClearPseudoConsole\(handle->hpc, TRUE\)/); + assert.doesNotMatch(patch, /node_modules\/node-pty\/build\//); +}); diff --git a/scripts/nodePtyConptyPatch.cjs b/scripts/nodePtyConptyPatch.cjs new file mode 100644 index 000000000..9bf942093 --- /dev/null +++ b/scripts/nodePtyConptyPatch.cjs @@ -0,0 +1,92 @@ +const fs = require("node:fs"); +const path = require("node:path"); +const { execFileSync } = require("node:child_process"); + +const ELECTRON_BUILDER_ARCH = { + 0: "ia32", + 1: "x64", + 2: "armv7l", + 3: "arm64", + 4: "universal", +}; + +function nodePtyArtifacts(projectDir) { + const releaseDir = path.join(projectDir, "node_modules", "node-pty", "build", "Release"); + return [ + { source: path.join(releaseDir, "conpty.node"), relative: "conpty.node" }, + { source: path.join(releaseDir, "conpty", "conpty.dll"), relative: path.join("conpty", "conpty.dll") }, + { source: path.join(releaseDir, "conpty", "OpenConsole.exe"), relative: path.join("conpty", "OpenConsole.exe") }, + ]; +} + +function rebuildPatchedNodePty({ + projectDir, + platform, + arch, + run = execFileSync, + exists = fs.existsSync, + logger = console, +}) { + if (platform !== "win32") return false; + const targetArch = typeof arch === "number" ? ELECTRON_BUILDER_ARCH[arch] : arch; + if (!targetArch || targetArch === "universal") { + throw new Error(`[nodePtyConptyPatch] Unsupported Windows architecture: ${String(arch)}`); + } + + const rebuildCli = path.join(projectDir, "node_modules", "@electron", "rebuild", "lib", "cli.js"); + logger.log(`[nodePtyConptyPatch] Rebuilding patched node-pty for Windows ${targetArch}`); + run(process.execPath, [ + rebuildCli, + "--force", + "--build-from-source", + "--only", + "node-pty", + "--arch", + targetArch, + ], { + cwd: projectDir, + stdio: "inherit", + }); + + const nodePtyDir = path.join(projectDir, "node_modules", "node-pty"); + run(process.execPath, [path.join(nodePtyDir, "scripts", "post-install.js")], { + cwd: projectDir, + stdio: "inherit", + env: { ...process.env, npm_config_arch: targetArch }, + }); + + const missingArtifacts = nodePtyArtifacts(projectDir) + .map(({ source }) => source) + .filter((filePath) => !exists(filePath)); + if (missingArtifacts.length > 0) { + throw new Error( + `[nodePtyConptyPatch] Patched node-pty artifacts missing: ${missingArtifacts.join(", ")}`, + ); + } + return true; +} + +function copyPatchedNodePtyToPackagedApp({ projectDir, resourcesDir, copy = fs.copyFileSync, mkdir = fs.mkdirSync }) { + const packagedReleaseDir = path.join( + resourcesDir, + "app.asar.unpacked", + "node_modules", + "node-pty", + "build", + "Release", + ); + const copied = []; + for (const artifact of nodePtyArtifacts(projectDir)) { + const destination = path.join(packagedReleaseDir, artifact.relative); + mkdir(path.dirname(destination), { recursive: true }); + copy(artifact.source, destination); + copied.push(destination); + } + return copied; +} + +module.exports = { + copyPatchedNodePtyToPackagedApp, + nodePtyArtifacts, + rebuildPatchedNodePty, +}; diff --git a/scripts/rebuildPatchedNodePty.cjs b/scripts/rebuildPatchedNodePty.cjs new file mode 100644 index 000000000..2be2210ad --- /dev/null +++ b/scripts/rebuildPatchedNodePty.cjs @@ -0,0 +1,7 @@ +const { rebuildPatchedNodePty } = require("./nodePtyConptyPatch.cjs"); + +rebuildPatchedNodePty({ + projectDir: process.cwd(), + platform: process.platform, + arch: process.env.npm_config_arch || process.arch, +});