feat(waiter): native app runs each venue's own UI bundle (self-hosted updater)

@capgo/capacitor-updater (MPL-2.0) in manual mode: autoUpdate off,
stats/update/channel URLs empty (nothing contacts Capgo), appReadyTimeout
15s (auto rollback), resetWhenUpdate (APK update → built-in bundle, venue
bundle re-fetched), autoDeletePrevious off (we keep one per venue).

src/native/bundles.js syncVenueBundle():
- manifest fetched over the pinned TLS link; zip downloaded natively over
  the LAN HTTP port with the MANDATORY sha256 check - a tampered zip never runs
- applied at once only when nobody is mid-service (after pairing / venue
  switch, or no one logged in); otherwise staged with next() → applied at the
  next background/restart; never a mid-service reload
- APK too old for the bundle (min_shell_build) → skipped + update banner
  linking to the venue's /downloads/xenia-waiter.apk
- a rolled-back version is blocklisted (attempt tracking + grace window) -
  without it the app re-applied the still-advertised broken bundle in a loop
- server without bundles (404) → built-in UI; bundles no venue needs deleted
BundleSync: notifyAppReady on start; sync at start, on resume, every 30 min.
saveVenue merges fields (bundleVersion); switchVenue flags immediate apply.

E2E (emulator vs two isolated venue stacks): fresh pairing applies the venue
bundle and it stays healthy; logged-in update staged, applied after
background, session kept; tampered zip refused; shell-too-old banner;
broken bundle rolled back automatically and not retried (80s + resume);
two venues each run their own bundle, round trips served from cache with 0
downloads, stale bundles cleaned; no *.capgo.app traffic. Step 5/7-era
rediscovery + cold-start suites and web modes still pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-28 18:24:48 +03:00
co-authored by Claude Opus 5.5
parent a1e415ac05
commit fe264f0a23
9 changed files with 320 additions and 4 deletions
+201
View File
@@ -0,0 +1,201 @@
/**
* Per-venue UI bundles (native app, plan step 8).
*
* Every venue server builds this UI for the app and publishes it as
* /downloads/waiter-bundle.json (manifest) + waiter-bundle-<hash>.zip
* (scripts/pack-bundle.mjs). The app runs the bundle of the venue it's paired
* with, so a server update reaches the phones with no APK to hand out, and each
* venue's phones always match that venue's server.
*
* Engine: @capgo/capacitor-updater in manual mode (capacitor.config.json):
* autoUpdate off, stats/update/channel URLs empty — nothing talks to Capgo;
* appReadyTimeout 15s — a bundle that doesn't call notifyAppReady() is rolled
* back automatically; resetWhenUpdate — an APK update returns to the built-in
* bundle and the venue's bundle is fetched again; autoDeletePrevious off —
* several venues can each need a bundle, cleanup() handles old ones.
*
* Integrity: the manifest is fetched through the WebView over the pinned TLS
* link (step 7). The zip is downloaded natively over the LAN's plain-HTTP port
* (the native downloader can't use the WebView's pin) and the updater rejects it
* unless its SHA-256 equals the manifest's — so a tampered zip never runs.
*
* When: a new bundle is applied at once only when nobody is mid-service (right
* after pairing / a venue switch, or nobody logged in). Otherwise it is staged
* and applied the next time the app goes to the background or restarts.
*
* Failed bundles: if a bundle is rolled back (it never reported ready), the
* server still advertises it — retrying would loop forever. Every activation is
* recorded; finding ourselves on another version later (and it isn't merely
* queued for the next background) means it failed, and that version is never
* tried again. A newer server version is tried normally.
*/
import { CapacitorUpdater } from '@capgo/capacitor-updater'
import { App } from '@capacitor/app'
import { APPLY_BUNDLE_NOW_KEY, getActiveVenue, listVenues, saveVenue, storageKey } from '../config/server'
const MANIFEST_TIMEOUT_MS = 8000
const ATTEMPT_KEY = 'xenia.bundleAttempt' // { version, at } of the last activation
const FAILED_KEY = 'xenia.bundleFailed' // versions that were rolled back
const ATTEMPT_GRACE_MS = 60_000 // > the updater's 15-30s ready window
function readJson(key, fallback) {
try {
return JSON.parse(localStorage.getItem(key) || 'null') ?? fallback
} catch {
return fallback
}
}
function writeJson(key, value) {
try {
localStorage.setItem(key, JSON.stringify(value))
} catch {
// storage unavailable — worst case a failed bundle is retried once more
}
}
const isFailed = version => readJson(FAILED_KEY, []).includes(version)
function markFailed(version) {
writeJson(FAILED_KEY, [...readJson(FAILED_KEY, []).filter(v => v !== version), version].slice(-20))
console.warn(`[bundles] ${version} was rolled back — not retrying it`)
}
/** Tell the updater this bundle started fine (otherwise it rolls back after 15s). */
export async function markBundleReady() {
try {
await CapacitorUpdater.notifyAppReady()
} catch (e) {
console.warn('[bundles] notifyAppReady failed', e)
}
}
async function fetchJson(url) {
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), MANIFEST_TIMEOUT_MS)
try {
const res = await fetch(url, { signal: controller.signal, cache: 'no-store' })
return { status: res.status, body: res.ok ? await res.json() : null }
} catch {
return { status: 0, body: null } // unreachable — keep whatever runs now
} finally {
clearTimeout(timer)
}
}
function validManifest(m) {
return m?.format === 1 && typeof m.version === 'string' && /^[\w.+-]+\.zip$/.test(m.file || '')
&& /^[0-9a-f]{64}$/.test(m.sha256 || '') && Number.isInteger(m.min_shell_build)
}
/** Plain-HTTP origin of the venue for the native zip download. */
async function httpOrigin(venue) {
const url = new URL(venue.baseUrl)
if (url.protocol === 'http:') return url.origin
const { body } = await fetchJson(`${venue.baseUrl}/api/system/identity`)
const port = Number(body?.http_port) || 80
return `http://${url.hostname}${port === 80 ? '' : `:${port}`}`
}
function applyNowAllowed() {
try {
if (sessionStorage.getItem(APPLY_BUNDLE_NOW_KEY)) return true
return !localStorage.getItem(storageKey('token')) // nobody logged in at this venue
} catch {
return false
}
}
/** Delete downloaded bundles no paired venue needs any more. */
async function cleanup(keepIds) {
const needed = new Set(listVenues().map(v => v.bundleVersion).filter(Boolean))
const { bundles } = await CapacitorUpdater.list()
for (const b of bundles) {
if (b.id === 'builtin' || keepIds.includes(b.id) || needed.has(b.version)) continue
try {
await CapacitorUpdater.delete({ id: b.id })
} catch {
// current/next bundles can't be deleted — fine
}
}
}
let running = null
/**
* Make the active venue's UI bundle the one this app runs.
* Resolves to { status, version? } — status is one of:
* no-venue | unreachable | no-bundle | shell-too-old | current | applied | staged
* | pending (just activated, ready window open) | failed (download/checksum)
* | failed-before (was rolled back, not retried)
*/
export function syncVenueBundle() {
if (!running) running = doSync().finally(() => { running = null })
return running
}
async function doSync() {
const venue = getActiveVenue()
if (!venue) return { status: 'no-venue' }
const { status, body: manifest } = await fetchJson(`${venue.baseUrl}/downloads/waiter-bundle.json`)
if (status === 0) return { status: 'unreachable' }
const { bundle: current } = await CapacitorUpdater.current()
if (!validManifest(manifest)) {
// Server predates per-venue bundles: run the app's own built-in UI
if (status === 404 && current?.id !== 'builtin' && applyNowAllowed()) {
sessionStorage.removeItem(APPLY_BUNDLE_NOW_KEY)
await CapacitorUpdater.reset()
}
return { status: 'no-bundle' }
}
const { build } = await App.getInfo()
if (Number(build) < manifest.min_shell_build) {
return { status: 'shell-too-old', version: manifest.version, apk: `${await httpOrigin(venue)}/downloads/xenia-waiter.apk` }
}
if (current?.version === manifest.version) {
if (venue.bundleVersion !== manifest.version) saveVenue({ ...venue, bundleVersion: manifest.version })
sessionStorage.removeItem(APPLY_BUNDLE_NOW_KEY)
localStorage.removeItem(ATTEMPT_KEY) // it started fine
return { status: 'current', version: manifest.version }
}
// Already activated this version, yet we're running something else:
// still queued for the next background → wait; activated moments ago → wait
// (never re-activate inside the ready window); otherwise it was rolled back.
const attempt = readJson(ATTEMPT_KEY, null)
if (attempt?.version === manifest.version) {
const queued = await CapacitorUpdater.getNextBundle().catch(() => null)
if (queued?.version === manifest.version) return { status: 'staged', version: manifest.version }
if (Date.now() - attempt.at < ATTEMPT_GRACE_MS) return { status: 'pending', version: manifest.version }
markFailed(manifest.version)
localStorage.removeItem(ATTEMPT_KEY)
}
if (isFailed(manifest.version)) return { status: 'failed-before', version: manifest.version }
try {
const { bundles } = await CapacitorUpdater.list()
let target = bundles.find(b => b.version === manifest.version && b.status !== 'error')
if (!target) {
target = await CapacitorUpdater.download({
url: `${await httpOrigin(venue)}/downloads/${manifest.file}`,
version: manifest.version,
checksum: manifest.sha256,
})
}
saveVenue({ ...venue, bundleVersion: manifest.version })
await cleanup([target.id, current?.id])
writeJson(ATTEMPT_KEY, { version: manifest.version, at: Date.now() })
if (applyNowAllowed()) {
sessionStorage.removeItem(APPLY_BUNDLE_NOW_KEY)
await CapacitorUpdater.set({ id: target.id }) // reloads into the new bundle
return { status: 'applied', version: manifest.version }
}
await CapacitorUpdater.next({ id: target.id }) // applied at next background/restart
return { status: 'staged', version: manifest.version }
} catch (e) {
console.warn('[bundles] update failed', e)
return { status: 'failed', version: manifest.version }
}
}