|
| 1 | +import { lstat } from "node:fs/promises"; |
| 2 | +import { posix } from "node:path"; |
| 3 | + |
| 4 | +import { |
| 5 | + collectProcessTreePids, |
| 6 | + listProcessSnapshots, |
| 7 | + processCommandExactlyRunsExecutable, |
| 8 | + stopProcesses, |
| 9 | + type StopProcessesResult, |
| 10 | +} from "@open-design/platform"; |
| 11 | + |
| 12 | +type RetirementLogger = { |
| 13 | + info(message: string, meta?: Record<string, unknown>): void; |
| 14 | + warn(message: string, meta?: Record<string, unknown>): void; |
| 15 | +}; |
| 16 | + |
| 17 | +export type ObsoleteInstalledOuterRetirementContext = { |
| 18 | + currentExecutablePath: string; |
| 19 | + currentPid: number; |
| 20 | + installedLaunchPath: string | null; |
| 21 | + logger: RetirementLogger; |
| 22 | + payloadDesktopProcess: boolean; |
| 23 | + payloadExecutablePath: string | null; |
| 24 | + platform: NodeJS.Platform; |
| 25 | +}; |
| 26 | + |
| 27 | +type ObsoleteInstalledOuterRetirementDeps = { |
| 28 | + listProcessSnapshots?: typeof listProcessSnapshots; |
| 29 | + stopProcesses?: typeof stopProcesses; |
| 30 | +}; |
| 31 | + |
| 32 | +export type ObsoleteInstalledOuterRetirementResult = |
| 33 | + | { |
| 34 | + reason: |
| 35 | + | "invalid-install-anchor" |
| 36 | + | "not-payload-desktop" |
| 37 | + | "same-executable" |
| 38 | + | "unsupported-platform" |
| 39 | + | "unsafe-current-descendant"; |
| 40 | + status: "skipped"; |
| 41 | + } |
| 42 | + | { |
| 43 | + executablePath: string; |
| 44 | + reason: "no-match"; |
| 45 | + status: "skipped"; |
| 46 | + } |
| 47 | + | { |
| 48 | + executablePath: string; |
| 49 | + result: StopProcessesResult; |
| 50 | + rootPids: number[]; |
| 51 | + status: "failed" | "retired"; |
| 52 | + treePids: number[]; |
| 53 | + }; |
| 54 | + |
| 55 | +function sameExecutablePath(left: string, right: string): boolean { |
| 56 | + return posix.normalize(left) === posix.normalize(right); |
| 57 | +} |
| 58 | + |
| 59 | +async function resolveInstalledOuterExecutable( |
| 60 | + installedLaunchPath: string | null, |
| 61 | + platform: NodeJS.Platform, |
| 62 | +): Promise<string | null> { |
| 63 | + if (installedLaunchPath == null || installedLaunchPath.length === 0) return null; |
| 64 | + if (platform !== "darwin" || !posix.isAbsolute(installedLaunchPath)) return null; |
| 65 | + |
| 66 | + const launchEntry = await lstat(installedLaunchPath).catch(() => null); |
| 67 | + if (launchEntry == null || launchEntry.isSymbolicLink()) return null; |
| 68 | + |
| 69 | + if (!launchEntry.isDirectory() || !installedLaunchPath.endsWith(".app")) return null; |
| 70 | + const appName = posix.basename(installedLaunchPath, ".app"); |
| 71 | + const executablePath = posix.join(installedLaunchPath, "Contents", "MacOS", appName); |
| 72 | + |
| 73 | + const executableEntry = await lstat(executablePath).catch(() => null); |
| 74 | + if (executableEntry == null || !executableEntry.isFile() || executableEntry.isSymbolicLink()) return null; |
| 75 | + return executablePath; |
| 76 | +} |
| 77 | + |
| 78 | +async function retireObsoleteInstalledOuter( |
| 79 | + context: ObsoleteInstalledOuterRetirementContext, |
| 80 | + deps: ObsoleteInstalledOuterRetirementDeps, |
| 81 | +): Promise<ObsoleteInstalledOuterRetirementResult> { |
| 82 | + if (!context.payloadDesktopProcess || context.payloadExecutablePath == null || !sameExecutablePath( |
| 83 | + context.currentExecutablePath, |
| 84 | + context.payloadExecutablePath, |
| 85 | + )) { |
| 86 | + return { reason: "not-payload-desktop", status: "skipped" }; |
| 87 | + } |
| 88 | + if (context.platform !== "darwin") { |
| 89 | + return { reason: "unsupported-platform", status: "skipped" }; |
| 90 | + } |
| 91 | + |
| 92 | + const executablePath = await resolveInstalledOuterExecutable(context.installedLaunchPath, context.platform); |
| 93 | + if (executablePath == null) return { reason: "invalid-install-anchor", status: "skipped" }; |
| 94 | + if (sameExecutablePath(executablePath, context.currentExecutablePath)) { |
| 95 | + return { reason: "same-executable", status: "skipped" }; |
| 96 | + } |
| 97 | + |
| 98 | + const snapshots = await (deps.listProcessSnapshots ?? listProcessSnapshots)(); |
| 99 | + const rootPids = snapshots |
| 100 | + .filter((snapshot) => snapshot.pid !== context.currentPid && processCommandExactlyRunsExecutable( |
| 101 | + snapshot.command, |
| 102 | + executablePath, |
| 103 | + "darwin", |
| 104 | + )) |
| 105 | + .map((snapshot) => snapshot.pid) |
| 106 | + .sort((left, right) => right - left); |
| 107 | + if (rootPids.length === 0) return { executablePath, reason: "no-match", status: "skipped" }; |
| 108 | + |
| 109 | + const safeRootPids = rootPids.filter((rootPid) => { |
| 110 | + const tree = collectProcessTreePids(snapshots, [rootPid]); |
| 111 | + return !tree.includes(context.currentPid); |
| 112 | + }); |
| 113 | + if (safeRootPids.length === 0) { |
| 114 | + context.logger.warn("skipped obsolete installed outer retirement because it contains current payload", { |
| 115 | + currentPid: context.currentPid, |
| 116 | + executablePath, |
| 117 | + rootPids, |
| 118 | + }); |
| 119 | + return { reason: "unsafe-current-descendant", status: "skipped" }; |
| 120 | + } |
| 121 | + |
| 122 | + const treePids = collectProcessTreePids(snapshots, safeRootPids); |
| 123 | + const result = await (deps.stopProcesses ?? stopProcesses)(treePids); |
| 124 | + const status = result.remainingPids.length === 0 ? "retired" : "failed"; |
| 125 | + const meta = { |
| 126 | + executablePath, |
| 127 | + forcedPids: result.forcedPids, |
| 128 | + remainingPids: result.remainingPids, |
| 129 | + rootPids: safeRootPids, |
| 130 | + stoppedPids: result.stoppedPids, |
| 131 | + treePids, |
| 132 | + }; |
| 133 | + if (status === "retired") { |
| 134 | + context.logger.info("retired obsolete installed outer", meta); |
| 135 | + } else { |
| 136 | + context.logger.warn("obsolete installed outer survived retirement", meta); |
| 137 | + } |
| 138 | + return { executablePath, result, rootPids: safeRootPids, status, treePids }; |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Build a re-usable, single-flight cleanup callback for desktop SHOW and quit. |
| 143 | + * A later invocation starts a fresh scan so a later LaunchServices open is not |
| 144 | + * hidden by a previously successful retirement. |
| 145 | + */ |
| 146 | +export function createObsoleteInstalledOuterRetirement( |
| 147 | + context: ObsoleteInstalledOuterRetirementContext, |
| 148 | + deps: ObsoleteInstalledOuterRetirementDeps = {}, |
| 149 | +): () => Promise<ObsoleteInstalledOuterRetirementResult> { |
| 150 | + let pending: Promise<ObsoleteInstalledOuterRetirementResult> | null = null; |
| 151 | + return () => { |
| 152 | + if (pending != null) return pending; |
| 153 | + pending = retireObsoleteInstalledOuter(context, deps).finally(() => { |
| 154 | + pending = null; |
| 155 | + }); |
| 156 | + return pending; |
| 157 | + }; |
| 158 | +} |
0 commit comments