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