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 #18308

opened this

Closed 5h 14m ago

torque/forge:torque/18273-a-browser-harness-in-the-gate-for-the-ta → main 5 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
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
7777 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.
7878
7979 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";
1515 import { pollUntil } from "./poll.ts";
1616
1717 /** 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[] = []) {
1919 const { argv, env, cwd } = browserCommand(binary, { profile, flags: ["--remote-debugging-port=0", ...flags] });
2020 return Bun.spawn(argv, { env, cwd, stdin: "ignore", stdout: "ignore", stderr: "pipe" });
2121 }
@@ -40,7 +40,7 @@ interface CdpMessage {
4040 * is bounded, so a stalled browser fails the test and the `finally` that
4141 * kills it runs, instead of hanging the shard until it is killed.
4242 */
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> {
4444 let timer: ReturnType<typeof setTimeout> | undefined;
4545 const late = new Promise<never>((_, reject) => {
4646 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> {
5656 const SEND_MS = 20_000;
5757
5858 /** A CDP session over one page's own devtools socket. */
59 class Cdp {
59 export class Cdp {
6060 private nextId = 1;
6161 private readonly pending = new Map<number, (msg: CdpMessage) => void>();
6262 private readonly waiters = new Map<string, () => void>();
@@ -85,14 +85,17 @@ class Cdp {
8585 this.listeners.set(method, [...(this.listeners.get(method) ?? []), listen]);
8686 }
8787
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> {
8992 const id = this.nextId++;
9093 return within(SEND_MS, method, new Promise((resolve, reject) => {
9194 this.pending.set(id, (msg) => {
9295 if (msg.error !== undefined) reject(new Error(msg.error.message));
9396 else resolve(msg.result);
9497 });
95 this.ws.send(JSON.stringify({ id, method, params }));
98 this.ws.send(JSON.stringify({ id, method, params, ...(sessionId === undefined ? {} : { sessionId }) }));
9699 }));
97100 }
98101
@@ -103,7 +106,7 @@ class Cdp {
103106 }
104107
105108 /** The devtools http endpoint's own list of open pages. */
106 async function findPageTarget(
109 export async function findPageTarget(
107110 debuggerWsUrl: string,
108111 ): Promise<{ webSocketDebuggerUrl: string }> {
109112 const httpBase = debuggerWsUrl
@@ -120,7 +123,7 @@ async function findPageTarget(
120123 }
121124
122125 /** Chromium prints its devtools address to stderr once it is ready to drive. */
123 async function readDebuggerUrl(
126 export async function readDebuggerUrl(
124127 stderr: ReadableStream<Uint8Array>,
125128 ): Promise<string> {
126129 const reader = stderr.getReader();

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs