(
row: ConnectionRow,
provider: CredentialProvider,
trigger: RefreshTrigger = "proactive",
)
| 1944 | ); |
| 1945 | |
| 1946 | const refreshConnectionToken = ( |
| 1947 | row: ConnectionRow, |
| 1948 | provider: CredentialProvider, |
| 1949 | trigger: RefreshTrigger = "proactive", |
| 1950 | ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> => |
| 1951 | // Share a single refresh per connection so concurrent resolves of the same |
| 1952 | // connection all await one refresh-token grant (the AS rotates the refresh |
| 1953 | // token; parallel grants would race on a consumed token — v1's refresh |
| 1954 | // deferred-map). The gate is cleared once the refresh settles so a later |
| 1955 | // expiry can refresh again. |
| 1956 | Effect.gen(function* () { |
| 1957 | const key = connectionKey(row); |
| 1958 | // Joining an in-flight grant is correct for BOTH triggers: whatever |
| 1959 | // that peer mints is newer than the token this fiber just saw rejected, |
| 1960 | // which is exactly what a reactive retry wants. The gate is cleared on |
| 1961 | // settle, so a 401 arriving after a refresh completed starts a fresh |
| 1962 | // grant rather than replaying the stale memoized one. |
| 1963 | const existing = refreshInFlight.get(key); |
| 1964 | if (existing) return yield* existing; |
| 1965 | // `Effect.cached` memoizes the grant onto a deferred: it runs once and |
| 1966 | // replays to every awaiter sharing this entry. |
| 1967 | const memoized = yield* Effect.cached(performTokenRefresh(row, provider, trigger)); |
| 1968 | const gated = memoized.pipe( |
| 1969 | Effect.ensuring(Effect.sync(() => refreshInFlight.delete(key))), |
| 1970 | ); |
| 1971 | // Re-check after building (a peer fiber may have registered first while |
| 1972 | // we built ours) so everyone converges on the same shared grant. |
| 1973 | const winner = refreshInFlight.get(key) ?? gated; |
| 1974 | if (winner === gated) refreshInFlight.set(key, gated); |
| 1975 | return yield* winner; |
| 1976 | }); |
| 1977 | |
| 1978 | // Resolve every named input of a connection (`variable → value`). A |
| 1979 | // single-secret connection yields `{ token: <value> }`; an apiKey method with |
no test coverage detected