(
row: ConnectionRow,
provider: CredentialProvider,
trigger: RefreshTrigger = "proactive",
)
| 2039 | ); |
| 2040 | |
| 2041 | const refreshConnectionToken = ( |
| 2042 | row: ConnectionRow, |
| 2043 | provider: CredentialProvider, |
| 2044 | trigger: RefreshTrigger = "proactive", |
| 2045 | ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> => |
| 2046 | // Share a single refresh per connection so concurrent resolves of the same |
| 2047 | // connection all await one refresh-token grant (the AS rotates the refresh |
| 2048 | // token; parallel grants would race on a consumed token — v1's refresh |
| 2049 | // deferred-map). The gate is cleared once the refresh settles so a later |
| 2050 | // expiry can refresh again. |
| 2051 | Effect.gen(function* () { |
| 2052 | const key = connectionKey(row); |
| 2053 | // Joining an in-flight grant is correct for BOTH triggers: whatever |
| 2054 | // that peer mints is newer than the token this fiber just saw rejected, |
| 2055 | // which is exactly what a reactive retry wants. The gate is cleared on |
| 2056 | // settle, so a 401 arriving after a refresh completed starts a fresh |
| 2057 | // grant rather than replaying the stale memoized one. |
| 2058 | const existing = refreshInFlight.get(key); |
| 2059 | if (existing) return yield* existing; |
| 2060 | // `Effect.cached` memoizes the grant onto a deferred: it runs once and |
| 2061 | // replays to every awaiter sharing this entry. |
| 2062 | const memoized = yield* Effect.cached(performTokenRefresh(row, provider, trigger)); |
| 2063 | const gated = memoized.pipe( |
| 2064 | Effect.ensuring(Effect.sync(() => refreshInFlight.delete(key))), |
| 2065 | ); |
| 2066 | // Re-check after building (a peer fiber may have registered first while |
| 2067 | // we built ours) so everyone converges on the same shared grant. |
| 2068 | const winner = refreshInFlight.get(key) ?? gated; |
| 2069 | if (winner === gated) refreshInFlight.set(key, gated); |
| 2070 | return yield* winner; |
| 2071 | }); |
| 2072 | |
| 2073 | // Resolve every named input of a connection (`variable → value`). A |
| 2074 | // single-secret connection yields `{ token: <value> }`; an apiKey method with |
no test coverage detected