Public: anyone can read this; the crew can change it.
A browser harness in the gate for the tab forge: a real service worker and a real form post #18284
Closed 9h 24m ago
torque/forge:torque/18273-a-browser-harness-in-the-gate-for-the-ta → main 5 commits fork
2 files
changed +14
−7
docs/features/tab.md +4
| @@ -77,3 +77,7 @@ Once started, the forge is the tab: its frame covers the viewport (`html[data-ru | ||
| 77 | 77 | It survives a reload and a reopened tab: the page remembers (`localStorage`) that the forge was started and the last page of it that was read, and starts it again on load. Every decision a reload makes is in `src/tab/restore.ts` and tested in `test/tab-whole.test.ts`: `startsItself` (starts only when this browser started it before), `openOn` (only a `/tab/f/` path is ever opened), `keepStorage` (asks only on the first Start). When the browser cleared the store but kept `localStorage`, the worker reports the store empty (`unmade`, no tables before the migration): every page of it carries *The browser cleared this forge since it last ran: Import an export to bring it back.* When the browser cleared both, the page is a first visit again: Start. A second tab cannot open the store while the first holds it; it says so and names the way out. If the browser clears the site's data the forge is gone: export first. |
| 78 | 78 | |
| 79 | 79 | Not done: a topbar identical to the hosted forge's, an export of the whole database file (the export is the app and its issues), a hosted forge reading the file, and the runner switch. The forge is still shown in a frame over /tab, not served as the tab's own page in the hosted shell: the database needs a dedicated worker (OPFS sync handles), which a page under `/tab/f/` loses on every navigation. The cold read by a no-context agent was not run. |
| 80 | ||
| 81 | ## The browser test (#18273, #18295) | |
| 82 | ||
| 83 | `test/tab-browser.test.ts` walks the tab forge's writes in headless Chromium (see its header). The frame's replies carry `Referrer-Policy: same-origin`, because under `no-referrer` a form post sends `Origin: null`, which the worker's guard refuses. |
test/helpers/chrome.ts +10 −7
| @@ -15,7 +15,7 @@ import { browserCommand, browserRefusal } from "../../src/looks/chromium.ts"; | ||
| 15 | 15 | import { pollUntil } from "./poll.ts"; |
| 16 | 16 | |
| 17 | 17 | /** Start a browser on `profile` with its DevTools port open, as every helper here does. */ |
| 18 | function start(binary: string, profile: string, flags: readonly string[] = []) { | |
| 18 | export function start(binary: string, profile: string, flags: readonly string[] = []) { | |
| 19 | 19 | const { argv, env, cwd } = browserCommand(binary, { profile, flags: ["--remote-debugging-port=0", ...flags] }); |
| 20 | 20 | return Bun.spawn(argv, { env, cwd, stdin: "ignore", stdout: "ignore", stderr: "pipe" }); |
| 21 | 21 | } |
| @@ -40,7 +40,7 @@ interface CdpMessage { | ||
| 40 | 40 | * is bounded, so a stalled browser fails the test and the `finally` that |
| 41 | 41 | * kills it runs, instead of hanging the shard until it is killed. |
| 42 | 42 | */ |
| 43 | function within<T>(ms: number, what: string, promise: Promise<T>): Promise<T> { | |
| 43 | export function within<T>(ms: number, what: string, promise: Promise<T>): Promise<T> { | |
| 44 | 44 | let timer: ReturnType<typeof setTimeout> | undefined; |
| 45 | 45 | const late = new Promise<never>((_, reject) => { |
| 46 | 46 | timer = setTimeout(() => reject(new Error(`chromium: ${what} took longer than ${ms} ms`)), ms); |
| @@ -56,7 +56,7 @@ function within<T>(ms: number, what: string, promise: Promise<T>): Promise<T> { | ||
| 56 | 56 | const SEND_MS = 20_000; |
| 57 | 57 | |
| 58 | 58 | /** A CDP session over one page's own devtools socket. */ |
| 59 | class Cdp { | |
| 59 | export class Cdp { | |
| 60 | 60 | private nextId = 1; |
| 61 | 61 | private readonly pending = new Map<number, (msg: CdpMessage) => void>(); |
| 62 | 62 | private readonly waiters = new Map<string, () => void>(); |
| @@ -85,14 +85,17 @@ class Cdp { | ||
| 85 | 85 | this.listeners.set(method, [...(this.listeners.get(method) ?? []), listen]); |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | send(method: string, params: object = {}): Promise<any> { | |
| 88 | /** | |
| 89 | * `sessionId` aims the command at a target attached through the browser's own socket (flat mode). | |
| 90 | */ | |
| 91 | send(method: string, params: object = {}, sessionId?: string): Promise<any> { | |
| 89 | 92 | const id = this.nextId++; |
| 90 | 93 | return within(SEND_MS, method, new Promise((resolve, reject) => { |
| 91 | 94 | this.pending.set(id, (msg) => { |
| 92 | 95 | if (msg.error !== undefined) reject(new Error(msg.error.message)); |
| 93 | 96 | else resolve(msg.result); |
| 94 | 97 | }); |
| 95 | this.ws.send(JSON.stringify({ id, method, params })); | |
| 98 | this.ws.send(JSON.stringify({ id, method, params, ...(sessionId === undefined ? {} : { sessionId }) })); | |
| 96 | 99 | })); |
| 97 | 100 | } |
| 98 | 101 | |
| @@ -103,7 +106,7 @@ class Cdp { | ||
| 103 | 106 | } |
| 104 | 107 | |
| 105 | 108 | /** The devtools http endpoint's own list of open pages. */ |
| 106 | async function findPageTarget( | |
| 109 | export async function findPageTarget( | |
| 107 | 110 | debuggerWsUrl: string, |
| 108 | 111 | ): Promise<{ webSocketDebuggerUrl: string }> { |
| 109 | 112 | const httpBase = debuggerWsUrl |
| @@ -120,7 +123,7 @@ async function findPageTarget( | ||
| 120 | 123 | } |
| 121 | 124 | |
| 122 | 125 | /** Chromium prints its devtools address to stderr once it is ready to drive. */ |
| 123 | async function readDebuggerUrl( | |
| 126 | export async function readDebuggerUrl( | |
| 124 | 127 | stderr: ReadableStream<Uint8Array>, |
| 125 | 128 | ): Promise<string> { |
| 126 | 129 | const reader = stderr.getReader(); |
new above
offline