Public: anyone can read this; the crew can change it.
Peek cards: read 4 KB of a body and strip markup in one pass (review fix missed by #16700) #16815
Check · Review · Land · Live done — Landed and live in production.
peek-review → main 0 commits 1 thread · 1 unresolved
4 files
changed +53
−6
docs/features/peek.md +2 −2
| @@ -6,10 +6,10 @@ row: A card for every #12 | ||
| 6 | 6 | |
| 7 | 7 | A link that reads `#12` or `owner/name#12`, in a comment, a pull's body, a receipt, the home feed or anywhere else the forge draws one, shows a small card on intent: after the pointer rests on it for 400 ms, at once on keyboard focus, and on touch after a 500 ms long press, as a sheet through the overlay host. A tap or a click still follows the link, and the click that ends a long press is the press's own. In Talk's thread every link to an issue, a pull or a repository on this forge peeks, whatever its words. A row that already shows its title beside the link (an issue list, a board) gets no card. |
| 8 | 8 | |
| 9 | The card says the reference and its state in a word (Open, Merged, Closed; Draft; how its checks went), the title, who opened it and when, and the first three lines of its body as plain text: fenced code, HTML, comments and scripts taken out, links and images down to their words, the marks of headings, lists, quotes, tables and emphasis dropped. A signed-in reader also gets Open and Talk about this. One card at a time; each link is asked once a page. Escape closes it and puts focus back on the link; while it is open the link is described by it (`aria-describedby`), and focus is never held inside it. | |
| 9 | The card says the reference and its state in a word (Open, Merged, Closed; Draft; how its checks went), the title, who opened it and when, and the first three lines of its body as plain text, read from its first 4 KB only (in one pass, so a body built to be slow to scan is not): fenced code, HTML, comments and scripts taken out, links and images down to their words, the marks of headings, lists, quotes, tables and emphasis dropped. A signed-in reader also gets Open and Talk about this. One card at a time; each link is asked once a page. Escape closes it and puts focus back on the link; while it is open the link is described by it (`aria-describedby`), and focus is never held inside it. | |
| 10 | 10 | |
| 11 | 11 | It is `GET /peek?href=<rooted path>` (`src/web/peek.ts`), read exactly as the link is: a repository the viewer cannot read (a signed-out visitor on a private one included), a sealed issue they may not see, a number that is not there, a pull asked as `/pulls/N` that is an issue, or any other path answers the same 404, byte for byte. Every answer is `Cache-Control: private, no-store` and `Vary: Cookie`, so no shared cache gives one viewer's card to another. A card is at most five statements. The page side is in the shell script, about 1.4 KB gzip on every page. |
| 12 | 12 | |
| 13 | Not here: cards for commits, people, or code and diff text; a card that updates while open; references to other forges. **In the terminal** there is no hover: the issue and repository screens open what a reference names. | |
| 13 | Not here: cards for commits, people, or code and diff text; a card that updates while open; references to other forges. **In the terminal** there is no hover, and no card is needed: the issue list (`src/tui/screens/issues.ts`) already gives each reference these facts as one row (the state, the title, the number, who opened it and when it last moved), and the issue screen opens it with its body. | |
| 14 | 14 | |
| 15 | 15 | Proof: `test/peek.test.ts` (the card, the one 404, the statement count, the plain lines, and in Chromium the hover, focus, Escape, long press and a list row with none). |
src/web/peek.ts +31 −2
| @@ -30,6 +30,32 @@ import { PEEK } from "./peek_client.ts"; | ||
| 30 | 30 | /** How many lines of a body the card shows, and how many characters at most. */ |
| 31 | 31 | const LINES_SHOWN = 3; |
| 32 | 32 | const BODY_SHOWN = 240; |
| 33 | /** | |
| 34 | * How much of a body is read at all, in characters: three short lines are in the first few, and the | |
| 35 | * route answers strangers, so no body (up to 64 KB) is scanned whole on their asking. | |
| 36 | */ | |
| 37 | export const BODY_READ = 4096; | |
| 38 | ||
| 39 | /** | |
| 40 | * `text` with every `open … close` span taken out, in one pass; an unclosed one runs to the end. | |
| 41 | */ | |
| 42 | function cut(text: string, open: RegExp, close: (opened: string) => RegExp): string { | |
| 43 | let out = ""; | |
| 44 | let at = 0; | |
| 45 | // Both searches run on `text` itself, case-insensitive by their flags: a lowercased copy can be | |
| 46 | // longer (`İ` lowercases to two code units), and its offsets would drift on the original. | |
| 47 | for (;;) { | |
| 48 | open.lastIndex = at; | |
| 49 | const m = open.exec(text); | |
| 50 | if (m === null) return out + text.slice(at); | |
| 51 | out += `${text.slice(at, m.index)} `; | |
| 52 | const shut = close(m[0]); | |
| 53 | shut.lastIndex = m.index + m[0].length; | |
| 54 | const end = shut.exec(text); | |
| 55 | if (end === null) return out; | |
| 56 | at = end.index + end[0].length; | |
| 57 | } | |
| 58 | } | |
| 33 | 59 | |
| 34 | 60 | /** The card's words, in one place. */ |
| 35 | 61 | const PEEK_WORDS = { |
| @@ -52,8 +78,11 @@ const PEEK_WORDS = { | ||
| 52 | 78 | export function plainLines(body: string, lines = LINES_SHOWN, chars = BODY_SHOWN): string { |
| 53 | 79 | const kept: string[] = []; |
| 54 | 80 | let fence: string | null = null; |
| 55 | // A comment, a script or a style says nothing a reader sees: taken out whole, words and all. | |
| 56 | const shown = body.replace(/<!--[\s\S]*?-->/g, "").replace(/<(script|style)\b[\s\S]*?(<\/\1\s*>|$)/gi, " "); | |
| 81 | // A comment, a script or a style says nothing a reader sees: taken out whole, words and all, each | |
| 82 | // in one linear pass over the bounded prefix (a lazy regex over an unclosed `<!--` repeated is | |
| 83 | // quadratic). | |
| 84 | const head = body.slice(0, BODY_READ); | |
| 85 | const shown = cut(cut(head, /<!--/g, () => /-->/g), /<(script|style)\b/gi, (o) => new RegExp(`</${o.slice(1)}>`, "gi")); | |
| 57 | 86 | for (const raw of shown.split(/\r?\n/)) { |
| 58 | 87 | const opens = /^\s*(`{3,}|~{3,})/.exec(raw); |
| 59 | 88 | if (fence !== null) { |
src/web/peek_client.ts +1 −1
| @@ -9,7 +9,7 @@ export const PEEK = /^\/([A-Za-z0-9][A-Za-z0-9_.-]{0,63})\/([A-Za-z0-9][A-Za-z0- | ||
| 9 | 9 | |
| 10 | 10 | /** The card's styles, on every page's sheet. Spacing is the scale. */ |
| 11 | 11 | export const peekCss = /* css */ ` |
| 12 | .peek { position: absolute; width: min(22rem, calc(100vw - 32px)); padding: 12px; border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--card); box-shadow: var(--shadow-2); transform-origin: top left; transition: opacity var(--dur-2) var(--ease), transform var(--dur-2) var(--ease); } | |
| 12 | .peek { position: absolute; width: min(22rem, calc(100vw - 32px)); padding: 12px; border-radius: var(--radius-lg); background: var(--card); box-shadow: var(--shadow-2); transform-origin: top left; transition: opacity var(--dur-2) var(--ease), transform var(--dur-2) var(--ease); } | |
| 13 | 13 | .peek[data-side="above"] { transform-origin: bottom left; } |
| 14 | 14 | .peek[data-warm] { transition: none; } |
| 15 | 15 | @starting-style { .peek { opacity: 0; transform: scale(.95); } } |
test/peek.test.ts +19 −1
| @@ -22,7 +22,7 @@ import { createSession } from "../src/core/sessions.ts"; | ||
| 22 | 22 | import { createUser, type User } from "../src/core/users.ts"; |
| 23 | 23 | import { findChromium } from "../src/looks/capture.ts"; |
| 24 | 24 | import { serve, type Servers } from "../src/http/server.ts"; |
| 25 | import { peekCard, plainLines } from "../src/web/peek.ts"; | |
| 25 | import { BODY_READ, peekCard, plainLines } from "../src/web/peek.ts"; | |
| 26 | 26 | import { chromiumRuns, evaluateLoaded } from "./helpers/chrome.ts"; |
| 27 | 27 | import { quietLogs } from "./helpers/globals.ts"; |
| 28 | 28 | import { promote } from "./helpers/members.ts"; |
| @@ -97,6 +97,24 @@ test("the body preview is plain text from its first lines: no Markdown, no HTML, | ||
| 97 | 97 | expect(plainLines("```\nonly code\n```")).toBe(""); |
| 98 | 98 | }); |
| 99 | 99 | |
| 100 | test("a body is read only so far, and in one pass: 64 KB of unclosed comments or scripts costs what its first 4 KB does", () => { | |
| 101 | for (const unit of ["<!--", "<script>", "<style "]) { | |
| 102 | const evil = unit.repeat(Math.ceil(65_536 / unit.length)); | |
| 103 | const started = performance.now(); | |
| 104 | expect(plainLines(evil)).toBe(""); | |
| 105 | // A hang guard, not a speed claim: the quadratic scan this replaces took minutes here. | |
| 106 | expect(performance.now() - started).toBeLessThan(5_000); | |
| 107 | } | |
| 108 | // Nothing past the bound is ever read: words after 4 KB of blank lines do not show. | |
| 109 | expect(plainLines(`${"\n".repeat(BODY_READ)}late words`)).toBe(""); | |
| 110 | expect(plainLines("a <!-- x --> b\n<SCRIPT>bad</script> c\n<style>s</style>d")).toBe("a b\nc\nd"); | |
| 111 | }); | |
| 112 | ||
| 113 | test("a character that lowercases longer does not shift what is cut: İ before a comment and a SCRIPT", () => { | |
| 114 | // `İ` lowercases to two code units: offsets from a lowercased copy would cut the wrong words. | |
| 115 | expect(plainLines("İİİ keep <!-- x --> this\n<SCRIPT>gone</SCRIPT> and İ <Style>s</STYLE>that")).toBe("İİİ keep this\nand İ that"); | |
| 116 | }); | |
| 117 | ||
| 100 | 118 | test("an issue's card: reference, state, title, who opened it and when, plain lines, and two safe actions for a signed-in reader only", async () => { |
| 101 | 119 | const res = await peek("/alice/harbor/issues/1"); |
| 102 | 120 | expect(res.status).toBe(200); |
new above
offline
lowercan differ in length fromtext('İ'.toLowerCase().lengthis 2), so every index taken fromloweris wrong when applied totextafter such a character. Search the original text case-insensitively instead, and add a test for it.