( deps: PollLoginStatusDeps, options: PollLoginStatusOptions, )
| 79 | | { status: 'aborted' } |
| 80 | |
| 81 | export async function pollLoginStatus( |
| 82 | deps: PollLoginStatusDeps, |
| 83 | options: PollLoginStatusOptions, |
| 84 | ): Promise<PollLoginStatusResult> { |
| 85 | const { sleep, logger, apiClient: providedApiClient } = deps |
| 86 | const { |
| 87 | baseUrl, |
| 88 | fingerprintId, |
| 89 | fingerprintHash, |
| 90 | expiresAt, |
| 91 | intervalMs = 5000, |
| 92 | timeoutMs = 5 * 60 * 1000, |
| 93 | shouldContinue, |
| 94 | } = options |
| 95 | |
| 96 | const now = deps.now ?? Date.now |
| 97 | const startTime = now() |
| 98 | let attempts = 0 |
| 99 | |
| 100 | const apiClient = |
| 101 | providedApiClient ?? |
| 102 | createCodebuffApiClient({ |
| 103 | baseUrl, |
| 104 | }) |
| 105 | |
| 106 | while (true) { |
| 107 | if (shouldContinue && !shouldContinue()) { |
| 108 | logger.warn('🛑 Polling aborted by caller') |
| 109 | return { status: 'aborted' } |
| 110 | } |
| 111 | |
| 112 | if (now() - startTime >= timeoutMs) { |
| 113 | logger.warn('⌛️ Login polling timed out') |
| 114 | return { status: 'timeout' } |
| 115 | } |
| 116 | |
| 117 | attempts += 1 |
| 118 | |
| 119 | try { |
| 120 | const response = await apiClient.loginStatus({ |
| 121 | fingerprintId, |
| 122 | fingerprintHash, |
| 123 | expiresAt, |
| 124 | }) |
| 125 | |
| 126 | if (!response.ok) { |
| 127 | if (response.status !== 401) { |
| 128 | logger.warn( |
| 129 | { |
| 130 | attempts, |
| 131 | status: response.status, |
| 132 | error: response.error, |
| 133 | }, |
| 134 | '⚠️ Unexpected status while polling', |
| 135 | ) |
| 136 | } |
| 137 | await sleep(intervalMs) |
| 138 | continue |
no test coverage detected