| 163 | }; |
| 164 | |
| 165 | const fetchJwksOnce = async ( |
| 166 | url: URL, |
| 167 | fetchImpl: typeof globalThis.fetch, |
| 168 | timeoutMs: number, |
| 169 | ): Promise<JSONWebKeySet> => { |
| 170 | const controller = new AbortController(); |
| 171 | const timer = setTimeout(() => controller.abort(), timeoutMs); |
| 172 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: fetch adapter must clear abort timer while preserving promise rejection behavior |
| 173 | try { |
| 174 | const response = await fetchImpl(url.toString(), { |
| 175 | method: "GET", |
| 176 | headers: { accept: "application/json" }, |
| 177 | signal: controller.signal, |
| 178 | }); |
| 179 | |
| 180 | if (!response.ok) { |
| 181 | // oxlint-disable-next-line executor/no-try-catch-or-throw, executor/no-error-constructor -- boundary: fetch-backed JWT key resolver must reject with the existing Error cause shape |
| 182 | throw new Error(`JWKS fetch failed: ${response.status} ${response.statusText}`); |
| 183 | } |
| 184 | |
| 185 | const body = await response.json(); |
| 186 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: fetch JSON validation maps Schema failures to the existing malformed JWKS rejection |
| 187 | try { |
| 188 | await decodeJsonWebKeySetPayload(body); |
| 189 | return body as JSONWebKeySet; |
| 190 | } catch { |
| 191 | // oxlint-disable-next-line executor/no-try-catch-or-throw, executor/no-error-constructor -- boundary: fetch JSON validation preserves the existing malformed JWKS rejection |
| 192 | throw new Error("JWKS fetch returned malformed payload"); |
| 193 | } |
| 194 | } finally { |
| 195 | clearTimeout(timer); |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | // --------------------------------------------------------------------------- |
| 200 | // Workers Cache API store |