From 86ab3cf6e471fa363fd092343a30d75731d711cc Mon Sep 17 00:00:00 2001 From: jaycdave88 Date: Sun, 19 Apr 2026 13:24:48 -0400 Subject: [PATCH] perf: add fetch timeouts to currency and pricing HTTP calls --- src/currency.ts | 3 ++- src/models.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/currency.ts b/src/currency.ts index 8788e07..c50d894 100644 --- a/src/currency.ts +++ b/src/currency.ts @@ -11,6 +11,7 @@ type CurrencyState = { } const CACHE_TTL_MS = 24 * 60 * 60 * 1000 +const FETCH_TIMEOUT_MS = 10_000 const FRANKFURTER_URL = 'https://api.frankfurter.app/latest?from=USD&to=' // Defensive bounds on any fetched FX rate. Outside this band the rate is either a parser bug // or a tampered Frankfurter response, and we refuse to multiply it into displayed costs. @@ -63,7 +64,7 @@ function getRateCachePath(): string { } async function fetchRate(code: string): Promise { - const response = await fetch(`${FRANKFURTER_URL}${code}`) + const response = await fetch(`${FRANKFURTER_URL}${code}`, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) }) if (!response.ok) throw new Error(`HTTP ${response.status}`) const data = await response.json() as { rates?: Record } const rate = data.rates?.[code] diff --git a/src/models.ts b/src/models.ts index 1eaaf2c..d4ad600 100644 --- a/src/models.ts +++ b/src/models.ts @@ -20,6 +20,7 @@ type LiteLLMEntry = { } const LITELLM_URL = 'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json' +const FETCH_TIMEOUT_MS = 10_000 const CACHE_TTL_MS = 24 * 60 * 60 * 1000 const WEB_SEARCH_COST = 0.01 @@ -74,7 +75,7 @@ function parseLiteLLMEntry(entry: LiteLLMEntry): ModelCosts | null { } async function fetchAndCachePricing(): Promise> { - const response = await fetch(LITELLM_URL) + const response = await fetch(LITELLM_URL, { signal: AbortSignal.timeout(FETCH_TIMEOUT_MS) }) if (!response.ok) throw new Error(`HTTP ${response.status}`) const data = await response.json() as Record const pricing = new Map()