( isNonInteractiveSession: boolean, )
| 517 | } |
| 518 | |
| 519 | export async function getApiKeyFromApiKeyHelper( |
| 520 | isNonInteractiveSession: boolean, |
| 521 | ): Promise<string | null> { |
| 522 | if (!getConfiguredApiKeyHelper()) return null |
| 523 | const ttl = calculateApiKeyHelperTTL() |
| 524 | if (_apiKeyHelperCache) { |
| 525 | if (Date.now() - _apiKeyHelperCache.timestamp < ttl) { |
| 526 | return _apiKeyHelperCache.value |
| 527 | } |
| 528 | // Stale — return stale value now, refresh in the background. |
| 529 | // `??=` banned here by eslint no-nullish-assign-object-call (bun bug). |
| 530 | if (!_apiKeyHelperInflight) { |
| 531 | _apiKeyHelperInflight = { |
| 532 | promise: _runAndCache( |
| 533 | isNonInteractiveSession, |
| 534 | false, |
| 535 | _apiKeyHelperEpoch, |
| 536 | ), |
| 537 | startedAt: null, |
| 538 | } |
| 539 | } |
| 540 | return _apiKeyHelperCache.value |
| 541 | } |
| 542 | // Cold cache — deduplicate concurrent calls |
| 543 | if (_apiKeyHelperInflight) return _apiKeyHelperInflight.promise |
| 544 | _apiKeyHelperInflight = { |
| 545 | promise: _runAndCache(isNonInteractiveSession, true, _apiKeyHelperEpoch), |
| 546 | startedAt: Date.now(), |
| 547 | } |
| 548 | return _apiKeyHelperInflight.promise |
| 549 | } |
| 550 | |
| 551 | async function _runAndCache( |
| 552 | isNonInteractiveSession: boolean, |
no test coverage detected