A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-browser.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-browser.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-browser.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 site icons (favicons) in tabs, address bar suggestions and the bookmarks panel 7249579 15 hours, 50 minutes ago History
keep-store.ts · 8.5 KB · 207 lines
1/**
2 * "Keep": a copy of a page, so its tab can be closed without losing what was in it. Each kept page is a folder with what the
3 * page looked like (an image of what was on screen), its text, and a small record (address, title, time, optional reason and
4 * tags). Kept pages are found again by searching their text, and looked at in a tab served by the browser itself
5 * (`dbb-kept://<id>/`), which works even when the original page has changed, logged you out or gone away.
6 *
7 * Local only: nothing here leaves the machine. Electron-free so it is unit-tested.
8 */
9
10import { randomBytes } from "node:crypto";
11import { promises as fs } from "node:fs";
12import { join } from "node:path";
13
14export const KEPT_SCHEME = "dbb-kept";
15const ID_RE = /^[0-9a-f]{16}$/;
16const MAX_TEXT = 400_000;
17
18export interface KeptRecord {
19 id: string;
20 url: string;
21 title: string;
22 /** Epoch ms. */
23 keptAt: number;
24 reason?: string;
25 tags: string[];
26 /** Characters of text kept. */
27 textChars: number;
28 imageBytes: number;
29 /** Copies saved to the person's account (askrobots Files), when connected. */
30 remote?: { files: Array<{ kind: "image" | "text"; filename: string; id?: string }>; error?: string };
31}
32
33/**
34 * A readable file name for a kept copy: "<title> (<site>, <date>).<ext>". Characters that file systems or URLs treat
35 * specially are dropped, and the name is kept short enough for any of them.
36 */
37export function keptFilename(r: Pick<KeptRecord, "title" | "url" | "keptAt">, ext: "png" | "txt"): string {
38 let host = "";
39 try {
40 host = new URL(r.url).hostname.replace(/^www\./, "");
41 } catch {
42 /* no site */
43 }
44 const clean = (x: string) => x.replace(/[\u0000-\u001f\/\\:*?"<>|#%{}^~\[\]`]+/g, " ").replace(/\s+/g, " ").trim();
45 let title = clean(r.title || "");
46 const titleIsSite = !title || title.toLowerCase() === host.toLowerCase();
47 if (titleIsSite) title = host || "Page";
48 if (title.length > 90) title = `${title.slice(0, 89).trimEnd()}…`;
49 const date = new Date(r.keptAt).toISOString().slice(0, 10);
50 return `${title} (${host && !titleIsSite ? `${host}, ` : ""}${date}).${ext}`;
51}
52
53export const keptUrl = (id: string, file = "") => `${KEPT_SCHEME}://${id}/${file}`;
54
55/** The kept page id and the file asked for, from a dbb-kept:// address, or null. */
56export function parseKeptUrl(url: string): { id: string; file: "" | "page.png" } | null {
57 try {
58 const u = new URL(url);
59 if (u.protocol !== `${KEPT_SCHEME}:` || !ID_RE.test(u.hostname)) return null;
60 const file = u.pathname.replace(/^\/+/, "");
61 if (file !== "" && file !== "page.png") return null;
62 return { id: u.hostname, file };
63 } catch {
64 return null;
65 }
66}
67
68const esc = (s: string) => s.replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c] as string);
69
70/** The page shown for a kept copy: the image and the text, nothing that runs. */
71export function keptPageHtml(r: KeptRecord, text: string): string {
72 const when = new Date(r.keptAt).toLocaleString();
73 const paras = text
74 .split(/\n{2,}/)
75 .map((p) => p.trim())
76 .filter(Boolean)
77 .map((p) => `<p>${esc(p).replace(/\n/g, "<br>")}</p>`)
78 .join("\n");
79 return `<!doctype html><html><head><meta charset="utf-8"><title>Kept: ${esc(r.title)}</title>
80<meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src ${KEPT_SCHEME}:; style-src 'unsafe-inline'">
81<style>
82body{font:16px/1.6 -apple-system,system-ui,sans-serif;margin:0;color:#222;background:#f6f6f4}
83header{position:sticky;top:0;background:#20242c;color:#e8e8ea;padding:10px 22px;font-size:13px;display:flex;gap:14px;align-items:baseline;flex-wrap:wrap}
84header b{color:#fff;font-size:14px} header a{color:#9cc3ff} header .r{color:#f0c674}
85main{max-width:980px;margin:0 auto;padding:18px 22px 60px}
86figure{margin:0 0 22px} figure img{max-width:100%;border:1px solid #ddd;border-radius:6px;box-shadow:0 2px 10px rgba(0,0,0,.08)}
87figcaption{color:#777;font-size:12px;margin-top:6px}
88.text{background:#fff;border:1px solid #e3e3e0;border-radius:8px;padding:18px 24px;white-space:normal}
89h2{font-size:15px;color:#555;margin:26px 0 8px}
90</style></head><body>
91<header><b>Kept copy</b><span>${esc(r.title)}</span><a href="${esc(r.url)}">${esc(r.url)}</a><span>kept ${esc(when)}</span>${r.reason ? `<span class="r">Why: ${esc(r.reason)}</span>` : ""}${r.tags.length ? `<span>${esc(r.tags.join(", "))}</span>` : ""}</header>
92<main>
93${r.imageBytes ? `<figure><img src="${keptUrl(r.id, "page.png")}" alt="What the page looked like"><figcaption>What was on screen when it was kept.</figcaption></figure>` : ""}
94<h2>Text of the page</h2>
95<div class="text">${paras || "<p><i>No text.</i></p>"}</div>
96</main></body></html>`;
97}
98
99export class KeepStore {
100 constructor(private dir: string) {}
101
102 private folder(id: string) {
103 if (!ID_RE.test(id)) throw new Error("not a kept page");
104 return join(this.dir, id);
105 }
106
107 async add(p: { url: string; title: string; text: string; png: Buffer | null; reason?: string; tags?: string[]; now?: number }): Promise<KeptRecord> {
108 const id = randomBytes(8).toString("hex");
109 const f = this.folder(id);
110 await fs.mkdir(f, { recursive: true, mode: 0o700 });
111 const text = p.text.slice(0, MAX_TEXT);
112 const r: KeptRecord = {
113 id,
114 url: p.url,
115 title: p.title.slice(0, 300) || p.url,
116 keptAt: p.now ?? Date.now(),
117 ...(p.reason ? { reason: p.reason.slice(0, 300) } : {}),
118 tags: (p.tags ?? []).map((t) => t.trim()).filter(Boolean).slice(0, 20),
119 textChars: text.length,
120 imageBytes: p.png?.length ?? 0,
121 };
122 await fs.writeFile(join(f, "text.txt"), text, { mode: 0o600 });
123 if (p.png) await fs.writeFile(join(f, "page.png"), p.png, { mode: 0o600 });
124 await fs.writeFile(join(f, "record.json"), JSON.stringify(r), { mode: 0o600 });
125 return r;
126 }
127
128 async get(id: string): Promise<{ record: KeptRecord; text: string } | null> {
129 try {
130 const f = this.folder(id);
131 const record = JSON.parse(await fs.readFile(join(f, "record.json"), "utf8")) as KeptRecord;
132 return { record, text: await fs.readFile(join(f, "text.txt"), "utf8").catch(() => "") };
133 } catch {
134 return null;
135 }
136 }
137
138 async image(id: string): Promise<Buffer | null> {
139 try {
140 return await fs.readFile(join(this.folder(id), "page.png"));
141 } catch {
142 return null;
143 }
144 }
145
146 /** Newest first. With a query, only pages whose title, address, reason, tags or text contain every word of it. */
147 async list(query = "", limit = 200): Promise<Array<KeptRecord & { snippet?: string }>> {
148 let ids: string[];
149 try {
150 ids = (await fs.readdir(this.dir)).filter((n) => ID_RE.test(n));
151 } catch {
152 return [];
153 }
154 const words = query.toLowerCase().split(/\s+/).filter(Boolean);
155 const out: Array<KeptRecord & { snippet?: string }> = [];
156 for (const id of ids) {
157 const g = await this.get(id);
158 if (!g) continue;
159 const r = g.record;
160 if (words.length) {
161 const hay = `${r.title}\n${r.url}\n${r.reason ?? ""}\n${r.tags.join(" ")}\n${g.text}`.toLowerCase();
162 if (!words.every((w) => hay.includes(w))) continue;
163 const at = g.text.toLowerCase().indexOf(words[0] as string);
164 out.push({ ...r, ...(at >= 0 ? { snippet: g.text.slice(Math.max(0, at - 60), at + 120).replace(/\s+/g, " ").trim() } : {}) });
165 } else out.push(r);
166 }
167 out.sort((a, b) => b.keptAt - a.keptAt);
168 return out.slice(0, limit);
169 }
170
171 /** Every record, newest first, without reading the text (for the address bar, which asks on every key). */
172 async records(limit = 500): Promise<KeptRecord[]> {
173 let ids: string[];
174 try {
175 ids = (await fs.readdir(this.dir)).filter((n) => ID_RE.test(n));
176 } catch {
177 return [];
178 }
179 const out: KeptRecord[] = [];
180 for (const id of ids) {
181 try {
182 out.push(JSON.parse(await fs.readFile(join(this.dir, id, "record.json"), "utf8")) as KeptRecord);
183 } catch {
184 /* a half-written copy */
185 }
186 }
187 return out.sort((a, b) => b.keptAt - a.keptAt).slice(0, limit);
188 }
189
190 /** Change a kept copy's record (for example once it has been saved to the person's account). */
191 async update(id: string, patch: Partial<Omit<KeptRecord, "id">>): Promise<KeptRecord | null> {
192 const g = await this.get(id);
193 if (!g) return null;
194 const r = { ...g.record, ...patch, id: g.record.id };
195 await fs.writeFile(join(this.folder(id), "record.json"), JSON.stringify(r), { mode: 0o600 });
196 return r;
197 }
198
199 async remove(id: string): Promise<boolean> {
200 try {
201 await fs.rm(this.folder(id), { recursive: true, force: true });
202 return true;
203 } catch {
204 return false;
205 }
206 }
207}