Public: anyone can read this; the crew can change it.
A Blender .blend shows the version and the data blocks it holds #16536
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/16023-a-blender-blend-shows-the-version-and-th → main 0 commits fork 3 threads · 3 unresolved
21 files
changed +386
−53
- docs/features/binary-facts.md
- docs/features/blend.md
- docs/features/file-kinds.md
- scripts/kinds-guide.ts
- src/http/mcp/tools/read_file.ts
- src/tui/screens/workspace.ts
- src/tui/screens/workspace_cad.ts
- src/tui/ui/blend-note.ts
- src/web/render/binary-facts.ts
- src/web/render/blend-read.ts
- src/web/render/blend.ts
- src/web/render/kinds.ts
- src/web/render/renderers.ts
- test/binary-facts.test.ts
- test/fuzz-renderers.test.ts
- test/helpers/blend.ts
- test/mcp-read-file.test.ts
- test/render-binary-video.test.ts
- test/render-blend.test.ts
- test/render-kinds.test.ts
- test/workspace-cad.test.ts
docs/features/binary-facts.md +2 −3
| @@ -1,13 +1,12 @@ | ||
| 1 | 1 | --- |
| 2 | 2 | topic: Files in the browser |
| 3 | row: A .dwg, .blend or .fbx file: format, version and size | |
| 3 | row: A .dwg or .fbx file: format, version and size | |
| 4 | 4 | --- |
| 5 | # A .dwg, .blend or .fbx file | |
| 5 | # A .dwg or .fbx file | |
| 6 | 6 | |
| 7 | 7 | The forge cannot draw these, but it says what they are: kind, version, size and a Download link. Code: `src/web/render/binary-facts.ts` (`binaryFacts`, pure), `src/web/render/binary-view.ts` (the page). In the terminal, open the file in the workspace (`src/tui/screens/workspace_cad.ts`, `cadView`): the same lines in place of a hex dump. |
| 8 | 8 | |
| 9 | 9 | - **dwg:** `AC10xx` in the first six bytes names the AutoCAD release (`AC1032` is 2018). |
| 10 | - **blend:** `BLENDER` and the version in the header (`BLENDER-v404` is 4.4; the Blender 5 header is read too). A gzip- or zstd-compressed `.blend` is not read: it stays a binary. | |
| 11 | 10 | - **fbx:** `Kaydara FBX Binary`, then the version number (7400 is 7.4). ASCII FBX stays text. |
| 12 | 11 | |
| 13 | 12 | Only the first 64 bytes are looked at, at any file size. The magic decides, never the name alone: a `.dwg` that does not open on `AC10xx` is an ordinary binary. A header cut before its version shows the kind and says so. |
docs/features/blend.md added +26
| @@ -0,0 +1,26 @@ | ||
| 1 | --- | |
| 2 | topic: Files in the browser | |
| 3 | row: A .blend file: version, pointer size, byte order and block counts | |
| 4 | --- | |
| 5 | # A .blend file | |
| 6 | ||
| 7 | A Blender file (`.blend`) shows its version and the data blocks it holds, counted by code, read from the header and block headers with no Blender and no unpacking to disk. Block contents are not opened. Code: `src/web/render/blend-read.ts` (`readBlend`, `sayBlend`, pure), `src/web/render/blend.ts` (the page `blend`), `src/tui/ui/blend-note.ts` (the terminal `blendNote`). | |
| 8 | ||
| 9 | ## What is shown | |
| 10 | ||
| 11 | - **Page:** a `dl` of version, pointer size (32 or 64-bit), byte order and block count; then a second `dl` of blocks by code, most common first (`SC` scene, `OB` object, `ME` mesh, `MA` material and so on, shown as `Name (CODE)`). Codes are sanitised and escaped. A file with no closing block says it may be cut short. | |
| 12 | - **Terminal:** in the workspace, the file's note is the size and the same facts, then one line per code with its count. | |
| 13 | - **MCP:** `read_file` carries `about: {version, codes}` for a `.blend`, from the same reader. | |
| 14 | ||
| 15 | ## Limits | |
| 16 | ||
| 17 | | Limit | Value | Why | | |
| 18 | |---|---|---| | |
| 19 | | Read whole | 5 MB (`RENDER_CAP.blend`) | The page needs every block header; a file over the cap shows the source view with one line why. | | |
| 20 | | Codes listed | 64 (`CODE_CAP`) | The rest are summed as "Other codes". | | |
| 21 | ||
| 22 | Every block length is checked against what is left of the file before it is stepped over, so a lying length ends the read instead of looping. | |
| 23 | ||
| 24 | ## Fallbacks | |
| 25 | ||
| 26 | A gzip- or zstd-compressed `.blend` is not decompressed: it stays a binary. A file that does not open on `BLENDER`, has an unreadable version, or is cut short keeps the source view with the line "not a readable Blender file". Legacy 32-bit, big-endian and the Blender 5 header are read. |
docs/features/file-kinds.md +2 −1
| @@ -32,7 +32,8 @@ The forge draws a file on its page when its kind is below and it is within the s | ||
| 32 | 32 | - **Font**, drawn up to 20 MB: `.ttf`, `.otf`, `.woff`, `.woff2` |
| 33 | 33 | - **Photoshop**, drawn up to 4 GB: `.psd` |
| 34 | 34 | - **GIMP**, drawn up to 20 MB: `.xcf` |
| 35 | - **CAD or 3D source (header facts)**, drawn up to 4 GB: `.dwg`, `.blend`, `.fbx` | |
| 35 | - **Blender file**, drawn up to 5 MB: `.blend` | |
| 36 | - **CAD or 3D source (header facts)**, drawn up to 4 GB: `.dwg`, `.fbx` | |
| 36 | 37 | - **PEM certificate**, drawn up to 1 MB: `.pem`, `.crt`, `.cer` |
| 37 | 38 | - **Bill of materials (CycloneDX, SPDX)**, drawn up to 5 MB: a .json with a bomFormat (CycloneDX) or spdxVersion (SPDX) key |
| 38 | 39 | - **WebAssembly module**, drawn up to 5 MB: `.wasm` |
scripts/kinds-guide.ts +1 −1
| @@ -11,7 +11,7 @@ export const END = "<!-- file-kinds:end -->"; | ||
| 11 | 11 | export const NAMES: Readonly<Record<FileKind, string>> = { |
| 12 | 12 | image: "Image", svg: "SVG", pdf: "PDF", html: "HTML page", audio: "Audio", video: "Video", csv: "Table (CSV, TSV, JSON lines)", |
| 13 | 13 | sqlite: "SQLite database", parquet: "Parquet", pcap: "Packet capture", torrent: "Torrent (BitTorrent metainfo)", pcd: "Point cloud (PCD)", midi: "MIDI file", model3d: "3D model", archive: "Archive", sheet: "Spreadsheet", document: "Word document", |
| 14 | slides: "Slides", layers: "Layered image", font: "Font", psd: "Photoshop", xcf: "GIMP", cad: "CAD or 3D source (header facts)", cert: "PEM certificate", sbom: "Bill of materials (CycloneDX, SPDX)", wasm: "WebAssembly module", config: "Config", markdown: "Markdown", | |
| 14 | slides: "Slides", layers: "Layered image", font: "Font", psd: "Photoshop", xcf: "GIMP", blend: "Blender file", cad: "CAD or 3D source (header facts)", cert: "PEM certificate", sbom: "Bill of materials (CycloneDX, SPDX)", wasm: "WebAssembly module", config: "Config", markdown: "Markdown", | |
| 15 | 15 | notebook: "Notebook", patch: "Patch", log: "Log", subtitles: "Subtitles", gpx: "GPS track", har: "HTTP archive", |
| 16 | 16 | geojson: "GeoJSON", kml: "KML", calendar: "Calendar", otio: "OpenTimelineIO", edl: "Edit list", playlist: "Playlist", fcpxml: "Final Cut XML", kdenlive: "Kdenlive project", |
| 17 | 17 | contacts: "Contacts", mail: "Email", gcode: "G-code", dxf: "DXF drawing", gerber: "Gerber layer", kicad: "KiCad board", mermaid: "Mermaid diagram", drawio: "draw.io diagram", |
src/http/mcp/tools/read_file.ts +11 −1
| @@ -13,13 +13,14 @@ import { aboutTorrent } from "../../../web/render/torrent.ts"; | ||
| 13 | 13 | import { aboutPcd } from "../../../web/render/pcd.ts"; |
| 14 | 14 | import { describeMedia } from "../../../text/mp4.ts"; |
| 15 | 15 | import { readXcf } from "../../../web/render/xcf-read.ts"; |
| 16 | import { readBlend, sayBlend } from "../../../web/render/blend-read.ts"; | |
| 16 | 17 | import { aboutM3u } from "../../../web/render/m3u.ts"; |
| 17 | 18 | import { aboutTfstate } from "../../../web/render/tfstate.ts"; |
| 18 | 19 | import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; |
| 19 | 20 | |
| 20 | 21 | export const tool: Tool = { |
| 21 | 22 | name: "read_file", |
| 22 | doc: "`{ repo, path, ref? }`: a file (utf-8 or base64) or a directory listing at a ref, as GET …/contents; MP4/MOV/M4A files also carry about: {kind, seconds, width?, height?, says}, a .pcd {kind: pcd, points, width, height, data, fields, bounds?, says}; .mid {bpm, tracks}; .ifc {says}; .torrent {name, size, infoHash, trackers, files}; .m3u {tracks, seconds}", | |
| 23 | doc: "`{ repo, path, ref? }`: a file (utf-8 or base64) or a directory listing at a ref, as GET …/contents; MP4/MOV/M4A files also carry about: {kind, seconds, width?, height?, says}, a .pcd {kind: pcd, points, width, height, data, fields, bounds?, says}; .mid {bpm, tracks}; .ifc {says}; .blend {version, codes}; .torrent {name, size, infoHash, trackers, files}; .m3u {tracks, seconds}", | |
| 23 | 24 | description: |
| 24 | 25 | "No shell? Read a file or list a directory at a ref (default branch); over 5 MB: tooLarge.", |
| 25 | 26 | inputSchema: { |
| @@ -97,6 +98,7 @@ function aboutFile(path: string, body: Uint8Array, text: string | undefined): Re | ||
| 97 | 98 | if (/\.torrent$/i.test(path)) return aboutTorrent(body); |
| 98 | 99 | if (/\.pcd$/i.test(path)) return aboutPcd(body); |
| 99 | 100 | if (/\.xcf$/i.test(path)) return aboutXcf(body); |
| 101 | if (/\.blend$/i.test(path)) return aboutBlend(body); | |
| 100 | 102 | if (text !== undefined && /\.tfstate$/i.test(path)) return aboutTfstate(text); |
| 101 | 103 | if (text !== undefined && /\.m3u8?$/i.test(path) && body.length <= RENDER_CAP.playlist) return aboutM3u(text); |
| 102 | 104 | const media = describeMedia(path, body); |
| @@ -142,3 +144,11 @@ function aboutXcf(body: Uint8Array): Record<string, unknown> { | ||
| 142 | 144 | const { width, height, mode, layerCount, layers } = x; |
| 143 | 145 | return { kind: "xcf", width, height, mode, layerCount, layers, says: `GIMP, ${width} × ${height} ${mode}, ${layerCount} ${layerCount === 1 ? "layer" : "layers"}` }; |
| 144 | 146 | } |
| 147 | ||
| 148 | /** A Blender file's version and block counts, the facts the blob page lists. */ | |
| 149 | function aboutBlend(body: Uint8Array): Record<string, unknown> | undefined { | |
| 150 | const b = readBlend(body); | |
| 151 | if (b === undefined) return undefined; | |
| 152 | const { version, pointerSize, endian, blocks, codes, other } = b; | |
| 153 | return { kind: "blend", version, pointerSize, endian, blocks, codes, other, says: sayBlend(b) }; | |
| 154 | } |
src/tui/screens/workspace.ts +3 −1
| @@ -142,6 +142,7 @@ import { usdNote } from "../ui/usd-note.ts"; | ||
| 142 | 142 | import { holdsExif, readExif } from "../../web/render/exif.ts"; |
| 143 | 143 | import { psdNote } from "../ui/psd-note.ts"; |
| 144 | 144 | import { xcfNote } from "../ui/xcf-note.ts"; |
| 145 | import { blendNote } from "../ui/blend-note.ts"; | |
| 145 | 146 | import { mediaNote } from "../ui/media-note.ts"; |
| 146 | 147 | import { plainReason } from "../../web/render/renderers.ts"; |
| 147 | 148 | import { undrawable, UNDRAWN } from "../../web/render/undrawable.ts"; |
| @@ -1634,7 +1635,8 @@ export function workspaceScreen( | ||
| 1634 | 1635 | archiveNote(t.path, read.value.body, read.value.size, formatSize) ?? |
| 1635 | 1636 | usdNote(t.path, read.value.body, read.value.size, formatSize) ?? |
| 1636 | 1637 | (kindByName(t.path) === "psd" ? psdNote(read.value.body, read.value.size, formatSize) : undefined) ?? |
| 1637 | (kindByName(t.path) === "xcf" ? xcfNote(read.value.body, read.value.size, formatSize) : undefined); | |
| 1638 | (kindByName(t.path) === "xcf" ? xcfNote(read.value.body, read.value.size, formatSize) : undefined) ?? | |
| 1639 | (kindByName(t.path) === "blend" ? blendNote(read.value.body, read.value.size, formatSize) : undefined); | |
| 1638 | 1640 | if (known !== undefined) note = known; |
| 1639 | 1641 | else { |
| 1640 | 1642 | // The page's last resort too: size, type and the first bytes as hex (pure ASCII, so |
src/tui/screens/workspace_cad.ts +1 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | /** |
| 2 | * A .dwg, .blend or .fbx file in the terminal workspace, read the way the page reads it: the same | |
| 2 | * A .dwg or .fbx file in the terminal workspace, read the way the page reads it: the same | |
| 3 | 3 | * `binaryFacts`, said as a few lines (kind, version, size) in place of a hex dump. The same gate |
| 4 | 4 | * too: the name and the first bytes must say so (`fileKind`). |
| 5 | 5 | * |
src/tui/ui/blend-note.ts added +16
| @@ -0,0 +1,16 @@ | ||
| 1 | /** | |
| 2 | * What the terminal says for a Blender .blend: version, pointer size, byte order and the block | |
| 3 | * counts as lines. Read through `readBlend`, as the page is, and only from the whole file. | |
| 4 | */ | |
| 5 | ||
| 6 | import { codeName, readBlend, sayBlend } from "../../web/render/blend-read.ts"; | |
| 7 | ||
| 8 | /** The facts and block table of a .blend; `undefined` for a file that is not a readable one. */ | |
| 9 | export function blendNote(body: Uint8Array, size: number, fmt: (bytes: number) => string): string | undefined { | |
| 10 | const b = body.length >= size ? readBlend(body) : undefined; | |
| 11 | if (b === undefined) return undefined; | |
| 12 | const lines = [`${fmt(size)} · ${sayBlend(b)}`]; | |
| 13 | for (const c of b.codes) lines.push(` ${codeName(c.code)}${codeName(c.code) === c.code ? "" : ` (${c.code})`}: ${c.count}`); | |
| 14 | if (b.other > 0) lines.push(` other codes: ${b.other}`); | |
| 15 | return lines.join("\n"); | |
| 16 | } |
src/web/render/binary-facts.ts +2 −19
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | /** |
| 2 | * What a CAD or 3D source says of itself in its first bytes: `.dwg` (AutoCAD), `.blend` (Blender) | |
| 2 | * What a CAD or 3D source says of itself in its first bytes: `.dwg` (AutoCAD) | |
| 3 | 3 | * and `.fbx` (Autodesk binary FBX). The forge cannot draw them, but it can name them: format, |
| 4 | 4 | * version and size, not raw bytes. Plain data in, plain data out; nothing past `FACTS_HEAD` bytes |
| 5 | 5 | * is looked at, and a name never decides alone (the magic must be there). |
| @@ -46,23 +46,6 @@ function dwg(head: Uint8Array): BinaryFacts | undefined { | ||
| 46 | 46 | : { format: "AutoCAD drawing", version: `AutoCAD ${release} (${code})` }; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | /** `BLENDER` then, in the old header, `_`/`-` (4/8-byte pointers), `v`/`V` (byte order), `404`. */ | |
| 50 | function blend(head: Uint8Array): BinaryFacts | undefined { | |
| 51 | if (text(head, 0, 7) !== "BLENDER") return undefined; | |
| 52 | const format = "Blender file"; | |
| 53 | // Blender 5 and later: BLENDER, a header size ("17"), "-", a format ("01"), "v", then "0500". | |
| 54 | const modern = /^(\d\d)-(\d\d)v(\d{4})$/.exec(text(head, 7, 10)); | |
| 55 | if (modern !== null) { | |
| 56 | const n = Number(modern[3]); | |
| 57 | return { format, version: `${Math.floor(n / 100)}.${n % 100}` }; | |
| 58 | } | |
| 59 | const old = /^[_-][vV](\d)(\d\d)$/.exec(text(head, 7, 5)); | |
| 60 | if (old !== null) return { format, version: `${old[1]}.${Number(old[2])}` }; | |
| 61 | return head.length < 12 | |
| 62 | ? { format, note: "The header is cut short: no version." } | |
| 63 | : { format, note: "The version in the header is not one this forge reads." }; | |
| 64 | } | |
| 65 | ||
| 66 | 49 | const FBX_MAGIC = "Kaydara FBX Binary \0"; |
| 67 | 50 | |
| 68 | 51 | /** `Kaydara FBX Binary \0`, two bytes (`1A 00`), then the version as a little-endian number. */ |
| @@ -74,7 +57,7 @@ function fbx(head: Uint8Array): BinaryFacts | undefined { | ||
| 74 | 57 | return { format, version: `${Math.floor(n / 1000)}.${Math.floor((n % 1000) / 100)}` }; |
| 75 | 58 | } |
| 76 | 59 | |
| 77 | const BY_EXTENSION: Readonly<Record<string, (head: Uint8Array) => BinaryFacts | undefined>> = { dwg, blend, fbx }; | |
| 60 | const BY_EXTENSION: Readonly<Record<string, (head: Uint8Array) => BinaryFacts | undefined>> = { dwg, fbx }; | |
| 78 | 61 | |
| 79 | 62 | /** The extensions this module reads, for the registry. */ |
| 80 | 63 | export const FACTS_EXTENSIONS: readonly string[] = Object.keys(BY_EXTENSION); |
src/web/render/blend-read.ts added +133
| @@ -0,0 +1,133 @@ | ||
| 1 | /** | |
| 2 | * Reading a Blender file (`.blend`): the version, pointer size and byte order from the file header, | |
| 3 | * and a count of the blocks by code (scenes, objects, meshes, materials...) from the block headers. | |
| 4 | * Pure: bytes in, plain data out, no markup, so the page, the terminal and the MCP file tool share | |
| 5 | * it. Format: Blender's `BLO_` file layout (`readfile.cc`, `BHead` and `LargeBHead8`). | |
| 6 | * | |
| 7 | * The file is untrusted and its body is already under the render cap. Each block header is 20 or | |
| 8 | * more bytes and a block is skipped by its own length, checked against what is left before it is | |
| 9 | * followed, so the walk consumes at least 20 bytes a turn and cannot outrun the file. A length that | |
| 10 | * lies (negative, past the end) makes the file unreadable. Only `CODE_CAP` distinct codes are kept; | |
| 11 | * the rest are counted as "other". Block contents are never read. | |
| 12 | */ | |
| 13 | ||
| 14 | /** Distinct block codes kept in the table. */ | |
| 15 | export const CODE_CAP = 64; | |
| 16 | ||
| 17 | export interface BlendCode { | |
| 18 | /** The code as the file writes it: `OB`, `ME`, `DATA`; only letters, digits and `_`. */ | |
| 19 | readonly code: string; | |
| 20 | readonly count: number; | |
| 21 | } | |
| 22 | ||
| 23 | export interface Blend { | |
| 24 | /** `4.4`; Blender writes the version of the build that saved the file. */ | |
| 25 | readonly version: string; | |
| 26 | /** Bytes in a pointer in the file: 4 or 8. */ | |
| 27 | readonly pointerSize: 4 | 8; | |
| 28 | readonly endian: "little" | "big"; | |
| 29 | /** Blocks walked, ENDB excluded. */ | |
| 30 | readonly blocks: number; | |
| 31 | /** Counts by code, most first, then by code. */ | |
| 32 | readonly codes: readonly BlendCode[]; | |
| 33 | /** Blocks whose code fell past `CODE_CAP` distinct codes. */ | |
| 34 | readonly other: number; | |
| 35 | /** Whether the walk met the ENDB block that closes a file. */ | |
| 36 | readonly ended: boolean; | |
| 37 | } | |
| 38 | ||
| 39 | /** What the data blocks with an ID code are, in words (the ID types of `DNA_ID_enums.h`). */ | |
| 40 | export const CODE_NAMES: Readonly<Record<string, string>> = { | |
| 41 | SC: "Scenes", OB: "Objects", ME: "Meshes", MA: "Materials", CA: "Cameras", LA: "Lights", | |
| 42 | WO: "Worlds", IM: "Images", TE: "Textures", GR: "Collections", AC: "Actions", AR: "Armatures", | |
| 43 | CU: "Curves", NT: "Node trees", LI: "Libraries", TX: "Texts", SO: "Sounds", BR: "Brushes", | |
| 44 | LS: "Line styles", PA: "Particle settings", WM: "Window managers", WS: "Workspaces", SCR: "Screens", | |
| 45 | GP: "Grease pencils", LT: "Lattices", MB: "Metaballs", KE: "Shape keys", LP: "Light probes", | |
| 46 | VF: "Fonts", CF: "Cache files", MC: "Movie clips", MSK: "Masks", PAL: "Palettes", PC: "Paint curves", | |
| 47 | SPK: "Speakers", CV: "Curves (hair)", PT: "Point clouds", VO: "Volumes", | |
| 48 | DATA: "Data (structs)", DNA1: "Structure catalogue", REND: "Render settings", TEST: "Thumbnail", | |
| 49 | GLOB: "Global state", USER: "User preferences", | |
| 50 | }; | |
| 51 | ||
| 52 | const ascii = (b: Uint8Array, at: number, n: number): string => { | |
| 53 | let s = ""; | |
| 54 | for (let i = at; i < at + n && i < b.length; i++) s += String.fromCharCode(b[i]!); | |
| 55 | return s; | |
| 56 | }; | |
| 57 | ||
| 58 | /** A code with its NUL padding off and anything but letters, digits and `_` made `?`. */ | |
| 59 | const cleanCode = (raw: string): string => raw.replace(/\0+$/, "").replace(/[^A-Za-z0-9_]/g, "?"); | |
| 60 | ||
| 61 | interface Header { | |
| 62 | readonly version: string; | |
| 63 | readonly pointerSize: 4 | 8; | |
| 64 | readonly endian: "little" | "big"; | |
| 65 | /** The file header's length. */ | |
| 66 | readonly at: number; | |
| 67 | /** The block header's length; the length field's offset and width follow from `large`. */ | |
| 68 | readonly head: number; | |
| 69 | readonly large: boolean; | |
| 70 | } | |
| 71 | ||
| 72 | /** The header, or undefined when the bytes do not open as an uncompressed .blend. */ | |
| 73 | function readHeader(b: Uint8Array): Header | undefined { | |
| 74 | if (ascii(b, 0, 7) !== "BLENDER") return undefined; | |
| 75 | // Blender 5 and later: a header size ("17"), "-", a format ("01"), "v", then the version "0500". | |
| 76 | const modern = /^(\d\d)-(\d\d)v(\d{4})$/.exec(ascii(b, 7, 10)); | |
| 77 | if (modern !== null) { | |
| 78 | if (modern[1] !== "17" || modern[2] !== "01") return undefined; | |
| 79 | const n = Number(modern[3]); | |
| 80 | return { version: `${Math.floor(n / 100)}.${n % 100}`, pointerSize: 8, endian: "little", at: 17, head: 32, large: true }; | |
| 81 | } | |
| 82 | const old = /^([_-])([vV])(\d)(\d\d)$/.exec(ascii(b, 7, 5)); | |
| 83 | if (old === null) return undefined; | |
| 84 | const pointerSize = old[1] === "_" ? 4 : 8; | |
| 85 | return { version: `${old[3]}.${Number(old[4])}`, pointerSize, endian: old[2] === "v" ? "little" : "big", at: 12, head: 16 + pointerSize, large: false }; | |
| 86 | } | |
| 87 | ||
| 88 | /** True when the bytes open as an uncompressed Blender file. */ | |
| 89 | export const isBlend = (b: Uint8Array): boolean => readHeader(b) !== undefined; | |
| 90 | ||
| 91 | /** Reads the facts; undefined for a file that is not a .blend or whose block lengths lie. */ | |
| 92 | export function readBlend(body: Uint8Array): Blend | undefined { | |
| 93 | const h = readHeader(body); | |
| 94 | if (h === undefined) return undefined; | |
| 95 | const v = new DataView(body.buffer, body.byteOffset, body.byteLength); | |
| 96 | const little = h.endian === "little"; | |
| 97 | const counts = new Map<string, number>(); | |
| 98 | let other = 0; | |
| 99 | let blocks = 0; | |
| 100 | let ended = false; | |
| 101 | let at = h.at; | |
| 102 | while (at + h.head <= body.length) { | |
| 103 | const code = cleanCode(ascii(body, at, 4)); | |
| 104 | // `len` is an i32 at byte 4 in the old header, an i64 at byte 16 in the large one. | |
| 105 | let len: number; | |
| 106 | if (h.large) { | |
| 107 | const hi = v.getInt32(at + 20, true); | |
| 108 | const lo = v.getUint32(at + 16, true); | |
| 109 | len = hi === 0 ? lo : -1; | |
| 110 | } else len = v.getInt32(at + 4, little); | |
| 111 | if (code === "ENDB") { | |
| 112 | ended = true; | |
| 113 | break; | |
| 114 | } | |
| 115 | if (len < 0 || len > body.length - (at + h.head)) return undefined; | |
| 116 | blocks++; | |
| 117 | const seen = counts.get(code); | |
| 118 | if (seen !== undefined) counts.set(code, seen + 1); | |
| 119 | else if (counts.size < CODE_CAP) counts.set(code, 1); | |
| 120 | else other++; | |
| 121 | at += h.head + len; | |
| 122 | } | |
| 123 | if (!ended && blocks === 0) return undefined; | |
| 124 | const codes = [...counts].map(([code, count]) => ({ code, count })).sort((a, b) => b.count - a.count || (a.code < b.code ? -1 : 1)); | |
| 125 | return { version: h.version, pointerSize: h.pointerSize, endian: h.endian, blocks, codes, other, ended }; | |
| 126 | } | |
| 127 | ||
| 128 | /** A block code in words: `Objects` for `OB`, the code itself when it is not known. */ | |
| 129 | export const codeName = (code: string): string => CODE_NAMES[code] ?? code; | |
| 130 | ||
| 131 | /** The facts in a line: `Blender 4.4, 64-bit little-endian, 38 blocks`. */ | |
| 132 | export const sayBlend = (b: Blend): string => | |
| 133 | `Blender ${b.version}, ${b.pointerSize * 8}-bit ${b.endian}-endian, ${b.blocks} ${b.blocks === 1 ? "block" : "blocks"}`; |
src/web/render/blend.ts added +36
| @@ -0,0 +1,36 @@ | ||
| 1 | /** | |
| 2 | * A Blender file on the blob page: version, pointer size and byte order as rows, then a table of | |
| 3 | * block codes (scenes, objects, meshes, materials...) with counts. Nothing is drawn and nothing is | |
| 4 | * run. A file that does not parse, or is compressed, keeps the source view with one line why. | |
| 5 | * Reading is in `blend-read.ts`. | |
| 6 | */ | |
| 7 | ||
| 8 | import { html, when } from "../html.ts"; | |
| 9 | import { fileSize } from "../ui.ts"; | |
| 10 | import { codeName, readBlend } from "./blend-read.ts"; | |
| 11 | import type { Rendered, RenderInput } from "./renderers.ts"; | |
| 12 | ||
| 13 | const row = (k: string, v: string) => html`<div class="status-row"><dt>${k}</dt><dd>${v}</dd></div>`; | |
| 14 | ||
| 15 | export function blend(input: RenderInput): Rendered { | |
| 16 | const b = input.body.length >= input.size ? readBlend(input.body) : undefined; | |
| 17 | if (b === undefined) return { content: html``, lines: 0, fallback: "not a readable Blender file" }; | |
| 18 | const name = input.path.slice(input.path.lastIndexOf("/") + 1); | |
| 19 | return { | |
| 20 | lines: 0, | |
| 21 | summary: `${fileSize(input.size)} · Blender ${b.version}`, | |
| 22 | content: html`<div> | |
| 23 | <dl class="status-rows" aria-label="${`${name} facts`}"> | |
| 24 | ${row("Version", b.version)} | |
| 25 | ${row("Pointers", `${b.pointerSize * 8}-bit`)} | |
| 26 | ${row("Byte order", b.endian === "little" ? "Little-endian" : "Big-endian")} | |
| 27 | ${row("Blocks", String(b.blocks))} | |
| 28 | </dl> | |
| 29 | <dl class="status-rows" aria-label="Blocks by code"> | |
| 30 | ${b.codes.map((c) => row(c.code === codeName(c.code) ? c.code : `${codeName(c.code)} (${c.code})`, String(c.count)))} | |
| 31 | ${when(b.other > 0, () => row("Other codes", String(b.other)))} | |
| 32 | </dl> | |
| 33 | ${when(!b.ended, () => html`<p class="muted small">The file has no closing block: it may be cut short.</p>`)} | |
| 34 | </div>`, | |
| 35 | }; | |
| 36 | } |
src/web/render/kinds.ts +5
| @@ -17,6 +17,7 @@ import { binaryFacts, FACTS_EXTENSIONS } from "./binary-facts.ts"; | ||
| 17 | 17 | import { isSbomHead } from "./sbom.ts"; |
| 18 | 18 | import { isTfstateHead } from "./tfstate.ts"; |
| 19 | 19 | import { isWasm } from "./wasm.ts"; |
| 20 | import { isBlend } from "./blend-read.ts"; | |
| 20 | 21 | import { isXcf } from "./xcf-read.ts"; |
| 21 | 22 | |
| 22 | 23 | export type FileKind = |
| @@ -44,6 +45,7 @@ export type FileKind = | ||
| 44 | 45 | | "font" |
| 45 | 46 | | "psd" |
| 46 | 47 | | "xcf" |
| 48 | | "blend" | |
| 47 | 49 | | "cad" |
| 48 | 50 | | "cert" |
| 49 | 51 | | "sbom" |
| @@ -153,6 +155,8 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = { | ||
| 153 | 155 | psd: 4_000 * MB, |
| 154 | 156 | // Layer headers sit anywhere in the body; the page reads it whole. |
| 155 | 157 | xcf: 20 * MB, |
| 158 | // Block headers are hopped by their lengths across the whole body, which the page reads whole. | |
| 159 | blend: 5 * MB, | |
| 156 | 160 | // Only the first 64 bytes are read, whatever the size. |
| 157 | 161 | cad: 4_000 * MB, |
| 158 | 162 | cert: 1 * MB, |
| @@ -280,6 +284,7 @@ const MEDIA: Readonly<Record<string, Media>> = { | ||
| 280 | 284 | bmp: { kind: "image", name: "BMP", type: "image/bmp", magic: ascii("BM") }, |
| 281 | 285 | ico: { kind: "image", name: "ICO", type: "image/x-icon", magic: bytes(0x00, 0x00, 0x01, 0x00) }, |
| 282 | 286 | psd: { kind: "psd", name: "Photoshop", type: "image/vnd.adobe.photoshop", magic: (h) => at(h, 0, "8BPS") && h[4] === 0 && h[5] === 1 }, |
| 287 | blend: { kind: "blend", name: "Blender", type: "application/x-blender", magic: (h) => isBlend(h) }, | |
| 283 | 288 | xcf: { kind: "xcf", name: "GIMP", type: "image/x-xcf", magic: (h) => isXcf(h) }, |
| 284 | 289 | svg: { kind: "svg", name: "SVG", type: "image/svg+xml", magic: svgRoot }, |
| 285 | 290 | html: { kind: "html", name: "HTML", type: "text/html; charset=utf-8", magic: textHead }, |
src/web/render/renderers.ts +2
| @@ -64,6 +64,7 @@ import { packageJson, readPackage } from "./package-json.ts"; | ||
| 64 | 64 | import { patch } from "./patch.ts"; |
| 65 | 65 | import { psd } from "./psd.ts"; |
| 66 | 66 | import { xcf } from "./xcf.ts"; |
| 67 | import { blend } from "./blend.ts"; | |
| 67 | 68 | import { pdf } from "./pdf.ts"; |
| 68 | 69 | import { dockerfile } from "./dockerfile.ts"; |
| 69 | 70 | import { workflow } from "./workflow.ts"; |
| @@ -219,6 +220,7 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = { | ||
| 219 | 220 | font, |
| 220 | 221 | psd, |
| 221 | 222 | xcf, |
| 223 | blend, | |
| 222 | 224 | cad: binaryView, |
| 223 | 225 | cert, |
| 224 | 226 | sbom, |
test/binary-facts.test.ts +1 −20
| @@ -1,4 +1,4 @@ | ||
| 1 | /** A .dwg, .blend or .fbx says what its header says: format, version, size; never raw bytes. */ | |
| 1 | /** A .dwg or .fbx says what its header says: format, version, size; never raw bytes. */ | |
| 2 | 2 | |
| 3 | 3 | import { describe, expect, test } from "bun:test"; |
| 4 | 4 | import type { Blob } from "../src/view/source.ts"; |
| @@ -10,8 +10,6 @@ const enc = (t: string): number[] => [...t].map((c) => c.charCodeAt(0)); | ||
| 10 | 10 | const pad = (b: number[], n = 200): Uint8Array => new Uint8Array([...b, ...new Array(Math.max(0, n - b.length)).fill(0)]); |
| 11 | 11 | |
| 12 | 12 | const DWG = pad(enc("AC1032")); |
| 13 | const BLEND = pad(enc("BLENDER-v404")); | |
| 14 | const BLEND5 = pad(enc("BLENDER17-01v0500")); | |
| 15 | 13 | const FBX = pad([...enc("Kaydara FBX Binary "), 0, 0x1a, 0, 0x28, 0x1c, 0, 0]); // 7208 |
| 16 | 14 | const FBX74 = pad([...enc("Kaydara FBX Binary "), 0, 0x1a, 0, 0xe8, 0x1c, 0, 0]); // 7400 |
| 17 | 15 | |
| @@ -33,20 +31,11 @@ describe("header facts", () => { | ||
| 33 | 31 | expect(binaryFacts("dwg", pad(enc("AC1015")))?.version).toBe("AutoCAD 2000 (AC1015)"); |
| 34 | 32 | expect(binaryFacts("dwg", pad(enc("AC1099")))?.note).toBeDefined(); |
| 35 | 33 | }); |
| 36 | test("blend: BLENDER and its version, old header and new", () => { | |
| 37 | expect(binaryFacts("blend", BLEND)).toEqual({ format: "Blender file", version: "4.4" }); | |
| 38 | expect(binaryFacts("blend", pad(enc("BLENDER_v279")))?.version).toBe("2.79"); | |
| 39 | expect(binaryFacts("blend", BLEND5)?.version).toBe("5.0"); | |
| 40 | }); | |
| 41 | 34 | test("fbx: Kaydara binary and its version", () => { |
| 42 | 35 | expect(binaryFacts("fbx", FBX)).toEqual({ format: "FBX binary", version: "7.2" }); |
| 43 | 36 | expect(binaryFacts("fbx", FBX74)?.version).toBe("7.4"); |
| 44 | 37 | }); |
| 45 | 38 | test("a truncated header is named, with no version, and never throws", () => { |
| 46 | expect(binaryFacts("blend", new Uint8Array(enc("BLENDER-v")))).toEqual({ | |
| 47 | format: "Blender file", | |
| 48 | note: "The header is cut short: no version.", | |
| 49 | }); | |
| 50 | 39 | expect(binaryFacts("fbx", FBX.subarray(0, 24))?.version).toBeUndefined(); |
| 51 | 40 | expect(binaryFacts("fbx", FBX.subarray(0, 24))?.note).toContain("cut short"); |
| 52 | 41 | expect(binaryFacts("dwg", new Uint8Array(enc("AC10")))).toBeUndefined(); |
| @@ -54,11 +43,9 @@ describe("header facts", () => { | ||
| 54 | 43 | }); |
| 55 | 44 | test("the magic decides: a name alone, or the wrong extension, says nothing", () => { |
| 56 | 45 | expect(binaryFacts("dwg", pad(enc("hello world")))).toBeUndefined(); |
| 57 | expect(binaryFacts("blend", DWG)).toBeUndefined(); | |
| 58 | 46 | expect(binaryFacts("png", DWG)).toBeUndefined(); |
| 59 | 47 | expect(fileKind("a.dwg", pad([1, 0, 2]))).toBe("binary"); |
| 60 | 48 | expect(fileKind("a.dwg", DWG)).toBe("cad"); |
| 61 | expect(fileKind("a.blend", BLEND)).toBe("cad"); | |
| 62 | 49 | expect(fileKind("a.fbx", FBX)).toBe("cad"); |
| 63 | 50 | expect(fileKind("a.txt", DWG)).toBe("binary"); |
| 64 | 51 | expect(kindByName("a.FBX")).toBe("cad"); |
| @@ -78,14 +65,8 @@ describe("the page", () => { | ||
| 78 | 65 | expect(page).toContain("AutoCAD 2018 (AC1032)"); |
| 79 | 66 | expect(page).toContain('href="/d?x=1"'); |
| 80 | 67 | expect(out.summary).toContain("AutoCAD drawing"); |
| 81 | expect(String(draw("scene.blend", BLEND).content)).toContain("4.4"); | |
| 82 | 68 | expect(String(draw("rig.fbx", FBX).content)).toContain("7.2"); |
| 83 | 69 | }); |
| 84 | test("a truncated file shows its kind and says the header is cut", () => { | |
| 85 | const page = String(draw("scene.blend", new Uint8Array(enc("BLENDER-v"))).content); | |
| 86 | expect(page).toContain("Blender file"); | |
| 87 | expect(page).toContain("cut short"); | |
| 88 | }); | |
| 89 | 70 | test("a file over the read ceiling is drawn from its head alone", () => { |
| 90 | 71 | const r = rendererFor("big.fbx", { kind: "too_large", size: 900_000_000, head: FBX } satisfies Blob)!; |
| 91 | 72 | expect(r.body.length).toBe(FBX.length); |
test/fuzz-renderers.test.ts +8
| @@ -65,6 +65,13 @@ function xcfSample(): string { | ||
| 65 | 65 | return head + u32(at) + u32(at + first.length) + u32(0) + u32(0) + first + layer("javascript:alert(1)"); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | /** A small valid Blender file as text: a header and three blocks, every byte below 0x80. */ | |
| 69 | function blendSample(): string { | |
| 70 | const le = (n: number) => String.fromCharCode(n & 127, 0, 0, 0); | |
| 71 | const block = (code: string, len: number) => code.padEnd(4, "\0") + le(len) + "\0".repeat(8) + le(0) + le(1) + "\0".repeat(len); | |
| 72 | return "BLENDER-v404" + block("OB", 8) + block("ME", 4) + block("<b>", 0) + block("ENDB", 0); | |
| 73 | } | |
| 74 | ||
| 68 | 75 | /** |
| 69 | 76 | * A valid file of each kind; a case is one of these with hostile text spliced in. The kind is what |
| 70 | 77 | * `fileKind` reads from the path and the first file, and the renderer fuzzed is the one `RENDERERS` |
| @@ -82,6 +89,7 @@ export const SAMPLES: Readonly<Record<string, { path: string; files: readonly st | ||
| 82 | 89 | ], |
| 83 | 90 | }, |
| 84 | 91 | xcf: { path: "a.xcf", files: [xcfSample()] }, |
| 92 | blend: { path: "a.blend", files: [blendSample()] }, | |
| 85 | 93 | tsv: { path: "data.tsv", files: ["a\tb\tc\n1\t2\t3\n"] }, |
| 86 | 94 | jsonl: { |
| 87 | 95 | path: "rows.jsonl", |
test/helpers/blend.ts added +34
| @@ -0,0 +1,34 @@ | ||
| 1 | /** | |
| 2 | * A Blender file built in memory: header and block headers with zero-filled bodies, no real data. | |
| 3 | */ | |
| 4 | ||
| 5 | const le32 = (n: number): number[] => [n & 255, (n >> 8) & 255, (n >> 16) & 255, (n >>> 24) & 255]; | |
| 6 | const be32 = (n: number): number[] => le32(n).reverse(); | |
| 7 | const text = (s: string): number[] => [...s].map((c) => c.charCodeAt(0)); | |
| 8 | const code = (c: string): number[] => [...text(c), 0, 0, 0, 0].slice(0, 4); | |
| 9 | ||
| 10 | export interface Opts { | |
| 11 | /** "legacy" 8-byte little-endian (default), "legacy32", "big", or "v5". */ | |
| 12 | readonly style?: "legacy" | "legacy32" | "big" | "v5"; | |
| 13 | /** Leave the ENDB block off. */ | |
| 14 | readonly noEnd?: boolean; | |
| 15 | /** Overrides the length field of the first block. */ | |
| 16 | readonly lie?: number; | |
| 17 | } | |
| 18 | ||
| 19 | /** `blocks` are [code, bodyLength] pairs, in order; ENDB is added unless `noEnd`. */ | |
| 20 | export function make(blocks: [string, number?][], o: Opts = {}): Uint8Array { | |
| 21 | const style = o.style ?? "legacy"; | |
| 22 | const out: number[] = []; | |
| 23 | const n = (v: number) => (style === "big" ? be32(v) : le32(v)); | |
| 24 | if (style === "v5") out.push(...text("BLENDER17-01v0500")); | |
| 25 | else out.push(...text(`BLENDER${style === "legacy32" ? "_" : "-"}${style === "big" ? "V" : "v"}404`)); | |
| 26 | const all: [string, number?][] = o.noEnd ? blocks : [...blocks, ["ENDB", 0]]; | |
| 27 | all.forEach(([c, len = 0], i) => { | |
| 28 | const l = i === 0 && o.lie !== undefined ? o.lie : len; | |
| 29 | if (style === "v5") out.push(...code(c), ...le32(0), ...new Array(8).fill(0), ...le32(l), 0, 0, 0, 0, ...le32(1), 0, 0, 0, 0); | |
| 30 | else out.push(...code(c), ...n(l), ...new Array(style === "legacy32" ? 4 : 8).fill(0), ...n(0), ...n(1)); | |
| 31 | out.push(...new Array(Math.max(0, len)).fill(0)); | |
| 32 | }); | |
| 33 | return new Uint8Array(out); | |
| 34 | } |
test/mcp-read-file.test.ts +9 −1
| @@ -13,6 +13,7 @@ import { quietLogs } from "./helpers/globals.ts"; | ||
| 13 | 13 | import { box, concat, ftyp, mvhd0, tkhd } from "./helpers/mp4-boxes.ts"; |
| 14 | 14 | import { IFC } from "./helpers/ifc.ts"; |
| 15 | 15 | import { make as xcfFile } from "./helpers/xcf.ts"; |
| 16 | import { make as blendFile } from "./helpers/blend.ts"; | |
| 16 | 17 | import { asciiBox, binaryBox } from "./helpers/stl.ts"; |
| 17 | 18 | |
| 18 | 19 | quietLogs(); |
| @@ -75,7 +76,7 @@ beforeAll(async () => { | ||
| 75 | 76 | { path: "board.kicad_pcb", content: '(kicad_pcb (version 20221018) (layers (0 "F.Cu" signal) (44 "Edge.Cuts" user)) (footprint "R") (via (at 1 1)) (gr_rect (start 0 0) (end 30 20) (layer "Edge.Cuts")))' }, |
| 76 | 77 | { path: "bad.kicad_pcb", content: "(kicad_pcb (version 1)" }, |
| 77 | 78 | ] })); |
| 78 | ok(await commitToBranch(app, owner, pub, { message: "gimp", files: [{ path: "art.xcf", content: xcfFile([["Sky", { w: 200, h: 100, x: 3, y: 4 }]]) }, { path: "bad.xcf", content: "gimp xcf nope" }] })); | |
| 79 | ok(await commitToBranch(app, owner, pub, { message: "gimp", files: [{ path: "art.xcf", content: xcfFile([["Sky", { w: 200, h: 100, x: 3, y: 4 }]]) }, { path: "bad.xcf", content: "gimp xcf nope" }, { path: "s.blend", content: blendFile([["OB", 4], ["OB", 4], ["ME", 0]]) }] })); | |
| 79 | 80 | const movie = concat(ftyp(), box("moov", mvhd0(600, 600 * 92), box("trak", tkhd(0, 1920, 1080)))); |
| 80 | 81 | ok(await commitToBranch(app, owner, pub, { message: "movie", files: [{ path: "clip.mp4", content: movie }] })); |
| 81 | 82 | ok( |
| @@ -202,6 +203,13 @@ test("a .xcf file carries its canvas and layers as about, or says it is unreadab | ||
| 202 | 203 | expect((await read(tok["stranger"]!, { repo: "ada/pub", path: "bad.xcf" })).value.about.says).toBe("GIMP, unreadable"); |
| 203 | 204 | }); |
| 204 | 205 | |
| 206 | test("a .blend file carries its version and block counts as about", async () => { | |
| 207 | const f = (await read(tok["stranger"]!, { repo: "ada/pub", path: "s.blend" })).value; | |
| 208 | expect(f.about).toMatchObject({ kind: "blend", version: "4.4", pointerSize: 8, endian: "little", blocks: 3 }); | |
| 209 | expect(f.about.codes[0]).toEqual({ code: "OB", count: 2 }); | |
| 210 | expect(f.about.says).toBe("Blender 4.4, 64-bit little-endian, 3 blocks"); | |
| 211 | }); | |
| 212 | ||
| 205 | 213 | test("a KiCad board is described by its layers, counts and outline size", async () => { |
| 206 | 214 | const t = tok["stranger"]!; |
| 207 | 215 | const good = (await read(t, { repo: "ada/pub", path: "board.kicad_pcb" })).value; |
test/render-binary-video.test.ts +2 −2
| @@ -65,8 +65,8 @@ describe("binaryView", () => { | ||
| 65 | 65 | }); |
| 66 | 66 | |
| 67 | 67 | test("a hostile download address is escaped", () => { |
| 68 | const out = binaryView({ ...input("a.blend", ascii("BLENDER-v404")), download: `/x"><script>1</script>` }); | |
| 68 | const out = binaryView({ ...input("a.dwg", ascii("AC1032")), download: `/x"><script>1</script>` }); | |
| 69 | 69 | expect(String(out.content)).not.toContain("<script>"); |
| 70 | expect(String(out.content)).toContain("Blender file"); | |
| 70 | expect(String(out.content)).toContain("AutoCAD drawing"); | |
| 71 | 71 | }); |
| 72 | 72 | }); |
test/render-blend.test.ts added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | /** | |
| 2 | * A .blend on the blob page and in the MCP file tool: header facts, block counts, bounded by | |
| 3 | * lengths. | |
| 4 | */ | |
| 5 | ||
| 6 | import { describe, expect, test } from "bun:test"; | |
| 7 | import { blend } from "../src/web/render/blend.ts"; | |
| 8 | import { CODE_CAP, readBlend, sayBlend } from "../src/web/render/blend-read.ts"; | |
| 9 | import { fileKind, RENDER_CAP } from "../src/web/render/kinds.ts"; | |
| 10 | import type { RenderInput } from "../src/web/render/renderers.ts"; | |
| 11 | import { rendererFor } from "../src/web/render/renderers.ts"; | |
| 12 | import { blendNote } from "../src/tui/ui/blend-note.ts"; | |
| 13 | import { make } from "./helpers/blend.ts"; | |
| 14 | ||
| 15 | const input = (body: Uint8Array, path = "a.blend", size = body.length): RenderInput => ({ path, body, size, media: "/m", download: "/d", all: true }); | |
| 16 | const SCENE: [string, number?][] = [["REND", 4], ["SC", 8], ["OB", 8], ["OB", 8], ["ME", 8], ["MA", 8], ["DATA", 16]]; | |
| 17 | ||
| 18 | describe("blend", () => { | |
| 19 | test("parse: version, pointer size, byte order and counts by code", () => { | |
| 20 | const b = readBlend(make(SCENE))!; | |
| 21 | expect([b.version, b.pointerSize, b.endian, b.blocks, b.ended]).toEqual(["4.4", 8, "little", 7, true]); | |
| 22 | expect(b.codes[0]).toEqual({ code: "OB", count: 2 }); | |
| 23 | expect(b.codes.map((c) => c.code).sort()).toEqual(["DATA", "MA", "ME", "OB", "REND", "SC"]); | |
| 24 | expect(sayBlend(b)).toBe("Blender 4.4, 64-bit little-endian, 7 blocks"); | |
| 25 | const small = readBlend(make(SCENE, { style: "legacy32" }))!; | |
| 26 | expect([small.pointerSize, small.blocks]).toEqual([4, 7]); | |
| 27 | const big = readBlend(make(SCENE, { style: "big" }))!; | |
| 28 | expect([big.endian, big.blocks]).toEqual(["big", 7]); | |
| 29 | const v5 = readBlend(make(SCENE, { style: "v5" }))!; | |
| 30 | expect([v5.version, v5.blocks, v5.codes[0]]).toEqual(["5.0", 7, { code: "OB", count: 2 }]); | |
| 31 | const out = blend(input(make(SCENE))); | |
| 32 | expect(out.fallback).toBeUndefined(); | |
| 33 | expect(out.content.text).toContain("Objects (OB)"); | |
| 34 | expect(out.content.text).toContain("Little-endian"); | |
| 35 | }); | |
| 36 | ||
| 37 | test("reached: the extension and signature open it through the registry", () => { | |
| 38 | const body = make(SCENE); | |
| 39 | expect(fileKind("a.blend", body)).toBe("blend"); | |
| 40 | expect(fileKind("a.blend", new Uint8Array([0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0]))).not.toBe("blend"); | |
| 41 | expect(rendererFor("a.blend", { kind: "binary", body, size: body.length })?.render).toBeDefined(); | |
| 42 | }); | |
| 43 | ||
| 44 | test("escape: a hostile block code and file name never become markup", () => { | |
| 45 | const out = blend(input(make([["<b>x", 4]]), `d/<img src=x onerror=alert(1)>.blend`)); | |
| 46 | const text = String(out.content); | |
| 47 | expect(text).not.toContain("<b>"); | |
| 48 | expect(text).not.toContain("<img"); | |
| 49 | expect(text).toContain("?b?x"); | |
| 50 | }); | |
| 51 | ||
| 52 | test("cap: the kind has a render cap, and a file cut short of its size is not read", () => { | |
| 53 | expect(RENDER_CAP.blend).toBe(5_000_000); | |
| 54 | const body = make(SCENE); | |
| 55 | expect(blend(input(body, "a.blend", body.length + 1)).fallback).toContain("not a readable"); | |
| 56 | }); | |
| 57 | ||
| 58 | test("malformed: lying lengths, cut files and other bytes fall back, never throw", () => { | |
| 59 | expect(readBlend(make(SCENE, { lie: 0x7fffffff }))).toBeUndefined(); | |
| 60 | expect(readBlend(make(SCENE, { lie: -1 }))).toBeUndefined(); | |
| 61 | expect(readBlend(make(SCENE, { style: "v5", lie: 0x7fffffff }))).toBeUndefined(); | |
| 62 | expect(readBlend(new Uint8Array(0))).toBeUndefined(); | |
| 63 | expect(readBlend(new TextEncoder().encode("BLENDER-v"))).toBeUndefined(); | |
| 64 | expect(readBlend(new TextEncoder().encode("BLENDER18-01v0500"))).toBeUndefined(); | |
| 65 | expect(readBlend(make([]))!.blocks).toBe(0); | |
| 66 | expect(readBlend(make([], { noEnd: true }))).toBeUndefined(); | |
| 67 | const whole = make(SCENE); | |
| 68 | for (let cut = 0; cut < whole.length; cut++) expect(() => readBlend(whole.subarray(0, cut))).not.toThrow(); | |
| 69 | expect(blend(input(whole.subarray(0, 40), "a.blend", whole.length)).fallback).toBeDefined(); | |
| 70 | expect(readBlend(make(SCENE, { noEnd: true }))!.ended).toBe(false); | |
| 71 | }); | |
| 72 | ||
| 73 | test("hostile: a flood of distinct codes is bounded by CODE_CAP", () => { | |
| 74 | const blocks: [string, number?][] = []; | |
| 75 | for (let i = 0; i < 5000; i++) blocks.push([String.fromCharCode(65 + (i % 26), 65 + ((i >> 5) % 26), 65 + ((i >> 10) % 26), 97 + (i % 7)), 0]); | |
| 76 | const b = readBlend(make(blocks))!; | |
| 77 | expect(b.codes.length).toBeLessThanOrEqual(CODE_CAP); | |
| 78 | expect(b.blocks).toBe(5000); | |
| 79 | expect(b.codes.reduce((n, c) => n + c.count, b.other)).toBe(5000); | |
| 80 | }); | |
| 81 | ||
| 82 | test("terminal note: lines from the same reader", () => { | |
| 83 | const note = blendNote(make(SCENE), make(SCENE).length, (n) => `${n} B`)!; | |
| 84 | expect(note).toContain("Blender 4.4"); | |
| 85 | expect(note).toContain("Objects (OB): 2"); | |
| 86 | expect(blendNote(new Uint8Array(4), 4, String)).toBeUndefined(); | |
| 87 | }); | |
| 88 | }); |
test/render-kinds.test.ts +1
| @@ -71,6 +71,7 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = { | ||
| 71 | 71 | otf: [enc("OTTO\0\0"), "font", "font/otf"], |
| 72 | 72 | woff: [enc("wOFF\0\0"), "font", "font/woff"], |
| 73 | 73 | psd: [of(0x38, 0x42, 0x50, 0x53, 0, 1), "psd", "image/vnd.adobe.photoshop"], |
| 74 | blend: [new TextEncoder().encode("BLENDER-v404"), "blend", "application/x-blender"], | |
| 74 | 75 | xcf: [new TextEncoder().encode("gimp xcf v003\0"), "xcf", "image/x-xcf"], |
| 75 | 76 | woff2: [enc("wOF2\0\0"), "font", "font/woff2"], |
| 76 | 77 | zip: [of(0x50, 0x4b, 3, 4, 0), "archive", "application/octet-stream"], |
test/workspace-cad.test.ts +3 −3
| @@ -1,4 +1,4 @@ | ||
| 1 | /** The terminal workspace says what a .dwg, .blend or .fbx header says, not a hex dump. */ | |
| 1 | /** The terminal workspace says what a .dwg or .fbx header says, not a hex dump. */ | |
| 2 | 2 | |
| 3 | 3 | import { expect, test } from "bun:test"; |
| 4 | 4 | import { cadView } from "../src/tui/screens/workspace_cad.ts"; |
| @@ -7,12 +7,12 @@ const enc = (t: string): number[] => [...t].map((c) => c.charCodeAt(0)); | ||
| 7 | 7 | const pad = (b: number[], n = 200) => new Uint8Array([...b, ...new Array(n - b.length).fill(0)]); |
| 8 | 8 | const size = (n: number) => `${n} B`; |
| 9 | 9 | |
| 10 | test("a dwg and a blend answer kind, version and size", () => { | |
| 10 | test("a dwg answers (a blend has its own note) kind, version and size", () => { | |
| 11 | 11 | const dwg = cadView("a/plan.dwg", pad(enc("AC1032")), 200, size)!; |
| 12 | 12 | expect(dwg).toContain("Kind: AutoCAD drawing"); |
| 13 | 13 | expect(dwg).toContain("Version: AutoCAD 2018 (AC1032)"); |
| 14 | 14 | expect(dwg).toContain("Size: 200 B"); |
| 15 | expect(cadView("m.blend", pad(enc("BLENDER-v404")), 200, size)).toContain("Version: 4.4"); | |
| 15 | expect(cadView("m.blend", pad(enc("BLENDER-v404")), 200, size)).toBeNull(); | |
| 16 | 16 | }); |
| 17 | 17 | |
| 18 | 18 | test("a name alone is not enough, and other files are not its", () => { |
new above
offline
The
.blendhow-to was removed here, but nodocs/features/blend.mdreplaces it. Add that page: page rows, terminal note, MCPaboutfields, the 5 MB cap, and that compressed files stay binaries.