MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / refreshConnectionToken

Function refreshConnectionToken

packages/core/sdk/src/executor.ts:2087–2117  ·  view source on GitHub ↗
(
      row: ConnectionRow,
      provider: CredentialProvider,
      trigger: RefreshTrigger = "proactive",
    )

Source from the content-addressed store, hash-verified

2085 );
2086
2087 const refreshConnectionToken = (
2088 row: ConnectionRow,
2089 provider: CredentialProvider,
2090 trigger: RefreshTrigger = "proactive",
2091 ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> =>
2092 // Share a single refresh per connection so concurrent resolves of the same
2093 // connection all await one refresh-token grant (the AS rotates the refresh
2094 // token; parallel grants would race on a consumed token — v1's refresh
2095 // deferred-map). The gate is cleared once the refresh settles so a later
2096 // expiry can refresh again.
2097 Effect.gen(function* () {
2098 const key = connectionKey(row);
2099 // Joining an in-flight grant is correct for BOTH triggers: whatever
2100 // that peer mints is newer than the token this fiber just saw rejected,
2101 // which is exactly what a reactive retry wants. The gate is cleared on
2102 // settle, so a 401 arriving after a refresh completed starts a fresh
2103 // grant rather than replaying the stale memoized one.
2104 const existing = refreshInFlight.get(key);
2105 if (existing) return yield* existing;
2106 // `Effect.cached` memoizes the grant onto a deferred: it runs once and
2107 // replays to every awaiter sharing this entry.
2108 const memoized = yield* Effect.cached(performTokenRefresh(row, provider, trigger));
2109 const gated = memoized.pipe(
2110 Effect.ensuring(Effect.sync(() => refreshInFlight.delete(key))),
2111 );
2112 // Re-check after building (a peer fiber may have registered first while
2113 // we built ours) so everyone converges on the same shared grant.
2114 const winner = refreshInFlight.get(key) ?? gated;
2115 if (winner === gated) refreshInFlight.set(key, gated);
2116 return yield* winner;
2117 });
2118
2119 // Resolve every named input of a connection (`variable → value`). A
2120 // single-secret connection yields `{ token: <value> }`; an apiKey method with

Callers 2

resolveConnectionValuesFunction · 0.85

Calls 6

connectionKeyFunction · 0.85
performTokenRefreshFunction · 0.85
setMethod · 0.80
getMethod · 0.65
syncMethod · 0.65
deleteMethod · 0.65

Tested by

no test coverage detected