(
row: ConnectionRow,
provider: CredentialProvider,
trigger: RefreshTrigger = "proactive",
)
| 2407 | ); |
| 2408 | |
| 2409 | const refreshConnectionToken = ( |
| 2410 | row: ConnectionRow, |
| 2411 | provider: CredentialProvider, |
| 2412 | trigger: RefreshTrigger = "proactive", |
| 2413 | ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> => |
| 2414 | // Share a single refresh per connection so concurrent resolves of the same |
| 2415 | // connection all await one refresh-token grant (the AS rotates the refresh |
| 2416 | // token; parallel grants would race on a consumed token — v1's refresh |
| 2417 | // deferred-map). The gate is cleared once the refresh settles so a later |
| 2418 | // expiry can refresh again. |
| 2419 | Effect.gen(function* () { |
| 2420 | const key = connectionKey(row); |
| 2421 | // Joining an in-flight grant is correct for BOTH triggers: whatever |
| 2422 | // that peer mints is newer than the token this fiber just saw rejected, |
| 2423 | // which is exactly what a reactive retry wants. The gate is cleared on |
| 2424 | // settle, so a 401 arriving after a refresh completed starts a fresh |
| 2425 | // grant rather than replaying the stale memoized one. |
| 2426 | const existing = refreshInFlight.get(key); |
| 2427 | if (existing) return yield* existing; |
| 2428 | // `Effect.cached` memoizes the grant onto a deferred: it runs once and |
| 2429 | // replays to every awaiter sharing this entry. |
| 2430 | const memoized = yield* Effect.cached(performTokenRefresh(row, provider, trigger)); |
| 2431 | const gated = memoized.pipe( |
| 2432 | Effect.ensuring(Effect.sync(() => refreshInFlight.delete(key))), |
| 2433 | ); |
| 2434 | // Re-check after building (a peer fiber may have registered first while |
| 2435 | // we built ours) so everyone converges on the same shared grant. |
| 2436 | const winner = refreshInFlight.get(key) ?? gated; |
| 2437 | if (winner === gated) refreshInFlight.set(key, gated); |
| 2438 | return yield* winner; |
| 2439 | }); |
| 2440 | |
| 2441 | // Resolve every named input of a connection (`variable → value`). A |
| 2442 | // single-secret connection yields `{ token: <value> }`; an apiKey method with |
no test coverage detected