Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

Self-update carries language packs forward — and refreshes them

nyblnetfaa6321314c2
diff --git a/kernel/src/update.ts b/kernel/src/update.ts
index d75dc850..2eb77483 100644
--- a/kernel/src/update.ts
+++ b/kernel/src/update.ts
@@ -160,6 +160,21 @@ export async function checkForUpdates(manifestUrl?: string): Promise<UpdateCheck
* Fetch the release shell, verify its hash against the signed manifest, and
* return the full updated .bento.html: this document inside the new app.
*/
+/**
+ * Run by buildUpdatedFile once the new version is known and verified, BEFORE
+ * the document is serialized into it. Lets an app bring version-bound extras
+ * up to date — language packs are the case this exists for.
+ *
+ * Kept as a hook rather than baked in because the kernel must not learn what
+ * a language pack is; it only knows the app may want a moment before the new
+ * shell is written.
+ */
+let prepareUpdate: ((version: string) => Promise<void>) | null = null
+
+export function registerUpdatePrepare(fn: (version: string) => Promise<void>): void {
+ prepareUpdate = fn
+}
+
export async function buildUpdatedFile(release: ReleaseInfo, doc: KernelDoc): Promise<string> {
const res = await fetch(release.url, { cache: 'no-store' })
if (!res.ok) throw new Error(`downloading the update failed (${res.status})`)
@@ -171,6 +186,20 @@ export async function buildUpdatedFile(release: ReleaseInfo, doc: KernelDoc): Pr
const shell = new DOMParser().parseFromString(new TextDecoder().decode(bytes), 'text/html')
if (!shell.getElementById('bento-doc'))
throw new Error('the downloaded update is not a Bento shell')
+
+ // BEST EFFORT, never fatal. If refreshing throws — offline, the new
+ // version's packs not published yet, anything — the update must still
+ // proceed and carry the EXISTING packs forward. Losing a language the
+ // author baked in is far worse than one that is a release out of date,
+ // because a stale pack still degrades per string while a missing one
+ // takes the whole language with it.
+ if (prepareUpdate) {
+ try {
+ await prepareUpdate(release.version)
+ } catch {
+ /* keep whatever the file already carries */
+ }
+ }
return serializeDocInto(shell, doc)
}
diff --git a/slides/src/i18n.ts b/slides/src/i18n.ts
index d0704d95..ccfe3c74 100644
--- a/slides/src/i18n.ts
+++ b/slides/src/i18n.ts
@@ -18,8 +18,9 @@
// node scripts/build-i18n.mjs
//
import { PACKED, PACKED_LOCALES } from './i18n/packed'
-import { readPacksFromShell, shellBlocksForPacks } from './packs'
+import { readPacksFromShell, refreshPacksForVersion, shellBlocksForPacks } from './packs'
import { registerShellBlocks } from '../../kernel/src/save.ts'
+import { registerUpdatePrepare } from '../../kernel/src/update.ts'
import { registerI18n } from '../../kernel/src/i18n.ts'
import type { LocaleChoice } from '../../kernel/src/i18n.ts'
@@ -66,6 +67,12 @@ readPacksFromShell()
// it travel and dropping it is enough to remove it.
registerShellBlocks(shellBlocksForPacks)
+// A self-update fetches a NEW shell and re-splices this document into it. The
+// block provider above already carries the file's packs across; this also
+// brings them UP TO the new version, so a translated deck doesn't drift back
+// toward English one release at a time. Best effort — see refreshPacksForVersion.
+registerUpdatePrepare(async (version) => { await refreshPacksForVersion(version) })
+
/**
* The BUNDLED locales only. Call sites that need the live list — including any
* language an installed pack added — must use `localeChoices()` instead: this
diff --git a/slides/src/packs.ts b/slides/src/packs.ts
index 7d6ef07c..ba6f9491 100644
--- a/slides/src/packs.ts
+++ b/slides/src/packs.ts
@@ -132,19 +132,60 @@ export function shellBlocksForPacks(): ShellBlock[] {
}))
}
-/**
- * Packs on offer from the release channel, minus those the file already has.
- * Returns [] when the index isn't published yet or the network is unavailable
- * — "no packs to offer" is a normal state, not an error worth shouting about.
- */
-export async function availablePacks(): Promise<PackListing[]> {
+/** The channel's whole index, unfiltered. [] if unpublished or unreachable. */
+async function fetchIndex(): Promise<PackListing[]> {
try {
const res = await fetch(`${channel()}/packs.json`, { cache: 'no-store' })
if (!res.ok) return []
const list = (await res.json()) as PackListing[]
- if (!Array.isArray(list)) return []
- return list.filter((p) => p?.lang && p?.url && !inFile.has(p.lang))
+ return Array.isArray(list) ? list.filter((p) => p?.lang && p?.url) : []
} catch {
return []
}
}
+
+/**
+ * Packs on offer from the release channel, minus those the file already has.
+ * "Nothing to offer" is a normal state, not an error worth shouting about.
+ */
+export async function availablePacks(): Promise<PackListing[]> {
+ return (await fetchIndex()).filter((p) => !inFile.has(p.lang))
+}
+
+/**
+ * Bring this file's packs up to a new app version, called from the update
+ * flow once the release is verified.
+ *
+ * WHY THIS EXISTS. A pack is written into the file at the version it was
+ * built for and then never changes, while the app around it keeps updating.
+ * Every release that adds UI strings makes that pack a little more
+ * incomplete — the new strings fall back to English, per string, silently.
+ * Over a few releases a fully translated deck drifts back toward English
+ * with nothing to fix it. Update is the one moment we know the version
+ * changed, and the packs are on the same signed channel, so it is also the
+ * cheapest moment to put them right.
+ *
+ * Refusing to fail is the whole point: any pack we cannot re-fetch is simply
+ * KEPT at its current version. Degraded beats absent.
+ */
+export async function refreshPacksForVersion(version: string): Promise<{ refreshed: string[]; kept: string[] }> {
+ const refreshed: string[] = []
+ const kept: string[] = []
+ const langs = [...inFile.keys()]
+ if (!langs.length) return { refreshed, kept }
+
+ const index = await fetchIndex()
+ for (const lang of langs) {
+ const listing = index.find((p) => p.lang === lang)
+ if (!listing) { kept.push(lang); continue }
+ const got = await fetchPack(listing)
+ if (typeof got === 'string' || got.lang !== lang) { kept.push(lang); continue }
+ // Only replace with something actually built for the incoming version —
+ // a channel still serving the old pack should leave the file untouched.
+ if (got.version && version && got.version !== version) { kept.push(lang); continue }
+ inFile.set(lang, got)
+ addPack(got, 'slides')
+ refreshed.push(lang)
+ }
+ return { refreshed, kept }
+}