From 4fff9d3494a1019b9f1344239b24ff567059963d Mon Sep 17 00:00:00 2001 From: QQ Date: Tue, 17 Nov 2026 08:45:56 +0800 Subject: [PATCH] Import packages/plugin/src/client/plugin-upgrade.ts --- packages/plugin/src/client/plugin-upgrade.ts | 73 ++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 packages/plugin/src/client/plugin-upgrade.ts diff --git a/packages/plugin/src/client/plugin-upgrade.ts b/packages/plugin/src/client/plugin-upgrade.ts new file mode 100644 index 0000000..f9360fa --- /dev/null +++ b/packages/plugin/src/client/plugin-upgrade.ts @@ -0,0 +1,73 @@ +import { compareVersions } from '../version-compare.js' +import type { RemoteStatus } from './types.js' + +/** + * What the Desktop popup can conclude after `plugin.update` died without a + * response. `upgraded`: the Host reports the target version. `unknown`: no + * proof the upgrade ran; the caller must re-enable the button. + */ +export type LostUpgradeOutcome = 'upgraded' | 'unknown' + +export interface LostUpgradeOptions { + intervalMs?: number + /** Total wait while the Host control route is unreachable (plugin being replaced). */ + unreachableDeadlineMs?: number + /** Consecutive reads of the old version from a live Host that prove nothing started. */ + stalledReads?: number + sleep?: (ms: number) => Promise +} + +const DEFAULTS = { + intervalMs: 1_000, + unreachableDeadlineMs: 60_000, + stalledReads: 3, +} + +/** + * A lost `plugin.update` response is ambiguous: the manager unloads this + * plugin once installation begins (the response dies with it), but a plain + * network error before the request reached the Host looks identical. Resolve + * it from the Host's own status instead of from the error text. + * + * - Version at or above `latest`: the upgrade happened. + * - Control route unreachable: keep waiting, the plugin may be reinstalling, + * until the deadline. + * - Live Host still on the old version for `stalledReads` reads in a row: the + * request never started an upgrade. + */ +export async function settleLostUpgrade( + readStatus: () => Promise, + latest: string, + options: LostUpgradeOptions = {}, +): Promise { + const intervalMs = options.intervalMs ?? DEFAULTS.intervalMs + const deadlineMs = options.unreachableDeadlineMs ?? DEFAULTS.unreachableDeadlineMs + const stalledReads = options.stalledReads ?? DEFAULTS.stalledReads + // `Promise.withResolvers` is not in this package's tsconfig `lib`. + const sleep = options.sleep ?? ((ms: number) => new Promise(resolve => setTimeout(resolve, ms))) + let waited = 0 + let stalled = 0 + while (waited <= deadlineMs) { + let status: RemoteStatus | undefined + try { + status = await readStatus() + } catch { + status = undefined + } + const current = status !== undefined && status.controlUnavailable !== true + ? status.update?.currentVersion + : undefined + if (current !== undefined) { + if (compareVersions(current, latest) >= 0) return 'upgraded' + stalled += 1 + if (stalled >= stalledReads) return 'unknown' + } else { + // Unreachable, or a freshly loaded plugin that has not finished its + // registry check: neither proves anything about the upgrade. + stalled = 0 + } + await sleep(intervalMs) + waited += intervalMs + } + return 'unknown' +}