Add timeout and validation to SearXNG search

This commit is contained in:
Joaquin
2026-03-04 10:02:51 -03:00
parent 86274326e9
commit 1763ee9d1f

View File

@@ -38,11 +38,30 @@ export const searchSearxng = async (
}); });
} }
const res = await fetch(url); const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 10000);
try {
const res = await fetch(url, {
signal: controller.signal,
});
if (!res.ok) {
throw new Error(`SearXNG error: ${res.statusText}`);
}
const data = await res.json(); const data = await res.json();
const results: SearxngSearchResult[] = data.results; const results: SearxngSearchResult[] = data.results;
const suggestions: string[] = data.suggestions; const suggestions: string[] = data.suggestions;
return { results, suggestions }; return { results, suggestions };
} catch (err: any) {
if (err.name === 'AbortError') {
throw new Error('SearXNG search timed out');
}
throw err;
} finally {
clearTimeout(timeoutId);
}
}; };