(location: AuthLocation)
| 209 | const FILE_PROVIDER_KEY = ProviderKey.make("file"); |
| 210 | |
| 211 | const makeFileProvider = (location: AuthLocation): CredentialProvider => { |
| 212 | let migrationComplete = false; |
| 213 | let migrationInFlight: Deferred.Deferred<void, StorageError> | null = null; |
| 214 | const ensureMigration = Effect.suspend(() => { |
| 215 | if (migrationComplete) return Effect.void; |
| 216 | if (migrationInFlight !== null) return Deferred.await(migrationInFlight); |
| 217 | |
| 218 | // Installing the latch inside Effect.suspend is synchronous, so a peer |
| 219 | // fiber can only observe either no attempt or this fully initialized one. |
| 220 | const latch = Deferred.makeUnsafe<void, StorageError>(); |
| 221 | migrationInFlight = latch; |
| 222 | return migrateLegacyAuthFile(location).pipe( |
| 223 | Effect.onExit((exit) => |
| 224 | Effect.gen(function* () { |
| 225 | if (Exit.isSuccess(exit)) migrationComplete = true; |
| 226 | yield* Deferred.done(latch, exit); |
| 227 | migrationInFlight = null; |
| 228 | }), |
| 229 | ), |
| 230 | ); |
| 231 | }); |
| 232 | |
| 233 | return { |
| 234 | key: FILE_PROVIDER_KEY, |
| 235 | writable: true, |
| 236 | |
| 237 | get: (id: ProviderItemId) => |
| 238 | ensureMigration.pipe( |
| 239 | Effect.andThen(Effect.suspend(() => readAll(location.filePath))), |
| 240 | Effect.map((data) => data[id] ?? null), |
| 241 | ), |
| 242 | |
| 243 | has: (id: ProviderItemId) => |
| 244 | ensureMigration.pipe( |
| 245 | Effect.andThen(Effect.suspend(() => readAll(location.filePath))), |
| 246 | Effect.map((data) => id in data), |
| 247 | ), |
| 248 | |
| 249 | set: (id: ProviderItemId, value: string) => |
| 250 | ensureMigration.pipe( |
| 251 | Effect.andThen( |
| 252 | Effect.gen(function* () { |
| 253 | const data = yield* readAll(location.filePath); |
| 254 | data[id] = value; |
| 255 | yield* writeAll(location.filePath, data); |
| 256 | }), |
| 257 | ), |
| 258 | ), |
| 259 | |
| 260 | delete: (id: ProviderItemId) => |
| 261 | ensureMigration.pipe( |
| 262 | Effect.andThen( |
| 263 | Effect.gen(function* () { |
| 264 | const data = yield* readAll(location.filePath); |
| 265 | if (id in data) { |
| 266 | delete data[id]; |
| 267 | yield* writeAll(location.filePath, data); |
| 268 | } |
no test coverage detected