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

An .xlsx file opens as a table: its sheet names and the first 200 rows of the first sheet #12714

Merged opened this

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

Merged

mason/forge:mason/12688-an-xlsx-file-opens-as-a-table-its-sheet → main 6 commits fork 1 thread · 1 unresolved

  • Merged. mason/12688-an-xlsx-file-opens-as-a-table-its-sheet was merged into main as 29e1791.

Review

Sign in to review this change.
12 files changed +578 −2
docs/features/README.md +1
@@ -244,6 +244,7 @@ Read with the guide page [Your apps](../guide-your-apps.md).
244244 - [Notebooks, JSON Lines and logs in the terminal](terminal-workspace-kinds.md)
245245 - [Shift a subtitle file](subtitle-shift.md)
246246 - [SQLite files as tables](sqlite-view.md)
247 - [Excel workbooks (.xlsx) as a table](xlsx.md)
247248 - [Subtitles as a transcript](subtitles.md)
248249 - [Terminal logs in colour](ansi-log.md)
249250
docs/features/xlsx.md added +11
@@ -0,0 +1,11 @@
1 # Spreadsheets (.xlsx)
2
3 A `.xlsx` file in a repository opens as a table, on the page and in the terminal, instead of a download link.
4
5 - **What opens:** a file named `.xlsx` whose bytes are a zip of an Office workbook. It shows the sheet names, then the first rows of the first sheet. The sheet list is not tabs: only the first sheet is ever shown, and the page says so.
6 - **Limits:** the first 200 rows and 30 columns (A to AD) of that sheet. When either is cut, the page says so and the raw file has the rest. A workbook part that inflates past 16 MB, and any file over the spreadsheet size cap, is not read.
7 - **What a cell shows:** its text. A formula shows the value the file cached and is never evaluated. Numbers are right-aligned on the page; booleans read TRUE or FALSE. Dates show the number the file stores (a serial day), not a date: the reader does not interpret number formats. A cell beginning `=`, `+`, `-` or `@` is plain text.
8 - **Only the first sheet:** other sheets are named, not drawn.
9 - **Fallback:** a file that is damaged, encrypted, truncated, zip64, or not a workbook keeps the page's source view, and the terminal's note, with a sentence saying why. Nothing is written or unpacked.
10 - **Web:** `src/web/render/xlsx.ts`; the page's table is a hairline table with no box around it.
11 - **Terminal:** open the file in a repository's workspace (`src/tui/screens/workspace_sheet.ts`). It draws the first sheet as a table with column letters A, B, C… above, the sheet names in the first rows; `j`/`k` scroll, `/` searches. It is read only: `e` says the file is not editable here.
src/tui/screens/workspace.ts +12 −1
@@ -76,6 +76,7 @@ import { parseEnv, ROW_CAP as ENV_ROW_CAP } from "../../web/render/env.ts";
7676 import { CARD_CAP, parseVcf } from "../../web/render/vcf-parse.ts";
7777 import { CUE_CAP, parseSubtitles } from "../../web/render/subtitles.ts";
7878 import { isJsonlPath, kindView, type KindLines } from "./workspace_kinds.ts";
79 import { sheetView } from "./workspace_sheet.ts";
7980 import { layoutTable, type Grid } from "../ui/grid.ts";
8081 import type { Ctx, Screen } from "../app/app.ts";
8182 import { stripAnsi } from "../term/ansi.ts";
@@ -719,6 +720,8 @@ class Tab {
719720 /** The parsed file, when its name says table. Parse errors ride along. */
720721 table: Table | null = null;
721722 grid: Grid = { header: "", rows: [] };
723 /** An .xlsx's first sheet: the bytes are not text, so the table is all there is. */
724 sheet: Table | null = null;
722725 /** Cues of a subtitle file that could not be read. */
723726 unread = 0;
724727 /** Why a file whose name promises a view is shown as source instead; "" when nothing failed. */
@@ -812,6 +815,8 @@ class Tab {
812815 if (source === null) {
813816 this.plain = [];
814817 this.lines = [];
818 this.table = this.sheet;
819 if (this.sheet !== null) this.grid = layoutTable(this.sheet);
815820 return;
816821 }
817822 const text = new TextDecoder().decode(source);
@@ -952,6 +957,7 @@ interface CacheEntry {
952957 readonly noteDetail: string;
953958 readonly raw: Uint8Array | null;
954959 readonly picture: Uint8Array | null;
960 readonly sheet: Table | null;
955961 }
956962
957963 // ---- what a workspace remembers between visits ----
@@ -1238,6 +1244,7 @@ export function workspaceScreen(
12381244 };
12391245 let raw: Uint8Array | null = null;
12401246 let picture: Uint8Array | null = null;
1247 let sheet: Table | null = null;
12411248 if (!read.ok)
12421249 note =
12431250 read.error === "not_found"
@@ -1259,14 +1266,17 @@ export function workspaceScreen(
12591266 // A binary blob whose *name* says picture is the one binary we do
12601267 // something with. Everything else keeps the honest note: showing
12611268 // bytes as text is how a terminal ends up beeping at you.
1269 const book = looksLikeImage(t.path) ? null : sheetView(t.path, read.value.body);
12621270 if (looksLikeImage(t.path)) picture = read.value.body;
1271 else if (book !== null && typeof book !== "string") sheet = book;
1272 else if (typeof book === "string") note = book;
12631273 else
12641274 note =
12651275 archiveNote(t.path, read.value.body, read.value.size, formatSize) ??
12661276 `Binary file, ${formatSize(read.value.size)}.`;
12671277 } else raw = read.value.body; // .html included: source here, the page preview is web-only
12681278 }
1269 entry = { note, noteTitle, noteDetail, raw, picture };
1279 entry = { note, noteTitle, noteDetail, raw, picture, sheet };
12701280 remember(key, entry);
12711281 }
12721282 if (stale(mine)) return;
@@ -1275,6 +1285,7 @@ export function workspaceScreen(
12751285 t.noteTitle = entry.noteTitle;
12761286 t.noteDetail = entry.noteDetail;
12771287 t.raw = entry.raw;
1288 t.sheet = entry.sheet;
12781289 t.image =
12791290 entry.picture === null
12801291 ? null
src/tui/screens/workspace_sheet.ts added +38
@@ -0,0 +1,38 @@
1 /**
2 * An .xlsx in the terminal workspace, read the way the page reads it: the same
3 * `readWorkbook`, drawn as a table of plain text. Rows 1 to 200 and columns A
4 * to AD of the first sheet, with the sheet names on top, one a row; only the first sheet
5 * is shown, and nothing is evaluated.
6 *
7 * `sheetView(path, body)` answers a table, or a sentence saying why not (then
8 * the workspace shows that as its note). `null` means "not a spreadsheet by
9 * name and bytes": the workspace keeps its binary note.
10 */
11
12 import { fileKind, HEAD_BYTES } from "../../web/render/kinds.ts";
13 import { readWorkbook, COLUMNS } from "../../web/render/xlsx.ts";
14 import type { Table } from "../../text/csv.ts";
15
16 /** Column letters, zero-based: 0 is A, 26 is AA. */
17 export function columnName(i: number): string {
18 let n = i + 1;
19 let out = "";
20 while (n > 0) {
21 out = String.fromCharCode(65 + ((n - 1) % 26)) + out;
22 n = Math.floor((n - 1) / 26);
23 }
24 return out;
25 }
26
27 export function sheetView(path: string, body: Uint8Array): Table | string | null {
28 if (fileKind(path, body.subarray(0, HEAD_BYTES)) !== "sheet") return null;
29 const book = readWorkbook(body);
30 if ("error" in book) return book.error;
31 const width = Math.min(COLUMNS, Math.max(1, ...book.rows.map((r) => r.length)));
32 const header = Array.from({ length: width }, (_, i) => columnName(i));
33 const pad = (cells: readonly string[]): string[] => [...cells, ...Array<string>(width - cells.length).fill("")];
34 const names = book.sheets.map((n, i) => pad([`Sheet ${i + 1}${i === 0 ? " (shown)" : ""}: ${n}`]));
35 const cut = book.cut ? [pad(["Cut at 200 rows and 30 columns"])] : [];
36 const rows = [...names, ...cut, pad([]), ...book.rows.map((r) => pad(r.map((c) => c.text)))];
37 return { header, rows, more: 0, delimiter: ",", columns: width };
38 }
src/web/render/archive.ts +52 −1
@@ -9,7 +9,7 @@
99 * pax `x` (§ extended header) and GNU `L` long-name records.
1010 */
1111
12 import { gunzipSync } from "node:zlib";
12 import { gunzipSync, inflateRawSync } from "node:zlib";
1313
1414 /** Entries kept for display, whatever the archive holds. */
1515 export const ENTRY_CAP = 2_000;
@@ -279,3 +279,54 @@ export function listArchive(
279279 throw e;
280280 }
281281 }
282
283 // ---- one entry ---------------------------------------------------------------
284
285 const LOCAL = 0x04034b50;
286
287 /**
288 * The inflated bytes of one named entry of a zip, or null when the zip has
289 * no such entry, is zip64, encrypted or damaged, or the entry's declared
290 * size or its actual inflation passes `cap` (a zip bomb). Nothing is written.
291 */
292 export function zipEntry(bytes: Uint8Array, name: string, cap: number): Uint8Array | null {
293 try {
294 const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
295 let eocd = -1;
296 for (let i = bytes.length - 22; i >= Math.max(0, bytes.length - 22 - 0xffff); i--) {
297 if (v.getUint32(i, true) === EOCD) {
298 eocd = i;
299 break;
300 }
301 }
302 if (eocd < 0) return null;
303 const total = v.getUint16(eocd + 10, true);
304 let p = v.getUint32(eocd + 16, true);
305 if (total === 0xffff || p === 0xffffffff || p > eocd) return null;
306 for (let n = 0; n < total; n++) {
307 if (p + 46 > bytes.length || v.getUint32(p, true) !== CENTRAL) return null;
308 const nameLen = v.getUint16(p + 28, true);
309 const next = p + 46 + nameLen + v.getUint16(p + 30, true) + v.getUint16(p + 32, true);
310 if (next > bytes.length) return null;
311 if (UTF8.decode(bytes.subarray(p + 46, p + 46 + nameLen)) !== name) {
312 p = next;
313 continue;
314 }
315 const flags = v.getUint16(p + 8, true);
316 const method = v.getUint16(p + 10, true);
317 const packed = v.getUint32(p + 20, true);
318 const size = v.getUint32(p + 24, true);
319 const at = v.getUint32(p + 42, true);
320 if (flags & 1 || (method !== 0 && method !== 8) || size === 0xffffffff || size > cap) return null;
321 if (at + 30 > bytes.length || v.getUint32(at, true) !== LOCAL) return null;
322 const start = at + 30 + v.getUint16(at + 26, true) + v.getUint16(at + 28, true);
323 if (start + packed > bytes.length) return null;
324 const data = bytes.subarray(start, start + packed);
325 if (method === 0) return data.length > cap ? null : data;
326 return inflateRawSync(data, { maxOutputLength: Math.max(1, cap) });
327 }
328 return null;
329 } catch {
330 return null;
331 }
332 }
src/web/render/kinds.ts +22
@@ -24,6 +24,7 @@ export type FileKind =
2424 | "sqlite"
2525 | "model3d"
2626 | "archive"
27 | "sheet"
2728 | "font"
2829 | "config"
2930 | "markdown"
@@ -73,6 +74,7 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = {
7374 sqlite: 5 * MB,
7475 model3d: 100 * MB,
7576 archive: 50 * MB,
77 sheet: 10 * MB,
7678 font: 20 * MB,
7779 config: 1 * MB,
7880 markdown: 1 * MB,
@@ -272,6 +274,23 @@ const isArchive = (path: string, head: Uint8Array): boolean => {
272274 return ARCHIVES[ext]?.(head) === true;
273275 };
274276
277 /**
278 * A spreadsheet: `.xlsx`, a zip whose first entries name `xl/workbook.xml`.
279 * Its media type is never a spreadsheet type; the media route sends octet-stream.
280 */
281 const isSheet = (path: string, head: Uint8Array): boolean =>
282 extension(path) === "xlsx" &&
283 bytes(0x50, 0x4b, 0x03, 0x04)(head) &&
284 isWorkbookHead(new TextDecoder("latin1").decode(head));
285
286 /**
287 * The head is 4 KB, so a workbook whose first parts are large never shows
288 * `xl/workbook.xml` there; every Office package opens on `[Content_Types].xml`,
289 * and the reader refuses what turns out not to be a workbook.
290 */
291 const isWorkbookHead = (head: string): boolean =>
292 head.includes("xl/workbook.xml") || head.slice(30, 49) === "[Content_Types].xml";
293
275294 /** An ASCII DXF opens on group code 0, then `SECTION`, whatever the file is named. */
276295 const isDxfHead = (head: Uint8Array): boolean =>
277296 /^\uFEFF?\s*0\r?\nSECTION\r?\n/.test(new TextDecoder().decode(head.subarray(0, 64)));
@@ -321,6 +340,7 @@ function confirmed(path: string, head: Uint8Array): Media | undefined {
321340 export function fileKind(path: string, head: Uint8Array): FileKind {
322341 const media = confirmed(path, head);
323342 if (media !== undefined) return media.kind;
343 if (isSheet(path, head)) return "sheet";
324344 if (isArchive(path, head)) return "archive";
325345 if (looksBinary(head)) return "binary";
326346 if (isLcovName(path)) return "lcov";
@@ -353,6 +373,7 @@ export function kindByName(path: string): FileKind {
353373 if (isDockerfileName(path)) return "dockerfile";
354374 if (isWorkflowName(path)) return "workflow";
355375 if (isComposeName(path)) return "compose";
376 if (ext === "xlsx") return "sheet";
356377 return MEDIA[ext]?.kind ?? TEXTUAL[ext] ?? "text";
357378 }
358379
@@ -383,4 +404,5 @@ export const KNOWN_EXTENSIONS: readonly string[] = [
383404 "zip",
384405 "tar",
385406 "tgz",
407 "xlsx",
386408 ];
src/web/render/renderers.ts +2
@@ -41,6 +41,7 @@ import { compose } from "./compose.ts";
4141 import { openapi } from "./openapi.ts";
4242 import { lcov } from "./lcov.ts";
4343 import { sarif } from "./sarif.ts";
44 import { sheet } from "./xlsx.ts";
4445 import { sqlite } from "./sqlite.ts";
4546 import { subtitles } from "./subtitles.ts";
4647 import { video } from "./video.ts";
@@ -123,6 +124,7 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = {
123124 compose,
124125 mail: eml,
125126 archive,
127 sheet,
126128 font,
127129 };
128130
src/web/render/xlsx.ts added +178
@@ -0,0 +1,178 @@
1 /**

Principles 6 and 7: the output is a card csv-view box around the table. Use rows and a hairline instead. Also add docs/features/xlsx.md and a src/tui/ screen, since this module has no terminal door.

2 * An .xlsx workbook as a table: its sheet names and the first rows of the
3 * first sheet, read from the zip with `zipEntry` and a small reader of the
4 * three XML parts that matter. Formulas show the value the file cached and
5 * are never evaluated; every cell is plain text, escaped on the way out.
6 * Anything missing, hostile or too large is `{ error }`, and the page keeps
7 * its source view.
8 */
9
10 import { escapeHtml, html, raw } from "../html.ts";
11 import { fileSize } from "../ui.ts";
12 import { zipEntry } from "./archive.ts";
13 import type { Rendered, RenderInput } from "./renderers.ts";
14
15 export const ROWS = 200;
16 export const COLUMNS = 30;
17 export const STRING_CAP = 20_000;
18 /** The most any one part may inflate to. */
19 export const PART_CAP = 16 * 1024 * 1024;
20
21 export interface Workbook {
22 readonly sheets: readonly string[];
23 /** The first sheet, up to `ROWS` rows of `COLUMNS` cells; numbers are marked. */
24 readonly rows: readonly (readonly Cell[])[];
25 /** Whether the first sheet holds more rows or columns than are kept. */
26 readonly cut: boolean;
27 }
28
29 export interface Cell {
30 readonly text: string;
31 readonly number: boolean;
32 }
33
34 const UTF8 = new TextDecoder();
35 const ENTITY: Record<string, string> = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };
36
37 /** XML character data as text; an unknown entity stays as written. */
38 const unescape = (s: string): string =>
39 s.replace(/&(?:#(\d{1,7})|#x([0-9a-fA-F]{1,6})|(amp|lt|gt|quot|apos));/g, (m, d, x, n) => {
40 if (n) return ENTITY[n]!;
41 const code = d ? Number(d) : parseInt(x, 16);
42 return code > 0 && code <= 0x10ffff && !(code >= 0xd800 && code < 0xe000) ? String.fromCodePoint(code) : m;
43 });
44
45 const attr = (tag: string, name: string): string | undefined => {
46 const m = new RegExp(`(?:^|\\s)${name}="([^"]*)"`).exec(tag);
47 return m === null ? undefined : unescape(m[1]!);
48 };
49
50 /** The text of every `<t>` in a fragment, joined. */
51 const texts = (xml: string): string => {
52 let out = "";
53 for (const m of xml.matchAll(/<t\b[^>]*?(?:\/>|>([\s\S]*?)<\/t>)/g)) out += unescape(m[1] ?? "");
54 return out;
55 };
56
57 const part = (zip: Uint8Array, name: string): string | null => {
58 const bytes = zipEntry(zip, name, PART_CAP);
59 return bytes === null ? null : UTF8.decode(bytes);
60 };
61
62 /** `B` is 1, `AA` is 26 (zero-based here). */
63 function column(ref: string): number {
64 let n = 0;
65 for (const ch of ref) n = n * 26 + (ch.charCodeAt(0) - 64);
66 return n - 1;
67 }
68
69 function sharedStrings(xml: string | null): string[] {
70 if (xml === null) return [];
71 const out: string[] = [];
72 for (const m of xml.matchAll(/<si\b[^>]*?(?:\/>|>([\s\S]*?)<\/si>)/g)) {
73 out.push(texts((m[1] ?? "").replace(/<rPh\b[\s\S]*?<\/rPh>/g, "")));
74 if (out.length >= STRING_CAP) break;
75 }
76 return out;
77 }
78
79 /** The workbook's sheet names and its first sheet's cells, or why not. */
80 export function readWorkbook(zip: Uint8Array): Workbook | { readonly error: string } {
81 const bad = { error: "This workbook is damaged or not a spreadsheet this page can read." };
82 const book = part(zip, "xl/workbook.xml");
83 if (book === null) return bad;
84 const sheets: { name: string; id: string | undefined }[] = [];
85 for (const m of book.matchAll(/<sheet\b([^>]*?)\/?>/g)) {
86 const name = attr(m[1]!, "name");
87 if (name !== undefined) sheets.push({ name, id: attr(m[1]!, "r:id") });
88 }
89 if (sheets.length === 0) return bad;
90 const rels = part(zip, "xl/_rels/workbook.xml.rels");
91 if (rels === null || sheets[0]!.id === undefined) return bad;
92 let target: string | undefined;
93 for (const m of rels.matchAll(/<Relationship\b([^>]*?)\/?>/g))
94 if (attr(m[1]!, "Id") === sheets[0]!.id) target = attr(m[1]!, "Target");
95 if (target === undefined) return bad;
96 const path = target.startsWith("/") ? target.slice(1) : `xl/${target}`;
97 if (path.includes("..")) return bad;
98 const sheet = part(zip, path);
99 if (sheet === null) return bad;
100 const strings = sharedStrings(part(zip, "xl/sharedStrings.xml"));
101
102 const rows: Cell[][] = [];
103 let cut = false;
104 let nextRow = 0;
105 for (const r of sheet.matchAll(/<row\b([^>]*?)(?:\/>|>([\s\S]*?)<\/row>)/g)) {
106 const given = Number(attr(r[1]!, "r"));
107 const index = Number.isInteger(given) && given > 0 ? given - 1 : nextRow;
108 nextRow = index + 1;
109 if (index >= ROWS) {
110 cut = true;
111 break;
112 }
113 const cells: Cell[] = [];
114 let following = 0;
115 for (const c of (r[2] ?? "").matchAll(/<c\b([^>]*?)(?:\/>|>([\s\S]*?)<\/c>)/g)) {
116 const ref = /^([A-Za-z]{1,3})\d*$/.exec(attr(c[1]!, "r") ?? "");
117 const col = ref === null ? following : column(ref[1]!.toUpperCase());
118 following = col + 1;
119 if (col >= COLUMNS) {
120 cut = true;
121 continue;
122 }
123 const body = c[2] ?? "";
124 const type = attr(c[1]!, "t") ?? "n";
125 const v = /<v\b[^>]*>([\s\S]*?)<\/v>/.exec(body);
126 let text = "";
127 let number = false;
128 if (type === "inlineStr") text = texts(body);
129 else if (v !== null) {
130 const value = unescape(v[1]!);
131 if (type === "s") text = strings[Number(value)] ?? "";
132 else if (type === "b") text = value === "1" ? "TRUE" : "FALSE";
133 else {
134 text = value;
135 number = type === "n" && value.trim() !== "" && Number.isFinite(Number(value));
136 }
137 }
138 while (cells.length < col) cells.push({ text: "", number: false });
139 cells[col] = { text, number };
140 }
141 while (rows.length < index) rows.push([]);
142 rows[index] = cells;
143 }
144 return { sheets: sheets.map((s) => s.name), rows, cut };
145 }
146
147 export function sheet(input: RenderInput): Rendered {
148 const book = readWorkbook(input.body);
149 if ("error" in book) return { content: html``, lines: 0, fallback: book.error };
150 const width = Math.max(1, ...book.rows.map((r) => r.length));
151 let body = "";
152 for (const row of book.rows) {
153 body += "<tr>";
154 for (let c = 0; c < width; c++) {
155 const cell = row[c];
156 body += `<td${cell?.number ? ' class="num"' : ""}>${escapeHtml(cell?.text ?? "")}</td>`;
157 }
158 body += "</tr>";
159 }
160 const names = book.sheets
161 .map((n, i) => `<li${i === 0 ? ' aria-current="true"' : ""}>${escapeHtml(n)}</li>`)
162 .join("");
163 const more = book.cut ? ` The first ${ROWS} rows and ${COLUMNS} columns are shown; the rest is in the raw file.` : "";
164 const content = html`<div class="csv-view">
165 <p class="muted small">Only the first sheet is shown.${more}</p>
166 <ul class="muted small" aria-label="Sheets in this workbook">${raw(names)}</ul>
167 <div class="csv-scroll">
168 <table class="table" data-dense aria-label="${input.path.slice(input.path.lastIndexOf("/") + 1)} first sheet">
169 <tbody>${raw(body)}</tbody>
170 </table>
171 </div>
172 </div>`;
173 return {
174 content,
175 lines: 0,
176 summary: `${book.sheets.length.toLocaleString()} ${book.sheets.length === 1 ? "sheet" : "sheets"} · ${book.rows.length.toLocaleString()} rows × ${width.toLocaleString()} columns · ${fileSize(input.size)}`,
177 };
178 }
test/helpers/zip.ts added +45
@@ -0,0 +1,45 @@
1 import { deflateRawSync } from "node:zlib";
2
3 const enc = (s: string): Uint8Array => new TextEncoder().encode(s);
4
5 /** A zip of named parts, deflated; `claim` overrides the declared size of every entry. */
6 export function zip(parts: Record<string, string | Uint8Array>, claim?: number): Uint8Array {
7 const chunks: Uint8Array[] = [];
8 const central: Uint8Array[] = [];
9 let offset = 0;
10 for (const [name, data] of Object.entries(parts)) {
11 const raw = typeof data === "string" ? enc(data) : data;
12 const packed = deflateRawSync(raw);
13 const n = enc(name);
14 const local = new Uint8Array(30 + n.length);
15 const lv = new DataView(local.buffer);
16 lv.setUint32(0, 0x04034b50, true);
17 lv.setUint16(8, 8, true);
18 lv.setUint32(18, packed.length, true);
19 lv.setUint32(22, claim ?? raw.length, true);
20 lv.setUint16(26, n.length, true);
21 local.set(n, 30);
22 const head = new Uint8Array(46 + n.length);
23 const cv = new DataView(head.buffer);
24 cv.setUint32(0, 0x02014b50, true);
25 cv.setUint16(10, 8, true);
26 cv.setUint32(20, packed.length, true);
27 cv.setUint32(24, claim ?? raw.length, true);
28 cv.setUint16(28, n.length, true);
29 cv.setUint32(42, offset, true);
30 head.set(n, 46);
31 chunks.push(local, packed);
32 central.push(head);
33 offset += local.length + packed.length;
34 }
35 const size = central.reduce((a, c) => a + c.length, 0);
36 const end = new Uint8Array(22);
37 const ev = new DataView(end.buffer);
38 ev.setUint32(0, 0x06054b50, true);
39 ev.setUint16(8, central.length, true);
40 ev.setUint16(10, central.length, true);
41 ev.setUint32(12, size, true);
42 ev.setUint32(16, offset, true);
43 return new Uint8Array(Buffer.concat([...chunks, ...central, end]));
44 }
45
test/render-kinds.test.ts +1
@@ -63,6 +63,7 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = {
6363 zip: [of(0x50, 0x4b, 3, 4, 0), "archive", "application/octet-stream"],
6464 tar: [enc("x".repeat(257) + "ustar\0"), "archive", "application/octet-stream"],
6565 tgz: [of(0x1f, 0x8b, 8), "archive", "application/octet-stream"],
66 xlsx: [enc("PK\x03\x04....xl/workbook.xml"), "sheet", "application/octet-stream"],
6667 ipynb: [enc("{}"), "notebook", "application/octet-stream"],
6768 md: [enc("# a\n"), "markdown", "application/octet-stream"],
6869 markdown: [enc("# a\n"), "markdown", "application/octet-stream"],
test/render-xlsx.test.ts added +188
@@ -0,0 +1,188 @@
1 /** An .xlsx as a table: the registry, the reader, the page, and the hostile files. */
2 import { describe, expect, test } from "bun:test";
3 import { zip } from "./helpers/zip.ts";
4 import { fileKind, mediaType } from "../src/web/render/kinds.ts";
5 import { RENDERERS } from "../src/web/render/renderers.ts";
6 import { zipEntry } from "../src/web/render/archive.ts";
7 import { columnName, sheetView } from "../src/tui/screens/workspace_sheet.ts";
8 import { COLUMNS, PART_CAP, ROWS, readWorkbook, sheet } from "../src/web/render/xlsx.ts";
9
10 const enc = (s: string): Uint8Array => new TextEncoder().encode(s);
11
12 const BOOK = `<workbook><sheets><sheet name="Totals &amp; Co" sheetId="1" r:id="rId1"/><sheet name="Other" sheetId="2" r:id="rId2"/></sheets></workbook>`;
13 const RELS = `<Relationships><Relationship Id="rId1" Type="x" Target="worksheets/sheet1.xml"/><Relationship Id="rId2" Type="x" Target="worksheets/sheet2.xml"/></Relationships>`;
14 const STRINGS = `<sst><si><t>Name</t></si><si><r><t>=cmd</t></r><r><t>|calc</t></r></si><si><t>&lt;b&gt;x&lt;/b&gt;</t></si></sst>`;
15 const SHEET = `<worksheet><sheetData>
16 <row r="1"><c r="A1" t="s"><v>0</v></c><c r="C1" t="s"><v>1</v></c></row>
17 <row r="2"><c r="A2" t="s"><v>2</v></c><c r="B2"><f>SUM(1,2)</f><v>-3</v></c><c r="C2" t="inlineStr"><is><t>@x</t></is></c><c r="D2" t="b"><v>1</v></c></row>
18 </sheetData></worksheet>`;
19 const parts = (extra: Record<string, string> = {}) => ({
20 "[Content_Types].xml": "<Types/>",
21 "xl/workbook.xml": BOOK,
22 "xl/_rels/workbook.xml.rels": RELS,
23 "xl/sharedStrings.xml": STRINGS,
24 "xl/worksheets/sheet1.xml": SHEET,
25 ...extra,
26 });
27 const draw = (body: Uint8Array) => sheet({ path: "a/Book.xlsx", size: body.length, body, media: "/m", download: "/d" });
28
29 describe("xlsx kind", () => {
30 test("a workbook is a sheet, served as octet-stream; a plain zip or wrong name is not", () => {
31 const book = zip(parts());
32 expect(fileKind("b.xlsx", book)).toBe("sheet");
33 expect(mediaType("b.xlsx", book)).toBe("application/octet-stream");
34 expect(fileKind("b.xlsx", zip({ "a.txt": "hi" }))).not.toBe("sheet");
35 expect(fileKind("b.xlsx", enc("plain text"))).not.toBe("sheet");
36 expect(RENDERERS.sheet).toBe(sheet);
37 });
38
39 test("a workbook whose first part pushes xl/workbook.xml past the 4 KB head is still a sheet", () => {
40 let x = 123456789;
41 const filler = new Uint8Array(6000).map(() => ((x = (x * 1103515245 + 12345) >>> 0) >>> 16) & 255);
42 const first = zip({ "[Content_Types].xml": filler, "xl/workbook.xml": BOOK, "xl/_rels/workbook.xml.rels": RELS, "xl/worksheets/sheet1.xml": SHEET });
43 expect(new TextDecoder("latin1").decode(first.subarray(0, 4096))).not.toContain("xl/workbook.xml");
44 expect(fileKind("b.xlsx", first.subarray(0, 4096))).toBe("sheet");
45 expect(fileKind("b.xlsx", zip({ "a.txt": filler }).subarray(0, 4096))).not.toBe("sheet");
46 expect(fileKind("b.docx", first.subarray(0, 4096))).not.toBe("sheet");
47 });
48
49 test("the page says only the first sheet is shown, and draws no card", () => {
50 const text = String(draw(zip(parts())).content);
51 expect(text).toContain("Only the first sheet is shown.");
52 expect(text).not.toContain("card");
53 });
54 });
55
56 describe("xlsx reader", () => {
57 test("sheet names, shared strings, cached formula values", () => {
58 const wb = readWorkbook(zip(parts()));
59 if ("error" in wb) throw new Error(wb.error);
60 expect(wb.sheets).toEqual(["Totals & Co", "Other"]);
61 expect(wb.rows[0]!.map((c) => c.text)).toEqual(["Name", "", "=cmd|calc"]);
62 expect(wb.rows[1]!.map((c) => c.text)).toEqual(["<b>x</b>", "-3", "@x", "TRUE"]);
63 expect(wb.rows[1]![1]!.number).toBe(true);
64 });
65
66 test("the page escapes every cell and name, and shows =+-@ cells as text", () => {
67 const out = draw(zip(parts()));
68 const text = String(out.content);
69 expect(out.fallback).toBeUndefined();
70 expect(text).toContain("&lt;b&gt;x&lt;/b&gt;");
71 expect(text).not.toContain("<b>x</b>");
72 expect(text).toContain("Totals &amp; Co");
73 expect(text).toContain("<td>=cmd|calc</td>");
74 expect(text).toContain('<td class="num">-3</td>');
75 expect(out.summary).toContain("2 sheets");
76 });
77
78 test("a + or - cell that is not a number is text, not a number", () => {
79 const rows = `<row r="1"><c r="A1"><v>+</v></c><c r="B1"><v>-</v></c><c r="C1"><v>+5</v></c><c r="D1"><v></v></c><c r="E1"><v>1e999</v></c></row>`;
80 const wb = readWorkbook(zip(parts({ "xl/worksheets/sheet1.xml": `<worksheet><sheetData>${rows}</sheetData></worksheet>` })));
81 if ("error" in wb) throw new Error(wb.error);
82 expect(wb.rows[0]!.map((c) => c.number)).toEqual([false, false, true, false, false]);
83 expect(wb.rows[0]!.map((c) => c.text).slice(0, 2)).toEqual(["+", "-"]);
84 const out = String(draw(zip(parts({ "xl/worksheets/sheet1.xml": `<worksheet><sheetData>${rows}</sheetData></worksheet>` }))).content);
85 expect(out).toContain("<td>+</td>");
86 expect(out).toContain("<td>-</td>");
87 });
88
89 test("rows and columns are capped", () => {
90 let rows = "";
91 for (let r = 1; r <= ROWS + 50; r++) {
92 rows += `<row r="${r}">`;
93 for (let c = 0; c < COLUMNS + 5; c++) rows += `<c r="${String.fromCharCode(65 + (c % 26))}${r}"><v>${c}</v></c>`;
94 rows += "</row>";
95 }
96 const wb = readWorkbook(zip(parts({ "xl/worksheets/sheet1.xml": `<worksheet><sheetData>${rows}</sheetData></worksheet>` })));
97 if ("error" in wb) throw new Error(wb.error);
98 expect(wb.rows.length).toBe(ROWS);
99 expect(Math.max(...wb.rows.map((r) => r.length))).toBeLessThanOrEqual(COLUMNS);
100 expect(wb.cut).toBe(true);
101 });
102
103 test("a truncated file falls back to the source view", () => {
104 const book = zip(parts());
105 for (const cut of [10, Math.floor(book.length / 2), book.length - 5]) {
106 const out = draw(book.subarray(0, cut));
107 expect(out.fallback).toBeDefined();
108 }
109 });
110
111 test("a missing part, a bad target, and an empty body fall back", () => {
112 const { "xl/worksheets/sheet1.xml": _, ...noSheet } = parts();
113 expect(draw(zip(noSheet)).fallback).toBeDefined();
114 expect(draw(zip(parts({ "xl/_rels/workbook.xml.rels": RELS.replace("worksheets/sheet1.xml", "../../x") }))).fallback).toBeDefined();
115 expect(draw(new Uint8Array(0)).fallback).toBeDefined();
116 });
117
118 test("a bomb-sized claim, and a real inflation past the cap, are refused", () => {
119 expect(draw(zip(parts(), PART_CAP + 1)).fallback).toBeDefined();
120 const big = "<worksheet><sheetData>" + "<row r=\"1\"/>".repeat(1_500_000) + "</sheetData></worksheet>";
121 expect(big.length).toBeGreaterThan(PART_CAP);
122 // The declared size lies low; the inflation limit still stops it.
123 const lying = zip(parts({ "xl/worksheets/sheet1.xml": big }));
124 const v = new DataView(lying.buffer, lying.byteOffset);
125 let p = v.getUint32(lying.length - 22 + 16, true);
126 while (v.getUint32(p, true) === 0x02014b50) {
127 v.setUint32(p + 24, 100, true);
128 p += 46 + v.getUint16(p + 28, true);
129 }
130 expect(draw(lying).fallback).toBeDefined();
131 });
132 });
133
134 describe("zipEntry refuses what it must not read", () => {
135 const flag = (body: Uint8Array, at: (v: DataView, p: number) => void): Uint8Array => {
136 const copy = body.slice();
137 const v = new DataView(copy.buffer);
138 at(v, v.getUint32(copy.length - 22 + 16, true));
139 return copy;
140 };
141 const one = zip({ "a.txt": "hello" });
142
143 test("the plain entry reads", () => {
144 expect(new TextDecoder().decode(zipEntry(one, "a.txt", 100)!)).toBe("hello");
145 });
146
147 test("an encrypted entry is null", () => {
148 expect(zipEntry(flag(one, (v, p) => v.setUint16(p + 8, 1, true)), "a.txt", 100)).toBeNull();
149 });
150
151 test("a stored entry bigger than the cap is null, whatever it declares", () => {
152 const stored = flag(one, (v, p) => {
153 v.setUint16(p + 10, 0, true);
154 v.setUint32(p + 24, 5, true);
155 });
156 expect(zipEntry(stored, "a.txt", 100)).not.toBeNull();
157 expect(zipEntry(stored, "a.txt", 3)).toBeNull();
158 const declaredBig = flag(one, (v, p) => v.setUint32(p + 24, 1_000_000, true));
159 expect(zipEntry(declaredBig, "a.txt", 100)).toBeNull();
160 });
161
162 test("a missing name is null", () => {
163 expect(zipEntry(one, "b.txt", 100)).toBeNull();
164 });
165 });
166
167 describe("the terminal door", () => {
168 test("column letters run A to Z, then AA", () => {
169 expect([0, 25, 26, 27, 701, 702].map(columnName)).toEqual(["A", "Z", "AA", "AB", "ZZ", "AAA"]);
170 });
171
172 test("sheet names on top, then the first rows as plain text", () => {
173 const t = sheetView("a/Book.xlsx", zip(parts()));
174 if (t === null || typeof t === "string") throw new Error(String(t));
175 expect(t.header).toEqual(["A", "B", "C", "D"]);
176 expect(t.rows[0]![0]).toBe("Sheet 1 (shown): Totals & Co");
177 expect(t.rows[1]![0]).toBe("Sheet 2: Other");
178 expect(t.rows[3]).toEqual(["Name", "", "=cmd|calc", ""]);
179 expect(t.rows[4]).toEqual(["<b>x</b>", "-3", "@x", "TRUE"]);
180 });
181
182 test("a damaged workbook is a sentence; other files are not this kind", () => {
183 const book = zip(parts());
184 expect(typeof sheetView("a/Book.xlsx", book.subarray(0, Math.floor(book.length / 2)))).toBe("string");
185 expect(sheetView("a/Book.zip", book)).toBeNull();
186 expect(sheetView("a/Book.xlsx", enc("text"))).toBeNull();
187 });
188 });
test/tui-workspace-formats.test.ts +28
@@ -9,6 +9,7 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test";
99 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1010 import type { App as Forge } from "../src/app.ts";
1111 import type { User } from "../src/core/users.ts";
12 import { zip } from "./helpers/zip.ts";
1213 import { quietLogs } from "./helpers/globals.ts";
1314 import { workspaceFixture, type Fixture } from "./helpers/tui-workspace.ts";
1415
@@ -686,4 +687,31 @@ describe("the terminal workspace, by file type", () => {
686687 h.stop();
687688 }
688689 });
690
691 test("an .xlsx opens as its first sheet's table, with the sheet names on top", async () => {
692 const made = await createRepo(forge, alice, { name: "books", visibility: "public", description: "sheets" });
693 if (!made.ok) throw new Error("repo failed");
694 const book = zip({
695 "[Content_Types].xml": "<Types/>",
696 "xl/workbook.xml": `<workbook><sheets><sheet name="Totals" sheetId="1" r:id="rId1"/><sheet name="Other" sheetId="2" r:id="rId2"/></sheets></workbook>`,
697 "xl/_rels/workbook.xml.rels": `<Relationships><Relationship Id="rId1" Target="worksheets/sheet1.xml"/></Relationships>`,
698 "xl/worksheets/sheet1.xml": `<worksheet><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>Widget</t></is></c><c r="B1"><v>42</v></c></row></sheetData></worksheet>`,
699 });
700 await commitToBranch(forge, alice, made.value, {
701 message: "seed",
702 files: [{ path: "sales.xlsx", content: book }],
703 });
704 const h = boot();
705 try {
706 await h.settle();
707 await open(h, "books");
708 await openFile(h, "sales.xlsx");
709 expect(h.text()).toContain("Sheet 1 (shown): Totals");
710 expect(h.text()).toContain("Sheet 2: Other");
711 expect(h.text()).toContain("Widget");
712 expect(h.text()).toContain("42");
713 } finally {
714 h.stop();
715 }
716 });
689717 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs