Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

slides: a save drops the assets nothing refers to, so a deck can shrink (#447)

nyblnet401cadeacd1a
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 84da5b95..729d014b 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1185,6 +1185,14 @@ jobs:
# is the regression.
run: node scripts/test-layouts.ts
+ - name: slides asset-pruning rig
+ # doc.assets was append-only: delete every slide, save, and the "empty"
+ # deck was still 20 MB (#442). A save now writes a copy with the
+ # unreferenced assets dropped. Weighted toward "kept", one case per
+ # reference form, because dropping too much is the worse failure — an
+ # image gone from a file with nothing on screen to say so.
+ run: node scripts/test-slides-assets.ts
+
- name: slides store rig
# The editor's undo/redo, selection and whole-deck replacement, at the
# store level rather than through the DOM. Bundled first for the same
diff --git a/scripts/test-export-secrets.ts b/scripts/test-export-secrets.ts
index f028db11..bbb0cdcb 100644
--- a/scripts/test-export-secrets.ts
+++ b/scripts/test-export-secrets.ts
@@ -218,8 +218,16 @@ console.log('\npasswords')
// serializeFile has exactly one legitimate caller left in the app: the
// documented window.bento.serialize() tooling hook, which is synchronous by
// contract and never writes a file for a person.
+//
+// A CALL, not a definition: slides/src/save.ts shadows serializeFile with a
+// wrapper (it prunes unreferenced assets before handing off to the kernel —
+// #442), and `function serializeFile(` is that wrapper being declared, not
+// the plain serializer being reached for. The wrapper's own call goes to the
+// kernel under the alias `kernelSerializeFile`, which this pattern does not
+// match — so a NEW call to the plain path anywhere in these files still fails
+// here, which is the property this check exists for.
const callers = ['slides/src/editor/editor.ts', 'slides/src/main.ts', 'slides/src/save.ts', 'slides/src/autosave.ts', 'slides/src/present.ts']
- .filter((f) => /\bserializeFile\(/.test(mask(read(f))))
+ .filter((f) => /(?<!function )\bserializeFile\(/.test(mask(read(f))))
ok(callers.length === 1 && callers[0] === 'slides/src/main.ts',
`serializeFile() is called from ${callers.join(', ') || 'nowhere'} — and only there`)
ok(/serialize:\s*\(\)\s*=>\s*\{[^}]*\bserializeFile\(store\.doc\)/.test(read('slides/src/main.ts')),
diff --git a/scripts/test-slides-assets.ts b/scripts/test-slides-assets.ts
new file mode 100644
index 00000000..b0bc78be
--- /dev/null
+++ b/scripts/test-slides-assets.ts
@@ -0,0 +1,136 @@
+#!/usr/bin/env node
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+// Asset-pruning rig for bento/slides (#442).
+//
+// node scripts/test-slides-assets.ts (Node ≥ 23.6 strips types natively)
+//
+// WHAT THIS PROVES. A save writes `pruneUnusedAssets(doc)`, not `doc`. That
+// function must drop what nothing refers to and keep EVERYTHING that is
+// referred to, in every form the format has — and the two halves are not
+// equally easy to get wrong. Dropping too little is the old bug (a deck only
+// ever grew; the report was an empty deck of 20 MB). Dropping too much is a
+// worse one: an image that vanishes from a file on save, with nothing on
+// screen to say so. So the cases below are weighted toward "kept", one per
+// reference form, and each is asserted individually rather than as a count —
+// a count of six would pass if the wrong six survived.
+//
+// The prune is pure and runs in node, which is why this rig can exist. The
+// facade wiring (slides/src/save.ts) is one line per serializer and is checked
+// by reading the file, since exercising it needs a DOM.
+
+import { readFileSync } from 'node:fs'
+import { fileURLToPath } from 'node:url'
+import { dirname, join } from 'node:path'
+import { pruneUnusedAssets, referencedAssetKeys } from '../slides/src/assets.ts'
+import type { BentoDoc } from '../slides/src/model.ts'
+
+let failures = 0
+let checks = 0
+function ok(cond: boolean, msg: string) {
+ checks++
+ if (!cond) { failures++; console.log(` FAIL ${msg}`) }
+ else console.log(` ok ${msg}`)
+}
+
+const PNG = 'data:image/png;base64,iVBORw0KGgo='
+const el = (o: Record<string, unknown>) => ({ id: 'e', x: 0, y: 0, w: 10, h: 10, ...o })
+const doc = (o: Partial<BentoDoc>): BentoDoc =>
+ ({ format: 'bento/slides', version: 1, title: 't', slides: [], ...o } as unknown as BentoDoc)
+
+console.log('every reference form keeps its asset')
+{
+ const d = doc({
+ assets: {
+ 'img': PNG, 'poster': PNG, 'vid': PNG, 'svg': '<svg/>',
+ 'gram': '{}', 'thm': '{}', 'font': PNG, 'layout-img': PNG,
+ },
+ slides: [{ id: 's1', elements: [
+ el({ id: 'i', type: 'image', src: 'asset:img' }),
+ el({ id: 'm', type: 'media', kind: 'video', src: 'asset:vid', poster: 'asset:poster' }),
+ el({ id: 'v', type: 'svg', asset: 'svg' }),
+ el({ id: 'c', type: 'code', grammarAssetId: 'gram', themeAssetId: 'thm' }),
+ ] } as never],
+ layouts: [{ id: 'L', elements: [el({ id: 'li', type: 'image', src: 'asset:layout-img' })] } as never],
+ fonts: [{ family: 'F', asset: 'font' }],
+ })
+ const out = pruneUnusedAssets(d)
+ ok(out === d, 'nothing to drop → the SAME object comes back (no copy, no allocation)')
+ const used = referencedAssetKeys(d)
+ for (const k of ['img', 'poster', 'vid', 'svg', 'gram', 'thm', 'font', 'layout-img']) {
+ ok(used.has(k), `referenced: ${k}`)
+ }
+ ok(used.size === 8, `exactly the eight referenced keys, no strays (got ${used.size})`)
+}
+
+console.log('\nthe report: delete everything, save, and the deck is empty for real')
+{
+ const d = doc({
+ assets: { a: PNG, b: PNG, c: PNG },
+ slides: [{ id: 's1', elements: [el({ id: 'i', type: 'image', src: 'asset:a' })] } as never],
+ })
+ // the user deletes every slide ("Start from scratch…")
+ const emptied = { ...d, slides: [] }
+ const out = pruneUnusedAssets(emptied)
+ ok(out !== emptied, 'something dropped → a copy comes back, not the live object')
+ ok(out.assets === undefined, 'no assets remain, and the empty map is removed rather than left as {}')
+ ok(emptied.assets !== undefined && Object.keys(emptied.assets).length === 3, 'the LIVE document still has all three (undo needs them)')
+}
+
+console.log('\npartial: only the unreferenced ones go')
+{
+ const d = doc({
+ assets: { keep: PNG, drop1: PNG, drop2: '<svg/>' },
+ slides: [{ id: 's1', elements: [el({ id: 'i', type: 'image', src: 'asset:keep' })] } as never],
+ })
+ const out = pruneUnusedAssets(d)
+ ok(!!out.assets && 'keep' in out.assets, 'keep survives')
+ ok(!!out.assets && !('drop1' in out.assets) && !('drop2' in out.assets), 'drop1 and drop2 are gone')
+ ok(out.assets?.keep === PNG, 'the surviving value is byte-identical')
+ ok(out.slides === d.slides, 'slides array is shared, not deep-copied (the prune only touches assets)')
+}
+
+console.log('\nblobs follow assets')
+{
+ const d = doc({
+ assets: { keep: PNG, drop: PNG },
+ blobs: { keep: { key: 'k', mime: 'image/png', size: 9 }, drop: { key: 'd', mime: 'image/png', size: 9 } },
+ slides: [{ id: 's1', elements: [el({ id: 'i', type: 'image', src: 'asset:keep' })] } as never],
+ })
+ const out = pruneUnusedAssets(d)
+ ok(!!out.blobs && 'keep' in out.blobs && !('drop' in out.blobs), 'a blob ref for a dropped asset is dropped too')
+ const d2 = doc({ assets: { drop: PNG }, blobs: { drop: { key: 'd', mime: 'image/png', size: 9 } }, slides: [] })
+ ok(pruneUnusedAssets(d2).blobs === undefined, 'an emptied blobs map is removed, like assets')
+}
+
+console.log('\ninline data: URIs are not assets and are left alone')
+{
+ const d = doc({
+ assets: { orphan: PNG },
+ slides: [{ id: 's1', elements: [el({ id: 'i', type: 'image', src: PNG })] } as never],
+ })
+ const out = pruneUnusedAssets(d)
+ ok(out.assets === undefined, 'orphan dropped; the inline image was never an asset ref')
+ ok((out.slides[0] as { elements: Array<{ src: string }> }).elements[0].src === PNG, 'the inline src is untouched')
+}
+
+console.log('\na document with no assets map is handed back untouched')
+{
+ const d = doc({ slides: [] })
+ ok(pruneUnusedAssets(d) === d, 'no assets → same object')
+}
+
+console.log('\nthe facade actually routes through the prune')
+{
+ const here = dirname(fileURLToPath(import.meta.url))
+ const facade = readFileSync(join(here, '../slides/src/save.ts'), 'utf8')
+ ok(/export function serializeAuto\([^)]*\)[^{]*\{[^}]*pruneUnusedAssets/.test(facade),
+ 'serializeAuto is shadowed and calls pruneUnusedAssets')
+ ok(/export function serializeFile\([^)]*\)[^{]*\{[^}]*pruneUnusedAssets/.test(facade),
+ 'serializeFile is shadowed and calls pruneUnusedAssets')
+ ok(/export \* from '\.\.\/\.\.\/kernel\/src\/save\.ts'/.test(facade),
+ 'everything else still comes from the kernel')
+}
+
+console.log(`\n${checks - failures}/${checks} checks passed`)
+if (failures) process.exit(1)
diff --git a/slides/src/assets.ts b/slides/src/assets.ts
new file mode 100644
index 00000000..9fca372b
--- /dev/null
+++ b/slides/src/assets.ts
@@ -0,0 +1,88 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+// Save-time pruning of doc.assets. Pure — no DOM — so the rig runs in node.
+
+import type { BentoDoc, Slide } from './model'
+
+/**
+ * Every asset key the document still refers to, in every form the format has.
+ *
+ * The forms are deliberately enumerated rather than found by a generic walk,
+ * because the format uses TWO conventions and a walk that knows only one
+ * would treat the other as unreferenced and delete it: `image.src` and
+ * `media.src`/`media.poster` say `asset:<key>`; `svg.asset`,
+ * `code.grammarAssetId`, `code.themeAssetId` and `fonts[].asset` hold the
+ * bare key. A new field that references an asset must be added HERE, or the
+ * asset it names is dropped from the file on the next save. That is the cost
+ * of this function existing, and the rig pins every form so a regression
+ * shows as a failed check rather than a missing image.
+ *
+ * Layouts are Slide-shaped and can hold assets a normal slide does not, so
+ * they are walked too. Interactive states and hover sets are ordinary slides
+ * and elements and need nothing special.
+ */
+export function referencedAssetKeys(doc: BentoDoc): Set<string> {
+ const used = new Set<string>()
+ const prefixed = (v: string | undefined) => { if (v?.startsWith('asset:')) used.add(v.slice(6)) }
+ const bare = (v: string | undefined) => { if (v) used.add(v) }
+ const walk = (slide: Slide) => {
+ for (const el of slide.elements) {
+ switch (el.type) {
+ case 'image': prefixed(el.src); break
+ case 'media': prefixed(el.src); prefixed(el.poster); break
+ case 'svg': bare(el.asset); break
+ case 'code': bare(el.grammarAssetId); bare(el.themeAssetId); break
+ }
+ }
+ }
+ doc.slides.forEach(walk)
+ doc.layouts?.forEach(walk)
+ doc.fonts?.forEach((f) => bare(f.asset))
+ return used
+}
+
+/**
+ * A copy of `doc` with every asset nothing refers to removed — what a SAVE
+ * should write. The live document is never touched: undo history in memory
+ * still holds the deleted element and the asset it pointed at, so ⌘Z after a
+ * save brings both back, and the following save keeps the asset because it is
+ * referenced again.
+ *
+ * Why this exists (#442): `doc.assets` was append-only. Add images, delete
+ * every slide, save — and the "empty" deck was still 20 MB, because deleting
+ * an element removes the reference and nothing ever removed the bytes. A deck
+ * could only ever grow.
+ *
+ * `blobs` (docs/blob-offload.md) is pruned to the same key set: a blob entry
+ * for an asset nothing references is a pointer to bytes nobody will fetch.
+ *
+ * Returns the SAME object when there is nothing to drop, so a caller can tell
+ * an untouched document from a copied one, and so the common case costs a
+ * walk and no allocation.
+ *
+ * Under live collaboration the CRDT syncs `assets` per key, so pruning the
+ * saved copy cannot lose a peer their asset — their replica keeps it. The one
+ * gap: a file that pruned key K, was reopened, and then receives a peer's op
+ * re-referencing K (an undo on the peer, say) will show that one image broken
+ * on that replica only, until it is re-added. Rare, non-corrupting, and the
+ * trade against every deck growing without bound is not close.
+ */
+export function pruneUnusedAssets(doc: BentoDoc): BentoDoc {
+ if (!doc.assets) return doc
+ const used = referencedAssetKeys(doc)
+ const assets: Record<string, string> = {}
+ let dropped = 0
+ for (const [key, value] of Object.entries(doc.assets)) {
+ if (used.has(key)) assets[key] = value
+ else dropped++
+ }
+ if (!dropped) return doc
+ const out: BentoDoc = { ...doc, assets }
+ if (Object.keys(assets).length === 0) delete out.assets
+ if (doc.blobs) {
+ const blobs = Object.fromEntries(Object.entries(doc.blobs).filter(([key]) => used.has(key)))
+ if (Object.keys(blobs).length) out.blobs = blobs
+ else delete out.blobs
+ }
+ return out
+}
diff --git a/slides/src/save.ts b/slides/src/save.ts
index aa230298..458d56b8 100644
--- a/slides/src/save.ts
+++ b/slides/src/save.ts
@@ -3,4 +3,27 @@
// Facade: self-save + bento/enc encryption live in the shared kernel now
// (kernel/src/save.ts). This path stays alive so slides code keeps importing
// './save' unchanged — see docs/PLATFORM.md §9.
+//
+// It is also where slides shapes what it hands the kernel. The two
+// serializers below are shadowed — a local export wins over `export *` — so
+// every path that writes a .bento.html from this app (⌘S write-back, the
+// download fallback, the four export copies, and window.bento.serialize())
+// first drops the assets nothing refers to. The kernel never learns the slides
+// document shape; it is handed a document with fewer bytes in it. See
+// pruneUnusedAssets for why, and for the live document being untouched.
export * from '../../kernel/src/save.ts'
+
+import { serializeAuto as kernelSerializeAuto, serializeFile as kernelSerializeFile } from '../../kernel/src/save.ts'
+import type { BentoDoc } from './model'
+import { pruneUnusedAssets } from './assets'
+
+/** Encryption-aware serializer, with unreferenced assets pruned from the copy it writes. */
+export function serializeAuto(doc: BentoDoc): Promise<string> {
+ return kernelSerializeAuto(pruneUnusedAssets(doc))
+}
+
+/** Plain serializer (tooling, window.bento.serialize), pruned the same way so
+ * a script sees the file a save would produce rather than a larger one. */
+export function serializeFile(doc: BentoDoc): string {
+ return kernelSerializeFile(pruneUnusedAssets(doc))
+}