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
downloads-store.ts · 5.1 KB · 144 lines
| 1 | /** |
| 2 | * The list of downloads (and pages saved as PDF), kept in the profile so it is still there after a restart, as in |
| 3 | * other browsers. It holds what the Downloads panel shows: name, the site, where the file went, its size, how it |
| 4 | * ended, and what was paid for it. Never the file's contents. |
| 5 | * |
| 6 | * A download that was still running when the browser closed is listed as interrupted. The newest MAX_KEPT are kept. |
| 7 | * |
| 8 | * Electron-free so it is unit-tested; main.ts feeds it from Electron's download items and writes the file. |
| 9 | */ |
| 10 | |
| 11 | export type DownloadState = "progressing" | "paused" | "completed" | "cancelled" | "interrupted"; |
| 12 | |
| 13 | export interface DownloadRecord { |
| 14 | id: number; |
| 15 | name: string; |
| 16 | host: string; |
| 17 | /** Where it was downloaded from, for Retry ("" for data: and blob: addresses, and for saved PDFs). */ |
| 18 | url: string; |
| 19 | total: number; |
| 20 | received: number; |
| 21 | state: DownloadState; |
| 22 | path: string; |
| 23 | /** The x402 amount paid for this URL just before the download started, e.g. "$0.001". */ |
| 24 | paid?: string; |
| 25 | /** macOS: whether the file was marked as downloaded from the internet (so Gatekeeper vets it). */ |
| 26 | quarantined?: boolean; |
| 27 | /** "pdf" for a page the browser saved as a PDF. */ |
| 28 | kind?: "pdf"; |
| 29 | startedAt: number; |
| 30 | endedAt?: number; |
| 31 | } |
| 32 | |
| 33 | export const MAX_KEPT = 500; |
| 34 | const STATES = new Set<DownloadState>(["progressing", "paused", "completed", "cancelled", "interrupted"]); |
| 35 | const running = (s: DownloadState) => s === "progressing" || s === "paused"; |
| 36 | |
| 37 | /** Only what a record may hold, from a file that could be damaged or old. */ |
| 38 | function clean(x: unknown): DownloadRecord | null { |
| 39 | if (!x || typeof x !== "object") return null; |
| 40 | const r = x as Record<string, unknown>; |
| 41 | const str = (v: unknown) => (typeof v === "string" ? v : ""); |
| 42 | const num = (v: unknown) => (typeof v === "number" && Number.isFinite(v) && v >= 0 ? v : 0); |
| 43 | if (!Number.isInteger(r.id) || (r.id as number) <= 0 || !str(r.name)) return null; |
| 44 | const state = STATES.has(r.state as DownloadState) ? (r.state as DownloadState) : "interrupted"; |
| 45 | const out: DownloadRecord = { |
| 46 | id: r.id as number, |
| 47 | name: str(r.name), |
| 48 | host: str(r.host), |
| 49 | url: /^https?:\/\//i.test(str(r.url)) ? str(r.url) : "", |
| 50 | total: num(r.total), |
| 51 | received: num(r.received), |
| 52 | state, |
| 53 | path: str(r.path), |
| 54 | startedAt: num(r.startedAt), |
| 55 | }; |
| 56 | if (typeof r.paid === "string" && r.paid) out.paid = r.paid; |
| 57 | if (typeof r.quarantined === "boolean") out.quarantined = r.quarantined; |
| 58 | if (r.kind === "pdf") out.kind = "pdf"; |
| 59 | if (num(r.endedAt)) out.endedAt = num(r.endedAt); |
| 60 | return out; |
| 61 | } |
| 62 | |
| 63 | export class DownloadList { |
| 64 | private items = new Map<number, DownloadRecord>(); |
| 65 | private seq = 0; |
| 66 | |
| 67 | /** `saved` is the file's text, or null when there is none; a damaged file is treated as empty. */ |
| 68 | constructor(saved: string | null, private save: (text: string) => void, private now: () => number = Date.now) { |
| 69 | if (!saved) return; |
| 70 | let list: unknown[] = []; |
| 71 | try { |
| 72 | const j = JSON.parse(saved) as { v?: number; items?: unknown[] }; |
| 73 | if (j && j.v === 1 && Array.isArray(j.items)) list = j.items; |
| 74 | } catch { |
| 75 | /* start empty */ |
| 76 | } |
| 77 | for (const x of list) { |
| 78 | const r = clean(x); |
| 79 | if (!r || this.items.has(r.id)) continue; |
| 80 | // Still running when the browser closed: it did not finish. |
| 81 | if (running(r.state)) r.state = "interrupted"; |
| 82 | this.items.set(r.id, r); |
| 83 | this.seq = Math.max(this.seq, r.id); |
| 84 | } |
| 85 | this.trim(); |
| 86 | } |
| 87 | |
| 88 | /** A new record (its id is given here), saved at once. */ |
| 89 | add(r: Omit<DownloadRecord, "id">): DownloadRecord { |
| 90 | const rec = { ...r, id: ++this.seq } as DownloadRecord; |
| 91 | this.items.set(rec.id, rec); |
| 92 | this.trim(); |
| 93 | this.flush(); |
| 94 | return rec; |
| 95 | } |
| 96 | |
| 97 | get(id: number): DownloadRecord | undefined { |
| 98 | return this.items.get(id); |
| 99 | } |
| 100 | |
| 101 | /** Changes to a record. Saved when it starts or ends; progress alone is saved by the caller now and then (flush). */ |
| 102 | update(id: number, change: Partial<Omit<DownloadRecord, "id">>): DownloadRecord | undefined { |
| 103 | const r = this.items.get(id); |
| 104 | if (!r) return undefined; |
| 105 | const was = r.state; |
| 106 | Object.assign(r, change); |
| 107 | if (running(was) && !running(r.state) && !r.endedAt) r.endedAt = this.now(); |
| 108 | if (was !== r.state) this.flush(); |
| 109 | return r; |
| 110 | } |
| 111 | |
| 112 | /** Oldest first, as they were started. */ |
| 113 | list(): DownloadRecord[] { |
| 114 | return [...this.items.values()]; |
| 115 | } |
| 116 | |
| 117 | /** Take one off the list (the file stays where it is). A running download can't be removed; cancel it first. */ |
| 118 | remove(id: number): boolean { |
| 119 | const r = this.items.get(id); |
| 120 | if (!r || running(r.state)) return false; |
| 121 | this.items.delete(id); |
| 122 | this.flush(); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | /** Take every finished one off the list (files stay); running ones stay. Returns how many went. */ |
| 127 | clear(): number { |
| 128 | let n = 0; |
| 129 | for (const [id, r] of this.items) if (!running(r.state)) (this.items.delete(id), n++); |
| 130 | if (n) this.flush(); |
| 131 | return n; |
| 132 | } |
| 133 | |
| 134 | flush(): void { |
| 135 | this.save(JSON.stringify({ v: 1, items: this.list() })); |
| 136 | } |
| 137 | |
| 138 | private trim(): void { |
| 139 | for (const [id, r] of this.items) { |
| 140 | if (this.items.size <= MAX_KEPT) break; |
| 141 | if (!running(r.state)) this.items.delete(id); |
| 142 | } |
| 143 | } |
| 144 | } |