(forceRefresh: boolean)
| 341 | }; |
| 342 | |
| 343 | const ensureFresh = async (forceRefresh: boolean): Promise<CacheEntry> => { |
| 344 | if (forceRefresh) return refreshBlocking(); |
| 345 | if (entry && isFresh(entry)) return entry; |
| 346 | |
| 347 | // Memory is stale but still usable: serve it now, revalidate behind it. |
| 348 | if (entry && isUsable(entry)) { |
| 349 | refreshInBackground(); |
| 350 | return entry; |
| 351 | } |
| 352 | |
| 353 | // Cold isolate — the L2 store saves us the upstream round trip. |
| 354 | const stored = await loadFromStore(); |
| 355 | if (stored) { |
| 356 | if (!isFresh(stored)) refreshInBackground(); |
| 357 | return stored; |
| 358 | } |
| 359 | |
| 360 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: a failed refresh must fall back to stale keys rather than fail the verify |
| 361 | try { |
| 362 | return await refreshBlocking(); |
| 363 | } catch (error) { |
| 364 | // Upstream is slow or down. Last good keys beat failing every request. |
| 365 | if (entry && isUsable(entry)) return entry; |
| 366 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: nothing usable is cached, so the upstream failure is the real answer |
| 367 | throw error; |
| 368 | } |
| 369 | }; |
| 370 | |
| 371 | const get: JWTVerifyGetKey = async (protectedHeader, token) => { |
| 372 | const current = await ensureFresh(false); |
no test coverage detected