Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

fix: prune orphaned assets on normal saves (#476)

nyblnet3508954a23cb
diff --git a/docs/DECISIONS.md b/docs/DECISIONS.md
index eb0d4d68..14f78015 100644
--- a/docs/DECISIONS.md
+++ b/docs/DECISIONS.md
@@ -6925,3 +6925,17 @@ have caught this: not "did the attribute survive" but "did anything execute".
correct options for its children: drop them with it, or sanitize them before
lifting them. Lifting first is always wrong, whatever the loop looks like.
And every attack case in a sanitizer rig gets a nested variant.
+
+## 2026-09-14 — Asset pruning must wrap the app's save entry point
+
+Issue #442's 1.1.0 follow-up exposed a facade trap. `slides/src/save.ts` had
+shadowed the kernel serializers, but its re-exported `saveFile` called the
+kernel's lexically-bound `serializeAuto`, so ordinary ⌘S bypassed slides'
+asset pruning. The result was a deck that still retained a deleted image's
+bytes until a different write path happened to serialize it.
+
+The decision is that every app-facing write entry point must hand the kernel a
+prepared document. Slides now shares `prepareForSave` across `saveFile`,
+`serializeAuto`, and `serializeFile`; the preparation remains copy-on-write so
+the live document keeps its assets for undo. `scripts/test-slides-assets.ts`
+checks the facade wiring as well as the pure reachability cases.
diff --git a/scripts/test-slides-assets.ts b/scripts/test-slides-assets.ts
index f91a0de7..9595cb20 100644
--- a/scripts/test-slides-assets.ts
+++ b/scripts/test-slides-assets.ts
@@ -16,8 +16,8 @@
// 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.
+// facade wiring (slides/src/save.ts) is checked by reading the file, since
+// exercising it needs a DOM.
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
@@ -132,10 +132,16 @@ 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(/function prepareForSave[\s\S]*pruneUnusedAssets\(adoptBuiltinFonts\(doc\)\)/.test(facade),
+ 'save preparation prunes assets and adopts fonts')
+ ok(/export function serializeAuto\([^)]*\)[\s\S]*prepareForSave\(doc\)/.test(facade),
+ 'serializeAuto uses save preparation')
+ ok(/export function serializeFile\([^)]*\)[\s\S]*prepareForSave\(doc\)/.test(facade),
+ 'serializeFile uses save preparation')
+ ok(/saveFile as kernelSaveFile/.test(facade),
+ 'slides aliases the kernel saveFile')
+ ok(/export function saveFile\([^)]*\)[\s\S]*kernelSaveFile\(prepareForSave\(doc\), forcePicker\)/.test(facade),
+ 'saveFile uses save preparation')
ok(/export \* from '\.\.\/\.\.\/kernel\/src\/save\.ts'/.test(facade),
'everything else still comes from the kernel')
}
diff --git a/scripts/test-slides-fonts.ts b/scripts/test-slides-fonts.ts
index e9082f80..01450274 100644
--- a/scripts/test-slides-fonts.ts
+++ b/scripts/test-slides-fonts.ts
@@ -102,8 +102,11 @@ ok(sizeBefore - sizeAfter > 80_000, `a deck embedding both faces shrinks by ${si
console.log('\nthe facade\n')
const root = join(dirname(fileURLToPath(import.meta.url)), '..')
const save = readFileSync(join(root, 'slides/src/save.ts'), 'utf8')
-ok(/kernelSerializeAuto\(pruneUnusedAssets\(adoptBuiltinFonts\(doc\)\)\)/.test(save) && /kernelSerializeFile\(pruneUnusedAssets\(adoptBuiltinFonts\(doc\)\)\)/.test(save),
- 'save.ts: both serializers adopt built-ins before pruning — every write from this app goes through it')
+ok(/function prepareForSave[\s\S]*pruneUnusedAssets\(adoptBuiltinFonts\(doc\)\)/.test(save) &&
+ /kernelSerializeAuto\(prepareForSave\(doc\)\)/.test(save) &&
+ /kernelSerializeFile\(prepareForSave\(doc\)\)/.test(save) &&
+ /kernelSaveFile\(prepareForSave\(doc\), forcePicker\)/.test(save),
+ 'save.ts: save entry point and serializers share built-in-font adoption and asset pruning')
ok(/resolveFontSrc\(doc, f\.asset\)/.test(readFileSync(join(root, 'slides/src/fonts.ts'), 'utf8')), 'injectFonts resolves through resolveFontSrc')
console.log(`\n${checks - failures}/${checks} checks passed`)
diff --git a/slides/src/save.ts b/slides/src/save.ts
index c7f218eb..a4df648c 100644
--- a/slides/src/save.ts
+++ b/slides/src/save.ts
@@ -4,27 +4,41 @@
// (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())
+// It is also where slides shapes what it hands the kernel. The save entry
+// point and 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 {
+ saveFile as kernelSaveFile,
+ serializeAuto as kernelSerializeAuto,
+ serializeFile as kernelSerializeFile,
+} from '../../kernel/src/save.ts'
import type { BentoDoc } from './model'
import { pruneUnusedAssets } from './assets'
import { adoptBuiltinFonts } from './fonts'
+function prepareForSave(doc: BentoDoc): BentoDoc {
+ return pruneUnusedAssets(adoptBuiltinFonts(doc))
+}
+
/** Encryption-aware serializer, with unreferenced assets pruned from the copy it writes. */
export function serializeAuto(doc: BentoDoc): Promise<string> {
- return kernelSerializeAuto(pruneUnusedAssets(adoptBuiltinFonts(doc)))
+ return kernelSerializeAuto(prepareForSave(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(adoptBuiltinFonts(doc)))
+ return kernelSerializeFile(prepareForSave(doc))
+}
+
+/** Save through the kernel after applying the same document-only transforms. */
+export function saveFile(doc: BentoDoc, forcePicker = false) {
+ return kernelSaveFile(prepareForSave(doc), forcePicker)
}