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

0 downloads

dbbasic-browser/apps/browser/src/reader-pages.ts
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
reader-pages.ts · 2.5 KB · 66 lines
1/**
2 * Where reader views live. Each reader tab is served from `dbb-reader://<id>/` by the main process, from this
3 * in-memory store — not from a one-shot `data:` URL. That matters for history: a `data:` URL IS the page, so
4 * Back/Forward re-loaded the snapshot taken BEFORE the AI answered (a forever-"Summarizing…" skeleton). Here the
5 * stored page is updated when the summary arrives, so any later load — Back, Forward, reload — shows the finished
6 * view instantly, with no new provider call.
7 *
8 * Electron-free so it can be unit-tested.
9 */
10
11import { randomBytes } from "node:crypto";
12
13export const READER_SCHEME = "dbb-reader";
14const ID_RE = /^[0-9a-f]{18}$/;
15
16export const readerUrl = (id: string) => `${READER_SCHEME}://${id}/`;
17
18/** The page id in a reader URL, or null for anything that isn't one of ours. */
19export function readerIdFromUrl(url: string): string | null {
20 try {
21 const u = new URL(url);
22 return u.protocol === `${READER_SCHEME}:` && ID_RE.test(u.hostname) ? u.hostname : null;
23 } catch {
24 return null;
25 }
26}
27
28export const EXPIRED_HTML = `<!doctype html><html><head><meta charset="utf-8"><title>Reader view expired</title>
29<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'">
30<style>body{font:17px/1.6 -apple-system,system-ui,sans-serif;max-width:560px;margin:90px auto;padding:0 22px;color:#444}h1{font-size:22px}</style></head>
31<body><h1>This reader view has expired</h1><p>Reader views are kept in memory for this session only, and the oldest are dropped. Open the original page and press <b>⌘⇧R</b> to read it again.</p></body></html>`;
32
33export class ReaderPages {
34 private pages = new Map<string, string>();
35 private readonly max: number;
36
37 constructor(max = 30) {
38 this.max = max;
39 }
40
41 /** Store a new page; returns its id. The oldest page is dropped when the store is full. */
42 put(html: string): string {
43 const id = randomBytes(9).toString("hex");
44 this.pages.set(id, html);
45 while (this.pages.size > this.max) {
46 const oldest = this.pages.keys().next().value as string;
47 this.pages.delete(oldest);
48 }
49 return id;
50 }
51
52 /** Replace a page's content (the summary arrived). A no-op if it was already evicted. */
53 update(id: string, html: string): boolean {
54 if (!this.pages.has(id)) return false;
55 this.pages.set(id, html);
56 return true;
57 }
58
59 get(id: string): string | undefined {
60 return this.pages.get(id);
61 }
62
63 get size(): number {
64 return this.pages.size;
65 }
66}