(pkce: PkceCodes, state: string)
| 415 | } |
| 416 | |
| 417 | function waitForOAuthCallback(pkce: PkceCodes, state: string): Promise<TokenResponse> { |
| 418 | // A previous in-flight authorize() that the user abandoned (or that is |
| 419 | // being superseded by a fresh attempt) still owns `pendingOAuth`. Reject |
| 420 | // it eagerly so its caller stops waiting on a state value that can never |
| 421 | // match the next callback. |
| 422 | if (pendingOAuth) { |
| 423 | pendingOAuth.reject(new Error("Superseded by a newer xAI authorize request")) |
| 424 | pendingOAuth = undefined |
| 425 | } |
| 426 | return new Promise((resolve, reject) => { |
| 427 | const timeout = setTimeout( |
| 428 | () => { |
| 429 | if (pendingOAuth) { |
| 430 | pendingOAuth = undefined |
| 431 | reject(new Error("OAuth callback timeout - authorization took too long")) |
| 432 | } |
| 433 | }, |
| 434 | 5 * 60 * 1000, |
| 435 | ) |
| 436 | |
| 437 | pendingOAuth = { |
| 438 | pkce, |
| 439 | state, |
| 440 | resolve: (tokens) => { |
| 441 | clearTimeout(timeout) |
| 442 | resolve(tokens) |
| 443 | }, |
| 444 | reject: (error) => { |
| 445 | clearTimeout(timeout) |
| 446 | reject(error) |
| 447 | }, |
| 448 | } |
| 449 | }) |
| 450 | } |
| 451 | |
| 452 | interface RefreshResult { |
| 453 | access: string |
no test coverage detected