fix: validate URL in fetchLlmsTxt

Prevent crash on invalid URLs.

Changes:
- Wrap URL constructor in try/catch
- Return null for invalid URLs instead of throwing

(cherry picked from commit 8b0acf314d0d8c84d6bf896438136e09caf8ba42)
This commit is contained in:
linked-danis
2026-03-12 14:09:23 +01:00
committed by Simon
parent 5695b3d6aa
commit b4acd02007

View File

@@ -61,7 +61,12 @@ const llmsTxtCache = new Map<string, string | null>()
/** Fetch /llms.txt for a URL's origin. Cached per origin, `null` = tried and not found. */ /** Fetch /llms.txt for a URL's origin. Cached per origin, `null` = tried and not found. */
export async function fetchLlmsTxt(url: string): Promise<string | null> { export async function fetchLlmsTxt(url: string): Promise<string | null> {
const origin = new URL(url).origin let origin: string
try {
origin = new URL(url).origin
} catch {
return null // Invalid URL
}
if (llmsTxtCache.has(origin)) return llmsTxtCache.get(origin)! if (llmsTxtCache.has(origin)) return llmsTxtCache.get(origin)!
const endpoint = `${origin}/llms.txt` const endpoint = `${origin}/llms.txt`