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

Function refreshConnectionToken

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

Source from the content-addressed store, hash-verified

1702 );
1703
1704 const refreshConnectionToken = (
1705 row: ConnectionRow,
1706 provider: CredentialProvider,
1707 trigger: RefreshTrigger = "proactive",
1708 ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> =>
1709 // Share a single refresh per connection so concurrent resolves of the same
1710 // connection all await one refresh-token grant (the AS rotates the refresh
1711 // token; parallel grants would race on a consumed token — v1's refresh
1712 // deferred-map). The gate is cleared once the refresh settles so a later
1713 // expiry can refresh again.
1714 Effect.gen(function* () {
1715 const key = connectionKey(row);
1716 // Joining an in-flight grant is correct for BOTH triggers: whatever
1717 // that peer mints is newer than the token this fiber just saw rejected,
1718 // which is exactly what a reactive retry wants. The gate is cleared on
1719 // settle, so a 401 arriving after a refresh completed starts a fresh
1720 // grant rather than replaying the stale memoized one.
1721 const existing = refreshInFlight.get(key);
1722 if (existing) return yield* existing;
1723 // `Effect.cached` memoizes the grant onto a deferred: it runs once and
1724 // replays to every awaiter sharing this entry.
1725 const memoized = yield* Effect.cached(performTokenRefresh(row, provider, trigger));
1726 const gated = memoized.pipe(
1727 Effect.ensuring(Effect.sync(() => refreshInFlight.delete(key))),
1728 );
1729 // Re-check after building (a peer fiber may have registered first while
1730 // we built ours) so everyone converges on the same shared grant.
1731 const winner = refreshInFlight.get(key) ?? gated;
1732 if (winner === gated) refreshInFlight.set(key, gated);
1733 return yield* winner;
1734 });
1735
1736 // Resolve every named input of a connection (`variable → value`). A
1737 // 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