()
| 124 | let state = load(); |
| 125 | |
| 126 | function load() { |
| 127 | memoryTokens.clear(); |
| 128 | if (!filePath) return emptyState(); |
| 129 | let loaded; |
| 130 | try { |
| 131 | const parsed = JSON.parse(readFileSync(filePath, "utf8")); |
| 132 | if (parsed?.version !== STORE_VERSION) return emptyState(); |
| 133 | loaded = { |
| 134 | ...emptyState(), |
| 135 | ...parsed, |
| 136 | connections: Array.isArray(parsed.connections) |
| 137 | ? parsed.connections |
| 138 | : [], |
| 139 | }; |
| 140 | } catch { |
| 141 | // Missing or corrupt store — start clean rather than blocking sign-in. |
| 142 | return emptyState(); |
| 143 | } |
| 144 | if (secrets) { |
| 145 | for (const connection of loaded.connections) { |
| 146 | try { |
| 147 | const secret = secrets.read(secretAccountFor(connection.id)); |
| 148 | const token = secret ? tokenFromSecret(secret) : null; |
| 149 | if (token) memoryTokens.set(connection.id, token); |
| 150 | } catch (error) { |
| 151 | // A locked keychain: the connection stays listed, needing sign-in. |
| 152 | log(error instanceof Error ? error.message : String(error)); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | return loaded; |
| 157 | } |
| 158 | |
| 159 | function persist() { |
| 160 | if (!filePath) return; |
no test coverage detected