feat(backup): add verified offline migration
This commit is contained in:
1 parent
cc9be25d2d
commit
3c81c3dca3
11 files changed
+174
-21
No files matched your search
+5
-5
@@ -7,13 +7,13 @@ const meta = new URL("../dist/.openai/", import.meta.url);
|
||||
|
||||
await rm(dist, { recursive: true, force: true });
|
||||
await Promise.all([mkdir(server, { recursive: true }), mkdir(meta, { recursive: true })]);
|
||||
const [rawIndexHtml, addonsJs, bootGuardJs, localDbJs, localDbWorkerJs, sqliteModuleJs, sqliteWasm, i18nJs, tasksJs, calendarJs, notificationsJs, i18nRenderersJs, desktopPersonalizationJs, desktopWidgetsJs, systemNavigationJs, browserAppJs, dataActionsJs, terminalAppJs, dbAdminJs, manifest, serviceWorker, icon, localeEn, localeZh] = await Promise.all([
|
||||
text("index.html"), text("addons.js"), text("boot-guard.js"), text("local-db.js"), text("local-db-worker.js"),
|
||||
const [rawIndexHtml, addonsJs, backupJs, bootGuardJs, localDbJs, localDbWorkerJs, sqliteModuleJs, sqliteWasm, i18nJs, tasksJs, calendarJs, notificationsJs, i18nRenderersJs, desktopPersonalizationJs, desktopWidgetsJs, systemNavigationJs, browserAppJs, dataActionsJs, terminalAppJs, dbAdminJs, manifest, serviceWorker, icon, localeEn, localeZh] = await Promise.all([
|
||||
text("index.html"), text("addons.js"), text("backup.js"), text("boot-guard.js"), text("local-db.js"), text("local-db-worker.js"),
|
||||
readFile(new URL("../node_modules/@sqlite.org/sqlite-wasm/dist/index.mjs", import.meta.url), "utf8"),
|
||||
readFile(new URL("../node_modules/@sqlite.org/sqlite-wasm/dist/sqlite3.wasm", import.meta.url)),
|
||||
text("i18n.js"), text("tasks.js"), text("calendar.js"), text("notifications.js"), text("i18n-renderers.js"), text("desktop-personalization.js"), text("desktop-widgets.js"), text("system-navigation.js"), text("browser-app.js"), text("data-actions.js"), text("terminal-app.js"), text("db-admin.js"), text("manifest.webmanifest"), text("service-worker.js"), text("life-os-icon.svg"), readFile(new URL("../public/locales/en.json", import.meta.url), "utf8"), readFile(new URL("../public/locales/zh-CN.json", import.meta.url), "utf8")
|
||||
]);
|
||||
const release = "1.19.0";
|
||||
const release = "1.20.0";
|
||||
const localeEnJs = `window.LifeOSLocales=window.LifeOSLocales||{};window.LifeOSLocales.en=${localeEn};`;
|
||||
const localeZhJs = `window.LifeOSLocales=window.LifeOSLocales||{};window.LifeOSLocales['zh-CN']=${localeZh};`;
|
||||
const extraLocaleCodes = ['zh-HK', 'zh-MO', 'zh-TW', 'ar', 'fr', 'ru', 'es', 'ja'];
|
||||
@@ -24,13 +24,13 @@ let indexHtml = rawIndexHtml.replace('src="addons.js"', `src="addons.js?v=${rele
|
||||
for (const [src, code] of [
|
||||
['boot-guard.js', bootGuardJs], ['local-db.js', localDbJs], ['locales/en.js', localeEnJs], ['locales/zh-CN.js', localeZhJs], ...extraLocaleCodes.map(code => [`locales/${code}.js`, localeScripts[code]]),
|
||||
['i18n.js', i18nJs], ['tasks.js', tasksJs], ['calendar.js', calendarJs],
|
||||
['addons.js', addonsJs], ['notifications.js', notificationsJs], ['i18n-renderers.js', i18nRenderersJs], ['desktop-personalization.js', desktopPersonalizationJs], ['desktop-widgets.js', desktopWidgetsJs], ['system-navigation.js', systemNavigationJs], ['browser-app.js', browserAppJs], ['data-actions.js', dataActionsJs], ['terminal-app.js', terminalAppJs], ['db-admin.js', dbAdminJs]
|
||||
['addons.js', addonsJs], ['notifications.js', notificationsJs], ['i18n-renderers.js', i18nRenderersJs], ['backup.js', backupJs], ['desktop-personalization.js', desktopPersonalizationJs], ['desktop-widgets.js', desktopWidgetsJs], ['system-navigation.js', systemNavigationJs], ['browser-app.js', browserAppJs], ['data-actions.js', dataActionsJs], ['terminal-app.js', terminalAppJs], ['db-admin.js', dbAdminJs]
|
||||
]) {
|
||||
const tag = `<script src="${src}${src === 'boot-guard.js' ? '' : `?v=${release}`}"></script>`;
|
||||
if (!indexHtml.includes(tag)) throw new Error(`Missing Life OS script: ${src}`);
|
||||
indexHtml = indexHtml.replace(tag, `<script>${code.replace(/<\/script/gi, '<\\/script')}</script>`);
|
||||
}
|
||||
await writeFile(new URL("assets.js", server), `export const indexHtml=${JSON.stringify(indexHtml)};\nexport const addonsJs=${JSON.stringify(addonsJs)};\nexport const bootGuardJs=${JSON.stringify(bootGuardJs)};\nexport const i18nJs=${JSON.stringify(i18nJs)};\nexport const localeEn=JSON.parse(${JSON.stringify(localeEn)});\nexport const localeZh=JSON.parse(${JSON.stringify(localeZh)});\nexport const localeEnJs=${JSON.stringify(localeEnJs)};\nexport const localeZhJs=${JSON.stringify(localeZhJs)};\nexport const localeScripts=${JSON.stringify(localeScripts)};\nexport const localeData=${JSON.stringify(localeData)};\nexport const tasksJs=${JSON.stringify(tasksJs)};\nexport const calendarJs=${JSON.stringify(calendarJs)};\nexport const notificationsJs=${JSON.stringify(notificationsJs)};\nexport const i18nRenderersJs=${JSON.stringify(i18nRenderersJs)};\nexport const desktopPersonalizationJs=${JSON.stringify(desktopPersonalizationJs)};\nexport const desktopWidgetsJs=${JSON.stringify(desktopWidgetsJs)};\nexport const systemNavigationJs=${JSON.stringify(systemNavigationJs)};\nexport const browserAppJs=${JSON.stringify(browserAppJs)};\nexport const dataActionsJs=${JSON.stringify(dataActionsJs)};\nexport const terminalAppJs=${JSON.stringify(terminalAppJs)};\nexport const dbAdminJs=${JSON.stringify(dbAdminJs)};\nexport const manifest=${JSON.stringify(manifest)};\nexport const serviceWorker=${JSON.stringify(serviceWorker)};\nexport const icon=${JSON.stringify(icon)};\n`);
|
||||
await writeFile(new URL("assets.js", server), `export const indexHtml=${JSON.stringify(indexHtml)};\nexport const addonsJs=${JSON.stringify(addonsJs)};\nexport const backupJs=${JSON.stringify(backupJs)};\nexport const bootGuardJs=${JSON.stringify(bootGuardJs)};\nexport const i18nJs=${JSON.stringify(i18nJs)};\nexport const localeEn=JSON.parse(${JSON.stringify(localeEn)});\nexport const localeZh=JSON.parse(${JSON.stringify(localeZh)});\nexport const localeEnJs=${JSON.stringify(localeEnJs)};\nexport const localeZhJs=${JSON.stringify(localeZhJs)};\nexport const localeScripts=${JSON.stringify(localeScripts)};\nexport const localeData=${JSON.stringify(localeData)};\nexport const tasksJs=${JSON.stringify(tasksJs)};\nexport const calendarJs=${JSON.stringify(calendarJs)};\nexport const notificationsJs=${JSON.stringify(notificationsJs)};\nexport const i18nRenderersJs=${JSON.stringify(i18nRenderersJs)};\nexport const desktopPersonalizationJs=${JSON.stringify(desktopPersonalizationJs)};\nexport const desktopWidgetsJs=${JSON.stringify(desktopWidgetsJs)};\nexport const systemNavigationJs=${JSON.stringify(systemNavigationJs)};\nexport const browserAppJs=${JSON.stringify(browserAppJs)};\nexport const dataActionsJs=${JSON.stringify(dataActionsJs)};\nexport const terminalAppJs=${JSON.stringify(terminalAppJs)};\nexport const dbAdminJs=${JSON.stringify(dbAdminJs)};\nexport const manifest=${JSON.stringify(manifest)};\nexport const serviceWorker=${JSON.stringify(serviceWorker)};\nexport const icon=${JSON.stringify(icon)};\n`);
|
||||
await writeFile(new URL("local-assets.js", server), `export const localDbWorkerJs=${JSON.stringify(localDbWorkerJs)};\nexport const sqliteModuleJs=${JSON.stringify(sqliteModuleJs)};\nexport const sqliteWasmBase64=${JSON.stringify(sqliteWasm.toString('base64'))};\n`);
|
||||
await copyFile(new URL("../worker/index.js", import.meta.url), new URL("index.js", server));
|
||||
await copyFile(new URL("../worker/cloud-state.js", import.meta.url), new URL("cloud-state.js", server));
|
||||
|
||||
+31
-5
@@ -5,10 +5,13 @@ const worker = await import(new URL("../dist/server/index.js", import.meta.url))
|
||||
assert.equal(typeof worker.default?.fetch, "function");
|
||||
const assets = await import(new URL("../dist/server/assets.js", import.meta.url));
|
||||
const source = await readFile(new URL("../public/index.html", import.meta.url), "utf8");
|
||||
assert.match(assets.indexHtml, /!\[1,2\]\.includes\(d\.schemaVersion\)/);
|
||||
assert.match(assets.indexHtml, /!\[1,2,3\]\.includes\(d\.schemaVersion\)/);
|
||||
assert.doesNotMatch(assets.indexHtml, /localizeDynamicText|createTreeWalker|NodeFilter\.SHOW_TEXT|const ZH=/);
|
||||
assert.match(assets.indexHtml, /window\.LifeOSLocales\.en=/);
|
||||
assert.match(assets.addonsJs, /window\.exportData = exportData/);
|
||||
assert.match(assets.backupJs, /schemaVersion: BACKUP_SCHEMA/);
|
||||
assert.match(assets.backupJs, /checksum: \{ algorithm: 'SHA-256'/);
|
||||
assert.match(assets.backupJs, /life-os-before-restore/);
|
||||
assert.match(assets.addonsJs, /schemaVersion: 2/);
|
||||
assert.match(assets.addonsJs, /settings: settings\(\), system: sys\(\)/);
|
||||
assert.match(assets.addonsJs, /widgetLayout/);
|
||||
@@ -65,14 +68,14 @@ assert.doesNotMatch(assets.indexHtml, /setTimeout\(applyLanguage\s*,/);
|
||||
assert.doesNotMatch(assets.addonsJs, /(?<![\w.])applyLanguage\s*\(/, "add-on actions must not call a missing global locale function");
|
||||
assert.match(assets.indexHtml, /window\.renderIcons=renderIcons;window\.renderAll=renderAll/, "shared renderers are explicitly exposed to locale scripts");
|
||||
const shippedScripts = [...assets.indexHtml.matchAll(/<script>([\s\S]*?)<\/script>/g)].map(match => match[1]);
|
||||
assert.equal(shippedScripts.length, 27, "the complete desktop script chain is present in the served HTML");
|
||||
assert.equal(shippedScripts.length, 28, "the complete desktop script chain is present in the served HTML");
|
||||
assert.match(assets.indexHtml, /LifeOSLocalDB/);
|
||||
assert.match(assets.indexHtml, /OPFS:\/lifeos\.sqlite/);
|
||||
assert.match(assets.serviceWorker, /sqlite3\.wasm/);
|
||||
assert.doesNotMatch(assets.indexHtml, /<script\s+src=/, "the desktop cannot silently fall back when an app script fails to load");
|
||||
assert.ok(assets.indexHtml.indexOf('window.systemApp =') < assets.indexHtml.lastIndexOf("window.init()"), "locale renderers run before desktop initialization");
|
||||
for (const script of shippedScripts) new vm.Script(script);
|
||||
for (const script of [assets.i18nJs, assets.dataActionsJs, assets.terminalAppJs, assets.dbAdminJs, assets.tasksJs, assets.calendarJs, assets.notificationsJs, assets.i18nRenderersJs, assets.desktopPersonalizationJs, assets.desktopWidgetsJs, assets.systemNavigationJs, assets.browserAppJs, assets.addonsJs]) new vm.Script(script);
|
||||
for (const script of [assets.i18nJs, assets.dataActionsJs, assets.terminalAppJs, assets.dbAdminJs, assets.tasksJs, assets.calendarJs, assets.notificationsJs, assets.i18nRenderersJs, assets.backupJs, assets.desktopPersonalizationJs, assets.desktopWidgetsJs, assets.systemNavigationJs, assets.browserAppJs, assets.addonsJs]) new vm.Script(script);
|
||||
const editState = { tasksBoard: { goals: [
|
||||
{ id: 'g1', title: 'Original goal', description: 'Old note', targetDate: '2026-11-01', showOnDesktop: true, done: false, createdAt: '2026-09-01T00:00:00Z' },
|
||||
{ id: 'g2', title: 'Finished goal', done: true, targetDate: '', createdAt: '2026-09-01T00:00:00Z' }
|
||||
@@ -272,7 +275,7 @@ assert.ok(command('date').message.length > 8);
|
||||
assert.ok(command('time').message.includes(':'));
|
||||
assert.ok(command('datetime').message.length > 12);
|
||||
assert.equal(command('uptime').message, 'terminal.uptime');
|
||||
assert.equal(command('version').message, 'Life OS v1.19.0');
|
||||
assert.equal(command('version').message, 'Life OS v1.20.0');
|
||||
assert.equal(command('status').message, 'terminal.status');
|
||||
assert.match(command('apps').message, /calendar/);
|
||||
assert.equal(command('echo "hello Life OS"').message, 'hello Life OS');
|
||||
@@ -293,6 +296,29 @@ const makeStorage = initial => {
|
||||
const values = new Map(Object.entries(initial || {}));
|
||||
return { getItem: key => values.get(key) ?? null, setItem: (key,value) => values.set(key,String(value)), values };
|
||||
};
|
||||
const backupStorage = makeStorage({
|
||||
lifeos_memories: '[]', lifeos_museum: '[]', lifeos_profile: '{}', lifeos_settings: '{}', lifeos_system: '{"schemaVersion":2}',
|
||||
lifeos_notifications_v1: '{"memory":{"enabled":true}}', lifeos_storage_mode_v1: 'local', lifeos_cloud_baseline_v3: '{"internal":true}'
|
||||
});
|
||||
Object.defineProperties(backupStorage, { length: { get: () => backupStorage.values.size } });
|
||||
backupStorage.key = index => [...backupStorage.values.keys()][index] ?? null;
|
||||
backupStorage.removeItem = key => backupStorage.values.delete(key);
|
||||
const backupContext = vm.createContext({
|
||||
localStorage: backupStorage, crypto: globalThis.crypto, TextEncoder, Blob, URL,
|
||||
document: { createElement: () => ({ click() {} }), querySelector: () => ({ innerHTML: '' }) },
|
||||
settings: () => ({}), save: (key, value) => backupStorage.setItem(key, JSON.stringify(value)),
|
||||
applyImport() {}, importData: null, setTimeout() {}
|
||||
});
|
||||
backupContext.window = backupContext;
|
||||
vm.runInContext(assets.backupJs, backupContext);
|
||||
const portableBackup = await backupContext.LifeOSBackup.create();
|
||||
assert.equal(portableBackup.schemaVersion, 3);
|
||||
assert.equal(portableBackup.records.lifeos_notifications_v1, '{"memory":{"enabled":true}}');
|
||||
assert.equal(portableBackup.records.lifeos_storage_mode_v1, undefined, 'device storage choice does not migrate');
|
||||
assert.equal(portableBackup.records.lifeos_cloud_baseline_v3, undefined, 'cloud internals do not migrate');
|
||||
assert.equal(await backupContext.LifeOSBackup.validate(portableBackup), true);
|
||||
portableBackup.records.lifeos_notifications_v1 = '{}';
|
||||
assert.equal(await backupContext.LifeOSBackup.validate(portableBackup), false, 'checksum rejects a modified backup');
|
||||
let legacySettings = { language: "auto", languageMode: "auto" };
|
||||
const calls = { icons: 0, render: 0, apply: 0, toast: 0 };
|
||||
const localStorage = makeStorage();
|
||||
@@ -376,7 +402,7 @@ const uiContext = vm.createContext({
|
||||
...Object.fromEntries(["icons", "tasks", "launcher", "calendar", "desktop", "boot", "modal"].map(id => [id, uiNodes.get(`#${id}`)]))
|
||||
});
|
||||
uiContext.window = uiContext;
|
||||
for (const index of [0, ...Array.from({ length: 10 }, (_, i) => i + 3), 13, 14, 19, 20]) vm.runInContext(shippedScripts[index], uiContext);
|
||||
for (const index of [0, ...Array.from({ length: 10 }, (_, i) => i + 3), 13, 14, 19, 21]) vm.runInContext(shippedScripts[index], uiContext);
|
||||
vm.runInContext("windows.system={classList:{contains(){return false}}}", uiContext);
|
||||
uiContext.setLanguage("zh");
|
||||
assert.equal(uiContext.LifeI18n.language(), "zh-CN");
|
||||
|
||||
Reference in new issue
Block a user