diff --git a/electron/autoLaunch.cjs b/electron/autoLaunch.cjs index 27b92e0e6..a236490dd 100644 --- a/electron/autoLaunch.cjs +++ b/electron/autoLaunch.cjs @@ -124,9 +124,8 @@ function setAutoLaunchEnabled(enabled, { try { app.setLoginItemSettings({ openAtLogin: wantEnabled, - // openAsHidden only applies on macOS App Store builds; Windows relies - // on the --hidden arg below, which main.cjs checks on cold start. - openAsHidden: wantEnabled, + // Windows relies on the --hidden arg below, which main.cjs checks on cold start. + // macOS 13+ detects login launches via wasOpenedAtLogin (openAsHidden was removed in Electron 44). path: execPath, args: wantEnabled ? [HIDDEN_LAUNCH_ARG] : [], }); diff --git a/electron/autoLaunch.test.cjs b/electron/autoLaunch.test.cjs index 4037f4075..e4db0c39c 100644 --- a/electron/autoLaunch.test.cjs +++ b/electron/autoLaunch.test.cjs @@ -204,7 +204,6 @@ test("setAutoLaunchEnabled(true) registers the hidden launch arg", () => { assert.deepEqual(capturedSettings, { openAtLogin: true, - openAsHidden: true, path: EXEC_PATH, args: [HIDDEN_LAUNCH_ARG], }); diff --git a/electron/bridges/clipboardFiles.cjs b/electron/bridges/clipboardFiles.cjs index 620649105..52a0377a3 100644 --- a/electron/bridges/clipboardFiles.cjs +++ b/electron/bridges/clipboardFiles.cjs @@ -90,6 +90,67 @@ function parseClipboardTextFilePaths(text, options = {}) { return collectExistingFiles(candidates, options); } +async function readClipboardFilesAsync(clipboard, options) { + try { + const items = await clipboard.read(); + if (!Array.isArray(items) || items.length === 0) return []; + + for (const item of items) { + const types = Array.isArray(item?.types) ? item.types : []; + if (typeof item?.getType !== "function") continue; + + // Electron 44+ maps native file references (CF_HDROP on Windows, + // NSFilenamesPboardType on macOS, text/uri-list on Linux) to text/uri-list. + for (const mime of URI_LIST_FORMATS) { + if (!types.includes(mime)) continue; + const blob = await item.getType(mime); + const text = typeof blob?.text === "function" ? await blob.text() : ""; + const files = parseClipboardTextFilePaths(text, options); + if (files.length > 0) return files; + } + + for (const rawFormat of HDROP_FORMATS) { + const osMime = `electron application/osclipboard;format="${rawFormat}"`; + if (!types.includes(osMime)) continue; + const blob = await item.getType(osMime); + if (typeof blob?.arrayBuffer !== "function") continue; + const files = collectExistingFiles( + decodeWindowsHDrop(Buffer.from(await blob.arrayBuffer())), + options, + ); + if (files.length > 0) return files; + } + + const fileNameWMime = 'electron application/osclipboard;format="FileNameW"'; + if (types.includes(fileNameWMime)) { + const blob = await item.getType(fileNameWMime); + if (typeof blob?.arrayBuffer === "function") { + const files = collectExistingFiles( + decodeWindowsFileNameW(Buffer.from(await blob.arrayBuffer())), + options, + ); + if (files.length > 0) return files; + } + } + + const fileNameMime = 'electron application/osclipboard;format="FileName"'; + if (types.includes(fileNameMime)) { + const blob = await item.getType(fileNameMime); + if (typeof blob?.arrayBuffer === "function") { + const files = collectExistingFiles( + decodeWindowsFileName(Buffer.from(await blob.arrayBuffer())), + options, + ); + if (files.length > 0) return files; + } + } + } + } catch { + return []; + } + return []; +} + function readClipboardFiles({ clipboard, fsImpl = fs, @@ -98,6 +159,12 @@ function readClipboardFiles({ if (!clipboard) return []; const options = { fsImpl, pathImpl }; + // Electron 44+ removed availableFormats/readBuffer and replaced clipboard.read + // with the async W3C ClipboardItem[] API. + if (typeof clipboard.availableFormats !== "function" && typeof clipboard.read === "function") { + return readClipboardFilesAsync(clipboard, options); + } + try { const formats = typeof clipboard.availableFormats === "function" ? clipboard.availableFormats() @@ -174,18 +241,61 @@ async function readClipboardImage({ clipboard, fsImpl = fs, tempDirBridge, + nativeImageImpl = null, now = () => new Date(), } = {}) { - if (!clipboard || typeof clipboard.readImage !== "function" || !tempDirBridge?.getTempFilePath) { + if (!clipboard || !tempDirBridge?.getTempFilePath) { return null; } try { - const image = clipboard.readImage(); - if (!image || (typeof image.isEmpty === "function" && image.isEmpty())) return null; - if (typeof image.toPNG !== "function") return null; + let content = null; + if (typeof clipboard.readImage === "function") { + const image = clipboard.readImage(); + if (!image || (typeof image.isEmpty === "function" && image.isEmpty())) return null; + if (typeof image.toPNG !== "function") return null; + content = image.toPNG(); + } else if (typeof clipboard.read === "function") { + const items = await clipboard.read(); + if (!Array.isArray(items) || items.length === 0) return null; + let matchedItem = null; + let matchedType = null; + for (const item of items) { + const types = Array.isArray(item?.types) ? item.types : []; + if (types.includes("image/png")) { + matchedItem = item; + matchedType = "image/png"; + break; + } + const fallbackType = types.find((type) => typeof type === "string" && type.startsWith("image/")); + if (fallbackType && !matchedItem) { + matchedItem = item; + matchedType = fallbackType; + } + } + if (!matchedItem || !matchedType || typeof matchedItem.getType !== "function") return null; + const blob = await matchedItem.getType(matchedType); + if (typeof blob?.arrayBuffer !== "function") return null; + const rawBuffer = Buffer.from(await blob.arrayBuffer()); + if (rawBuffer.length === 0) return null; + if (matchedType === "image/png") { + content = rawBuffer; + } else { + const ni = nativeImageImpl || (() => { + try { return require("electron").nativeImage; } catch { return null; } + })(); + if (ni && typeof ni.createFromBuffer === "function") { + const img = ni.createFromBuffer(rawBuffer); + if (!img || (typeof img.isEmpty === "function" && img.isEmpty())) return null; + content = img.toPNG(); + } else { + content = rawBuffer; + } + } + } else { + return null; + } - const content = image.toPNG(); if (!Buffer.isBuffer(content) || content.length === 0) return null; const name = createClipboardImageFileName(now()); @@ -202,6 +312,27 @@ async function readClipboardImage({ } } +async function hasClipboardImageAsync(clipboard) { + try { + if (typeof clipboard.read === "function") { + const items = await clipboard.read(); + if (Array.isArray(items)) { + return items.some( + (item) => Array.isArray(item?.types) + && item.types.some((type) => typeof type === "string" && type.startsWith("image/")), + ); + } + } + if (typeof clipboard.has === "function") { + if (await clipboard.has("image/png")) return true; + if (await clipboard.has("image/jpeg")) return true; + } + } catch { + return false; + } + return false; +} + /** * Peek whether the OS clipboard currently holds an image without writing a * temp file. Used by local terminal paste to decide whether to forward Ctrl+V @@ -209,7 +340,13 @@ async function readClipboardImage({ * turns the chord into a clipboard paste event. */ function hasClipboardImage({ clipboard } = {}) { - if (!clipboard || typeof clipboard.readImage !== "function") return false; + if (!clipboard) return false; + if (typeof clipboard.readImage !== "function") { + if (typeof clipboard.read === "function" || typeof clipboard.has === "function") { + return hasClipboardImageAsync(clipboard); + } + return false; + } try { if (typeof clipboard.availableFormats === "function") { try { diff --git a/electron/bridges/clipboardFiles.test.cjs b/electron/bridges/clipboardFiles.test.cjs index 119263fd9..74100a2cc 100644 --- a/electron/bridges/clipboardFiles.test.cjs +++ b/electron/bridges/clipboardFiles.test.cjs @@ -305,3 +305,64 @@ test("hasClipboardImage is false when clipboard API is unavailable", () => { assert.equal(hasClipboardImage({ clipboard: null }), false); assert.equal(hasClipboardImage({ clipboard: {} }), false); }); + +test("readClipboardFiles supports Electron 44+ async W3C clipboard.read() text/uri-list", async () => { + const fsImpl = createFs({ + "/Users/me/report.pdf": "file", + "/Users/me/docs": "directory", + }); + const clipboard = { + read: async () => [ + { + types: ["text/uri-list"], + getType: async (type) => { + assert.equal(type, "text/uri-list"); + return new Blob(["file:///Users/me/report.pdf\r\nfile:///Users/me/docs\r\n"]); + }, + }, + ], + }; + + const files = await readClipboardFiles({ clipboard, fsImpl, pathImpl: require("node:path") }); + assert.deepEqual(files, [ + { path: "/Users/me/report.pdf", name: "report.pdf", isDirectory: false, size: 42 }, + { path: "/Users/me/docs", name: "docs", isDirectory: true, size: 0 }, + ]); +}); + +test("readClipboardImage and hasClipboardImage support Electron 44+ async W3C clipboard.read()", async () => { + const pngBytes = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); + const written = []; + const clipboard = { + read: async () => [ + { + types: ["text/plain", "image/png"], + getType: async (type) => { + assert.equal(type, "image/png"); + return new Blob([pngBytes], { type: "image/png" }); + }, + }, + ], + }; + + assert.equal(await hasClipboardImage({ clipboard }), true); + const saved = await readClipboardImage({ + clipboard, + fsImpl: { + promises: { + writeFile: async (filePath, buf) => { + written.push({ filePath, buf }); + }, + }, + }, + tempDirBridge: { + getTempFilePath: (name) => `/tmp/${name}`, + }, + now: () => new Date(2026, 10, 27, 12, 0, 0, 5), + }); + assert.equal(saved.mediaType, "image/png"); + assert.equal(saved.size, pngBytes.length); + assert.equal(written.length, 1); + assert.deepEqual(written[0].buf, pngBytes); +}); + diff --git a/electron/main/registerBridges.cjs b/electron/main/registerBridges.cjs index 4dcc38433..ba7aded39 100644 --- a/electron/main/registerBridges.cjs +++ b/electron/main/registerBridges.cjs @@ -402,7 +402,7 @@ function createBridgeRegistrar(context) { const owner = electronModule.BrowserWindow.fromWebContents(event.sender); const result = await electronModule.dialog.showOpenDialog(owner || undefined, { title: "Select file to import", - properties: ["openFile", "showHiddenFiles"], + properties: process.platform === "linux" ? ["openFile"] : ["openFile", "showHiddenFiles"], filters: [{ name: "All Files", extensions: ["*"] }], }); return result.canceled ? null : result.filePaths[0] ?? null; @@ -1180,7 +1180,8 @@ function createBridgeRegistrar(context) { // Clipboard helpers for renderer fallback paths (e.g. Monaco paste in Electron) ipcMain.handle("netcatty:clipboard:readText", async () => { try { - return clipboard?.readText?.() || ""; + const text = await clipboard?.readText?.(); + return typeof text === "string" ? text : ""; } catch { return ""; } @@ -1189,7 +1190,7 @@ function createBridgeRegistrar(context) { ipcMain.handle("netcatty:clipboard:writeText", async (_event, text) => { try { if (typeof clipboard?.writeText !== "function") return false; - clipboard.writeText(typeof text === "string" ? text : ""); + await clipboard.writeText(typeof text === "string" ? text : ""); return true; } catch { return false; @@ -1197,15 +1198,15 @@ function createBridgeRegistrar(context) { }); ipcMain.handle("netcatty:clipboard:readFiles", async () => { - return readClipboardFiles({ clipboard, fsImpl: fs, pathImpl: path }); + return await readClipboardFiles({ clipboard, fsImpl: fs, pathImpl: path }); }); ipcMain.handle("netcatty:clipboard:readImage", async () => { - return readClipboardImage({ clipboard, fsImpl: fs, tempDirBridge }); + return await readClipboardImage({ clipboard, fsImpl: fs, tempDirBridge }); }); ipcMain.handle("netcatty:clipboard:hasImage", async () => { - return hasClipboardImage({ clipboard }); + return await hasClipboardImage({ clipboard }); }); // Select an application from system file picker @@ -1288,7 +1289,7 @@ function createBridgeRegistrar(context) { title: title || "Select File", defaultPath: defaultPath || os.homedir(), filters: filters || [{ name: "All Files", extensions: ["*"] }], - properties: ["openFile", "showHiddenFiles"], + properties: process.platform === "linux" ? ["openFile"] : ["openFile", "showHiddenFiles"], }); if (result.canceled || !result.filePaths.length) { diff --git a/electron/plugins/hostInteractionService.cjs b/electron/plugins/hostInteractionService.cjs index 74bb8f5cb..d743f2ac8 100644 --- a/electron/plugins/hostInteractionService.cjs +++ b/electron/plugins/hostInteractionService.cjs @@ -631,8 +631,8 @@ class PluginHostInteractionService { await context.assertActive(); this.#assertWindow(sender); const entry = { context, params }; this.#audit(entry, "completed"); - if (params.kind === "clipboard.write") { this.clipboard.writeText(params.text); return { cancelled: false, value: true }; } - const text = assertTextBytes(this.clipboard.readText(), LIMITS.clipboardBytes); + if (params.kind === "clipboard.write") { await this.clipboard.writeText(params.text); return { cancelled: false, value: true }; } + const text = assertTextBytes(await this.clipboard.readText(), LIMITS.clipboardBytes); this.quotaManager.chargeBytes(context.runtimeId, "clipboard", Buffer.byteLength(text)); await context.assertActive(); return { cancelled: false, value: text }; @@ -653,14 +653,36 @@ class PluginHostInteractionService { if (terminalTarget) entry.terminalWatch = this.terminalSelection.watch(); context.signal?.addEventListener("abort", entry.onAbort, { once: true }); if (context.signal?.aborted) { entry.onAbort(); return; } - try { - if (params.kind === "clipboard.read") { - entry.previewText = assertTextBytes(this.clipboard.readText(), LIMITS.clipboardBytes); - this.quotaManager.chargeBytes(context.runtimeId, "clipboard", Buffer.byteLength(entry.previewText)); - entry.phase = "preview"; + const finishPresent = (rawClipboardText) => { + if (this.pending.get(entry.id) !== entry) return; + try { + if (params.kind === "clipboard.read") { + entry.previewText = assertTextBytes(rawClipboardText, LIMITS.clipboardBytes); + this.quotaManager.chargeBytes(context.runtimeId, "clipboard", Buffer.byteLength(entry.previewText)); + entry.phase = "preview"; + } + this.#present(entry); + } catch { this.#finish(entry, null, fail("unavailable", "Host interaction window is unavailable")); } + }; + if (params.kind === "clipboard.read") { + let readResult; + try { readResult = this.clipboard.readText(); } + catch { this.#finish(entry, null, fail("unavailable", "Host interaction window is unavailable")); return; } + if (readResult && typeof readResult.then === "function") { + readResult.then( + (text) => { + if (this.pending.get(entry.id) !== entry) return; + if (context.signal?.aborted) { entry.onAbort(); return; } + finishPresent(text); + }, + () => { this.#finish(entry, null, fail("unavailable", "Host interaction window is unavailable")); }, + ); + return; } - this.#present(entry); - } catch { this.#finish(entry, null, fail("unavailable", "Host interaction window is unavailable")); } + finishPresent(readResult); + return; + } + finishPresent(null); }); } @@ -730,7 +752,7 @@ class PluginHostInteractionService { if (response.action !== "preview") throw fail("invalidArgument", "Clipboard sharing requires a preview action"); this.#audit(entry, "preview-confirmed"); let rawText; - try { rawText = this.clipboard.readText(); } + try { rawText = await this.clipboard.readText(); } catch { throw fail("unavailable", "Host clipboard text is unavailable"); } const text = assertTextBytes(rawText, LIMITS.clipboardBytes); this.quotaManager.chargeBytes(entry.context.runtimeId, "clipboard", Buffer.byteLength(text)); @@ -758,7 +780,7 @@ class PluginHostInteractionService { value = Object.freeze({ text: entry.previewText, ...this.terminalSelection.issue(entry.terminalTarget, entry.context) }); } if (entry.params.kind === "clipboard.write") { - try { this.clipboard.writeText(entry.params.text); } + try { await this.clipboard.writeText(entry.params.text); } catch { throw fail("unavailable", "Host clipboard write failed"); } } return this.#finish(entry, { cancelled: false, value }); diff --git a/electron/preload/stageUploadFile.cjs b/electron/preload/stageUploadFile.cjs index d72668bc1..4d4ebd97a 100644 --- a/electron/preload/stageUploadFile.cjs +++ b/electron/preload/stageUploadFile.cjs @@ -28,7 +28,7 @@ async function stageRendererFileToTemp(file, localPath, fsImpl, signal = null) { if (signal?.aborted) throw cancellationError(); if (done) break; if (!value) continue; - const chunk = Buffer.from(value.buffer, value.byteOffset, value.byteLength); + const chunk = new Uint8Array(value.buffer, value.byteOffset, value.byteLength); let chunkOffset = 0; while (chunkOffset < chunk.length) { if (signal?.aborted) throw cancellationError(); diff --git a/package-lock.json b/package-lock.json index 5b0ccc264..2842cbc53 100644 --- a/package-lock.json +++ b/package-lock.json @@ -95,7 +95,7 @@ "clsx": "2.1.1", "concurrently": "^9.2.1", "cross-env": "^10.1.0", - "electron": "^42.11.12", + "electron": "^45.5.0", "electron-builder": "^26.15.2", "esbuild": "0.27.2", "eslint": "^9.39.1", @@ -11803,9 +11803,9 @@ } }, "node_modules/electron": { - "version": "42.11.12", - "resolved": "https://registry.npmjs.org/electron/-/electron-42.11.12.tgz", - "integrity": "sha512-fHlCHh1uqfvWykQUq8eoSoB5q1gej7z/KKd3m7rqEEhHhihm7qz/EvBIkDat7vQoIq3kGTJcrskLbJ6UFkYGyg==", + "version": "45.5.0", + "resolved": "https://registry.npmjs.org/electron/-/electron-45.5.0.tgz", + "integrity": "sha512-rBQ0Czu1Qrtrs6Fygq7Qtfcgkhpt79SylMkaFb1AhtXItdxh0Wq3ERQoPfBYECYf+FxptAiXKCZ7p+H/39CWIA==", "dev": true, "license": "MIT", "dependencies": { @@ -16770,9 +16770,9 @@ "license": "MIT" }, "node_modules/node-abi": { - "version": "4.31.0", - "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-4.31.0.tgz", - "integrity": "sha512-Erq5w/t3syw3s4sDsUaX4QttIdBPsGKTT1DTRsCkTonGggczhlDKm/wDX3o+HPJpQ41EjXCbcmXf0tgr5YZJXw==", + "version": "4.43.0", + "resolved": "https://registry.npmjs.org/node-abi/-/node-abi-4.43.0.tgz", + "integrity": "sha512-sey5XPoGJ2VBCcwtHhvwSbMMkYvXyvZkn+FjHsoFij3bcWjJRN4+3Zt7GsZOkgZ8J0maJFCWsYtralZDJVQncg==", "devOptional": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index c099816e7..2a05e7afc 100644 --- a/package.json +++ b/package.json @@ -164,7 +164,7 @@ "clsx": "2.1.1", "concurrently": "^9.2.1", "cross-env": "^10.1.0", - "electron": "^42.11.12", + "electron": "^45.5.0", "electron-builder": "^26.15.2", "esbuild": "0.27.2", "eslint": "^9.39.1", @@ -210,7 +210,7 @@ "overrides": { "cpu-features": "npm:empty-npm-package@1.0.0", "axios": "1.13.5", - "node-abi": "4.31.0", + "node-abi": "4.43.0", "@electron/asar": "4.2.0" } }