starfleet-ui
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#6 · main · 2 min 01 s
0Étoiles
0Forks
0Issues
| 1 | import { createHash } from 'node:crypto'; |
| 2 | |
| 3 | /** |
| 4 | * Tiny in-process TTL cache for hot server-side reads (system details, influence). |
| 5 | * |
| 6 | * - Stale-while-revalidate: an expired entry is served immediately while one refresh runs |
| 7 | * in the background, so a page never waits on a slow galaxy-wide refetch twice. |
| 8 | * - Bounded: entries past `maxStale` are dropped, and the map never exceeds `MAX_ENTRIES` |
| 9 | * (oldest-inserted evicted first). |
| 10 | * - Concurrent misses for the same key share one in-flight request. |
| 11 | */ |
| 12 | |
| 13 | interface CacheEntry<T> { |
| 14 | value: T; |
| 15 | expires: number; |
| 16 | } |
| 17 | |
| 18 | const MAX_ENTRIES = 500; |
| 19 | const store = new Map<string, CacheEntry<unknown>>(); |
| 20 | const inflight = new Map<string, Promise<unknown>>(); |
| 21 | |
| 22 | /** Cache namespace for a captain: a hash of the full key, never the key itself. */ |
| 23 | export function captainKey(token: string): string { |
| 24 | return createHash('sha256').update(token).digest('hex').slice(0, 24); |
| 25 | } |
| 26 | |
| 27 | function refresh<T>(key: string, ttlMs: number, produce: () => Promise<T>): Promise<T> { |
| 28 | const running = inflight.get(key) as Promise<T> | undefined; |
| 29 | if (running) return running; |
| 30 | const p = produce() |
| 31 | .then((value) => { |
| 32 | store.delete(key); // re-insert so Map order tracks freshness |
| 33 | store.set(key, { value, expires: Date.now() + ttlMs }); |
| 34 | while (store.size > MAX_ENTRIES) store.delete(store.keys().next().value!); |
| 35 | return value; |
| 36 | }) |
| 37 | .finally(() => inflight.delete(key)); |
| 38 | inflight.set(key, p); |
| 39 | return p; |
| 40 | } |
| 41 | |
| 42 | export async function cached<T>( |
| 43 | key: string, |
| 44 | ttlMs: number, |
| 45 | produce: () => Promise<T>, |
| 46 | { maxStaleMs = ttlMs * 10 }: { maxStaleMs?: number } = {} |
| 47 | ): Promise<T> { |
| 48 | const hit = store.get(key) as CacheEntry<T> | undefined; |
| 49 | const now = Date.now(); |
| 50 | if (hit && hit.expires > now) return hit.value; |
| 51 | if (hit && hit.expires + maxStaleMs > now) { |
| 52 | refresh(key, ttlMs, produce).catch(() => {}); // keep serving stale on failure |
| 53 | return hit.value; |
| 54 | } |
| 55 | if (hit) store.delete(key); |
| 56 | return refresh(key, ttlMs, produce); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Map over `items` with at most `limit` calls in flight, settling every one (like Promise.allSettled). |
| 61 | * Keeps galaxy-wide fan-outs under the API's 120 requests/minute. |
| 62 | */ |
| 63 | export async function mapSettled<T, R>( |
| 64 | items: readonly T[], |
| 65 | limit: number, |
| 66 | fn: (item: T) => Promise<R> |
| 67 | ): Promise<PromiseSettledResult<R>[]> { |
| 68 | const results: PromiseSettledResult<R>[] = new Array(items.length); |
| 69 | let next = 0; |
| 70 | async function worker() { |
| 71 | while (next < items.length) { |
| 72 | const i = next++; |
| 73 | try { |
| 74 | results[i] = { status: 'fulfilled', value: await fn(items[i]) }; |
| 75 | } catch (reason) { |
| 76 | results[i] = { status: 'rejected', reason }; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker)); |
| 81 | return results; |
| 82 | } |