Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
kernel: offload the largest pictures once a deck's inline total passes 256 KB (#552)
nyblnet2e5b4e589da4
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 5d4dba16..25e32866 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -11,6 +11,13 @@ pre-1.0.## [Unreleased]+- **A deck with lots of small images can be shared live.** Sharing sends big+ pictures separately from the document, but only ones over about 64 KB — so a+ deck with many smaller images (a wall of icons, say) could still add up to+ more than a live session's frame could carry. Those now travel separately+ too, largest first, once the pictures kept in the document pass a+ quarter-megabyte together.+- **A document in Downloads or Documents remembers where it lives.** Chromewill not let any app hold a standing grant to those folders, so a deck keptthere used to show the full save dialog on every reopen. Bento now remembersdiff --git a/kernel/src/sync/session.ts b/kernel/src/sync/session.tsindex 687a3ec3..e49f29fc 100644--- a/kernel/src/sync/session.ts+++ b/kernel/src/sync/session.ts@@ -325,6 +325,41 @@ const HEARTBEAT_MS = 5000// `away` so the UI can dim rather than wait.const PEER_TTL_MS = 75000+/** Offload once the inline assets TOTAL runs past this, even if no single one is+ * over BLOB_INLINE_MAX — the "fifty 50 KB icons" deck whose sum overflows a+ * frame. Below the relay MAX_FRAME with room for the document and its state. */+const INLINE_TOTAL_MAX = 256 * 1024++/**+ * Which inline assets to offload to blobs. Pure, so it is tested without a relay+ * (session.offloadAssets does the upload). Two rules:+ * · per-asset — anything over `inlineMax` (too big to ride in an op);+ * · cumulative — when the inline TOTAL is over `totalMax`, the LARGEST inline+ * assets, biggest first, until the remaining inline bytes are back under it.+ * `offloadable` is false for a raw-SVG asset (not a data: URI) — it stays inline+ * because there is nothing to blob; it still counts toward the total.+ */+export function assetsToOffload(+ entries: Array<{ key: string; len: number; offloadable: boolean }>,+ inlineMax: number,+ totalMax: number,+): Set<string> {+ const picks = new Set<string>()+ let total = 0+ for (const e of entries) total += e.len+ for (const e of entries) if (e.offloadable && e.len > inlineMax) picks.add(e.key)+ if (total > totalMax) {+ let rem = total+ for (const e of entries) if (picks.has(e.key)) rem -= e.len+ for (const e of entries.filter((e) => !picks.has(e.key) && e.offloadable).sort((a, b) => b.len - a.len)) {+ if (rem <= totalMax) break+ picks.add(e.key)+ rem -= e.len+ }+ }+ return picks+}+export class SyncSession {readonly actor: stringstate!: SyncEngine@@ -482,6 +517,12 @@ export class SyncSession {* IS small enough to sync. Runs after a local edit; the reference lands on* the next flush like any other change.*+ * TWO rules pick what to offload — see assetsToOffload: anything over+ * BLOB_INLINE_MAX (too big for an op), AND, when the inline TOTAL runs past+ * INLINE_TOTAL_MAX, the largest inline assets until it is back under, so a+ * deck of fifty 50 KB icons (none over 64 KB alone) does not sum past the+ * snapshot/frame ceiling.+ ** The bytes are always cached locally even when the upload fails, because* the cache is per-ORIGIN: same-machine tabs syncing over BroadcastChannel* resolve from it with no relay involved at all.@@ -489,12 +530,15 @@ export class SyncSession {private async offloadAssets() {const doc = this.store.docconst assets = doc.assets ?? {}+ const entries = Object.entries(assets)+ .filter(([k, v]) => typeof v === 'string' && !doc.blobs?.[k])+ .map(([k, v]) => ({ key: k, len: (v as string).length, offloadable: dataUriToBytes(v as string) !== null }))+ const picks = assetsToOffload(entries, BLOB_INLINE_MAX, INLINE_TOTAL_MAX)+ if (!picks.size) returnconst creds = this.blobCreds()- for (const [k, v] of Object.entries(assets)) {- if (typeof v !== 'string' || v.length <= BLOB_INLINE_MAX) continue- if (doc.blobs?.[k]) continue // already published- const parsed = dataUriToBytes(v)- if (!parsed) continue // raw SVG markup, not binary — leave it inline+ for (const k of picks) {+ const parsed = dataUriToBytes(assets[k] as string)+ if (!parsed) continue // offloadable was true, but be defensiveif (encodedSize(parsed.bytes.length) > MAX_BLOB) {this.notify('blob-too-large', k)continuediff --git a/scripts/test-sync-session.ts b/scripts/test-sync-session.tsindex 8fc9d01e..e7829f69 100644--- a/scripts/test-sync-session.ts+++ b/scripts/test-sync-session.ts@@ -54,7 +54,7 @@ const { register } = await import('node:module');register('./lib/ts-resolve-hooks.mjs', import.meta.url);const { Store } = await import('../slides/src/store.ts');-const { SyncSession } = await import('../slides/src/sync/session.ts');+const { SyncSession, assetsToOffload } = await import('../slides/src/sync/session.ts');const { newDoc, emptySlide } = await import('../slides/src/model.ts');const { SYNC_V } = await import('../kernel/src/sync/crdt.ts');@@ -382,6 +382,29 @@ H('presence: a peer last seen 60s ago survives; 80s is swept');}}+// ---- cumulative offload: the inline total, not just per-asset size ---------+H('cumulative offload picks the largest inline assets when the total overflows');+{+ const K = 1024;+ // fifty 50 KB icons — none over 64 KB, but 2.5 MB inline together+ const icons = Array.from({ length: 50 }, (_, i) => ({ key: `i${i}`, len: 50 * K, offloadable: true }));+ const picks = assetsToOffload(icons, 64 * K, 256 * K);+ const left = icons.filter(e => !picks.has(e.key)).reduce((n, e) => n + e.len, 0);+ ok(picks.size > 0 && left <= 256 * K,+ `fifty 50 KB icons: enough offloaded to get inline under 256 KB (${picks.size} offloaded, ${(left / K) | 0} KB left)`);+ // per-asset rule still applies; small assets under the total stay inline+ const mixed = [{ key: 'big', len: 100 * K, offloadable: true }, { key: 's1', len: 10 * K, offloadable: true }, { key: 's2', len: 10 * K, offloadable: true }];+ const p2 = assetsToOffload(mixed, 64 * K, 256 * K);+ ok(p2.has('big') && !p2.has('s1') && !p2.has('s2'), 'one over-64 KB asset offloads; small ones under the total stay inline');+ // a deck under the total keeps everything inline — no needless offload+ const small = Array.from({ length: 4 }, (_, i) => ({ key: `s${i}`, len: 40 * K, offloadable: true }));+ ok(assetsToOffload(small, 64 * K, 256 * K).size === 0, 'a deck under the total keeps its assets inline');+ // a raw-SVG asset (not offloadable) is never picked, even when it pushes the total over+ const withSvg = [{ key: 'svg', len: 300 * K, offloadable: false }, { key: 'png', len: 200 * K, offloadable: true }];+ const p4 = assetsToOffload(withSvg, 64 * K, 256 * K);+ ok(!p4.has('svg') && p4.has('png'), 'a raw-SVG asset stays inline (nothing to blob); the offloadable one goes');+}+console.log(`\n${checks - failures}/${checks} checks passed`);// BroadcastChannel and the heartbeat keep node's event loop aliveprocess.exit(failures ? 1 : 0);