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
request-kinds.ts · 3.3 KB · 81 lines
| 1 | /** |
| 2 | * What kind of request a page made, for our network layer. |
| 3 | * |
| 4 | * The Request our protocol handler gets has lost what Chromium's loader knows: it says mode "cors" and no destination for |
| 5 | * everything, and carries no Origin. Built from guesses, our fetch-metadata headers told a site that a frame was a page, |
| 6 | * a script was a fetch, and everything came from nowhere (Sec-Fetch-Site: none). Bot checks such as Cloudflare's |
| 7 | * Turnstile compare those headers with what a real browser sends, and never passed. |
| 8 | * |
| 9 | * Electron's webRequest.onBeforeRequest still sees the page's own request first, with its real type ("script", |
| 10 | * "subFrame", "xhr", …) and the frame that made it. main.ts records that here; the transport takes the record for the |
| 11 | * same method and address a moment later. Records are kept a few seconds and used once. |
| 12 | * |
| 13 | * Electron-free so it is unit-tested. |
| 14 | */ |
| 15 | |
| 16 | /** Electron's resourceType names. */ |
| 17 | export type ResourceType = |
| 18 | | "mainFrame" | "subFrame" | "stylesheet" | "script" | "image" | "font" | "object" | "xhr" | "ping" | "cspReport" | "media" | "webSocket" | "other"; |
| 19 | |
| 20 | export interface RequestKind { |
| 21 | type: ResourceType; |
| 22 | /** The origin of the document that made the request (for a frame being loaded, its parent's), or null when unknown. */ |
| 23 | initiator: string | null; |
| 24 | } |
| 25 | |
| 26 | const KEEP_MS = 10_000; |
| 27 | const MAX = 2000; |
| 28 | |
| 29 | const originOf = (u: string | null | undefined): string | null => { |
| 30 | if (!u) return null; |
| 31 | try { |
| 32 | const o = new URL(u).origin; |
| 33 | return o === "null" ? null : o; |
| 34 | } catch { |
| 35 | return null; |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | export class RequestKinds { |
| 40 | private q = new Map<string, Array<RequestKind & { at: number }>>(); |
| 41 | private n = 0; |
| 42 | |
| 43 | constructor(private now: () => number = Date.now) {} |
| 44 | |
| 45 | /** |
| 46 | * One request as webRequest saw it. `frameUrl` is the frame making the request (for a frame being loaded, that new frame, |
| 47 | * which has no document yet); `parentUrl` its parent. |
| 48 | */ |
| 49 | record(d: { method: string; url: string; type: string; frameUrl?: string | null; parentUrl?: string | null; referrer?: string | null }): void { |
| 50 | const type = (d.type || "other") as ResourceType; |
| 51 | const initiator = |
| 52 | type === "mainFrame" ? originOf(d.referrer) : type === "subFrame" ? (originOf(d.parentUrl) ?? originOf(d.referrer)) : (originOf(d.frameUrl) ?? originOf(d.referrer)); |
| 53 | const key = `${d.method.toUpperCase()} ${d.url}`; |
| 54 | const list = this.q.get(key) ?? []; |
| 55 | list.push({ type, initiator, at: this.now() }); |
| 56 | this.q.set(key, list); |
| 57 | if (++this.n % 200 === 0) this.prune(); |
| 58 | } |
| 59 | |
| 60 | /** The kind of the oldest waiting request for this method and address, used once; null when none was seen. */ |
| 61 | take(method: string, url: string): RequestKind | null { |
| 62 | const key = `${method.toUpperCase()} ${url}`; |
| 63 | const list = this.q.get(key); |
| 64 | if (!list) return null; |
| 65 | const t = this.now(); |
| 66 | while (list.length && t - list[0]!.at > KEEP_MS) list.shift(); |
| 67 | const k = list.shift(); |
| 68 | if (!list.length) this.q.delete(key); |
| 69 | return k ? { type: k.type, initiator: k.initiator } : null; |
| 70 | } |
| 71 | |
| 72 | private prune(): void { |
| 73 | const t = this.now(); |
| 74 | for (const [key, list] of this.q) { |
| 75 | const kept = list.filter((x) => t - x.at <= KEEP_MS); |
| 76 | if (kept.length) this.q.set(key, kept); |
| 77 | else this.q.delete(key); |
| 78 | } |
| 79 | while (this.q.size > MAX) this.q.delete(this.q.keys().next().value as string); |
| 80 | } |
| 81 | } |