diff --git a/electron/bridges/terminalBridge.bareMoshClient.test.cjs b/electron/bridges/terminalBridge.bareMoshClient.test.cjs index 43e78198d..9a8fa9d26 100644 --- a/electron/bridges/terminalBridge.bareMoshClient.test.cjs +++ b/electron/bridges/terminalBridge.bareMoshClient.test.cjs @@ -3,6 +3,7 @@ const assert = require("node:assert/strict"); const fs = require("node:fs"); const os = require("node:os"); const path = require("node:path"); +const { StringDecoder } = require("node:string_decoder"); const { addBundledMoshDllPath, @@ -10,6 +11,7 @@ const { addBundledMoshTerminfoEnv, resolveBareMoshClient, } = require("./terminalBridge.cjs"); +const { createMoshSessionApi } = require("./terminalBridge/moshSession.cjs"); function makeTmp() { return fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-mosh-resolve-")); @@ -221,6 +223,26 @@ test("Windows mosh runtime env includes DLL path and terminfo", () => { assert.equal(env.TERMINFO_DIRS, terminfo); }); +test("mosh UTF-8 decoder preserves fragmented Chinese output", () => { + const { createMoshUtf8Decoder } = createMoshSessionApi({ + StringDecoder, + Buffer, + }); + const decode = createMoshUtf8Decoder(); + const fixture = Buffer.from("mosh: 连接恢复,终端输出正常\n", "utf8"); + const chunks = [ + fixture.subarray(0, 9), + fixture.subarray(9, 11), + fixture.subarray(11, 17), + fixture.subarray(17), + ]; + + const decoded = chunks.map((chunk) => decode(chunk)).join(""); + + assert.equal(decoded, "mosh: 连接恢复,终端输出正常\n"); + assert.equal(decoded.includes("\uFFFD"), false); +}); + test("removed Mosh client detection APIs are not exposed to the renderer", () => { const bridgeSource = fs.readFileSync(path.join(__dirname, "terminalBridge.cjs"), "utf8"); const preloadSource = fs.readFileSync(path.join(__dirname, "..", "preload.cjs"), "utf8"); diff --git a/electron/bridges/terminalBridge.cjs b/electron/bridges/terminalBridge.cjs index 3b0e21ec1..c0500eed6 100644 --- a/electron/bridges/terminalBridge.cjs +++ b/electron/bridges/terminalBridge.cjs @@ -557,6 +557,7 @@ const { addBundledMoshDllPath, addBundledMoshTerminfoEnv, addBundledMoshRuntimeEnv, + createMoshUtf8Decoder, buildMoshSshAuthArgs, cleanupMoshAuthTempFiles, startMoshSessionViaHandshake, @@ -1536,6 +1537,7 @@ module.exports = { addBundledMoshDllPath, addBundledMoshTerminfoEnv, addBundledMoshRuntimeEnv, + createMoshUtf8Decoder, startEtSession, execOnEtSession, bundledEtClient, diff --git a/electron/bridges/terminalBridge/moshSession.cjs b/electron/bridges/terminalBridge/moshSession.cjs index b2cb387c4..22b679edc 100644 --- a/electron/bridges/terminalBridge/moshSession.cjs +++ b/electron/bridges/terminalBridge/moshSession.cjs @@ -142,6 +142,15 @@ function createMoshSessionApi(ctx) { addBundledMoshTerminfoEnv(env, bareClient, opts); return env; } + + function createMoshUtf8Decoder() { + const decoder = new StringDecoder("utf8"); + return (chunk) => { + if (Buffer.isBuffer(chunk)) return decoder.write(chunk); + if (chunk instanceof Uint8Array) return decoder.write(Buffer.from(chunk)); + return chunk == null ? "" : String(chunk); + }; + } function stripMoshPromptControls(text) { // eslint-disable-next-line no-control-regex @@ -619,9 +628,11 @@ function createMoshSessionApi(ctx) { sentry.consume(data); }); } else { + const decodeMoshOutput = createMoshUtf8Decoder(); mcPty.onData((data) => { if (!shouldProcessSessionOutput(session)) return; - const str = data.toString("utf8"); + const str = decodeMoshOutput(data); + if (!str) return; trackSessionIdlePrompt(session, str); bufferData(str); sessionLogStreamManager.appendData(sessionId, str); @@ -700,6 +711,7 @@ function createMoshSessionApi(ctx) { addBundledMoshDllPath, addBundledMoshTerminfoEnv, addBundledMoshRuntimeEnv, + createMoshUtf8Decoder, buildMoshSshAuthArgs, cleanupMoshAuthTempFiles, startMoshSessionViaHandshake, diff --git a/scripts/fetch-mosh-binaries.cjs b/scripts/fetch-mosh-binaries.cjs index 6a4f65af6..637e6f286 100755 --- a/scripts/fetch-mosh-binaries.cjs +++ b/scripts/fetch-mosh-binaries.cjs @@ -23,11 +23,9 @@ // MOSH_BIN_RES_DIR — override output dir for tests. // MOSH_BIN_ALLOW_UNVERIFIED=true — explicit local escape hatch for mirrors // without SHA256SUMS. Never use for release builds. -// MOSH_BIN_FORCE_WINDOWS_CYGWIN=true — debug escape hatch for the upstream -// Cygwin Windows bundle. The default Windows x64 asset -// is the FluentTerminal-pinned standalone client because -// the current Cygwin build clears the terminal and never -// renders remote output on Windows. +// MOSH_BIN_FORCE_WINDOWS_CYGWIN=true — legacy debug knob kept for older +// automation. Windows now prefers the released bundle +// with its runtime helpers when SHA256SUMS lists it. // MOSH_BIN_WINDOWS_LEGACY_URL / MOSH_BIN_WINDOWS_LEGACY_SHA256 — test/mirror // overrides for that pinned Windows fallback. @@ -54,10 +52,8 @@ const WINDOWS_LEGACY_FLUENT_MOSH_CLIENT = { // Using flat names in the release for SHA256SUMS readability, then // fanning out into platform-arch subdirs locally. // -// Linux/macOS targets are tar.gz bundles containing the binary plus the -// runtime helpers each platform needs. Windows x64 defaults to the -// SHA256-pinned FluentTerminal standalone exe because the tested Cygwin -// bundle clears the terminal and never renders remote output on Windows. +// Linux/macOS/Windows bundle targets are tar.gz archives containing the +// binary plus the runtime helpers each platform needs. // Bundling terminfo lets bundled Posix mosh-client builds work on // minimal hosts that don't have a // system ncurses-base — see issue #890. @@ -87,7 +83,6 @@ const TARGETS = [ platform: "win32", arch: "x64", file: "mosh-client-win32-x64.tar.gz", localDir: "win32-x64", extract: "tar.gz", legacy: WINDOWS_LEGACY_FLUENT_MOSH_CLIENT, - preferLegacy: true, }, ]; @@ -115,7 +110,12 @@ function selectReleaseAsset(target, sums, opts = {}) { if (sums.size === 0) return primary; if (sums.has(target.file)) return primary; if (sums.has(target.legacy.file)) { - return applyReleaseAssetOverrides({ file: target.legacy.file, local: target.legacy.local }, opts); + const expected = sums.get(target.legacy.file); + const fallback = applyReleaseAssetOverrides(target.legacy, opts); + if (fallback.id && fallback.sha256 && expected !== fallback.sha256) { + return fallback; + } + return { file: target.legacy.file, local: target.legacy.local, sha256: expected }; } return primary; } diff --git a/scripts/fetch-mosh-binaries.test.cjs b/scripts/fetch-mosh-binaries.test.cjs index 458ed82fd..e87ce570e 100644 --- a/scripts/fetch-mosh-binaries.test.cjs +++ b/scripts/fetch-mosh-binaries.test.cjs @@ -328,7 +328,7 @@ test("selectReleaseAsset stays on the primary when SHA256SUMS is empty (unverifi assert.equal(selectReleaseAsset(target, new Map()).file, "mosh-client-linux-x64.tar.gz"); }); -test("selectReleaseAsset prefers the pinned Windows standalone client by default", () => { +test("selectReleaseAsset prefers the released Windows bundle by default", () => { const target = { platform: "win32", arch: "x64", file: "mosh-client-win32-x64.tar.gz", localDir: "win32-x64", extract: "tar.gz", @@ -339,15 +339,14 @@ test("selectReleaseAsset prefers the pinned Windows standalone client by default url: "https://example.test/mosh-client.exe", sha256: "abc", }, - preferLegacy: true, }; const sums = new Map([["mosh-client-win32-x64.tar.gz", "def"]]); - assert.equal(selectReleaseAsset(target, sums).file, "mosh-client-win32-x64.exe"); + assert.equal(selectReleaseAsset(target, sums).file, "mosh-client-win32-x64.tar.gz"); assert.equal(selectReleaseAsset(target, sums, { forceWindowsCygwin: true }).file, "mosh-client-win32-x64.tar.gz"); }); -test("selectReleaseAsset uses the released Windows standalone asset when published", () => { +test("selectReleaseAsset falls back to the Windows standalone asset when the bundle is absent", () => { const target = { platform: "win32", arch: "x64", file: "mosh-client-win32-x64.tar.gz", localDir: "win32-x64", extract: "tar.gz", @@ -358,7 +357,6 @@ test("selectReleaseAsset uses the released Windows standalone asset when publish url: "https://example.test/mosh-client.exe", sha256: "abc", }, - preferLegacy: true, }; const asset = selectReleaseAsset(target, new Map([["mosh-client-win32-x64.exe", "abc"]])); @@ -379,7 +377,6 @@ test("selectReleaseAsset ignores a released Windows asset when its checksum is n url: "https://example.test/mosh-client.exe", sha256: "abc", }, - preferLegacy: true, }; const asset = selectReleaseAsset(target, new Map([["mosh-client-win32-x64.exe", "def"]])); @@ -389,18 +386,18 @@ test("selectReleaseAsset ignores a released Windows asset when its checksum is n assert.equal(asset.sha256, "abc"); }); -test("fetch-mosh-binaries downloads the pinned Windows standalone client", async (t) => { +test("fetch-mosh-binaries downloads the released Windows bundle by default", async (t) => { const resDir = path.join(makeTmp(t), "resources", "mosh"); - const flat = Buffer.from("working-windows-standalone"); - fs.mkdirSync(path.join(resDir, "win32-x64", "mosh-client-win32-x64-dlls"), { recursive: true }); - fs.mkdirSync(path.join(resDir, "win32-x64", "terminfo", "78"), { recursive: true }); - fs.writeFileSync(path.join(resDir, "win32-x64", "mosh-client-win32-x64-dlls", "cygwin1.dll"), "stale"); - fs.writeFileSync(path.join(resDir, "win32-x64", "terminfo", "78", "xterm-256color"), "stale"); - const baseUrl = await serveAssets(t, { - SHA256SUMS: "", + const tar = makeTarGz(t, { + "mosh-client.exe": "exe", + "mosh-client-win32-x64-dlls/cygwin1.dll": "dll", + "terminfo/x/xterm-256color": "terminfo", }); - const legacyBaseUrl = await serveAssets(t, { - "mosh-client-win32-x64.exe": flat, + fs.mkdirSync(path.join(resDir, "win32-x64", "mosh-client-win32-x64-dlls"), { recursive: true }); + fs.writeFileSync(path.join(resDir, "win32-x64", "mosh-client-win32-x64-dlls", "cygwin1.dll"), "stale"); + const baseUrl = await serveAssets(t, { + "mosh-client-win32-x64.tar.gz": tar, + SHA256SUMS: `${sha256(tar)} mosh-client-win32-x64.tar.gz\n`, }); await execFileAsync(process.execPath, [script, "--platform=win32", "--arch=x64"], { @@ -409,18 +406,14 @@ test("fetch-mosh-binaries downloads the pinned Windows standalone client", async MOSH_BIN_RELEASE: "test", MOSH_BIN_BASE_URL: baseUrl, MOSH_BIN_RES_DIR: resDir, - MOSH_BIN_WINDOWS_LEGACY_URL: `${legacyBaseUrl}/mosh-client-win32-x64.exe`, - MOSH_BIN_WINDOWS_LEGACY_SHA256: sha256(flat), CI: "true", }, stdio: "pipe", }); - const dest = path.join(resDir, "win32-x64", "mosh-client.exe"); - assert.equal(fs.existsSync(dest), true); - assert.equal(fs.readFileSync(dest, "utf8"), "working-windows-standalone"); - assert.equal(fs.existsSync(path.join(resDir, "win32-x64", "mosh-client-win32-x64-dlls")), false); - assert.equal(fs.existsSync(path.join(resDir, "win32-x64", "terminfo")), false); + assert.equal(fs.readFileSync(path.join(resDir, "win32-x64", "mosh-client.exe"), "utf8"), "exe"); + assert.equal(fs.readFileSync(path.join(resDir, "win32-x64", "mosh-client-win32-x64-dlls", "cygwin1.dll"), "utf8"), "dll"); + assert.equal(fs.readFileSync(path.join(resDir, "win32-x64", "terminfo", "x", "xterm-256color"), "utf8"), "terminfo"); }); test("fetch-mosh-binaries falls back to the legacy flat asset for older releases", async (t) => {