(
detectGh: DetectGhFn | undefined,
options: GhTokenSourceOptions = {},
)
| 19 | const DEFAULT_TTL_MS = 60_000; |
| 20 | |
| 21 | export function createGhTokenSource( |
| 22 | detectGh: DetectGhFn | undefined, |
| 23 | options: GhTokenSourceOptions = {}, |
| 24 | ): GhTokenSource { |
| 25 | const ttlMs = options.ttlMs ?? DEFAULT_TTL_MS; |
| 26 | const now = options.now ?? Date.now; |
| 27 | const cache = new Map<string, CacheEntry>(); |
| 28 | |
| 29 | return { |
| 30 | get(host: string): RelayGhToken | null { |
| 31 | if (!detectGh) return null; |
| 32 | |
| 33 | const t = now(); |
| 34 | const cached = cache.get(host); |
| 35 | if (cached && cached.expiresAt > t) { |
| 36 | return cached.token != null ? { token: cached.token, host } : null; |
| 37 | } |
| 38 | |
| 39 | const result = detectGh(host); |
| 40 | const token = result.available && result.token ? result.token : null; |
| 41 | cache.set(host, { token, expiresAt: t + ttlMs }); |
| 42 | return token != null ? { token, host } : null; |
| 43 | }, |
| 44 | |
| 45 | invalidate(): void { |
| 46 | cache.clear(); |
| 47 | }, |
| 48 | }; |
| 49 | } |
no outgoing calls
no test coverage detected