|
| 1 | +// Internal: resolve the snapshot file name for a given live auth snapshot. |
| 2 | +// Used by save/use/external-sync — kept under _internal/ because the |
| 3 | +// public surface only exposes the orchestrator wrappers. |
| 4 | + |
| 5 | +import { |
| 6 | + AccountNameInferenceError, |
| 7 | +} from "../errors"; |
| 8 | +import { parseAuthSnapshotFile } from "../auth-parser"; |
| 9 | +import { loadRegistry } from "../registry"; |
| 10 | +import { ParsedAuthSnapshot } from "../types"; |
| 11 | +import { listAccountNames } from "../read/listing"; |
| 12 | +import { accountFilePath, normalizeAccountName } from "../naming"; |
| 13 | +import { pathExists } from "./fs-helpers"; |
| 14 | +import { |
| 15 | + registryEntrySharesEmail, |
| 16 | + registryEntrySharesIdentity, |
| 17 | + snapshotsShareEmail, |
| 18 | + snapshotsShareIdentity, |
| 19 | +} from "../identity/equality"; |
| 20 | + |
| 21 | +export type ResolvedAccountNameSource = "active" | "existing" | "inferred"; |
| 22 | + |
| 23 | +export interface ResolvedDefaultAccountName { |
| 24 | + name: string; |
| 25 | + source: ResolvedAccountNameSource; |
| 26 | + forceOverwrite?: boolean; |
| 27 | +} |
| 28 | + |
| 29 | +export interface ResolvedLoginAccountName { |
| 30 | + name: string; |
| 31 | + source: ResolvedAccountNameSource; |
| 32 | + forceOverwrite?: boolean; |
| 33 | +} |
| 34 | + |
| 35 | +function orderReloginSnapshotCandidates( |
| 36 | + accountNames: string[], |
| 37 | + incomingSnapshot: ParsedAuthSnapshot, |
| 38 | + activeName: string | null, |
| 39 | +): string[] { |
| 40 | + const ordered: string[] = []; |
| 41 | + const add = (name: string | null | undefined): void => { |
| 42 | + if (!name || !accountNames.includes(name) || ordered.includes(name)) return; |
| 43 | + ordered.push(name); |
| 44 | + }; |
| 45 | + |
| 46 | + add(activeName); |
| 47 | + |
| 48 | + const incomingEmail = incomingSnapshot.email?.trim().toLowerCase(); |
| 49 | + if (incomingEmail) { |
| 50 | + try { |
| 51 | + add(normalizeAccountName(incomingEmail)); |
| 52 | + } catch { |
| 53 | + // Invalid email-shaped snapshot names fall through to identity scan. |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + for (const name of accountNames) { |
| 58 | + add(name); |
| 59 | + } |
| 60 | + |
| 61 | + return ordered; |
| 62 | +} |
| 63 | + |
| 64 | +async function resolveRegistryAccountNameForIncomingSnapshot( |
| 65 | + incomingSnapshot: ParsedAuthSnapshot, |
| 66 | + candidates: string[], |
| 67 | + activeName: string | null, |
| 68 | +): Promise<ResolvedDefaultAccountName | null> { |
| 69 | + const registry = await loadRegistry(); |
| 70 | + let activeEmailMatch: ResolvedDefaultAccountName | null = null; |
| 71 | + |
| 72 | + for (const name of candidates) { |
| 73 | + const entry = registry.accounts[name]; |
| 74 | + if (!entry || !(await pathExists(accountFilePath(name)))) continue; |
| 75 | + |
| 76 | + if (registryEntrySharesIdentity(entry, incomingSnapshot)) { |
| 77 | + return { |
| 78 | + name, |
| 79 | + source: activeName === name ? "active" : "existing", |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + if (!activeEmailMatch && registryEntrySharesEmail(entry, incomingSnapshot)) { |
| 84 | + activeEmailMatch = { |
| 85 | + name, |
| 86 | + source: activeName === name ? "active" : "existing", |
| 87 | + forceOverwrite: true, |
| 88 | + }; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return activeEmailMatch; |
| 93 | +} |
| 94 | + |
| 95 | +export async function resolveExistingAccountNameForIncomingSnapshot( |
| 96 | + incomingSnapshot: ParsedAuthSnapshot, |
| 97 | + activeName: string | null, |
| 98 | +): Promise<ResolvedDefaultAccountName | null> { |
| 99 | + let emailMatch: ResolvedDefaultAccountName | null = null; |
| 100 | + const accountNames = await listAccountNames(); |
| 101 | + const candidates = orderReloginSnapshotCandidates( |
| 102 | + accountNames, |
| 103 | + incomingSnapshot, |
| 104 | + activeName, |
| 105 | + ); |
| 106 | + const registryMatch = await resolveRegistryAccountNameForIncomingSnapshot( |
| 107 | + incomingSnapshot, |
| 108 | + candidates, |
| 109 | + activeName, |
| 110 | + ); |
| 111 | + if (registryMatch) { |
| 112 | + return registryMatch; |
| 113 | + } |
| 114 | + |
| 115 | + for (const name of candidates) { |
| 116 | + const snapshotPath = accountFilePath(name); |
| 117 | + if (!(await pathExists(snapshotPath))) continue; |
| 118 | + |
| 119 | + const existingSnapshot = await parseAuthSnapshotFile(snapshotPath); |
| 120 | + if (snapshotsShareIdentity(existingSnapshot, incomingSnapshot)) { |
| 121 | + return { |
| 122 | + name, |
| 123 | + source: activeName === name ? "active" : "existing", |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + if (!emailMatch && snapshotsShareEmail(existingSnapshot, incomingSnapshot)) { |
| 128 | + emailMatch = { |
| 129 | + name, |
| 130 | + source: activeName === name ? "active" : "existing", |
| 131 | + forceOverwrite: true, |
| 132 | + }; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return emailMatch; |
| 137 | +} |
| 138 | + |
| 139 | +export async function resolveUniqueInferredName( |
| 140 | + baseName: string, |
| 141 | + incomingSnapshot: ParsedAuthSnapshot, |
| 142 | +): Promise<string> { |
| 143 | + const hasMatchingIdentity = async (name: string): Promise<boolean> => { |
| 144 | + const parsed = await parseAuthSnapshotFile(accountFilePath(name)); |
| 145 | + return snapshotsShareIdentity(parsed, incomingSnapshot); |
| 146 | + }; |
| 147 | + |
| 148 | + const basePath = accountFilePath(baseName); |
| 149 | + if (!(await pathExists(basePath))) { |
| 150 | + return baseName; |
| 151 | + } |
| 152 | + if (await hasMatchingIdentity(baseName)) { |
| 153 | + return baseName; |
| 154 | + } |
| 155 | + |
| 156 | + for (let i = 2; i <= 99; i += 1) { |
| 157 | + const candidate = normalizeAccountName(`${baseName}--dup-${i}`); |
| 158 | + const candidatePath = accountFilePath(candidate); |
| 159 | + if (!(await pathExists(candidatePath))) { |
| 160 | + return candidate; |
| 161 | + } |
| 162 | + if (await hasMatchingIdentity(candidate)) { |
| 163 | + return candidate; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + throw new AccountNameInferenceError(); |
| 168 | +} |
| 169 | + |
| 170 | +export async function inferAccountNameFromSnapshot( |
| 171 | + incomingSnapshot: ParsedAuthSnapshot, |
| 172 | +): Promise<string> { |
| 173 | + const email = incomingSnapshot.email?.trim().toLowerCase(); |
| 174 | + if (!email || !email.includes("@")) { |
| 175 | + throw new AccountNameInferenceError(); |
| 176 | + } |
| 177 | + |
| 178 | + const baseCandidate = normalizeAccountName(email); |
| 179 | + return resolveUniqueInferredName(baseCandidate, incomingSnapshot); |
| 180 | +} |
| 181 | + |
| 182 | +export async function resolveLoginAccountNameForSnapshot( |
| 183 | + incomingSnapshot: ParsedAuthSnapshot, |
| 184 | + activeName: string | null, |
| 185 | +): Promise<ResolvedLoginAccountName> { |
| 186 | + const existing = await resolveExistingAccountNameForIncomingSnapshot( |
| 187 | + incomingSnapshot, |
| 188 | + activeName, |
| 189 | + ); |
| 190 | + if (existing) return existing; |
| 191 | + |
| 192 | + return { |
| 193 | + name: await inferAccountNameFromSnapshot(incomingSnapshot), |
| 194 | + source: "inferred", |
| 195 | + }; |
| 196 | +} |
0 commit comments