Repository navigation
Expand file tree
/
Copy pathapi.ts
More file actions
53 lines (48 loc) · 2.14 KB
/
Copy pathapi.ts
File metadata and controls
53 lines (48 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// search.nixos.org's Elasticsearch backend, with the public credentials its own page uses.
import { storage } from "@backslash/api";
import { type Backend, FALLBACK, indexName, parseBackend, scriptUrl } from "./lib.ts";
const SITE = "https://search.nixos.org";
const DAY = 24 * 60 * 60 * 1000;
/** The index version and channels change a few times a year: read them from the site, once a day. */
export async function backend(signal?: AbortSignal, force = false): Promise<Backend> {
const cached = (await storage.get("backend").catch(() => undefined)) as unknown as
| { at: number; value: Backend }
| undefined;
if (!force && cached && Date.now() - cached.at < DAY) return cached.value;
try {
const page = await (await fetch(`${SITE}/packages`, { signal })).text();
const script = scriptUrl(page);
const found = script ? parseBackend(await (await fetch(script, { signal })).text()) : undefined;
if (found) {
await storage.set("backend", { at: Date.now(), value: found } as never).catch(() => {});
return found;
}
} catch (e) {
if (signal?.aborted) throw e;
}
return cached?.value ?? FALLBACK;
}
async function post(b: Backend, channel: string, body: unknown, signal?: AbortSignal): Promise<Response> {
try {
return await fetch(`${SITE}/backend/${indexName(b, channel)}/_search`, {
method: "POST",
signal,
headers: { Authorization: `Basic ${btoa(`${b.username}:${b.password}`)}`, "Content-Type": "application/json" },
body: JSON.stringify(body),
});
} catch (e) {
if (signal?.aborted) throw e;
throw new Error("search.nixos.org can't be reached: check your connection");
}
}
/** Runs a query on a channel's index; reads the index name again if it moved. */
export async function query(channel: string, body: unknown, signal?: AbortSignal): Promise<any> {
let b = await backend(signal);
let res = await post(b, channel, body, signal);
if (res.status === 404 || res.status === 401) {
b = await backend(signal, true);
res = await post(b, channel, body, signal);
}
if (!res.ok) throw new Error(`search.nixos.org answered ${res.status}`);
return await res.json();
}