MCPcopy Index your code
hub / github.com/inkeep/open-knowledge / createGhTokenSource

Function createGhTokenSource

packages/server/src/gh-token-source.ts:21–49  ·  view source on GitHub ↗
(
  detectGh: DetectGhFn | undefined,
  options: GhTokenSourceOptions = {},
)

Source from the content-addressed store, hash-verified

19const DEFAULT_TTL_MS = 60_000;
20
21export 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}

Callers 2

constructorMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected