MCPcopy Create free account
hub / github.com/Noumena-Network/code / scheduleFromExpiresIn

Function scheduleFromExpiresIn

src/bridge/jwtUtils.ts:148–164  ·  view source on GitHub ↗

* Schedule refresh using an explicit TTL (seconds until expiry) rather * than decoding a JWT's exp claim. Used by callers whose JWT is opaque * (e.g. POST /v1/code/sessions/{id}/bridge returns expires_in directly).

(
    sessionId: string,
    expiresInSeconds: number,
  )

Source from the content-addressed store, hash-verified

146 * (e.g. POST /v1/code/sessions/{id}/bridge returns expires_in directly).
147 */
148 function scheduleFromExpiresIn(
149 sessionId: string,
150 expiresInSeconds: number,
151 ): void {
152 const existing = timers.get(sessionId)
153 if (existing) clearTimeout(existing)
154 const gen = nextGeneration(sessionId)
155 // Clamp to 30s floor — if refreshBufferMs exceeds the server's expires_in
156 // (e.g. very large buffer for frequent-refresh testing, or server shortens
157 // expires_in unexpectedly), unclamped delayMs ≤ 0 would tight-loop.
158 const delayMs = Math.max(expiresInSeconds * 1000 - refreshBufferMs, 30_000)
159 logForDebugging(
160 `[${label}:token] Scheduled token refresh for sessionId=${sessionId} in ${formatDuration(delayMs)} (expires_in=${expiresInSeconds}s, buffer=${refreshBufferMs / 1000}s)`,
161 )
162 const timer = setTimeout(doRefresh, delayMs, sessionId, gen)
163 timers.set(sessionId, timer)
164 }
165
166 async function doRefresh(sessionId: string, gen: number): Promise<void> {
167 let oauthToken: string | undefined

Callers

nothing calls this directly

Calls 6

nextGenerationFunction · 0.85
maxMethod · 0.80
setMethod · 0.80
formatDurationFunction · 0.70
logForDebuggingFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected