mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 07:32:54 +00:00
* fix(auto-update): commit app to quit before quitAndInstall on macOS (#1215) macOS in-place auto-update downloaded and unpacked the new version but never installed it: the app appeared to close, ShipIt never ran, and no restart happened. A full uninstall + reinstall did not help; only a manual DMG replace worked. Root cause is a code-level coordination bug, not the release pipeline. The published mac zips are correctly Developer-ID signed, notarized, and stapled (Team H7WS5L2ML4, consistent across 1.1.17 and 1.1.20), and latest-mac.yml is well-formed — so Squirrel.Mac signature validation passes. The failure is that quitAndInstall() drives app.quit() while two normal-quit behaviors keep the process alive: 1. the main-window close handler hides to tray when close-to-tray is enabled (it only closes when isQuitting is true), and 2. the before-quit dirty-editor guard preventDefault()s the quit for a 5s renderer round-trip. Either keeps the parent process running, so Squirrel.Mac's ShipIt helper — which waits on the parent PID to die before swapping the bundle — lands in launchd "pending spawn / on-demand-only" limbo and the service is removed without installing. This matches the reporter's diagnosis exactly ("ShipIt 没有真正启动安装器", launchd on-demand-only). Fix: before quitAndInstall fires app.quit(), mark the app as quitting for an update via windowManager.setQuittingForUpdate(true). That sets isQuitting (bypassing close-to-tray) and the before-quit handler now returns early when isQuittingForUpdate() is true (skipping the dirty-editor round-trip), so the process exits cleanly and ShipIt can run. The same fix also covers the latent Windows NSIS case where close-to-tray would block an in-place update. If the install never actually quits the app (quitAndInstall throws, or returns without app.quit() on a Squirrel follow-up error / stale download), the quitting-for-update flags are rolled back — synchronously on throw, and via a short unref'd watchdog otherwise — so the app does not get stuck permanently bypassing close-to-tray and the quit guard. Tests: unit tests for the new windowManager flags and for the install handler — ordering (setQuittingForUpdate before quitAndInstall), tray cleanup still runs, no-op when the updater fails to load, rollback on synchronous throw, and watchdog rollback when the app never quits. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(auto-update): keep dirty-editor guard during update install setQuittingForUpdate only bypasses close-to-tray (so the window actually closes and Squirrel.Mac's ShipIt can swap the bundle); it must NOT skip the unsaved-work guard, or clicking "Restart Now" with a dirty SFTP editor would silently lose edits. If the user cancels to save, the quit aborts and autoUpdateBridge's watchdog clears the quitting-for-update flags. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(auto-update): clear update-quit state when the quit is cancelled When the user clicks "Restart Now" with a dirty editor open, the before-quit guard cancels the quit (settle "stay"). The update path had already called setQuittingForUpdate(true) (which flips isQuitting=true to bypass close-to-tray for the install), so without clearing it the app stays in a quitting state — close-to-tray and other !isQuitting-gated behavior bypassed — until the 10s watchdog fires. Clear it immediately on the cancelled-quit path (#1215 review). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(auto-update): check for unsaved editors before quitAndInstall (#1215) The previous fix bypassed close-to-tray so the app process actually exits and Squirrel.Mac's ShipIt can swap the bundle, while keeping the before-quit dirty-editor guard as the unsaved-work safety net. But on macOS that net has a hole: quitAndInstall() closes the window FIRST and only then fires before-quit. Once setQuittingForUpdate(true) lets the main window truly close (instead of hiding to tray), the before-quit guard can run after the window is already gone — isReachableByUser is false, so it commits the quit and silently drops unsaved SFTP edits. Fix: move the dirty-editor check to the moment the user clicks "Restart Now", in the install handler, BEFORE setQuittingForUpdate / quitAndInstall — while the window and renderer are still alive: - dirty -> abort the install (don't set the quitting flags, don't quitAndInstall) and broadcast netcatty:update:needs-save so the renderer prompts the user to save and retry. - clean -> proceed with the existing flow (commit-to-quit, tray cleanup, quitAndInstall, watchdog). - no reachable main window / crashed renderer -> install directly (no user to ask), matching the before-quit fail-open path. The before-quit dirty guard is kept as defense-in-depth: if the window is still reachable it re-checks (clean, since we just verified), and if it's already gone it lets the quit through — which is now safe because the install handler already confirmed there were no unsaved editors. The request/reply/timeout round-trip is extracted into a shared helper, electron/bridges/dirtyEditorGuard.cjs (queryDirtyEditors), so the install handler and main.cjs's before-quit guard use one implementation. main.cjs's before-quit is refactored onto it (behavior preserved: sender-filtered reply, fail-open timeout, and the setQuittingForUpdate(false) rollback when the user cancels to save). The needs-save notice is BROADCAST to every window, not just the queried main window: "Restart to Update" can be clicked from the Settings window, which would otherwise see the click do nothing. preload exposes onUpdateNeedsSave; the subscription lives in useUpdateCheck (state layer), and both consumers — App.tsx (main window) and SettingsPage (settings window) — pass an onNeedsSave callback that shows an actionable toast ("save your editors, then click Restart Now again") in en / zh-CN / ru. Also lengthen the quitting-for-update rollback watchdog from 10s to 60s. On macOS quitAndInstall() can return while Squirrel is still pulling the downloaded ZIP from the local update server before it closes the windows; on a large/slow update that can exceed 10s. Clearing isQuitting that early would let the eventual native quit hit a non-quitting close-to-tray handler and strand the install again — the exact #1215 failure. The longer window only fires when the app is realistically stuck, at the cost of close-to-tray staying bypassed a little longer in the rare genuine-failure case. Tests: queryDirtyEditors (result / no-dirty / timeout / wrong-sender / dead or crashed webContents / send-throws / no-ipcMain paths); install handler pre-check (dirty -> no quitAndInstall + needs-save broadcast to all windows; clean -> quitAndInstall runs; no main window -> installs without asking). Existing install-handler tests updated for the now-async handler. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
576 lines
22 KiB
JavaScript
576 lines
22 KiB
JavaScript
/**
|
|
* Auto-Update Bridge
|
|
*
|
|
* Wraps electron-updater to provide IPC-driven update checks, downloads, and
|
|
* install-on-quit. Designed around a "prompt" model: the renderer asks to
|
|
* check, then explicitly triggers download and install.
|
|
*
|
|
* Platforms where auto-update is NOT supported (Linux deb/rpm/snap) get a
|
|
* graceful { available: false, error } response so the renderer can fall back
|
|
* to a manual "open GitHub releases" link.
|
|
*/
|
|
|
|
let _deps = null;
|
|
|
|
/**
|
|
* Read the persisted auto-update preference from a JSON file in userData.
|
|
* Returns true (default) if the file doesn't exist or is unreadable.
|
|
*/
|
|
function readAutoUpdatePreference() {
|
|
try {
|
|
const { app } = _deps?.electronModule || {};
|
|
if (!app) return true;
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const prefPath = path.join(app.getPath('userData'), 'auto-update-pref.json');
|
|
const data = JSON.parse(fs.readFileSync(prefPath, 'utf8'));
|
|
return data.enabled !== false;
|
|
} catch {
|
|
return true; // default to enabled
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Persist the auto-update preference to a JSON file in userData.
|
|
*/
|
|
function writeAutoUpdatePreference(enabled) {
|
|
try {
|
|
const { app } = _deps?.electronModule || {};
|
|
if (!app) return;
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
const prefPath = path.join(app.getPath('userData'), 'auto-update-pref.json');
|
|
fs.writeFileSync(prefPath, JSON.stringify({ enabled }), 'utf8');
|
|
} catch (err) {
|
|
console.warn('[AutoUpdate] Failed to write preference:', err?.message || err);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns true when the current packaging format supports electron-updater
|
|
* (macOS zip/dmg, Windows NSIS, Linux AppImage).
|
|
*/
|
|
function isAutoUpdateSupported() {
|
|
if (process.platform === "darwin" || process.platform === "win32") {
|
|
return true;
|
|
}
|
|
// Linux: only AppImage supports in-place update.
|
|
// The APPIMAGE env variable is set by the AppImage runtime.
|
|
if (process.platform === "linux" && process.env.APPIMAGE) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/** Lazily resolved autoUpdater — avoids importing electron-updater in
|
|
* contexts where native modules might not be available. */
|
|
let _autoUpdater = null;
|
|
|
|
/** Guard against duplicate listener registration */
|
|
let _listenersRegistered = false;
|
|
|
|
/** Track whether a download is in progress to distinguish download errors from check errors */
|
|
let _isDownloading = false;
|
|
|
|
/** Track whether a checkForUpdates call is in flight (set before call, cleared on result event) */
|
|
let _isChecking = false;
|
|
|
|
/**
|
|
* Snapshot of the last known update status so newly opened windows can hydrate
|
|
* without waiting for the next IPC event.
|
|
* @type {{ status: 'idle' | 'downloading' | 'ready' | 'error', percent: number, error: string | null, version: string | null, isChecking: boolean }}
|
|
*/
|
|
let _lastStatus = { status: 'idle', percent: 0, error: null, version: null, isChecking: false };
|
|
function getAutoUpdater() {
|
|
if (_autoUpdater) return _autoUpdater;
|
|
try {
|
|
const { autoUpdater } = require("electron-updater");
|
|
autoUpdater.autoDownload = readAutoUpdatePreference();
|
|
autoUpdater.autoInstallOnAppQuit = false;
|
|
// Silence the default electron-log transport (we log ourselves).
|
|
autoUpdater.logger = null;
|
|
_autoUpdater = autoUpdater;
|
|
return autoUpdater;
|
|
} catch (err) {
|
|
console.error("[AutoUpdate] Failed to load electron-updater:", err?.message || err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register persistent global IPC event listeners for auto-download flow.
|
|
* Called once in init(). Forwards electron-updater events to the renderer
|
|
* even when no manual download was initiated.
|
|
*/
|
|
function setupGlobalListeners() {
|
|
if (_listenersRegistered) return;
|
|
const updater = getAutoUpdater();
|
|
if (!updater) return;
|
|
_listenersRegistered = true;
|
|
|
|
updater.on("update-not-available", () => {
|
|
_isChecking = false;
|
|
// Reset stale status so late-opening windows don't hydrate from a
|
|
// previous 'error' or 'ready' snapshot after a "no update" check.
|
|
_lastStatus = { status: 'idle', percent: 0, error: null, version: null, isChecking: false };
|
|
broadcastToAllWindows("netcatty:update:update-not-available", {});
|
|
});
|
|
|
|
updater.on("update-available", (info) => {
|
|
_isChecking = false;
|
|
// Only track as downloading when autoDownload is enabled — otherwise no
|
|
// download will actually start and the status would be stuck at 0%.
|
|
// Use 'available' so late-opening windows can still hydrate the version.
|
|
const willDownload = updater.autoDownload !== false;
|
|
_isDownloading = willDownload;
|
|
_lastStatus = { status: willDownload ? 'downloading' : 'available', percent: 0, error: null, version: info.version || null, isChecking: false };
|
|
broadcastToAllWindows("netcatty:update:update-available", {
|
|
version: info.version || "",
|
|
releaseNotes: typeof info.releaseNotes === "string" ? info.releaseNotes : "",
|
|
releaseDate: info.releaseDate || null,
|
|
});
|
|
});
|
|
|
|
updater.on("download-progress", (info) => {
|
|
_lastStatus.percent = Math.round(info.percent ?? 0);
|
|
broadcastToAllWindows("netcatty:update:download-progress", {
|
|
percent: info.percent ?? 0,
|
|
bytesPerSecond: info.bytesPerSecond ?? 0,
|
|
transferred: info.transferred ?? 0,
|
|
total: info.total ?? 0,
|
|
});
|
|
});
|
|
|
|
updater.on("update-downloaded", () => {
|
|
_isDownloading = false;
|
|
_lastStatus = { ..._lastStatus, status: 'ready', percent: 100 };
|
|
broadcastToAllWindows("netcatty:update:downloaded");
|
|
});
|
|
|
|
updater.on("error", (err) => {
|
|
_isChecking = false;
|
|
// Only broadcast download-phase errors; check-phase errors (e.g. network failures
|
|
// during checkForUpdates) are not download failures and must not set autoDownloadStatus.
|
|
if (!_isDownloading) {
|
|
_lastStatus = { ..._lastStatus, isChecking: false };
|
|
console.warn("[AutoUpdate] Check-phase error (not broadcast to renderer):", err?.message || err);
|
|
return;
|
|
}
|
|
_isDownloading = false;
|
|
const errorMsg = err?.message || "Unknown update error";
|
|
_lastStatus = { ..._lastStatus, status: 'error', error: errorMsg };
|
|
broadcastToAllWindows("netcatty:update:error", {
|
|
error: errorMsg,
|
|
});
|
|
});
|
|
|
|
console.log("[AutoUpdate] Global listeners registered");
|
|
}
|
|
|
|
/**
|
|
* Trigger an automatic update check after a delay.
|
|
* No-op on platforms that don't support auto-update (Linux deb/rpm/snap).
|
|
* Called from main process after the main window is created.
|
|
*
|
|
* @param {number} delayMs - Milliseconds to wait before checking (default: 5000)
|
|
*/
|
|
let _autoCheckTimer = null;
|
|
|
|
function startAutoCheck(delayMs = 5000) {
|
|
if (!isAutoUpdateSupported()) {
|
|
console.log("[AutoUpdate] Platform does not support auto-update, skipping auto-check");
|
|
return;
|
|
}
|
|
// Cancel any existing timer to avoid duplicate concurrent checks
|
|
// (e.g. from multiple windows initializing or re-enable toggle).
|
|
cancelAutoCheck();
|
|
_autoCheckTimer = setTimeout(async () => {
|
|
_autoCheckTimer = null;
|
|
const updater = getAutoUpdater();
|
|
if (!updater) {
|
|
console.warn("[AutoUpdate] Auto-check skipped — updater not available");
|
|
return;
|
|
}
|
|
// Respect autoDownload flag — the renderer may have disabled it via IPC
|
|
// before this timer fires.
|
|
if (updater.autoDownload === false) {
|
|
console.log("[AutoUpdate] Auto-check skipped — autoDownload is disabled");
|
|
return;
|
|
}
|
|
_isChecking = true;
|
|
_lastStatus = { ..._lastStatus, isChecking: true };
|
|
try {
|
|
console.log("[AutoUpdate] Starting automatic update check...");
|
|
await updater.checkForUpdates();
|
|
} catch (err) {
|
|
_isChecking = false;
|
|
_lastStatus = { ..._lastStatus, isChecking: false };
|
|
console.warn("[AutoUpdate] Auto-check failed:", err?.message || err);
|
|
}
|
|
}, delayMs);
|
|
}
|
|
|
|
/**
|
|
* Cancel a pending startAutoCheck timer. Called when the renderer triggers
|
|
* a manual check to avoid racing with the queued auto-check.
|
|
*/
|
|
function cancelAutoCheck() {
|
|
if (_autoCheckTimer) {
|
|
clearTimeout(_autoCheckTimer);
|
|
_autoCheckTimer = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Flip the windowManager "quitting for update" flag, swallowing the case where
|
|
* the window manager module isn't available. Used by the install handler to
|
|
* commit the app to a clean quit before quitAndInstall, and to roll back if the
|
|
* install never actually quits (#1215).
|
|
*/
|
|
function setQuittingForUpdate(enabled) {
|
|
try {
|
|
const windowManager = require("./windowManager.cjs");
|
|
windowManager.setQuittingForUpdate(!!enabled);
|
|
} catch {
|
|
// ignore — window manager may not be available
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The webContents of the main window, or null if there's no usable main window
|
|
* to talk to. Used by the install handler to (a) ask the renderer about unsaved
|
|
* editors before committing to a quit, and (b) tell it to surface a "save
|
|
* first" notice. Targets the main window specifically (not getAllWindows()[0])
|
|
* so we never query the tray panel / settings window, whose renderers don't
|
|
* participate in the dirty-editor protocol.
|
|
*/
|
|
function getMainWebContents() {
|
|
try {
|
|
const windowManager = require("./windowManager.cjs");
|
|
const win = windowManager.getMainWindow?.();
|
|
if (!win || win.isDestroyed?.()) return null;
|
|
const wc = win.webContents;
|
|
if (!wc || wc.isDestroyed?.() || wc.isCrashed?.()) return null;
|
|
return wc;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Tell the renderer that the update can't install yet because there are unsaved
|
|
* editors. The renderer surfaces a toast asking the user to save, then click
|
|
* "Restart Now" again.
|
|
*
|
|
* Broadcast to ALL windows, not just the main one: the install can be triggered
|
|
* from the Settings window's "Restart to Update" button, and that's the focused
|
|
* window the user is looking at. Sending only to the (possibly hidden/behind)
|
|
* main window would make the click appear to do nothing (#1215 review). The
|
|
* unsaved editors live in the main window, but every window surfaces the same
|
|
* "save first" notice so it lands wherever the user is.
|
|
*/
|
|
function notifyNeedsSave() {
|
|
broadcastToAllWindows("netcatty:update:needs-save");
|
|
}
|
|
|
|
/** Max time to wait for the renderer's unsaved-editors reply before the install
|
|
* fails open and proceeds (matches the before-quit guard timeout). */
|
|
const INSTALL_DIRTY_CHECK_TIMEOUT_MS = 5000;
|
|
|
|
/**
|
|
* Ask the main-window renderer whether it has unsaved editor changes, reusing
|
|
* the shared dirty-editor round-trip. Resolves false (fail open) if the helper
|
|
* is unavailable, so a missing module can never block an install. `ipcMain` is
|
|
* the instance passed to registerHandlers; the helper needs it to listen for
|
|
* the reply.
|
|
*/
|
|
function queryDirtyEditorsSafe(webContents, ipcMain) {
|
|
try {
|
|
const { queryDirtyEditors } = require("./dirtyEditorGuard.cjs");
|
|
return queryDirtyEditors(webContents, INSTALL_DIRTY_CHECK_TIMEOUT_MS, { ipcMain });
|
|
} catch (err) {
|
|
console.warn("[AutoUpdate] dirty-editor guard unavailable:", err?.message || err);
|
|
return Promise.resolve(false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* If quitAndInstall doesn't lead to the app actually quitting (it returns
|
|
* without app.quit(), e.g. on a Squirrel.Mac follow-up error or a stale
|
|
* downloaded file), the quitting-for-update flags would stay set and
|
|
* permanently bypass close-to-tray + the dirty-editor quit guard. This
|
|
* watchdog clears them if we're still running after a grace period.
|
|
*
|
|
* The grace period is deliberately long. On macOS quitAndInstall() can return
|
|
* while Squirrel.Mac is still pulling the already-downloaded ZIP from the local
|
|
* update server before it actually closes the windows; for a large/slow update
|
|
* that second stage can take well over 10s. If the watchdog cleared isQuitting
|
|
* during that window, the eventual native quit would hit a *non*-quitting
|
|
* close-to-tray handler and get stranded again — the exact #1215 failure. So we
|
|
* only roll back after a window long enough that the app is realistically stuck,
|
|
* not merely slow. The cost of waiting longer is just that close-to-tray stays
|
|
* bypassed a bit longer in the rare genuine-failure case (#1215 review).
|
|
*/
|
|
let _quittingForUpdateWatchdog = null;
|
|
const QUITTING_FOR_UPDATE_WATCHDOG_MS = 60000;
|
|
|
|
function scheduleQuittingForUpdateWatchdog() {
|
|
if (_quittingForUpdateWatchdog) {
|
|
clearTimeout(_quittingForUpdateWatchdog);
|
|
}
|
|
_quittingForUpdateWatchdog = setTimeout(() => {
|
|
_quittingForUpdateWatchdog = null;
|
|
// Still alive after the grace period — the install did not quit the app.
|
|
console.warn("[AutoUpdate] App still running after quitAndInstall; clearing quitting-for-update state");
|
|
setQuittingForUpdate(false);
|
|
}, QUITTING_FOR_UPDATE_WATCHDOG_MS);
|
|
// Don't let the watchdog keep the event loop (and thus the process) alive —
|
|
// if the app is otherwise ready to quit, the timer must not block it.
|
|
if (typeof _quittingForUpdateWatchdog.unref === "function") {
|
|
_quittingForUpdateWatchdog.unref();
|
|
}
|
|
}
|
|
|
|
function init(deps) {
|
|
_deps = deps;
|
|
setupGlobalListeners();
|
|
}
|
|
|
|
/**
|
|
* Broadcast an IPC event to all non-destroyed BrowserWindows.
|
|
* Ensures both the main window and settings window always receive
|
|
* auto-update events.
|
|
* @param {string} channel
|
|
* @param {unknown} [payload]
|
|
*/
|
|
function broadcastToAllWindows(channel, payload) {
|
|
try {
|
|
const { BrowserWindow } = _deps?.electronModule || {};
|
|
if (!BrowserWindow) return;
|
|
const windows = BrowserWindow.getAllWindows();
|
|
for (const win of windows) {
|
|
if (!win.isDestroyed()) {
|
|
if (payload !== undefined) {
|
|
win.webContents.send(channel, payload);
|
|
} else {
|
|
win.webContents.send(channel);
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {
|
|
console.warn("[AutoUpdate] broadcastToAllWindows failed:", err?.message || err);
|
|
}
|
|
}
|
|
|
|
function registerHandlers(ipcMain) {
|
|
// ---- Check for updates ------------------------------------------------
|
|
ipcMain.handle("netcatty:update:check", async () => {
|
|
// Cancel any pending auto-check to prevent concurrent checkForUpdates()
|
|
// calls — electron-updater rejects them and surfaces false errors.
|
|
cancelAutoCheck();
|
|
|
|
if (!isAutoUpdateSupported()) {
|
|
return {
|
|
available: false,
|
|
supported: false,
|
|
error: "Auto-update is not supported on this platform/package format.",
|
|
};
|
|
}
|
|
|
|
const updater = getAutoUpdater();
|
|
if (!updater) {
|
|
return {
|
|
available: false,
|
|
supported: false,
|
|
error: "Update module failed to load.",
|
|
};
|
|
}
|
|
|
|
// If a check is already in flight (e.g. from startAutoCheck), don't
|
|
// start a concurrent one — electron-updater rejects it and surfaces a
|
|
// confusing error. Return a sentinel so the renderer knows to wait.
|
|
if (_isChecking) {
|
|
return { available: false, supported: true, checking: true };
|
|
}
|
|
|
|
// If a download is already in progress or the update is ready to install,
|
|
// skip the check entirely — calling checkForUpdates() while downloading
|
|
// can cause electron-updater to error, which corrupts the download state
|
|
// and forces the user to download manually (GitHub issue #522).
|
|
if (_isDownloading) {
|
|
return { available: true, supported: true, downloading: true, version: _lastStatus.version };
|
|
}
|
|
if (_lastStatus.status === 'ready') {
|
|
return { available: true, supported: true, ready: true, version: _lastStatus.version };
|
|
}
|
|
|
|
try {
|
|
_isChecking = true;
|
|
_lastStatus = { ..._lastStatus, isChecking: true };
|
|
const result = await updater.checkForUpdates();
|
|
if (!result || !result.updateInfo) {
|
|
return { available: false, supported: true };
|
|
}
|
|
|
|
const { version, releaseNotes, releaseDate } = result.updateInfo;
|
|
|
|
// Compare with current version using semver ordering.
|
|
// Only report an update when the feed version is strictly newer,
|
|
// avoiding false positives for pre-release or nightly builds.
|
|
const { app } = _deps?.electronModule || {};
|
|
const currentVersion = app?.getVersion?.() || "0.0.0";
|
|
const isNewer = currentVersion.localeCompare(version, undefined, { numeric: true, sensitivity: 'base' }) < 0;
|
|
if (!isNewer) {
|
|
return { available: false, supported: true };
|
|
}
|
|
|
|
return {
|
|
available: true,
|
|
supported: true,
|
|
version,
|
|
releaseNotes: typeof releaseNotes === "string" ? releaseNotes : "",
|
|
releaseDate: releaseDate || null,
|
|
};
|
|
} catch (err) {
|
|
_isChecking = false;
|
|
_lastStatus = { ..._lastStatus, isChecking: false };
|
|
console.warn("[AutoUpdate] Check failed:", err?.message || err);
|
|
return {
|
|
available: false,
|
|
supported: true,
|
|
error: err?.message || "Unknown update check error",
|
|
};
|
|
}
|
|
});
|
|
|
|
// ---- Download update ---------------------------------------------------
|
|
ipcMain.handle("netcatty:update:download", async () => {
|
|
if (_isDownloading) {
|
|
return { success: true };
|
|
}
|
|
const updater = getAutoUpdater();
|
|
if (!updater) {
|
|
return { success: false, error: "Update module not available." };
|
|
}
|
|
try {
|
|
_isDownloading = true;
|
|
_lastStatus = { ..._lastStatus, status: 'downloading', percent: 0, error: null };
|
|
await updater.downloadUpdate();
|
|
return { success: true };
|
|
} catch (err) {
|
|
_isDownloading = false;
|
|
_lastStatus = { ..._lastStatus, status: 'error', error: err?.message || "Download failed", percent: 0 };
|
|
// Don't broadcast here — the global updater "error" listener already handles it
|
|
console.error("[AutoUpdate] Download failed:", err?.message || err);
|
|
return { success: false, error: err?.message || "Download failed" };
|
|
}
|
|
});
|
|
|
|
// ---- Get current update status (for late-opening windows) ---------------
|
|
ipcMain.handle("netcatty:update:getStatus", () => {
|
|
return { ..._lastStatus };
|
|
});
|
|
|
|
// ---- Install (quit & install) ------------------------------------------
|
|
ipcMain.handle("netcatty:update:install", async () => {
|
|
const updater = getAutoUpdater();
|
|
if (!updater) return;
|
|
|
|
// Check for unsaved editors BEFORE committing to a quit (#1215 review).
|
|
//
|
|
// On macOS quitAndInstall() closes windows first and only then fires
|
|
// before-quit. Once setQuittingForUpdate(true) lets the main window
|
|
// actually close (instead of hiding to tray), the before-quit dirty-editor
|
|
// guard can run after the window is already gone — isReachableByUser is
|
|
// false, so it commits the quit and silently drops unsaved SFTP edits.
|
|
//
|
|
// So we ask the renderer here, while the window and renderer are still
|
|
// alive. If there's unsaved work, abort the install (don't touch the
|
|
// quitting flags, don't quitAndInstall) and tell the renderer to prompt the
|
|
// user to save; they can click "Restart Now" again afterwards. If the main
|
|
// window isn't reachable (no window / crashed renderer) there's no user to
|
|
// ask, so we install directly — matching the before-quit fail-open path.
|
|
const mainWc = getMainWebContents();
|
|
if (mainWc) {
|
|
const hasDirty = await queryDirtyEditorsSafe(mainWc, ipcMain);
|
|
if (hasDirty) {
|
|
// Broadcast so the notice reaches whichever window the user clicked
|
|
// from (main or Settings), not just the main window we queried.
|
|
notifyNeedsSave();
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Commit the app to a real quit BEFORE quitAndInstall fires app.quit().
|
|
// Without this the in-place install silently fails (#1215): the main-window
|
|
// close handler hides to tray when close-to-tray is on, so the process
|
|
// stays alive and Squirrel.Mac's ShipIt helper — which waits on the parent
|
|
// PID to die before swapping the bundle — ends up in launchd "pending
|
|
// spawn" limbo and never installs. setQuittingForUpdate(true) sets
|
|
// isQuitting so close-to-tray is bypassed and the window actually closes.
|
|
setQuittingForUpdate(true);
|
|
|
|
// On macOS, the system tray keeps the app process alive even after all
|
|
// windows are closed, which prevents quitAndInstall from completing.
|
|
// Destroy the tray (and its panel window) before quitting so the app
|
|
// can exit cleanly and the installer can proceed.
|
|
try {
|
|
const globalShortcutBridge = require("./globalShortcutBridge.cjs");
|
|
globalShortcutBridge.cleanup();
|
|
} catch {
|
|
// ignore — bridge may not be available
|
|
}
|
|
|
|
try {
|
|
updater.quitAndInstall(false, true);
|
|
} catch (err) {
|
|
// quitAndInstall threw synchronously — the app will NOT quit. Roll back
|
|
// the quitting-for-update flags so later closes/quits behave normally
|
|
// instead of permanently bypassing close-to-tray + the dirty-editor
|
|
// guard (#1215 review).
|
|
console.error("[AutoUpdate] quitAndInstall failed:", err?.message || err);
|
|
setQuittingForUpdate(false);
|
|
return;
|
|
}
|
|
|
|
// quitAndInstall can also fail to quit asynchronously (e.g. Squirrel.Mac's
|
|
// follow-up check errors, or a stale/missing downloaded file) — it returns
|
|
// without app.quit() ever firing. A watchdog clears the flags if we're
|
|
// still alive shortly after, so the app doesn't get stuck in a state where
|
|
// every window close bypasses close-to-tray and the dirty-editor guard.
|
|
scheduleQuittingForUpdateWatchdog();
|
|
});
|
|
|
|
// ---- Get auto-update preference -----------------------------------------
|
|
ipcMain.handle("netcatty:update:getAutoUpdate", () => {
|
|
return { enabled: readAutoUpdatePreference() };
|
|
});
|
|
|
|
// ---- Enable/disable auto-update ----------------------------------------
|
|
let _prevAutoDownloadEnabled = readAutoUpdatePreference();
|
|
ipcMain.handle("netcatty:update:setAutoUpdate", (_event, { enabled }) => {
|
|
const wasEnabled = _prevAutoDownloadEnabled;
|
|
_prevAutoDownloadEnabled = !!enabled;
|
|
const updater = getAutoUpdater();
|
|
if (updater) {
|
|
updater.autoDownload = !!enabled;
|
|
console.log("[AutoUpdate] autoDownload set to:", !!enabled);
|
|
}
|
|
// Persist so the preference survives app restarts
|
|
writeAutoUpdatePreference(!!enabled);
|
|
if (!enabled) {
|
|
cancelAutoCheck();
|
|
} else if (!wasEnabled && !_isChecking) {
|
|
// Only re-schedule when actually re-enabling (not on every mount sync),
|
|
// to avoid duplicate checks from multiple windows initializing.
|
|
// Skip if a check is already in flight to prevent concurrent calls.
|
|
startAutoCheck(2000);
|
|
}
|
|
return { success: true };
|
|
});
|
|
|
|
console.log("[AutoUpdate] Handlers registered");
|
|
}
|
|
|
|
module.exports = { init, registerHandlers, isAutoUpdateSupported, startAutoCheck };
|