Repository navigation
Expand file tree
/
Copy pathdevdocs.ts
More file actions
73 lines (63 loc) · 3.04 KB
/
Copy pathdevdocs.ts
File metadata and controls
73 lines (63 loc) · 3.04 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// DevDocs: the list of documentations (cached a week), their indexes (kept for the session), what is enabled.
import { storage } from "@backslash/api";
import { compactDoc, DEFAULT_DOCS, type Doc, type Entry, resolveDocs, splitList } from "./lib.ts";
const WEEK = 7 * 86400_000;
export async function getJson<T>(url: string, signal?: AbortSignal): Promise<T> {
const res = await reach(url, signal);
if (!res.ok) throw new Error(`${new URL(url).host} answered ${res.status}`);
return await res.json() as T;
}
/** fetch(), with an error that says which site could not be reached. */
export async function reach(url: string, signal?: AbortSignal): Promise<Response> {
try {
return await fetch(url, { signal });
} catch (err) {
if (signal?.aborted) throw err;
throw new Error(`Can't reach ${new URL(url).host}: check your connection (${(err as Error).message})`);
}
}
let docsPromise: Promise<Doc[]> | undefined;
/** Every documentation DevDocs has, from storage when fresh enough. */
export function allDocs(force = false): Promise<Doc[]> {
if (!docsPromise || force) {
docsPromise = (async () => {
const cached = (await storage.get("docs")) as unknown as { at: number; docs: Doc[] } | undefined;
if (!force && cached && Date.now() - cached.at < WEEK && cached.docs.length) return cached.docs;
try {
const docs = (await getJson<Doc[]>("https://devdocs.io/docs.json")).map(compactDoc);
await storage.set("docs", JSON.parse(JSON.stringify({ at: Date.now(), docs })));
return docs;
} catch (err) {
if (cached?.docs.length) return cached.docs; // offline: an old list is better than none
throw err;
}
})();
docsPromise.catch(() => (docsPromise = undefined));
}
return docsPromise;
}
/** Slugs picked with "Choose DevDocs"; undefined until the user picks. */
export const chosen = () => storage.get<string[]>("enabled");
export const setChosen = (slugs: string[]) => storage.set("enabled", slugs);
/** Preferences plus the picked ones; the defaults until either says something. */
export async function enabledDocs(prefs: { docs?: string }): Promise<{ docs: Doc[]; fromPrefs: Set<string> }> {
const docs = await allDocs();
const fromPrefs = resolveDocs(splitList(prefs.docs), docs);
const picked = await chosen();
const slugs = picked ?? (fromPrefs.length ? [] : resolveDocs(DEFAULT_DOCS, docs));
const all = [...new Set([...fromPrefs, ...slugs])];
const bySlug = new Map(docs.map((d) => [d.slug, d]));
return { docs: all.flatMap((s) => bySlug.get(s) ?? []), fromPrefs: new Set(fromPrefs) };
}
const indexes = new Map<string, Promise<Entry[]>>();
/** A doc's index. Not tied to a keystroke's signal: it keeps loading while the user types. */
export function docIndex(doc: Doc): Promise<Entry[]> {
let p = indexes.get(doc.slug);
if (!p) {
p = getJson<{ entries: Entry[] }>(`https://documents.devdocs.io/${doc.slug}/index.json?${doc.mtime ?? ""}`)
.then((r) => r.entries ?? []);
p.catch(() => indexes.delete(doc.slug));
indexes.set(doc.slug, p);
}
return p;
}