Public: anyone can read this; the crew can change it.

.mbox and .proto files open as a message list and an outline #13990

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13936-mbox-and-proto-files-open-as-a-message-l → main 6 commits fork 2 threads · 2 unresolved

  • Merged. scout/13936-mbox-and-proto-files-open-as-a-message-l was merged into main as 19780fb.

Review

Sign in to review this change.
12 files changed +530 −1
docs/features/README.md +1
@@ -297,6 +297,7 @@ Read with the guide page [Your apps](../guide-your-apps.md).
297297 - [A compose file opens as its services](file-compose.md)
298298 - [A GitHub Actions workflow opens as its jobs](file-workflow.md)
299299 - [A GraphQL schema opens as its types](file-graphql.md)
300 - [A `.mbox` file opens as its messages and a `.proto` as its outline](file-mbox-proto.md)
300301 - [A JUnit XML report opens as its results](file-junit.md)
301302 - [An .eml file opens as a message](file-eml.md)
302303 - [An lcov coverage file opens as its coverage](file-lcov.md)
docs/features/file-kinds.md +2
@@ -56,6 +56,8 @@ The forge draws a file on its page when its kind is below and it is within the s
5656 - **Compose file**, drawn up to 1 MB: `compose.yaml`, `compose.yml`, `docker-compose.yaml`, `docker-compose.yml`
5757 - **SQL**, drawn up to 1 MB: `.sql`
5858 - **GraphQL**, drawn up to 1 MB: `.graphql`, `.gql`
59 - **Mailbox**, drawn up to 5 MB: `.mbox`
60 - **Protocol Buffers**, drawn up to 1 MB: `.proto`
5961 - **Other text**, drawn up to 1 MB: any other text
6062 - **Other binary**, none: download only: any other bytes
6163
docs/features/file-mbox-proto.md added +9
@@ -0,0 +1,9 @@
1 # A `.mbox` file opens as its messages and a `.proto` file as its outline
2
3 Both open as tables instead of raw text. Source is one tap away (`?view=source`).
4
5 - **`.mbox`:** one row per message with From, Subject and Date (RFC 2047 words decoded), the first 200 shown and the rest counted. Bodies are never read or drawn. A file not starting with `From ` opens as text with one line saying why. Files over 5 MB open as text.
6 - **`.proto`:** each message (nested ones as `Outer.Inner`), enum and service, with fields (label, type, number; `oneof` and `map<>` included), enum values and rpcs (with `stream`). Options, reserved ranges and anything unreadable are skipped. 500 blocks and 500 rows per block, then `more not shown`. Files over 1 MB open as text.
7 - Every value is escaped, drawn on the server; no script, no dependency. The terminal shows the same rows.
8
9 Code: `src/web/render/mbox.ts`, `src/web/render/proto.ts`, `src/web/render/kinds.ts`. Test: `test/render-mbox-proto.test.ts`.
scripts/kinds-guide.ts +1 −1
@@ -18,7 +18,7 @@ export const NAMES: Readonly<Record<FileKind, string>> = {
1818 excalidraw: "Excalidraw", step: "STEP model", sarif: "SARIF report", lcov: "Coverage (lcov)", junit: "JUnit report",
1919 opml: "OPML", feed: "RSS or Atom feed", env: "Env example", openapi: "OpenAPI", package: "package.json", makefile: "Makefile",
2020 dockerfile: "Dockerfile", workflow: "GitHub workflow", compose: "Compose file", sql: "SQL", graphql: "GraphQL",
21 text: "Other text", binary: "Other binary",
21 mbox: "Mailbox", proto: "Protocol Buffers", text: "Other text", binary: "Other binary",
2222 };
2323
2424 /** 1_000_000 as `1 MB`; 4_000_000_000 as `4 GB`; no cap as `none: download only`. */
src/tui/screens/workspace.ts +41
@@ -73,6 +73,8 @@ import { parseOpenApi } from "../../web/render/openapi.ts";
7373 import { parseDockerfile } from "../../web/render/dockerfile.ts";
7474 import { parseWorkflow } from "../../web/render/workflow.ts";
7575 import { parseGraphql } from "../../web/render/graphql.ts";
76 import { parseMbox } from "../../web/render/mbox.ts";
77 import { parseProto } from "../../web/render/proto.ts";
7678 import { parseSql } from "../../web/render/sql.ts";
7779 import { RESULT_CAP, parseSarif } from "../../web/render/sarif.ts";
7880 import { RECORD_CAP, parseLcov, worstFirst, type Ratio } from "../../web/render/lcov.ts";
@@ -689,6 +691,35 @@ function graphqlTable(source: string): Table | string {
689691 return { header: ["Block", "Field", "Arguments", "Type", "Directives"], rows, more: doc.total - doc.blocks.length, delimiter: ",", columns: 5 };
690692 }
691693
694 /**
695 * A mailbox as a table: a row per message (first 200), the rest counted. Shares the web's parser.
696 */
697 function mboxTable(source: string): Table | string {
698 const doc = parseMbox(source);
699 if (doc === null) return "not drawn as a mailbox: this file does not start with a `From ` line";
700 const dash = (v: string): string => (v === "" ? "-" : v);
701 const rows = doc.messages.map((m) => [dash(m.from), dash(m.subject), dash(m.date)]);
702 return { header: ["From", "Subject", "Date"], rows, more: doc.total - doc.messages.length, delimiter: ",", columns: 3 };
703 }
704
705 /** A .proto file as a table: a row per field, enum value or rpc. Shares the web's parser. */
706 function protoTable(source: string): Table | string {
707 const doc = parseProto(source);
708 if (doc === null) return "not drawn as an outline: this file holds no message, enum or service";
709 const rows = doc.blocks.flatMap((b) => {
710 const at = `${b.kind} ${b.name}`;
711 if (b.rows.length === 0) return [[at, "-", "-", "-"]];
712 return b.rows.map((r) =>
713 b.kind === "message"
714 ? [at, r.name, `${r.a === "" ? "" : `${r.a} `}${r.b}`, r.c]
715 : b.kind === "enum"
716 ? [at, r.name, "-", r.b]
717 : [at, r.name, `${r.a} -> ${r.b}`, "-"],
718 );
719 });
720 return { header: ["Block", "Name", "Type", "Number"], rows, more: doc.total - doc.blocks.length, delimiter: ",", columns: 4 };
721 }
722
692723 /** A JUnit report, by the web's own rule (file name, or root element). */
693724 function isJunitPath(path: string, source: Uint8Array): boolean {
694725 return /\.xml$/i.test(path) && fileKind(path, source.subarray(0, 4096)) === "junit";
@@ -1059,6 +1090,16 @@ class Tab {
10591090 this.table = typeof schema === "string" ? null : schema;
10601091 this.fault = typeof schema === "string" ? schema : "";
10611092 if (this.table !== null) this.grid = layoutTable(this.table);
1093 } else if (kindByName(this.path) === "mbox") {
1094 const mail = mboxTable(new TextDecoder().decode(source));
1095 this.table = typeof mail === "string" ? null : mail;
1096 this.fault = typeof mail === "string" ? mail : "";
1097 if (this.table !== null) this.grid = layoutTable(this.table);
1098 } else if (kindByName(this.path) === "proto") {
1099 const outline = protoTable(new TextDecoder().decode(source));
1100 this.table = typeof outline === "string" ? null : outline;
1101 this.fault = typeof outline === "string" ? outline : "";
1102 if (this.table !== null) this.grid = layoutTable(this.table);
10621103 } else if (isJunitPath(this.path, source)) {
10631104 const results = junitTable(new TextDecoder().decode(source));
10641105 this.table = typeof results === "string" ? null : results;
src/web/render/kinds.ts +6
@@ -66,6 +66,8 @@ export type FileKind =
6666 | "compose"
6767 | "sql"
6868 | "graphql"
69 | "mbox"
70 | "proto"
6971 | "text"
7072 | "binary";
7173
@@ -142,6 +144,8 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = {
142144 compose: 1 * MB,
143145 sql: 1 * MB,
144146 graphql: 1 * MB,
147 mbox: 5 * MB,
148 proto: 1 * MB,
145149 text: TEXT_CAP,
146150 binary: 0,
147151 };
@@ -285,6 +289,8 @@ const TEXTUAL: Readonly<Record<string, FileKind>> = {
285289 sql: "sql",
286290 graphql: "graphql",
287291 gql: "graphql",
292 mbox: "mbox",
293 proto: "proto",
288294 };
289295
290296 /** The three example names; a plain `.env` is never one. */
src/web/render/mbox.ts added +76
@@ -0,0 +1,76 @@
1 /**
2 * A `.mbox` file as its messages: one row per message with From, Subject and
3 * Date, the first `MESSAGE_CAP` shown and the rest counted. A message starts
4 * at a line beginning `From ` (the mbox separator); its headers are the lines
5 * up to the first blank line, unfolded, RFC 2047 words decoded. Bodies are
6 * never read or drawn. Pure; never throws; every value is escaped by the
7 * template and clipped.
8 */
9
10 import { html } from "../html.ts";
11 import { fileSize } from "../ui.ts";
12 import { decodeWords } from "./eml-parse.ts";
13 import type { Rendered, RenderInput } from "./renderers.ts";
14
15 export const MESSAGE_CAP = 200;
16 const VALUE_CAP = 300;
17
18 export interface MboxMessage {
19 readonly from: string;
20 readonly subject: string;
21 readonly date: string;
22 }
23
24 export interface MboxDoc {
25 readonly messages: readonly MboxMessage[];
26 readonly total: number;
27 }
28
29 const clip = (s: string): string => (s.length > VALUE_CAP ? `${s.slice(0, VALUE_CAP)}…` : s);
30
31 /** The wanted headers of one message's head lines (up to the blank line). */
32 function headers(lines: readonly string[]): MboxMessage {
33 const found = new Map<string, string>();
34 let name = "";
35 for (const line of lines) {
36 if (line === "") break;
37 if (/^[ \t]/.test(line)) {
38 if (name !== "" && found.has(name)) found.set(name, `${found.get(name)} ${line.trim()}`);
39 continue;
40 }
41 const m = /^([!-9;-~]+):[ \t]*(.*)$/.exec(line);
42 name = m ? m[1]!.toLowerCase() : "";
43 if (m && !found.has(name) && (name === "from" || name === "subject" || name === "date")) found.set(name, m[2]!.trim());
44 else name = "";
45 }
46 const get = (k: string): string => clip(decodeWords(found.get(k) ?? "").replace(/[\u0000-\u001f\u007f]/g, " ").trim());
47 return { from: get("from"), subject: get("subject"), date: get("date") };
48 }
49
50 /** Null when the text holds no `From ` separator at its start. */
51 export function parseMbox(source: string): MboxDoc | null {
52 if (!source.startsWith("From ")) return null;
53 const lines = source.split(/\r?\n/);
54 const messages: MboxMessage[] = [];
55 let total = 0;
56 for (let i = 0; i < lines.length; i++) {
57 if (!lines[i]!.startsWith("From ")) continue;
58 // a separator is only one after a blank line, or at the start
59 if (i > 0 && lines[i - 1] !== "") continue;
60 total++;
61 if (messages.length < MESSAGE_CAP) messages.push(headers(lines.slice(i + 1, i + 201)));
62 }
63 return { messages, total };
64 }
65
66 export function mbox(input: RenderInput): Rendered {
67 const doc = parseMbox(new TextDecoder().decode(input.body));
68 if (doc === null) return { content: html``, lines: 0, fallback: "This file does not start with a `From ` line, so it is not drawn as a mailbox." };
69 const dash = (v: string): string => (v === "" ? "—" : v);
70 const rows = doc.messages.map((m) => html`<tr><td>${dash(m.from)}</td><th scope="row">${dash(m.subject)}</th><td>${dash(m.date)}</td></tr>`);
71 const more = doc.total - doc.messages.length;
72 const foot = more > 0 ? html`<p class="muted small">${more.toLocaleString()} more not shown</p>` : html``;
73 const content = html`<div class="table-scroll"><table class="table" data-dense aria-label="Messages"><thead><tr><th scope="col">From</th><th scope="col">Subject</th><th scope="col">Date</th></tr></thead><tbody>${rows}</tbody></table></div>${foot}`;
74 const n = doc.total;
75 return { content, lines: 0, summary: `${n.toLocaleString()} ${n === 1 ? "message" : "messages"} · ${fileSize(input.size)}` };
76 }
src/web/render/proto.ts added +255
@@ -0,0 +1,255 @@
1 /**

Class proto-view is undocumented in docs/design/areas/; drop it or document it (DESIGN.md: new component classes go in the area doc).

2 * A `.proto` file (Protocol Buffers) as an outline: each message, enum and
3 * service with its fields, values or rpcs. The reader is its own and small:
4 * it drops comments, tokenises, and walks definitions; nested messages are
5 * listed as `Outer.Inner`. What it does not understand (options, extensions,
6 * groups, a malformed body) is skipped, never an error. A file with no
7 * message, enum or service is not drawn: the source shows with the reason.
8 * Every name is escaped by the template; at most `BLOCK_CAP` blocks and
9 * `ROW_CAP` rows per block are drawn.
10 */
11
12 import { html, type Html } from "../html.ts";
13 import { fileSize } from "../ui.ts";
14 import type { Rendered, RenderInput } from "./renderers.ts";
15
16 export const BLOCK_CAP = 500;
17 export const ROW_CAP = 500;
18 const DEPTH_CAP = 8;
19
20 export interface ProtoRow {
21 /** Field: label (`repeated`, `optional`, `oneof x`, ""); rpc: request. */
22 readonly a: string;
23 /** Field: type; enum: number; rpc: response. */
24 readonly b: string;
25 /** Field: name; enum and rpc: name. */
26 readonly name: string;
27 /** Field: number. */
28 readonly c: string;
29 }
30
31 export interface ProtoBlock {
32 readonly kind: "message" | "enum" | "service";
33 readonly name: string;
34 readonly rows: readonly ProtoRow[];
35 readonly more: number;
36 }
37
38 export interface ProtoDoc {
39 readonly blocks: readonly ProtoBlock[];
40 readonly total: number;
41 readonly package: string;
42 readonly syntax: string;
43 }
44
45 /** Comments out; strings and punctuation become tokens. */
46 function tokens(s: string): string[] {
47 const out: string[] = [];
48 const re = /\/\/[^\n]*|\/\*[\s\S]*?(?:\*\/|$)|"(?:[^"\\\n]|\\.)*"?|'(?:[^'\\\n]|\\.)*'?|[A-Za-z_][\w.]*|-?\d[\w.+-]*|[{}()\[\]<>;=,]|\S/g;
49 for (let m = re.exec(s); m !== null; m = re.exec(s)) {
50 const t = m[0];
51 if (t.startsWith("//") || t.startsWith("/*")) continue;
52 out.push(t.replace(/[\u0000-\u001f\u007f-\u009f]/g, "?").slice(0, 200));
53 }
54 return out;
55 }
56
57 const NAME = /^[A-Za-z_.][\w.]*$/;
58
59 export function parseProto(source: string): ProtoDoc | null {
60 const t = tokens(source);
61 const blocks: ProtoBlock[] = [];
62 let total = 0;
63 let pkg = "";
64 let syntax = "";
65 let i = 0;
66
67 const skipStatement = (): void => {
68 let depth = 0;
69 while (i < t.length) {
70 const x = t[i++]!;
71 if (x === "{" || x === "[" || x === "(") depth++;
72 else if (x === "}" || x === "]" || x === ")") {
73 if (depth === 0) {
74 // a closer of the enclosing body ends this statement; a stray bracket is dropped
75 if (x === "}") i--;
76 return;
77 }
78 depth--;
79 if (depth === 0 && x === "}") return;
80 } else if (x === ";" && depth === 0) return;
81 }
82 };
83 const skipBlock = (): void => {
84 // at `{`
85 let depth = 0;
86 while (i < t.length) {
87 const x = t[i++]!;
88 if (x === "{") depth++;
89 else if (x === "}" && --depth === 0) return;
90 }
91 };
92 const emit = (kind: ProtoBlock["kind"], name: string, rows: ProtoRow[]): void => {
93 total++;
94 if (blocks.length < BLOCK_CAP) blocks.push({ kind, name, rows: rows.slice(0, ROW_CAP), more: Math.max(0, rows.length - ROW_CAP) });
95 };
96
97 const message = (name: string, depth: number): void => {
98 // at `{`
99 i++;
100 const rows: ProtoRow[] = [];
101 const nested: Array<() => void> = [];
102 let oneof = "";
103 while (i < t.length && t[i] !== "}") {
104 const w = t[i]!;
105 if (w === ";") {
106 i++;
107 } else if ((w === "message" || w === "enum") && NAME.test(t[i + 1] ?? "") && t[i + 2] === "{") {
108 const n = `${name}.${t[i + 1]}`;
109 i += 2;
110 if (depth >= DEPTH_CAP) skipBlock();
111 else if (w === "message") {
112 const at = i;
113 nested.push(() => {
114 i = at;
115 message(n, depth + 1);
116 });
117 skipBlock();
118 } else enumBody(n);
119 } else if (w === "oneof" && NAME.test(t[i + 1] ?? "") && t[i + 2] === "{") {
120 oneof = t[i + 1]!;
121 i += 3;
122 while (i < t.length && t[i] !== "}") field(rows, `oneof ${oneof}`);
123 i++;
124 oneof = "";
125 } else if (w === "option" || w === "reserved" || w === "extensions" || w === "extend" || w === "group") {
126 skipStatement();
127 } else field(rows, "");
128 }
129 i++;
130 emit("message", name, rows);
131 for (const run of nested) {
132 const save = i;
133 run();
134 i = save;
135 }
136 };
137
138 /** One field statement at `i`; always advances. */
139 const field = (rows: ProtoRow[], label: string): void => {
140 const from = i;
141 let lab = label;
142 if (t[i] === "repeated" || t[i] === "optional" || t[i] === "required") {
143 lab = label === "" ? t[i]! : `${label} ${t[i]}`;
144 i++;
145 }
146 let type = t[i] ?? "";
147 if (type === "map" && t[i + 1] === "<") {
148 const close = t.indexOf(">", i);
149 if (close === -1 || close - i > 6) return void skipStatement();
150 type = `map<${t.slice(i + 2, close).join("")}>`;
151 i = close + 1;
152 } else i++;
153 const name = t[i];
154 if (!NAME.test(type) && !type.startsWith("map<") || name === undefined || !NAME.test(name) || t[i + 1] !== "=") {
155 i = from;
156 return void skipStatement();
157 }
158 const num = t[i + 2] ?? "";
159 i += 3;
160 skipStatement();
161 rows.push({ a: lab, b: type, name, c: num });
162 };
163
164 const enumBody = (name: string): void => {
165 // at `{`
166 i++;
167 const rows: ProtoRow[] = [];
168 while (i < t.length && t[i] !== "}") {
169 if (NAME.test(t[i]!) && t[i] !== "option" && t[i] !== "reserved" && t[i + 1] === "=") {
170 rows.push({ a: "", b: t[i + 2] ?? "", name: t[i]!, c: "" });
171 i += 3;
172 }
173 skipStatement();
174 }
175 i++;
176 emit("enum", name, rows);
177 };
178
179 const service = (name: string): void => {
180 i++;
181 const rows: ProtoRow[] = [];
182 while (i < t.length && t[i] !== "}") {
183 if (t[i] === "rpc" && NAME.test(t[i + 1] ?? "") && t[i + 2] === "(") {
184 const rpc = t[i + 1]!;
185 i += 3;
186 let req = "";
187 if (t[i] === "stream") (req = "stream "), i++;
188 req += t[i] ?? "";
189 i += 2; // name and `)`
190 let res = "";
191 if (t[i] === "returns" && t[i + 1] === "(") {
192 i += 2;
193 if (t[i] === "stream") (res = "stream "), i++;
194 res += t[i] ?? "";
195 i += 2;
196 }
197 rows.push({ a: req, b: res, name: rpc, c: "" });
198 }
199 skipStatement();
200 }
201 i++;
202 emit("service", name, rows);
203 };
204
205 while (i < t.length) {
206 const w = t[i]!;
207 const name = t[i + 1] ?? "";
208 if ((w === "message" || w === "enum" || w === "service") && NAME.test(name) && t[i + 2] === "{") {
209 i += 2;
210 if (w === "message") message(name, 0);
211 else if (w === "enum") enumBody(name);
212 else service(name);
213 } else if (w === "package" && NAME.test(name)) {
214 pkg = name;
215 skipStatement();
216 } else if (w === "syntax" && t[i + 1] === "=") {
217 syntax = (t[i + 2] ?? "").replace(/^["']|["']$/g, "");
218 skipStatement();
219 } else if (w === "}") i++;
220 else if (w === "{") skipBlock();
221 else skipStatement();
222 }
223 if (total === 0) return null;
224 return { blocks, total, package: pkg.slice(0, 200), syntax: syntax.slice(0, 40) };
225 }
226
227 function block(b: ProtoBlock): Html {
228 const head = html`<h3>${b.kind} <code>${b.name}</code></h3>`;
229 const more = b.more > 0 ? html`<p class="muted small">${b.more.toLocaleString()} more not shown</p>` : html``;
230 if (b.rows.length === 0) return head;
231 let cols: Html;
232 let rows: Html[];
233 if (b.kind === "message") {
234 cols = html`<th scope="col">Field</th><th scope="col">Type</th><th scope="col">Number</th>`;
235 rows = b.rows.map((r) => html`<tr><th scope="row"><code>${r.name}</code></th><td><code>${r.a === "" ? "" : `${r.a} `}${r.b}</code></td><td>${r.c}</td></tr>`);
236 } else if (b.kind === "enum") {
237 cols = html`<th scope="col">Value</th><th scope="col">Number</th>`;
238 rows = b.rows.map((r) => html`<tr><th scope="row"><code>${r.name}</code></th><td>${r.b}</td></tr>`);
239 } else {
240 cols = html`<th scope="col">Rpc</th><th scope="col">Request</th><th scope="col">Response</th>`;
241 rows = b.rows.map((r) => html`<tr><th scope="row"><code>${r.name}</code></th><td><code>${r.a}</code></td><td><code>${r.b}</code></td></tr>`);
242 }
243 return html`${head}<div class="table-scroll"><table class="table" data-dense aria-label="${b.name}"><thead><tr>${cols}</tr></thead><tbody>${rows}</tbody></table></div>${more}`;
244 }
245
246 export function proto(input: RenderInput): Rendered {
247 const doc = parseProto(new TextDecoder().decode(input.body));
248 if (doc === null) return { content: html``, lines: 0, fallback: "This file holds no message, enum or service to draw." };
249 const meta = doc.package === "" ? html`` : html`<p class="muted small">package <code>${doc.package}</code></p>`;
250 const more = doc.total - doc.blocks.length;
251 const foot = more > 0 ? html`<p class="muted small">${more.toLocaleString()} more not shown</p>` : html``;
252 const content = html`<div>${meta}${doc.blocks.map(block)}${foot}</div>`;
253 const n = doc.total;
254 return { content, lines: 0, summary: `${n.toLocaleString()} ${n === 1 ? "definition" : "definitions"} · ${fileSize(input.size)}` };
255 }
src/web/render/renderers.ts +4
@@ -13,6 +13,8 @@ import { isFormConfig } from "./config-format.ts";
1313 import { envView } from "./env.ts";
1414 import { sqlView } from "./sql.ts";
1515 import { graphql } from "./graphql.ts";
16 import { mbox } from "./mbox.ts";
17 import { proto } from "./proto.ts";
1618 import { eml } from "./eml.ts";
1719 import { csv } from "./csv.ts";
1820 import { geojson } from "./geojson.ts";
@@ -153,6 +155,8 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = {
153155 env: envView,
154156 sql: sqlView,
155157 graphql,
158 mbox,
159 proto,
156160 openapi,
157161 package: packageJson,
158162 makefile,
test/render-kinds.test.ts +2
@@ -102,6 +102,8 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = {
102102 sql: [enc("CREATE TABLE a (id int);\n"), "sql", "application/octet-stream"],
103103 graphql: [enc("type A { b: Int }\n"), "graphql", "application/octet-stream"],
104104 gql: [enc("type A { b: Int }\n"), "graphql", "application/octet-stream"],
105 mbox: [enc("From [email protected] Mon Jan 1 00:00:00 2024\nSubject: x\n\nhi\n"), "mbox", "application/octet-stream"],
106 proto: [enc('syntax = "proto3";\nmessage A {}\n'), "proto", "application/octet-stream"],
105107 sarif: [enc('{"runs":[]}'), "sarif", "application/octet-stream"],
106108 opml: [enc("<opml/>"), "opml", "application/octet-stream"],
107109 rss: [enc("<rss/>"), "feed", "application/octet-stream"],
test/render-mbox-proto.test.ts added +112
@@ -0,0 +1,112 @@
1 /**

Proto hostile tests ("hostile names and strings are escaped", "hostile type is escaped") pass vacuously: they only assert the raw tag is absent. Assert the escaped text is present, using inputs the tokenizer keeps.

2 * .mbox as a message list and .proto as an outline: shape, caps, hostile text escaped, the
3 * registry.
4 */
5 import { describe, expect, test } from "bun:test";
6 import { MESSAGE_CAP, mbox, parseMbox } from "../src/web/render/mbox.ts";
7 import { parseProto, proto } from "../src/web/render/proto.ts";
8 import { kindByName, RENDER_CAP } from "../src/web/render/kinds.ts";
9 import { RENDERERS } from "../src/web/render/renderers.ts";
10
11 const enc = (s: string): Uint8Array => new TextEncoder().encode(s);
12 const input = (path: string, text: string) => ({ path, size: text.length, body: enc(text), media: "/m", download: "/d" });
13 const BAD = `<script>alert(1)</script>`;
14
15 describe("mbox", () => {
16 test("registered", () => {
17 expect(kindByName("a/b.mbox")).toBe("mbox");
18 expect(RENDERERS.mbox).toBe(mbox);
19 expect(RENDER_CAP.mbox).toBe(5_000_000);
20 });
21
22 test("lists from, subject, date, decoding folded and encoded headers", () => {
23 const doc = parseMbox(
24 "From [email protected] Mon Jan 1 00:00:00 2024\nFrom: Ann <[email protected]>\nSubject: =?UTF-8?B?aGVsbG8=?=\n world\nDate: Mon, 1 Jan 2024\n\n>From body line\n\nFrom [email protected] Tue\nFrom: Bob\nSubject: two\n\nx\n",
25 )!;
26 expect(doc.total).toBe(2);
27 expect(doc.messages[0]).toEqual({ from: "Ann <[email protected]>", subject: "hello world", date: "Mon, 1 Jan 2024" });
28 expect(doc.messages[1]!.subject).toBe("two");
29 });
30
31 test("not an mbox: fallback", () => {
32 expect(mbox(input("a.mbox", "hello")).fallback).toBeDefined();
33 });
34
35 test("first 200 shown, the rest counted", () => {
36 const text = Array.from({ length: MESSAGE_CAP + 5 }, (_, n) => `From x Mon\nSubject: s${n}\n\nbody\n\n`).join("");
37 const r = mbox(input("a.mbox", text));
38 expect(String(r.content)).toContain("5 more not shown");
39 expect(String(r.content)).toContain("s199<");
40 expect(String(r.content)).not.toContain("s200<");
41 });
42
43 test("hostile headers are escaped", () => {
44 const r = mbox(input("a.mbox", `From x\nFrom: ${BAD}\nSubject: ${BAD}\nDate: "><img src=x onerror=1>\n\n`));
45 const out = String(r.content);
46 expect(out).not.toContain("<script>");
47 expect(out).not.toContain("<img");
48 expect(out).toContain("&lt;script&gt;");
49 });
50 });
51
52 const PROTO = `// c
53 syntax = "proto3";
54 package demo.v1;
55 import "other.proto";
56 message User {
57 string name = 1; // trailing
58 repeated int32 ids = 2 [packed = true];
59 map<string, Role> roles = 3;
60 oneof pick { string a = 4; int32 b = 5; }
61 reserved 9, 10;
62 message Inner { bool ok = 1; }
63 enum Kind { K_A = 0; K_B = 1; }
64 }
65 enum Role { OTHER = 0; ADMIN = 1 [deprecated = true]; }
66 service Users {
67 rpc Get (GetReq) returns (User);
68 rpc Watch (stream GetReq) returns (stream User) { option deprecated = true; }
69 }
70 `;
71
72 describe("proto", () => {
73 test("registered", () => {
74 expect(kindByName("x.proto")).toBe("proto");
75 expect(RENDERERS.proto).toBe(proto);
76 });
77
78 test("messages, fields, enums, services", () => {
79 const doc = parseProto(PROTO)!;
80 expect(doc.package).toBe("demo.v1");
81 expect(doc.syntax).toBe("proto3");
82 expect(doc.blocks.map((b) => `${b.kind} ${b.name}`)).toEqual(["enum User.Kind", "message User", "message User.Inner", "enum Role", "service Users"]);
83 const user = doc.blocks[1]!;
84 expect(user.rows.map((r) => [r.a, r.b, r.name, r.c])).toEqual([
85 ["", "string", "name", "1"],
86 ["repeated", "int32", "ids", "2"],
87 ["", "map<string,Role>", "roles", "3"],
88 ["oneof pick", "string", "a", "4"],
89 ["oneof pick", "int32", "b", "5"],
90 ]);
91 expect(doc.blocks[3]!.rows.map((r) => [r.name, r.b])).toEqual([["OTHER", "0"], ["ADMIN", "1"]]);
92 expect(doc.blocks[4]!.rows.map((r) => [r.name, r.a, r.b])).toEqual([["Get", "GetReq", "User"], ["Watch", "stream GetReq", "stream User"]]);
93 });
94
95 test("no definition: fallback; garbage never throws", () => {
96 expect(proto(input("a.proto", 'syntax = "proto3";')).fallback).toBeDefined();
97 expect(() => parseProto("message { { } } enum A { service B {{{ rpc (")).not.toThrow();
98 });
99
100 test("hostile strings the tokenizer keeps are escaped, hostile names are not drawn", () => {
101 const src = [
102 'message A { string f = "<b>n</b>"; string <script> = 2; }',
103 'enum E { V = "<i>v</i>"; }',
104 'service S { rpc M ("<u>q</u>") returns ("<s>r</s>"); }',
105 'message <img> {}',
106 ].join("\n");
107 const out = String(proto(input("a.proto", src)).content);
108 for (const tag of ["<b>", "<i>", "<u>", "<s>", "<script>", "<img"]) expect(out).not.toContain(tag);
109 for (const kept of ["&lt;b&gt;n&lt;/b&gt;", "&lt;i&gt;v&lt;/i&gt;", "&lt;u&gt;q&lt;/u&gt;", "&lt;s&gt;r&lt;/s&gt;"]) expect(out).toContain(kept);
110 expect(parseProto(src)!.blocks.map((x) => x.name)).toEqual(["A", "E", "S"]);
111 });
112 });
test/tui-workspace-formats.test.ts +21
@@ -588,6 +588,27 @@ describe("the terminal workspace, by file type", () => {
588588 }
589589 });
590590
591 test("a .mbox opens as its messages and a .proto as its outline", async () => {
592 const made = await createRepo(forge, alice, { name: "mboxrepo", visibility: "public", description: "m" });
593 if (!made.ok) throw new Error("repo failed");
594 const mb = "From [email protected] Mon Jan 1 00:00:00 2024\nFrom: Ann <[email protected]>\nSubject: Hello there\nDate: Mon, 1 Jan 2024\n\nbody\n";
595 const pr = 'syntax = "proto3";\nmessage Widget { string label = 1; }\n';
596 await commitToBranch(forge, alice, made.value, { message: "seed", files: [{ path: "a.mbox", content: mb }, { path: "w.proto", content: pr }] });
597 const h = boot();
598 try {
599 await h.settle();
600 await open(h, "mboxrepo");
601 await openFile(h, "a.mbox");
602 expect(h.text()).toContain("Hello there");
603 await h.press("escape");
604 await openFile(h, "w.proto");
605 expect(h.text()).toContain("Widget");
606 expect(h.text()).toContain("label");
607 } finally {
608 h.stop();
609 }
610 });
611
591612 test("a JUnit report opens as its totals and failing cases, sharing the web parser", async () => {
592613 const made = await createRepo(forge, alice, { name: "junitrepo", visibility: "public", description: "tests" });
593614 if (!made.ok) throw new Error("repo failed");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs