@parcha/pi-browser 0.1.0-beta.36 → 0.1.0-beta.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.1.0-beta.37 (2026-11-13)
6
+
7
+ - A run's live view (its browser, desktop or phone, watched and taken over) works when its runner is on another host than the server, as in the compose stack's server and runners. Breaking for a SQLite store: its schema version moves to 9, so a server refuses a SQLite store an earlier version made; start it on a fresh data directory. A Postgres store migrates itself (migration 0007). A runner names the host the server reaches it at (`AGENTRUN_RUNNER_LIVE_HOST`, listening there or on `AGENTRUN_RUNNER_LIVE_BIND`), and each viewer's row in the store carries that host (a new nullable `computer_viewers.host`). The server dials a row's host only when it is one of its runners' (`AGENTRUN_RUNNER_HOSTS`), and signs each request it makes of the viewer with an HMAC under a key derived from the seal key; a runner's viewer off loopback refuses, before any WebSocket, a request with no good, fresh proof for its own viewer, and the viewer's token still decides who watches. The hop between server and runner is plain HTTP, for a private network. With neither setting, every viewer stays on loopback as before. `@parcha/pi-browser`'s `createLiveView` takes `listen` (the address its own server listens on and the host its links name) and `admit` (a host's own check of every page request and upgrade).
8
+ - A desktop's or a phone's screen is watched and taken over live, as the browser's is. The runner opens its view as it claims the run's computer: `openComputerView` (computer-live.ts) starts the computer's viewer on loopback and hands it over in the store under the run's claim (`computer_viewers`, the token sealed); a stale claim is refused `fenced`, and an open that fails is refused `failed` in words with nothing left listening. Its WebSocket speaks the browser viewer's protocol: hello, ready with Take over, the screen's frames (JPEG or PNG) each sent once the viewer acked the one before (a frame held back for a slow viewer is replaced by a newer one, and the replaced frame is acked), and Take over and Hand back through the run's hold, the same take-over and hand-back the routes write. Input is mapped by one `createActionMapper` per take and reaches the computer only while the person holds it, as the run's own hold says at that moment, so input stops as soon as a take ends; otherwise it is answered `held` ("Take over to drive."), and what a desktop refuses (ctrl+alt+F1, ctrl+alt+Backspace) comes back in words. The mouse buttons a take holds are let go when the viewer loses focus, before a hand back, and once when a take ends on the run's side or the viewer goes. One viewer per computer; a viewer that goes mid-take hands the take back (`away`). The runner now journals the hold of every run's desktop or phone (computerForClaim hands it to the run's spec), so `GET /v2/runs/{id}/computer` answers live with `kind` `desktop` or `phone` and who drives, and the viewer's own page (Take over and Hand back; a phone's back, home and recents) is served under `/v2/runs/{id}/computer/live/`, its socket proxied to the runner's endpoint with the same checks as the browser's. `@parcha/pi-browser/live-view` exports its WebSocket server and token helpers for it. A desktop or phone run whose screen is not open answers `GET /v2/runs/{id}/computer` `none` with its kind, no longer `off` with "this run's agent has no computer" (#2170). The viewer types from a phone's or tablet's soft keyboard too (its `beforeinput` text, deletes and Enter), with a desktop's keys still sent once. Under `--faux`, a run on the faux desktop starts and is claimed by the runner (its start check and the runner's claim read `--faux`'s computers beside the package's), and its screen is watched and taken over through this same viewer; the faux desktop's old page under `/faux/desktops/` is gone.
9
+ - The pi family and Chord move from 1.5.0 to 1.5.2 together (pi-ai, pi-agent-core, pi-coding-agent, pi-codemode, pi-durable, chord). Pi's model catalog now lists `claude-fable-5-2`, so a pi run can use it. Chord's package root no longer exports `Context` or `JsonValue`; they come from `@earendil-works/chord/context` and `@earendil-works/chord/json`, which a host that imports them from the root must follow.
10
+ - `web_fetch`'s text (a page read directly, `readPage`) leaves out what the page hides from everyone who reads it: an element with the `hidden` attribute, an inline `display:none` or `visibility:hidden`, a closed `<dialog>`, a class the page's own `<style>` sets to `display:none` or `visibility:hidden`, and the common pop-up and display-none utility classes (`mfp-hide`, `modal`, `d-none`, `hidden`, `hide`, `invisible`, `is-hidden`, `u-hidden`). A record page's help pop-ups, hidden until a click, were read as the record, so an agent took their boilerplate ("notice of intent to dissolve") for the company's own. Text only a screen reader reads (`sr-only`, `visually-hidden`, `wb-inv`, `screen-reader-text`) and text marked `aria-hidden` stay in the text: each is shown to someone. Only the page's live style elements count (none in a comment, a script, a template or a noscript, and only those for the screen); a later declaration or rule that shows the element wins (inline over a class, an author `display` over the `hidden` attribute), and where the outcome is unclear (an `!important` mix, classes in conflict, a class a compound selector shows) the text is kept. An element hidden by visibility alone is kept whole when anything shown inside it is set visible again (visibility is inherited and a descendant can override it; one that is itself, or sits inside, a `display:none` element shows nothing); `display:none` is not, so its element always goes. A class or style value is read with its character references decoded. A hidden element that no end tag closes is still read. Pages read through Parallel's extract are unchanged.
11
+
5
12
  ## 0.1.0-beta.36 (2026-11-13)
6
13
 
7
14
  - The browser tool has one stack: whether the agent's computer is a browser or the tool plane picks the vendor, every session is a browser computer of the computer registry's providers, made at the tool's call under the run's labels, and pi-browser's raw-CDP driver (`cdpDriver()`) drives it. Stagehand drives no run's browser now. Breaking: Browserbase and Kernel browsers now refuse run code and file uploads (browser_upload), and their snapshot is the raw-CDP list; every browser computer uses one driver. Breaking: pi-browser's `./providers/browserbase` and `./providers/kernel` exports and its `@browserbasehq/sdk` peer dependency are gone (a hosted browser is a browser computer provider of `@parcha/agentrun-computer`), and so are Browserbase's and Kernel's `create(spec, { settings })` vendor-field bag and its `BrowserbaseSessionSettings` and `KernelBrowserSettings` types (put a session's options in `spec.browser`).
package/README.md CHANGED
@@ -10,7 +10,7 @@ Your agent gets its own browser, and you can take over and hand it back.
10
10
 
11
11
  If the process dies, a pi-durable host recovers the session and does not repeat an action. See [For hosts](#for-hosts).
12
12
 
13
- Part of [AgentRun](https://github.com/Parcha-ai/agentrun). Node `>=24.22.3`, pi 1.5.0 exactly (its
13
+ Part of [AgentRun](https://github.com/Parcha-ai/agentrun). Node `>=24.22.3`, pi 1.5.2 exactly (its
14
14
  pi-durable and chord peers). ESM only: the
15
15
  package has `import` entries and no `require` entry.
16
16
 
@@ -18,7 +18,7 @@ package has `import` entries and no `require` entry.
18
18
 
19
19
  ```sh
20
20
  npm install @parcha/pi-browser@latest # the library: providers, driver, durable adapter
21
- npm install @parcha/pi-browser@latest @earendil-works/pi-durable@1.5.0 @earendil-works/chord@1.5.0 @earendil-works/pi-ai@1.5.0 # a pi-durable host: its peers, pinned to 1.5.0
21
+ npm install @parcha/pi-browser@latest @earendil-works/pi-durable@1.5.2 @earendil-works/chord@1.5.2 @earendil-works/pi-ai@1.5.2 # a pi-durable host: its peers, pinned to 1.5.2
22
22
  ```
23
23
 
24
24
  ## Quick start
@@ -259,7 +259,7 @@ Stagehand, undici, zod and the two HTML packages are dependencies. Three optiona
259
259
  |---|---|
260
260
  | `@earendil-works/pi-durable` and `@earendil-works/chord` | `/durable`, the adapter for a pi-durable Harness. |
261
261
 
262
- The pi peers are `@earendil-works/pi-durable` and `chord` `1.5.0` exactly. CI tests pi 1.5.0.
262
+ The pi peers are `@earendil-works/pi-durable` and `chord` `1.5.2` exactly. CI tests pi 1.5.2.
263
263
 
264
264
  ## Entries
265
265
 
@@ -1,6 +1,11 @@
1
1
  /** An entity decoder for a run of text or an attribute value: node-html-parser's in the worker, entities.ts's on the
2
2
  * event loop. */
3
3
  export type Decode = (text: string) => string;
4
+ /** A style element of a page: what its start tag holds between its name and its ">" (insideOf), and its text. */
5
+ export type Style = {
6
+ inside: string;
7
+ css: string;
8
+ };
4
9
  /** A page without its script, style, svg, noscript and template elements and its comments, in steps so a caller on the
5
10
  * event loop can yield between them (dropInert, all at once). One whose end is missing runs to the end of the page, as
6
11
  * a browser reads it; but an svg's start tag goes alone, since a browser lets the HTML that follows out of an unclosed
@@ -19,9 +24,72 @@ export declare class InertDrop {
19
24
  step(chars: number): boolean;
20
25
  /** The page as dropped so far (the whole of it once step has answered true). */
21
26
  result(): string;
27
+ /** The page's own style elements dropped so far, in page order: what each start tag holds (insideOf) and its text. */
28
+ styles(): readonly Style[];
22
29
  }
23
30
  /** `html` without its inert blocks, all at once (see InertDrop). */
24
31
  export declare function dropInert(html: string): string;
32
+ /**
33
+ * The classes common frameworks hide an element from everyone with, whatever the page's own style says (it is often an
34
+ * external stylesheet a page read never fetches): mfp-hide (Magnific Popup: a pop-up's content until it opens, as on
35
+ * Canada.ca's record pages), modal (Bootstrap: a modal until it opens), d-none (Bootstrap 4+), hidden (Tailwind, Bootstrap
36
+ * 3), hide (Bootstrap 2), invisible (Tailwind, Bootstrap: visibility), is-hidden (Bulma), u-hidden (SUIT utilities). The
37
+ * classes that hide text from the screen only (sr-only, visually-hidden, wb-inv, screen-reader-text) are not here: a
38
+ * screen reader reads them, and they are often a record's field labels and table captions, the words its values hang
39
+ * on. aria-hidden is read for the same reason the other way: it hides from a screen reader only, and is usually shown.
40
+ */
41
+ export declare const HIDING_CLASSES: ReadonlySet<string>;
42
+ /** The two properties that hide an element from everyone. */
43
+ type Showing = "display" | "visibility";
44
+ /** What a property comes to: "hide" (display none, visibility hidden or collapse), "show" (any other value that
45
+ * decides it), or "doubt" (a value that hands it elsewhere, or declarations whose winner is unclear). */
46
+ type Outcome = "hide" | "show" | "doubt";
47
+ /** A property's last declaration: its outcome, and whether it was !important. */
48
+ type Declared = {
49
+ outcome: Outcome;
50
+ important: boolean;
51
+ };
52
+ /** What a declaration list, a class or an element declares of each property it sets. */
53
+ export type Declarations = Partial<Record<Showing, Declared>>;
54
+ /** What a page's own live styles declare of each class, by its one-class rules in page order. */
55
+ export type PageHiding = ReadonlyMap<string, Declarations>;
56
+ /**
57
+ * What `styles` (a page's own live style elements, InertDrop's styles) declare of each class's display and visibility:
58
+ * a rule at the top level whose selector is that one class (`.x` in `.x, .y { … }`) declares for it, a later rule's
59
+ * declaration after an earlier one's (after); a class a selector names otherwise (`.open .x`, `.x:hover`, `div.x`) is in
60
+ * doubt for each property that rule sets, since when it applies is not known here. A rule inside an at-rule (`@media`)
61
+ * is passed, and a style element not for the screen (forScreen) counts for nothing. Its style text is read once, at
62
+ * most STYLE_READ_CHARS of it, each character a bounded number of times.
63
+ */
64
+ export declare function pageHidingOf(styles: readonly Style[]): PageHiding;
65
+ /**
66
+ * A page without the elements it hides from everyone (hides), in steps so a caller on the event loop can yield between
67
+ * them (dropHidden, all at once). Run on a page its inert blocks are dropped from (InertDrop), with what its own live
68
+ * styles declare (pageHidingOf). A hidden element goes from its start tag to the end tag that closes it, a same-name
69
+ * element inside it counted; one that no end tag closes hides nothing, since where it ends is not known without a full
70
+ * tree. A hidden element that holds no content (VOID) goes alone. Visibility is inherited and a descendant can set it
71
+ * back: an element hidden by visibility alone goes only if nothing inside it shows itself again (hides' `shows`), and is
72
+ * kept whole otherwise. Only a descendant that is shown counts: one display-hidden itself, or inside a display-hidden
73
+ * element within the visibility-hidden one, shows nothing. display:none is not undone by a descendant, so its element
74
+ * always goes. Linear: one pass pairs each start tag with its end tag (an open-tag stack per name), marking the
75
+ * innermost open visibility-hidden element when a shown start tag inside it shows itself, and handing the mark to the
76
+ * element around it (its `outer`) as each closes; each tag costs a bounded amount of work, however the page nests or
77
+ * fails to (an element closed out of order is kept, and its stack entry left to be popped when it reaches the top).
78
+ * Then the hidden ranges are cut, the outermost of each nest.
79
+ */
80
+ export declare class HiddenDrop {
81
+ #private;
82
+ readonly html: string;
83
+ readonly page: PageHiding;
84
+ readonly decode: Decode;
85
+ constructor(html: string, page: PageHiding, decode: Decode);
86
+ /** Pairs the tags found in the next `chars` characters or so; true once the whole page is read. */
87
+ step(chars: number): boolean;
88
+ /** The page without its hidden ranges (once step has answered true; the page as it is before that). */
89
+ result(): string;
90
+ }
91
+ /** `html` without the elements it hides from everyone, all at once (see HiddenDrop); `page` and `decode` as there. */
92
+ export declare function dropHidden(html: string, page: PageHiding, decode: Decode): string;
25
93
  /** `html` cut to at most `max` characters, after the last tag that ends before the cut, so no tag is left half; at
26
94
  * `max` itself when that tag ends more than 64 KiB before it (a page of one long text), or one before it when `max`
27
95
  * falls between the two halves of a character written as a surrogate pair, so no half of one is left. */
@@ -93,3 +161,4 @@ export declare class PageScan {
93
161
  text: string;
94
162
  };
95
163
  }
164
+ export {};
@@ -26,6 +26,7 @@ export class InertDrop {
26
26
  #next;
27
27
  #unclosed = new Map();
28
28
  #kept = [];
29
+ #styles = [];
29
30
  /** Where the scan is, and where the HTML kept since the last block dropped starts. */
30
31
  #at = 0;
31
32
  #from = 0;
@@ -84,8 +85,12 @@ export class InertDrop {
84
85
  end = close >= 0 ? close : html.length;
85
86
  }
86
87
  else {
87
- const close = this.#endTagEnd(name, end);
88
- end = close >= 0 ? close : name === "svg" ? end : html.length;
88
+ const close = this.#endTag(name, end);
89
+ // A style element of the page itself: never one inside a comment, a script, a template, a noscript or an svg,
90
+ // since each of those goes whole, its insides unread.
91
+ if (name === "style")
92
+ this.#styles.push({ inside: insideOf(html, name, tag.lt, tag.gt), css: html.slice(end, close ? close.start : html.length) });
93
+ end = close ? close.end : name === "svg" ? end : html.length;
89
94
  }
90
95
  }
91
96
  this.#kept.push(html.slice(this.#from, tag.lt));
@@ -166,6 +171,8 @@ export class InertDrop {
166
171
  }
167
172
  /** The page as dropped so far (the whole of it once step has answered true). */
168
173
  result() { return this.#kept.length === 1 ? this.#kept[0] : this.#kept.join(""); }
174
+ /** The page's own style elements dropped so far, in page order: what each start tag holds (insideOf) and its text. */
175
+ styles() { return this.#styles; }
169
176
  }
170
177
  /** `html` without its inert blocks, all at once (see InertDrop). */
171
178
  export function dropInert(html) {
@@ -173,6 +180,447 @@ export function dropInert(html) {
173
180
  drop.step(Number.POSITIVE_INFINITY);
174
181
  return drop.result();
175
182
  }
183
+ /**
184
+ * The classes common frameworks hide an element from everyone with, whatever the page's own style says (it is often an
185
+ * external stylesheet a page read never fetches): mfp-hide (Magnific Popup: a pop-up's content until it opens, as on
186
+ * Canada.ca's record pages), modal (Bootstrap: a modal until it opens), d-none (Bootstrap 4+), hidden (Tailwind, Bootstrap
187
+ * 3), hide (Bootstrap 2), invisible (Tailwind, Bootstrap: visibility), is-hidden (Bulma), u-hidden (SUIT utilities). The
188
+ * classes that hide text from the screen only (sr-only, visually-hidden, wb-inv, screen-reader-text) are not here: a
189
+ * screen reader reads them, and they are often a record's field labels and table captions, the words its values hang
190
+ * on. aria-hidden is read for the same reason the other way: it hides from a screen reader only, and is usually shown.
191
+ */
192
+ export const HIDING_CLASSES = new Set(["mfp-hide", "modal", "d-none", "hidden", "hide", "invisible", "is-hidden", "u-hidden"]);
193
+ /** The most of a page's style text read for what it hides (pageHidingOf). */
194
+ const STYLE_READ_CHARS = 1_000_000;
195
+ /** A selector of one class alone (`.x`), and every class a selector names. */
196
+ const ONE_CLASS = /^\s*\.(-?[_a-zA-Z][_a-zA-Z0-9-]*)\s*$/;
197
+ const CLASSES_IN = /\.(-?[_a-zA-Z][_a-zA-Z0-9-]*)/g;
198
+ /** Elements that never hold content: hidden, only the tag itself goes. */
199
+ const VOID = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"]);
200
+ /** The media a style element counts for here: none named, or the screen among them. */
201
+ const SCREEN_MEDIA = new Set(["", "all", "screen", "only screen", "only all"]);
202
+ const DEFERRING = new Set(["inherit", "unset", "revert", "revert-layer"]);
203
+ /** What `value` of `property` comes to. */
204
+ function outcomeOf(property, value) {
205
+ if (!value || DEFERRING.has(value))
206
+ return "doubt";
207
+ if (property === "display")
208
+ return value === "none" ? "hide" : "show";
209
+ return value === "hidden" || value === "collapse" ? "hide" : value === "visible" ? "show" : "doubt";
210
+ }
211
+ /** `next` declared after `prior` of one property, as the cascade takes it among declarations of equal weight: the later
212
+ * wins, but an earlier !important one that a later plain one contradicts makes it doubt (that mix is not modelled). */
213
+ function after(prior, next) {
214
+ if (!prior)
215
+ return next;
216
+ if (prior.outcome === "doubt" || next.outcome === "doubt")
217
+ return { outcome: "doubt", important: prior.important || next.important };
218
+ if (prior.important && !next.important && prior.outcome !== next.outcome)
219
+ return { outcome: "doubt", important: true };
220
+ return next;
221
+ }
222
+ /** `text` split at the semicolons that end a declaration: one inside a quoted string (either quote, backslash escapes), a
223
+ * parenthesis (url(...)) or a comment does not end it, and a comment's text is dropped. */
224
+ function splitDeclarations(text) {
225
+ const parts = [];
226
+ let current = "";
227
+ let quote = null;
228
+ let depth = 0;
229
+ for (let i = 0; i < text.length; i++) {
230
+ const ch = text[i];
231
+ if (quote) {
232
+ current += ch;
233
+ if (ch === "\\" && i + 1 < text.length)
234
+ current += text[++i];
235
+ else if (ch === quote)
236
+ quote = null;
237
+ }
238
+ else if (ch === "/" && text[i + 1] === "*") {
239
+ const end = text.indexOf("*/", i + 2);
240
+ i = end < 0 ? text.length : end + 1;
241
+ current += " "; // a comment is a boundary: display:n/**/one is two words, not none
242
+ }
243
+ else if (ch === '"' || ch === "'") {
244
+ quote = ch;
245
+ current += ch;
246
+ }
247
+ else if (ch === "\\" && i + 1 < text.length) {
248
+ current += ch + text[++i];
249
+ }
250
+ else if (ch === "(") {
251
+ depth++;
252
+ current += ch;
253
+ }
254
+ else if (ch === ")") {
255
+ if (depth > 0)
256
+ depth--;
257
+ current += ch;
258
+ }
259
+ else if (ch === ";" && depth === 0) {
260
+ parts.push(current);
261
+ current = "";
262
+ }
263
+ else
264
+ current += ch;
265
+ }
266
+ parts.push(current);
267
+ return parts;
268
+ }
269
+ /** What a declaration list (a style attribute's value, a rule's block) declares of display and visibility, each
270
+ * property's declarations taken in order (after). One pass over `text`. */
271
+ function declarationsOf(text) {
272
+ const declared = {};
273
+ for (const part of splitDeclarations(text)) {
274
+ const colon = part.indexOf(":");
275
+ if (colon < 0)
276
+ continue;
277
+ const property = part.slice(0, colon).trim().toLowerCase();
278
+ if (property !== "display" && property !== "visibility")
279
+ continue;
280
+ let value = part.slice(colon + 1).trim().toLowerCase();
281
+ const important = /!\s*important$/.test(value);
282
+ if (important)
283
+ value = value.replace(/!\s*important$/, "").trim();
284
+ declared[property] = after(declared[property], { outcome: outcomeOf(property, value), important });
285
+ }
286
+ return declared;
287
+ }
288
+ /** Whether a style element whose start tag holds `inside` applies to the screen: no media named, or one of them all or
289
+ * screen (a media query, or a value written with references, does not count: in doubt its rules hide nothing). */
290
+ function forScreen(inside) {
291
+ let media = null;
292
+ eachAttribute(inside, (name, value) => {
293
+ if (name.toLowerCase() !== "media")
294
+ return false;
295
+ media = value;
296
+ return true;
297
+ });
298
+ if (media === null)
299
+ return true;
300
+ return media.split(",").some((one) => SCREEN_MEDIA.has(one.trim().toLowerCase().replace(/\s+/g, " ")));
301
+ }
302
+ /**
303
+ * What `styles` (a page's own live style elements, InertDrop's styles) declare of each class's display and visibility:
304
+ * a rule at the top level whose selector is that one class (`.x` in `.x, .y { … }`) declares for it, a later rule's
305
+ * declaration after an earlier one's (after); a class a selector names otherwise (`.open .x`, `.x:hover`, `div.x`) is in
306
+ * doubt for each property that rule sets, since when it applies is not known here. A rule inside an at-rule (`@media`)
307
+ * is passed, and a style element not for the screen (forScreen) counts for nothing. Its style text is read once, at
308
+ * most STYLE_READ_CHARS of it, each character a bounded number of times.
309
+ */
310
+ export function pageHidingOf(styles) {
311
+ const hiding = new Map();
312
+ const declare = (name, declared) => {
313
+ const known = hiding.get(name) ?? {};
314
+ for (const property of ["display", "visibility"]) {
315
+ const next = declared[property];
316
+ if (next)
317
+ known[property] = after(known[property], next);
318
+ }
319
+ hiding.set(name, known);
320
+ };
321
+ let read = 0;
322
+ for (const { inside, css } of styles) {
323
+ if (read >= STYLE_READ_CHARS)
324
+ break;
325
+ const text = css.slice(0, STYLE_READ_CHARS - read);
326
+ read += text.length;
327
+ if (!forScreen(inside))
328
+ continue;
329
+ rulesOf(text, (selectors, block) => {
330
+ const declared = declarationsOf(block);
331
+ if (!declared.display && !declared.visibility)
332
+ return;
333
+ for (const selector of selectors.split(",")) {
334
+ const one = ONE_CLASS.exec(selector);
335
+ if (one) {
336
+ declare(one[1], declared);
337
+ continue;
338
+ }
339
+ const doubt = {};
340
+ for (const property of ["display", "visibility"])
341
+ if (declared[property])
342
+ doubt[property] = { outcome: "doubt", important: false };
343
+ for (const named of selector.matchAll(CLASSES_IN))
344
+ declare(named[1], doubt);
345
+ }
346
+ });
347
+ }
348
+ return hiding;
349
+ }
350
+ /** Each rule at the top level of `css` (its comments dropped): its selector text and its declaration block; an at-rule
351
+ * (a statement to its ";", or its block whole) is passed. One forward pass: each character is read once. */
352
+ function rulesOf(css, rule) {
353
+ const text = css.includes("/*") ? css.replace(/\/\*[\s\S]*?(?:\*\/|$)/g, " ") : css;
354
+ const { length } = text;
355
+ for (let at = 0; at < length;) {
356
+ const code = text.charCodeAt(at);
357
+ // Between rules: spaces, and a stray ";" or "}".
358
+ if (isSpace(code) || code === 59 || code === 125) {
359
+ at += 1;
360
+ continue;
361
+ }
362
+ const start = at;
363
+ let stop = at;
364
+ while (stop < length && text.charCodeAt(stop) !== 123 && text.charCodeAt(stop) !== 59)
365
+ stop += 1;
366
+ if (stop >= length)
367
+ break;
368
+ // A ";" before any "{": an at-rule statement ("@import …;"), or text that is no rule: passed.
369
+ if (text.charCodeAt(stop) === 59) {
370
+ at = stop + 1;
371
+ continue;
372
+ }
373
+ let depth = 1;
374
+ let end = stop + 1;
375
+ for (; end < length && depth > 0; end += 1) {
376
+ const c = text.charCodeAt(end);
377
+ if (c === 123)
378
+ depth += 1;
379
+ else if (c === 125)
380
+ depth -= 1;
381
+ }
382
+ if (code !== 64)
383
+ rule(text.slice(start, stop), text.slice(stop + 1, depth === 0 ? end - 1 : end));
384
+ at = end;
385
+ }
386
+ }
387
+ /** One property's outcome among an element's classes: theirs where every class that declares it agrees and none is in
388
+ * doubt; doubt where they disagree (which rule comes last across classes is not modelled), !important if any is. */
389
+ function amongClasses(declared) {
390
+ if (declared.length === 0)
391
+ return undefined;
392
+ const important = declared.some((one) => one.important);
393
+ const outcomes = new Set(declared.map((one) => one.outcome));
394
+ return { outcome: outcomes.size === 1 ? declared[0].outcome : "doubt", important };
395
+ }
396
+ /**
397
+ * Whether a start tag named `name`, holding `inside` (insideOf), hides its element from everyone. Each property is
398
+ * decided on its own: an inline declaration over its classes' (unless a class's !important one it contradicts, then
399
+ * doubt), the classes' over the browser's own (the hidden attribute and a dialog not open, display none). Its classes
400
+ * declare by the page's one-class rules (`page`) and HIDING_CLASSES. `why`: "display" when display comes to "hide",
401
+ * else "visibility" when visibility does, else null; doubt hides nothing. `shows`: the element sets visibility itself
402
+ * to anything but hidden (or in doubt), so a visibility-hidden ancestor's hiding may not reach it. A class or style
403
+ * value's character references are decoded first (`decode`).
404
+ */
405
+ function hides(name, inside, page, decode) {
406
+ let hiddenAttribute = false;
407
+ let open = false;
408
+ let style = "";
409
+ let classes = "";
410
+ eachAttribute(inside, (named, value) => {
411
+ const attribute = named.toLowerCase();
412
+ if (attribute === "hidden")
413
+ hiddenAttribute = true;
414
+ else if (attribute === "open")
415
+ open = true;
416
+ else if (attribute === "style" && !style)
417
+ style = value.includes("&") ? decode(value) : value;
418
+ else if (attribute === "class" && !classes)
419
+ classes = value.includes("&") ? decode(value) : value;
420
+ return false;
421
+ });
422
+ const inline = style ? declarationsOf(style) : {};
423
+ const byClass = { display: [], visibility: [] };
424
+ for (const one of classes.split(/\s+/)) {
425
+ if (!one)
426
+ continue;
427
+ if (HIDING_CLASSES.has(one))
428
+ byClass[one === "invisible" ? "visibility" : "display"].push({ outcome: "hide", important: false });
429
+ const declared = page.get(one);
430
+ if (declared?.display)
431
+ byClass.display.push(declared.display);
432
+ if (declared?.visibility)
433
+ byClass.visibility.push(declared.visibility);
434
+ }
435
+ const browser = hiddenAttribute || (name === "dialog" && !open);
436
+ const decided = (property) => {
437
+ const own = inline[property];
438
+ const fromClasses = amongClasses(byClass[property]);
439
+ if (own)
440
+ return fromClasses?.important && !own.important && fromClasses.outcome !== own.outcome ? "doubt" : own.outcome;
441
+ if (fromClasses)
442
+ return fromClasses.outcome;
443
+ return property === "display" && browser ? "hide" : undefined;
444
+ };
445
+ const visibility = decided("visibility");
446
+ return { why: decided("display") === "hide" ? "display" : visibility === "hide" ? "visibility" : null, shows: visibility === "show" || visibility === "doubt" };
447
+ }
448
+ /**
449
+ * A page without the elements it hides from everyone (hides), in steps so a caller on the event loop can yield between
450
+ * them (dropHidden, all at once). Run on a page its inert blocks are dropped from (InertDrop), with what its own live
451
+ * styles declare (pageHidingOf). A hidden element goes from its start tag to the end tag that closes it, a same-name
452
+ * element inside it counted; one that no end tag closes hides nothing, since where it ends is not known without a full
453
+ * tree. A hidden element that holds no content (VOID) goes alone. Visibility is inherited and a descendant can set it
454
+ * back: an element hidden by visibility alone goes only if nothing inside it shows itself again (hides' `shows`), and is
455
+ * kept whole otherwise. Only a descendant that is shown counts: one display-hidden itself, or inside a display-hidden
456
+ * element within the visibility-hidden one, shows nothing. display:none is not undone by a descendant, so its element
457
+ * always goes. Linear: one pass pairs each start tag with its end tag (an open-tag stack per name), marking the
458
+ * innermost open visibility-hidden element when a shown start tag inside it shows itself, and handing the mark to the
459
+ * element around it (its `outer`) as each closes; each tag costs a bounded amount of work, however the page nests or
460
+ * fails to (an element closed out of order is kept, and its stack entry left to be popped when it reaches the top).
461
+ * Then the hidden ranges are cut, the outermost of each nest.
462
+ */
463
+ export class HiddenDrop {
464
+ html;
465
+ page;
466
+ decode;
467
+ #next;
468
+ #open = new Map();
469
+ /** The visibility-hidden elements open now, outermost first; one closed out of order stays until it reaches the top. */
470
+ #invisible = [];
471
+ /** How many display-hidden elements are open now. */
472
+ #displayOpen = 0;
473
+ #ranges = [];
474
+ #at = 0;
475
+ #done = false;
476
+ constructor(html, page, decode) {
477
+ this.html = html;
478
+ this.page = page;
479
+ this.decode = decode;
480
+ this.#next = tagsOf(html);
481
+ }
482
+ /** Pairs the tags found in the next `chars` characters or so; true once the whole page is read. */
483
+ step(chars) {
484
+ const { html } = this;
485
+ const until = this.#at + chars;
486
+ while (!this.#done && this.#at < until) {
487
+ const tag = this.#next(this.#at);
488
+ if (!tag || tag.gt < 0) {
489
+ this.#done = true;
490
+ break;
491
+ }
492
+ this.#at = tag.gt + 1;
493
+ const { name } = tag;
494
+ if (!name)
495
+ continue;
496
+ if (tag.end) {
497
+ const open = this.#open.get(name)?.pop();
498
+ if (open) {
499
+ // An end tag closes what its element left open: a p or li opened after it ends here (HTML's optional end tags).
500
+ for (const implied of IMPLIED_END) {
501
+ const stack = this.#open.get(implied);
502
+ while (stack && stack.length > 0 && stack[stack.length - 1].lt > open.lt)
503
+ this.#close(stack.pop(), tag.lt);
504
+ }
505
+ this.#close(open, tag.gt + 1);
506
+ }
507
+ continue;
508
+ }
509
+ // A start tag that closes an open p or li (optional end tags): that element ends here, so its hiding stops here and
510
+ // never reaches text a browser shows.
511
+ if (P_CLOSERS.has(name))
512
+ this.#closeImplied("p", tag.lt);
513
+ if (name === "li") {
514
+ const last = this.#open.get("li")?.at(-1);
515
+ // Only an li not inside a list opened after it.
516
+ if (last && !LISTS.some((list) => (this.#open.get(list)?.at(-1)?.lt ?? -1) > last.lt))
517
+ this.#closeImplied("li", tag.lt);
518
+ }
519
+ const { why, shows } = hides(name, insideOf(html, name, tag.lt, tag.gt), this.page, this.decode);
520
+ // It shows itself inside a visibility-hidden element, and is shown (not display-hidden itself, and no display-hidden
521
+ // element open between them): that element is kept (and, as it closes, those around it).
522
+ const inner = this.#invisible.at(-1);
523
+ if (shows && why !== "display" && inner && this.#displayOpen === inner.displayOpen)
524
+ inner.shown = true;
525
+ const hidden = why !== null;
526
+ if (VOID.has(name)) {
527
+ if (hidden)
528
+ this.#ranges.push([tag.lt, tag.gt + 1]);
529
+ continue;
530
+ }
531
+ // Text, not markup, to its end tag (an iframe's fallback, an xmp): its "<" are not tags.
532
+ if (TEXT_ONLY.has(name)) {
533
+ const closer = CLOSER[name];
534
+ closer.lastIndex = tag.gt + 1;
535
+ const close = closer.exec(html);
536
+ if (hidden && close)
537
+ this.#ranges.push([tag.lt, close.index + close[0].length]);
538
+ if (!close) {
539
+ this.#done = true;
540
+ break;
541
+ }
542
+ this.#at = close.index + close[0].length;
543
+ continue;
544
+ }
545
+ let open = this.#open.get(name);
546
+ if (!open)
547
+ this.#open.set(name, (open = []));
548
+ const opened = { lt: tag.lt, why, shown: false, closed: false, outer: inner, displayOpen: this.#displayOpen };
549
+ open.push(opened);
550
+ if (why === "visibility")
551
+ this.#invisible.push(opened);
552
+ if (why === "display")
553
+ this.#displayOpen += 1;
554
+ }
555
+ return this.#done;
556
+ }
557
+ /** The innermost open `name` element, ended by a start tag at `at` that implies its end tag. */
558
+ #closeImplied(name, at) {
559
+ const open = this.#open.get(name)?.pop();
560
+ if (open)
561
+ this.#close(open, at);
562
+ }
563
+ /** `opened` closed by the end tag that ends at `end`: a display-hidden element goes; a visibility-hidden one goes
564
+ * unless something shown inside it showed itself again, and is then kept with its mark handed to the one around it
565
+ * (`outer`). One closed out of nesting order (by a stray end tag) is kept, since where its hiding reaches is in doubt,
566
+ * and its stack entry is left, marked closed, to be popped when it reaches the top: no search, no shift. */
567
+ #close(opened, end) {
568
+ if (opened.why === "display") {
569
+ this.#displayOpen = Math.max(0, this.#displayOpen - 1);
570
+ this.#ranges.push([opened.lt, end]);
571
+ return;
572
+ }
573
+ if (opened.why !== "visibility")
574
+ return;
575
+ const invisible = this.#invisible;
576
+ opened.closed = true;
577
+ if (invisible.at(-1) === opened) {
578
+ invisible.pop();
579
+ while (invisible.at(-1)?.closed)
580
+ invisible.pop();
581
+ }
582
+ else
583
+ opened.shown = true;
584
+ if (!opened.shown) {
585
+ this.#ranges.push([opened.lt, end]);
586
+ return;
587
+ }
588
+ // Kept, it shows in the one around it only if no display-hidden element was open between them.
589
+ if (opened.outer && opened.displayOpen === opened.outer.displayOpen)
590
+ opened.outer.shown = true;
591
+ }
592
+ /** The page without its hidden ranges (once step has answered true; the page as it is before that). */
593
+ result() {
594
+ if (!this.#done || this.#ranges.length === 0)
595
+ return this.html;
596
+ const ranges = this.#ranges.sort((a, b) => a[0] - b[0]);
597
+ const kept = [];
598
+ let from = 0;
599
+ for (const [start, end] of ranges) {
600
+ if (end <= from)
601
+ continue;
602
+ if (start > from)
603
+ kept.push(this.html.slice(from, start));
604
+ from = Math.max(from, end);
605
+ }
606
+ kept.push(this.html.slice(from));
607
+ return kept.join("");
608
+ }
609
+ }
610
+ /** Elements whose end tag HTML lets a page omit; an end tag of an element around one ends it too. */
611
+ const IMPLIED_END = ["p", "li"];
612
+ const LISTS = ["ul", "ol", "menu"];
613
+ /** Start tags that close an open p. */
614
+ const P_CLOSERS = new Set([
615
+ "address", "article", "aside", "blockquote", "details", "div", "dl", "fieldset", "figcaption", "figure", "footer", "form",
616
+ "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "main", "menu", "nav", "ol", "p", "pre", "section", "table", "ul",
617
+ ]);
618
+ /** `html` without the elements it hides from everyone, all at once (see HiddenDrop); `page` and `decode` as there. */
619
+ export function dropHidden(html, page, decode) {
620
+ const drop = new HiddenDrop(html, page, decode);
621
+ drop.step(Number.POSITIVE_INFINITY);
622
+ return drop.result();
623
+ }
176
624
  /** `html` cut to at most `max` characters, after the last tag that ends before the cut, so no tag is left half; at
177
625
  * `max` itself when that tag ends more than 64 KiB before it (a page of one long text), or one before it when `max`
178
626
  * falls between the two halves of a character written as a surrogate pair, so no half of one is left. */
@@ -275,6 +723,18 @@ const TITLE_END = /<\/title(?=[\s/>])/gi;
275
723
  * tagsOf ends a tag by: a quote right after the "=" and its spaces; one that no quote closes runs to the tag's end.
276
724
  */
277
725
  function attrOf(inside, name, decode) {
726
+ let found = null;
727
+ eachAttribute(inside, (named, value) => {
728
+ if (named.length !== name.length || named.toLowerCase() !== name)
729
+ return false;
730
+ found = value.includes("&") ? decode(value) : value;
731
+ return true;
732
+ });
733
+ return found;
734
+ }
735
+ /** Each attribute of a tag's `inside` (insideOf), in order, read as attrOf says: `visit` gets its name as written and its
736
+ * raw value ("" for one with no value), and stops the walk by answering true. */
737
+ function eachAttribute(inside, visit) {
278
738
  const { length } = inside;
279
739
  for (let at = 0; at < length;) {
280
740
  const code = inside.charCodeAt(at);
@@ -286,12 +746,12 @@ function attrOf(inside, name, decode) {
286
746
  do
287
747
  at += 1;
288
748
  while (at < length && !endsName(inside.charCodeAt(at)));
289
- const named = at - from === name.length && inside.slice(from, at).toLowerCase() === name;
749
+ const name = inside.slice(from, at);
290
750
  while (isSpace(inside.charCodeAt(at)))
291
751
  at += 1;
292
752
  if (inside.charCodeAt(at) !== EQUALS) {
293
- if (named)
294
- return "";
753
+ if (visit(name, ""))
754
+ return;
295
755
  continue;
296
756
  }
297
757
  at += 1;
@@ -311,12 +771,9 @@ function attrOf(inside, name, decode) {
311
771
  at += 1;
312
772
  end = at;
313
773
  }
314
- if (named) {
315
- const value = inside.slice(start, end);
316
- return value.includes("&") ? decode(value) : value;
317
- }
774
+ if (visit(name, inside.slice(start, end)))
775
+ return;
318
776
  }
319
- return null;
320
777
  }
321
778
  /** What a tag at `lt` named `name` holds between its name and its ">" at `gt`. */
322
779
  const insideOf = (html, name, lt, gt) => html.slice(lt + 1 + name.length, gt);
@@ -41,6 +41,9 @@ export function isPrivateAddress(ip) {
41
41
  return true;
42
42
  if (groups.slice(0, 5).every((group) => group === 0) && groups[5] === 0xffff)
43
43
  return isPrivateAddress([groups[6] >> 8, groups[6] & 0xff, groups[7] >> 8, groups[7] & 0xff].join("."));
44
+ // 6to4 (2002::/16, RFC 3056) carries an IPv4 address in its next 32 bits, which a relay delivers to: judged as that one.
45
+ if (groups[0] === 0x2002)
46
+ return isPrivateAddress([groups[1] >> 8, groups[1] & 0xff, groups[2] >> 8, groups[2] & 0xff].join("."));
44
47
  return V6_PRIVATE.some(([base, bits]) => inV6(groups, v6Groups(base), bits));
45
48
  }
46
49
  /** Answers `address` for any name: the address a request was checked against is the one it connects to. */
@@ -1,7 +1,8 @@
1
1
  // A fetched HTML page read as its title, its text and its links, without ever holding the event loop. The host's tools
2
2
  // run in the run's own process, so a read that holds the loop holds the run's heartbeat with it, and a run whose
3
3
  // heartbeat lapses is fenced: go.dev/dl, 3.15 MB of tables, held it past 240 s in one markdown conversion. So one linear
4
- // pass drops the blocks that hold no readable text (page-scan.ts); what is left is cut at READ_MAX_CHARS (the page is
4
+ // pass drops the blocks that hold no readable text, and another the elements the page hides from everyone (page-scan.ts:
5
+ // a help pop-up until its link is clicked is not the page's text); what is left is cut at READ_MAX_CHARS (the page is
5
6
  // then `truncated`, and `html_chars` is its whole length); and a worker thread converts that to markdown, in pieces
6
7
  // small enough that the converter stays linear (page-markdown.ts), within a deadline. A worker that has not answered by
7
8
  // then, or that fails, is stopped, and the text is the page's plain text, read on the loop in steps that yield it
@@ -10,7 +11,7 @@
10
11
  import { setImmediate as yieldToLoop } from "node:timers/promises";
11
12
  import { Worker } from "node:worker_threads";
12
13
  import { decodeEntities } from "./entities.js";
13
- import { cutHtml, InertDrop, PageScan } from "./page-scan.js";
14
+ import { cutHtml, HiddenDrop, InertDrop, pageHidingOf, PageScan } from "./page-scan.js";
14
15
  /** The most of a page's HTML that is read, counted once its script, style, svg, noscript and template blocks and its
15
16
  * comments are dropped. A million characters of go.dev/dl converts in about 0.8 s on a busy 48-core host and gives
16
17
  * 590,000 characters of markdown, ten times the 60,000 one web_fetch answer carries. */
@@ -28,7 +29,11 @@ const STEP_CHARS = 32_768;
28
29
  */
29
30
  export async function readPage(html, url, { deadlineMs = READ_DEADLINE_MS, signal } = {}) {
30
31
  signal?.throwIfAborted();
31
- const kept = await dropOnLoop(html, signal);
32
+ // What the page hides from everyone who reads it (a help pop-up until its link is clicked, a closed dialog) is not
33
+ // its text: what its own live style elements declare is read from those the inert drop took out.
34
+ const inert = new InertDrop(html);
35
+ await stepsOnLoop(inert, signal);
36
+ const kept = await dropHiddenOnLoop(inert.result(), pageHidingOf(inert.styles()), signal);
32
37
  const read = cutHtml(kept, READ_MAX_CHARS);
33
38
  const cut = read.length < kept.length ? { truncated: true, html_chars: html.length } : {};
34
39
  await yieldToLoop();
@@ -78,10 +83,21 @@ function inWorker(job, deadlineMs, signal) {
78
83
  /** `html` without its inert blocks (InertDrop), STEP_CHARS at a time with the loop yielded between steps. */
79
84
  export async function dropOnLoop(html, signal) {
80
85
  const drop = new InertDrop(html);
81
- while (!drop.step(STEP_CHARS)) {
86
+ await stepsOnLoop(drop, signal);
87
+ return drop.result();
88
+ }
89
+ /** Runs `pass` STEP_CHARS at a time, the loop yielded between steps, to its end. */
90
+ async function stepsOnLoop(pass, signal) {
91
+ while (!pass.step(STEP_CHARS)) {
82
92
  await yieldToLoop();
83
93
  signal?.throwIfAborted();
84
94
  }
95
+ }
96
+ /** `html` without the elements it hides from everyone (HiddenDrop), with `page` what its own live styles declare
97
+ * (pageHidingOf), STEP_CHARS at a time with the loop yielded between steps. */
98
+ async function dropHiddenOnLoop(html, page, signal) {
99
+ const drop = new HiddenDrop(html, page, decodeEntities);
100
+ await stepsOnLoop(drop, signal);
85
101
  return drop.result();
86
102
  }
87
103
  /** `html`'s plain text, title and links, scanned STEP_CHARS at a time with the loop yielded between steps. */
@@ -1,4 +1,4 @@
1
- import type { Context } from "@earendil-works/chord";
1
+ import type { Context } from "@earendil-works/chord/context";
2
2
  import { type ConversationDocToken, type ConversationId, type DocumentReader, type Harness, type TaskId, type Tx } from "@earendil-works/pi-durable";
3
3
  import type { BrowserConfig, BrowserProvider, HandoffOutcome, HandoffPort, InventoryRow, ReconcileReport, ReleaseReason, SessionChange, SessionRecord, SessionsState } from "../core/host.js";
4
4
  import type { LeaseRef, LeaseState } from "../core/lease.js";
package/dist/durable.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Context } from "@earendil-works/chord";
1
+ import type { Context } from "@earendil-works/chord/context";
2
2
  import { type Conversation, type ConversationDocToken, type ConversationId, type Extension, type Harness, type SessionDocToken, type ToolRegistration, type Tx } from "@earendil-works/pi-durable";
3
3
  import { type DriverFactory } from "./core/custody.js";
4
4
  import type { Decisions } from "./core/decisions.js";
@@ -3,3 +3,5 @@ export { liveViewProvider } from "./provider.js";
3
3
  export * from "./protocol.js";
4
4
  export { createDrivingGates, drivingGates, personDrivingFailure, PERSON_DRIVING, withModelCall, type DrivingOptions } from "../core/driving.js";
5
5
  export { viewerAssets } from "./viewer.js";
6
+ export { acceptWebSocket, type WsConn, type WsOptions } from "./ws.js";
7
+ export { newToken, newViewerId, tokenMatches } from "./token.js";
@@ -3,3 +3,7 @@ export { liveViewProvider } from "./provider.js";
3
3
  export * from "./protocol.js";
4
4
  export { createDrivingGates, drivingGates, personDrivingFailure, PERSON_DRIVING, withModelCall } from "../core/driving.js";
5
5
  export { viewerAssets } from "./viewer.js";
6
+ // The WebSocket server and the viewer's credentials, for another live view built on this protocol (serve's computer
7
+ // viewer, computer-live.ts): the same handshake, frames and token checks.
8
+ export { acceptWebSocket } from "./ws.js";
9
+ export { newToken, newViewerId, tokenMatches } from "./token.js";
@@ -8,6 +8,17 @@ export type LiveViewOptions = {
8
8
  /** Where the host mounts `handle` and `upgrade`, e.g. `https://host.example/live`. Its origin is the viewer's own origin, which an upgrade's Origin
9
9
  * header must equal. Absent: the live view starts its own server on 127.0.0.1 (never 0.0.0.0), on an ephemeral port, at the first `open`. */
10
10
  baseUrl?: string;
11
+ /** With no `baseUrl`: the address its own server listens on, and the host the viewer's link names, for a host whose
12
+ * viewer is reached from another machine (its own proxy, never a person's browser directly). Absent: loopback for
13
+ * both. A host that listens off loopback gives `admit` too. */
14
+ listen?: {
15
+ readonly bind: string;
16
+ readonly host: string;
17
+ };
18
+ /** Asked of every page request and upgrade before anything else is read of it: false is a 403. The host's own check
19
+ * that the request comes from its own proxy (a signed header, say). Absent: every request is asked only what it is
20
+ * asked today. */
21
+ admit?: (req: IncomingMessage) => boolean;
11
22
  /** The screencast and input (stream.ts); the default is the package's own. A test passes a fake. */
12
23
  stream?: StreamFactory;
13
24
  /** The person/model mutex. Absent, the package's own process-wide registry (`drivingGates`, core/driving.ts), the one the toolset's model calls use, so a
@@ -5,6 +5,8 @@ import { openStream } from "./stream.js";
5
5
  import { newToken, newViewerId, tokenMatches } from "./token.js";
6
6
  import { viewerAssets } from "./viewer.js";
7
7
  import { acceptWebSocket } from "./ws.js";
8
+ /** Where the live view's own server listens with no `listen`: this machine alone. */
9
+ const LOOPBACK_HOST = "127.0.0.1"; // key-gate: loopback-rule the default, this machine alone
8
10
  const ID = /^[A-Za-z0-9_-]{22}$/;
9
11
  /** A viewer that cannot take frames as fast as they come drops them: past this many unsent bytes a frame is acknowledged unsent. */
10
12
  const SLOW_VIEWER_BYTES = 512 * 1024;
@@ -65,6 +67,11 @@ export function createLiveView(options) {
65
67
  const upgradeRoute = /^\/v\/([^/]+)\/ws$/.exec(rel);
66
68
  if (!page && !upgradeRoute && rel !== "/viewer.js" && rel !== "/viewer.css")
67
69
  return false;
70
+ if (options.admit && !options.admit(req)) {
71
+ res.writeHead(403, { ...securityHeaders(), "Content-Length": 0 });
72
+ res.end();
73
+ return true;
74
+ }
68
75
  const reply = (status, type, body) => { res.writeHead(status, { ...securityHeaders(), "Content-Type": type, "Content-Length": Buffer.byteLength(body) }); res.end(req.method === "HEAD" ? undefined : body); return true; };
69
76
  if (req.method !== "GET" && req.method !== "HEAD")
70
77
  return reply(405, "text/plain", "method not allowed");
@@ -90,6 +97,8 @@ export function createLiveView(options) {
90
97
  // A browser always sends its page's origin on a WebSocket; another tab's script cannot forge it. Anything but the viewer's own is refused.
91
98
  if (origin === null || req.headers.origin !== origin)
92
99
  return deny("403 Forbidden");
100
+ if (options.admit && !options.admit(req))
101
+ return deny("403 Forbidden");
93
102
  const entry = ID.test(match[1]) ? byId.get(match[1]) : undefined;
94
103
  if (!entry || entry.released)
95
104
  return deny("404 Not Found");
@@ -377,8 +386,10 @@ export function createLiveView(options) {
377
386
  s.on("upgrade", (req, socket, head) => { if (!upgrade(req, socket, head))
378
387
  socket.destroy(); });
379
388
  s.once("error", reject);
380
- // Loopback only, never 0.0.0.0: the host that wants more mounts the handler on its own server.
381
- s.listen(0, "127.0.0.1", () => { s.unref(); server = s; origin = `http://127.0.0.1:${s.address().port}`; resolve(); });
389
+ // Loopback unless the host names another address (`listen`, with its `admit`): the host that wants more mounts the
390
+ // handler on its own server.
391
+ const at = options.listen ?? { bind: LOOPBACK_HOST, host: LOOPBACK_HOST };
392
+ s.listen(0, at.bind, () => { s.unref(); server = s; origin = `http://${at.host}:${s.address().port}`; resolve(); });
382
393
  });
383
394
  return starting;
384
395
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcha/pi-browser",
3
- "version": "0.1.0-beta.36",
3
+ "version": "0.1.0-beta.37",
4
4
  "description": "Your agent gets its own browser, and you can take over and hand it back.",
5
5
  "keywords": [
6
6
  "pi-durable",
@@ -78,8 +78,8 @@
78
78
  "zod": "~4.6.6"
79
79
  },
80
80
  "peerDependencies": {
81
- "@earendil-works/chord": "1.5.0",
82
- "@earendil-works/pi-durable": "1.5.0"
81
+ "@earendil-works/chord": "1.5.2",
82
+ "@earendil-works/pi-durable": "1.5.2"
83
83
  },
84
84
  "peerDependenciesMeta": {
85
85
  "@earendil-works/chord": {
@@ -90,9 +90,9 @@
90
90
  }
91
91
  },
92
92
  "devDependencies": {
93
- "@earendil-works/chord": "1.5.0",
94
- "@earendil-works/pi-ai": "1.5.0",
95
- "@earendil-works/pi-durable": "1.5.0"
93
+ "@earendil-works/chord": "1.5.2",
94
+ "@earendil-works/pi-ai": "1.5.2",
95
+ "@earendil-works/pi-durable": "1.5.2"
96
96
  },
97
97
  "repository": {
98
98
  "type": "git",