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
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
intercept.ts · 2.8 KB · 65 lines
| 1 | /** |
| 2 | * The https interception core — deliberately Electron-free so it can be unit-tested |
| 3 | * without a display. |
| 4 | * |
| 5 | * In Electron this is wired via `protocol.handle('https', handler)`: once we handle |
| 6 | * https, EVERY https request in the browser flows through here, and we are responsible |
| 7 | * for actually fetching the content ourselves. That is exactly the hook x402 needs — |
| 8 | * see a 402, sign, retry, hand back the paid response — and it is the thing an MV3 |
| 9 | * extension structurally cannot do. |
| 10 | * |
| 11 | * Two subtleties this module exists to get right: |
| 12 | * |
| 13 | * 1. Recursion. The engine fetches the network using a `fetch` we supply. If that |
| 14 | * fetch is Electron's plain `net.fetch`, it re-enters THIS handler and loops |
| 15 | * forever. The main process must pass a fetch that bypasses custom protocol |
| 16 | * handlers; `bypassingFetch()` documents and enforces that contract. |
| 17 | * |
| 18 | * 2. topOrigin. Budgets are scoped to (page the user is looking at × origin being |
| 19 | * paid). A subresource must be billed against ITS PAGE, never itself, or the |
| 20 | * pair-scoped rate cap is meaningless. We derive that page origin from the |
| 21 | * request's referrer. |
| 22 | */ |
| 23 | |
| 24 | import type { X402Engine } from "@dbbasic/x402-engine"; |
| 25 | |
| 26 | /** A fetch that goes straight to the network without re-entering our protocol handler. */ |
| 27 | export type RawFetch = (input: Request) => Promise<Response>; |
| 28 | |
| 29 | /** |
| 30 | * The origin of the page that initiated this request, used to scope budgets. |
| 31 | * Top-level navigations have no referrer → "" (their own budget scope), which is |
| 32 | * correct: the user chose to go there. |
| 33 | */ |
| 34 | export function topOriginOf(request: Request): string { |
| 35 | // WHATWG Request.referrer, then the Referer header as a fallback. |
| 36 | const ref = request.referrer && request.referrer !== "about:client" ? request.referrer : request.headers.get("referer"); |
| 37 | if (!ref) return ""; |
| 38 | try { |
| 39 | return new URL(ref).origin; |
| 40 | } catch { |
| 41 | return ""; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Wrap a network fetch (Electron's `net.fetch`) so the engine's internal requests go |
| 47 | * straight to the wire instead of looping back through `protocol.handle('https')`. |
| 48 | * `bypass` is the function that sets Electron's bypassCustomProtocolHandlers flag; we |
| 49 | * take it as a parameter so this module never imports electron. |
| 50 | */ |
| 51 | export function bypassingFetch(bypass: (input: Request) => Promise<Response>): RawFetch { |
| 52 | return (input: Request) => bypass(input); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Build the `protocol.handle('https', ...)` handler. Every https request in the |
| 57 | * browser is routed through the engine, which pays any 402 transparently and returns |
| 58 | * the final response for the renderer to display. |
| 59 | */ |
| 60 | export function createPaymentHandler(engine: X402Engine): (request: Request) => Promise<Response> { |
| 61 | return async (request: Request): Promise<Response> => { |
| 62 | const { response } = await engine.fetch(request, topOriginOf(request)); |
| 63 | return response; |
| 64 | }; |
| 65 | } |