(url: string, init: RequestInit)
| 91 | async function fetchWithTimeout(url: string, init: RequestInit): Promise<Response | undefined> { |
| 92 | try { |
| 93 | return await fetch(url, { |
| 94 | ...init, |
| 95 | signal: AbortSignal.timeout(MODEL_VALIDATION_TIMEOUT_MS), |
| 96 | }); |
| 97 | } catch { |
| 98 | return undefined; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | function isTransientValidationStatus(status: number): boolean { |
| 103 | return status === 429 || (status >= 500 && status <= 599); |
| 104 | } |
| 105 | |
| 106 | async function waitBeforeValidationRetry(attempt: number): Promise<void> { |
| 107 | const delayMs = MODEL_VALIDATION_RETRY_BASE_DELAY_MS * 2 ** attempt; |
| 108 | await new Promise(resolve => setTimeout(resolve, delayMs)); |
| 109 | } |
| 110 | |
| 111 | async function validateEndpoint(url: string, init: RequestInit): Promise<EndpointValidationResult> { |
| 112 | for (let attempt = 0; attempt < MODEL_VALIDATION_MAX_ATTEMPTS; attempt += 1) { |
| 113 | const response = await fetchWithTimeout(url, init); |
| 114 | if (!response) { |
| 115 | if (attempt < MODEL_VALIDATION_MAX_ATTEMPTS - 1) { |
| 116 | await waitBeforeValidationRetry(attempt); |
| 117 | } |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | if (!response.ok) { |
| 122 | if (!isTransientValidationStatus(response.status)) { |
| 123 | return { type: 'http-error', status: response.status }; |
| 124 | } |
| 125 | if (attempt < MODEL_VALIDATION_MAX_ATTEMPTS - 1) { |
| 126 | await waitBeforeValidationRetry(attempt); |
| 127 | } |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | let body: unknown; |
| 132 | try { |
| 133 | body = await response.json(); |
| 134 | } catch { |
| 135 | if (attempt < MODEL_VALIDATION_MAX_ATTEMPTS - 1) { |
| 136 | await waitBeforeValidationRetry(attempt); |
no test coverage detected