@bacnh85/pi-web 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/searxng.ts DELETED
@@ -1,63 +0,0 @@
1
- // SearXNG metasearch client.
2
-
3
- import { sanitizeSnippet } from "./format";
4
- import { signalWithTimeout, withRetry } from "./retry";
5
-
6
- export interface SearxngResultItem {
7
- title: string;
8
- url: string;
9
- snippet: string;
10
- engine: string;
11
- category: string;
12
- score?: number;
13
- publishedDate: string;
14
- }
15
-
16
- export interface SearxngResponse {
17
- results: SearxngResultItem[];
18
- [key: string]: unknown;
19
- }
20
-
21
- export async function fetchSearxngResults(
22
- params: Record<string, unknown>,
23
- baseUrl: string,
24
- signal?: AbortSignal,
25
- ): Promise<SearxngResponse> {
26
- return withRetry(async () => {
27
- const limit = Math.min(50, Math.max(1, (params.count as number) ?? 5));
28
- const url = new URL(`${baseUrl}/search`);
29
- url.searchParams.set("q", params.query as string);
30
- url.searchParams.set("format", "json");
31
- url.searchParams.set("pageno", String(Math.max(1, (params.pageno as number) ?? 1)));
32
- for (const key of ["categories", "engines", "language", "time_range", "safesearch"] as const) {
33
- const val = params[key];
34
- if (val !== undefined && val !== "") url.searchParams.set(key, String(val));
35
- }
36
- const timeoutMs = (params.timeout_ms as number) || 15000;
37
- const response = await fetch(url, {
38
- headers: { Accept: "application/json", "User-Agent": "pi-web/0.1 SearXNG" },
39
- signal: signalWithTimeout(timeoutMs, signal),
40
- });
41
- const text = await response.text();
42
- if (!response.ok) {
43
- const hint =
44
- response.status === 403
45
- ? "\nHint: enable JSON output in SearXNG settings.yml with search.formats including json."
46
- : "";
47
- throw new Error(`HTTP ${response.status}: ${response.statusText}${hint}${text ? `\n${text}` : ""}`);
48
- }
49
- const data: any = text ? JSON.parse(text) : {};
50
- const results = (data.results || [])
51
- .slice(0, limit)
52
- .map((r: any) => ({
53
- title: sanitizeSnippet(r.title || ""),
54
- url: r.url || "",
55
- snippet: sanitizeSnippet(r.content || r.snippet || ""),
56
- engine: r.engine || "",
57
- category: r.category || "",
58
- score: r.score,
59
- publishedDate: r.publishedDate || r.published_date || "",
60
- }));
61
- return { ...data, results };
62
- }, { maxRetries: 2, retryableErrors: ["timeout", "econn", "etimedout", "network", "socket"] });
63
- }
File without changes
File without changes