( isNonInteractiveSession: boolean, )
| 469 | } |
| 470 | |
| 471 | export async function getApiKeyFromApiKeyHelper( |
| 472 | isNonInteractiveSession: boolean, |
| 473 | ): Promise<string | null> { |
| 474 | if (!getConfiguredApiKeyHelper()) return null |
| 475 | const ttl = calculateApiKeyHelperTTL() |
| 476 | if (_apiKeyHelperCache) { |
| 477 | if (Date.now() - _apiKeyHelperCache.timestamp < ttl) { |
| 478 | return _apiKeyHelperCache.value |
| 479 | } |
| 480 | // Stale — return stale value now, refresh in the background. |
| 481 | // `??=` banned here by eslint no-nullish-assign-object-call (bun bug). |
| 482 | if (!_apiKeyHelperInflight) { |
| 483 | _apiKeyHelperInflight = { |
| 484 | promise: _runAndCache( |
| 485 | isNonInteractiveSession, |
| 486 | false, |
| 487 | _apiKeyHelperEpoch, |
| 488 | ), |
| 489 | startedAt: null, |
| 490 | } |
| 491 | } |
| 492 | return _apiKeyHelperCache.value |
| 493 | } |
| 494 | // Cold cache — deduplicate concurrent calls |
| 495 | if (_apiKeyHelperInflight) return _apiKeyHelperInflight.promise |
| 496 | _apiKeyHelperInflight = { |
| 497 | promise: _runAndCache(isNonInteractiveSession, true, _apiKeyHelperEpoch), |
| 498 | startedAt: Date.now(), |
| 499 | } |
| 500 | return _apiKeyHelperInflight.promise |
| 501 | } |
| 502 | |
| 503 | async function _runAndCache( |
| 504 | isNonInteractiveSession: boolean, |
no test coverage detected