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, 56 minutes ago History
downloads.mjs · 5.1 KB · 82 lines
1// Downloads: tracked, announced, saved safely, linked to the payment that unlocked them.
2// (The e2e runner sets DBB_DOWNLOAD_DIR so nothing blocks on the native Save dialog.)
3import { execFileSync } from "node:child_process";
4import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
5import { join } from "node:path";
6import { sleep } from "./lib.mjs";
7
8export default async function (ctx) {
9 const { call: T, check, base } = ctx;
10 const dir = ctx.downloadDir;
11 const c = await ctx.chrome();
12 const list = () => T("downloads.list");
13 const finished = async (name, ms = 15000) => {
14 const end = Date.now() + ms;
15 for (;;) {
16 const d = (await list()).find((x) => x.name === name && x.state !== "progressing");
17 if (d || Date.now() > end) return d;
18 await sleep(100);
19 }
20 };
21
22 check((await list()).length === 0, "no downloads at the start");
23
24 // ---- a small attachment ----
25 const e0 = (await T("x402.events", { since: 0 })).next;
26 T("page.goto", { url: base + "/download", wait: false }).catch(() => {});
27 let d = await finished("hello-report.txt");
28 check(d?.state === "completed", "a download completes", `${d?.name} ${d?.received} B`);
29 check(d && existsSync(d.path) && d.path.startsWith(dir), "…and the file was saved where we said", d?.path?.replace(dir, "<dir>"));
30 check(d && readFileSync(d.path, "utf8").startsWith("hello from the demo server"), "…with the right content");
31 check(d && statSync(d.path).size === d.received && d.received > 1000, "…and the recorded size matches the file");
32 const evs = (await T("x402.events", { since: e0 })).events.filter((e) => e.kind === "download");
33 check(evs.length >= 2 && evs[0].state === "progressing" && evs.at(-1).state === "completed", "it was announced: started, then completed", evs.map((e) => e.state).join(" → "));
34 check(((await T("tabs.list")).find((t) => t.active).url || "").startsWith(base), "the tab stays on the page it was on (a download doesn't navigate away)");
35
36 // ---- the same file again: never overwrite ----
37 T("page.goto", { url: base + "/download", wait: false }).catch(() => {});
38 await sleep(1500);
39 const names = readdirSync(dir).sort();
40 check(names.includes("hello-report.txt") && names.includes("hello-report (1).txt"), "a second download of the same name gets a new name instead of overwriting", names.join(", "));
41
42 // ---- a hostile filename cannot escape the folder ----
43 T("page.goto", { url: base + "/download/evil", wait: false }).catch(() => {});
44 d = (await (async () => { for (let i = 0; i < 100; i++) { const x = (await list()).find((r) => /pwned/.test(r.name)); if (x && x.state !== "progressing") return x; await sleep(100); } })());
45 check(!!d && !/[\\/]/.test(d.name) && d.path.startsWith(dir + "/"), "a '../../../../tmp/…' filename is neutralized and stays inside the folder", d?.name);
46 check(!existsSync("/tmp/dbb-pwned.txt"), "nothing was written outside the download folder");
47
48 // ---- progress on a big, slow file ----
49 const seen = [];
50 let sawStatus = "";
51 T("page.goto", { url: base + "/download/big", wait: false }).catch(() => {});
52 const t0 = Date.now();
53 while (Date.now() - t0 < 20000) {
54 const r = (await list()).find((x) => x.name === "big.bin");
55 if (r) {
56 if (!seen.length || seen.at(-1) !== r.received) seen.push(r.received);
57 if (r.state === "progressing" && !sawStatus) sawStatus = (await c.ev(`(() => { const s = document.getElementById('status'); return s.classList.contains('show') ? s.innerText.replace(/\\s+/g, ' ').trim() : '' })()`)) || "";
58 if (r.state !== "progressing") break;
59 }
60 await sleep(120);
61 }
62 const big = (await list()).find((x) => x.name === "big.bin");
63 check(big?.state === "completed" && big.received === 3 * 1024 * 1024, "a 3 MB trickling file completes with exactly the right size", `${big?.received} B`);
64 check(seen.length >= 3 && seen.every((v, i) => i === 0 || v >= seen[i - 1]), "progress was reported while it ran (byte count grew)", `${seen.length} updates, e.g. ${seen.slice(0, 3).join(" → ")}`);
65 check(/Downloading big\.bin/.test(sawStatus), "the status line said what was happening", sawStatus);
66 check(statSync(big.path).size === 3 * 1024 * 1024, "the saved file is the full size on disk");
67
68 // ---- a PAID download: linked to the payment ----
69 await T("x402.approval", { mode: "auto", maxUsd: 1 });
70 T("page.goto", { url: base + "/download/paid", wait: false }).catch(() => {});
71 d = await finished("dataset.csv");
72 await T("x402.approval", { mode: "ask", maxUsd: 0 });
73 check(d?.state === "completed" && readFileSync(d.path, "utf8").startsWith("id,value"), "a paid download completes after the payment", d?.name);
74 check(d?.paid === "$0.001", "…and the record carries the amount that unlocked it", d?.paid);
75
76 // ---- macOS quarantine: a downloaded file must be marked so Gatekeeper vets anything you run ----
77 if (process.platform === "darwin" && big) {
78 let q = "";
79 try { q = execFileSync("xattr", ["-p", "com.apple.quarantine", big.path], { encoding: "utf8" }).trim(); } catch { /* absent */ }
80 check(q.length > 0, "the file carries the com.apple.quarantine attribute (Gatekeeper will vet it)", q || "ABSENT");
81 }
82}