From 1763ee9d1fe46b9979777e7672f542de1e02c96c Mon Sep 17 00:00:00 2001 From: Joaquin Date: Wed, 4 Mar 2026 10:02:51 -0300 Subject: [PATCH] Add timeout and validation to SearXNG search --- src/lib/searxng.ts | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/lib/searxng.ts b/src/lib/searxng.ts index a0bcd835..87767e09 100644 --- a/src/lib/searxng.ts +++ b/src/lib/searxng.ts @@ -38,11 +38,30 @@ export const searchSearxng = async ( }); } - const res = await fetch(url); - const data = await res.json(); + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), 10000); - const results: SearxngSearchResult[] = data.results; - const suggestions: string[] = data.suggestions; + try { + const res = await fetch(url, { + signal: controller.signal, + }); - return { results, suggestions }; + if (!res.ok) { + throw new Error(`SearXNG error: ${res.statusText}`); + } + + const data = await res.json(); + + const results: SearxngSearchResult[] = data.results; + const suggestions: string[] = data.suggestions; + + return { results, suggestions }; + } catch (err: any) { + if (err.name === 'AbortError') { + throw new Error('SearXNG search timed out'); + } + throw err; + } finally { + clearTimeout(timeoutId); + } };