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

Function schedule

src/bridge/jwtUtils.ts:103–141  ·  view source on GitHub ↗
(sessionId: string, token: string)

Source from the content-addressed store, hash-verified

101 }
102
103 function schedule(sessionId: string, token: string): void {
104 const expiry = decodeJwtExpiry(token)
105 if (!expiry) {
106 // Token is not a decodable JWT (e.g. an OAuth token passed from the
107 // REPL bridge WebSocket open handler). Preserve any existing timer
108 // (such as the follow-up refresh set by doRefresh) so the refresh
109 // chain is not broken.
110 logForDebugging(
111 `[${label}:token] Could not decode JWT expiry for sessionId=${sessionId}, token prefix=${token.slice(0, 15)}…, keeping existing timer`,
112 )
113 return
114 }
115
116 // Clear any existing refresh timer — we have a concrete expiry to replace it.
117 const existing = timers.get(sessionId)
118 if (existing) {
119 clearTimeout(existing)
120 }
121
122 // Bump generation to invalidate any in-flight async doRefresh.
123 const gen = nextGeneration(sessionId)
124
125 const expiryDate = new Date(expiry * 1000).toISOString()
126 const delayMs = expiry * 1000 - Date.now() - refreshBufferMs
127 if (delayMs <= 0) {
128 logForDebugging(
129 `[${label}:token] Token for sessionId=${sessionId} expires=${expiryDate} (past or within buffer), refreshing immediately`,
130 )
131 void doRefresh(sessionId, gen)
132 return
133 }
134
135 logForDebugging(
136 `[${label}:token] Scheduled token refresh for sessionId=${sessionId} in ${formatDuration(delayMs)} (expires=${expiryDate}, buffer=${refreshBufferMs / 1000}s)`,
137 )
138
139 const timer = setTimeout(doRefresh, delayMs, sessionId, gen)
140 timers.set(sessionId, timer)
141 }
142
143 /**
144 * Schedule refresh using an explicit TTL (seconds until expiry) rather

Callers 2

tickMethod · 0.85
startHeartbeatMethod · 0.85

Calls 7

decodeJwtExpiryFunction · 0.85
nextGenerationFunction · 0.85
doRefreshFunction · 0.85
setMethod · 0.80
formatDurationFunction · 0.70
logForDebuggingFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected