web: move the saved session to kwsnap:session-v2 and delete older versions

This commit is contained in:
Pooya Parsa
2026-11-19 17:31:41 +00:00
parent 943efbe2e0
commit 3699f3aee4
3 changed files with 25 additions and 6 deletions

View File

@@ -488,8 +488,9 @@ sets sessionStorage `kw-resume` and reloads the same URL; the load reads it
once and resumes without asking. A plain load offers to resume (`ui.ask`
with Resume and Boot fresh) before the kernel starts.
- `session.ts` keeps one session in IndexedDB `kwsnap:session-v1`, its own
database: `meta` (time, the page's kernel URL, the banner, the device
- `session.ts` keeps one session in IndexedDB `kwsnap:session-v2`, its own
database (the first open on a page deletes `kwsnap:session-v1` and any
other older version, unawaited): `meta` (time, the page's kernel URL, the banner, the device
options, the disks' snapshot state, the console's last 64 KiB) and
`bytes` (the `encodeSnapshot()` Blob), written in one transaction. It
never opens `kwdisk:kwdata-v1`.

View File

@@ -40,7 +40,7 @@
"use strict";
const DATA_DISK = "kwdisk:kwdata-v1";
/** Databases the page is known to open, for browsers without indexedDB.databases(). */
const KNOWN_DBS = [DATA_DISK, "kwsnap:session-v1"];
const KNOWN_DBS = [DATA_DISK, "kwsnap:session-v2", "kwsnap:session-v1"];
const out = document.getElementById("log");
const log = (s) => { out.textContent += s + "\n"; };

View File

@@ -2,8 +2,9 @@
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
// The suspended session: Suspend (Ctrl-A z, KW.suspend()) saves the running
// machine with the SDK's snapshot({ stop: true }) and keeps it here, in
// IndexedDB "kwsnap:session-v1" (its own database; the data disk's
// kwdisk:kwdata-v1 is never touched here); the next load offers to resume
// IndexedDB "kwsnap:session-v2" (its own database, older versions are
// deleted on first use; the data disk's kwdisk:kwdata-v1 is never touched
// here); the next load offers to resume
// it (boot.ts). One session at a time: "meta" (small, read to decide
// whether to offer) and "bytes" (the encoded snapshot, a Blob) are written
// in one transaction and deleted together.
@@ -16,9 +17,25 @@
// guest keeps writing /data, which makes it unrestorable within seconds.
import { decodeSnapshot, encodeSnapshot, type DiskSnapshotState, type KernelSnapshot } from "./sdk.ts";
const DB = "kwsnap:session-v1";
const DB_PREFIX = "kwsnap:session-v";
const DB_VERSION = 2;
const DB = `${DB_PREFIX}${DB_VERSION}`;
const STORE = "session";
let oldDropped = false;
/**
* Delete the databases of older versions (kwsnap:session-v1 up to the one
* before DB_VERSION), once per page. Not awaited: deleting a missing one is
* a no-op, and one another tab still holds is deleted when that tab closes.
*/
function dropOldDbs(): void {
if (oldDropped) return;
oldDropped = true;
for (let v = 1; v < DB_VERSION; v++) {
try { indexedDB.deleteDatabase(`${DB_PREFIX}${v}`); } catch { /* ignore */ }
}
}
/** What the page needs to decide whether it can offer to resume. */
export interface SessionMeta {
/** Date.now() when it was saved. */
@@ -59,6 +76,7 @@ const reqP = <T>(r: IDBRequest<T>): Promise<T> =>
new Promise((res, rej) => { r.onsuccess = () => res(r.result); r.onerror = () => rej(r.error); });
function openDb(): Promise<IDBDatabase> {
dropOldDbs();
return new Promise((resolve, reject) => {
const r = indexedDB.open(DB, 1);
r.onupgradeneeded = () => { r.result.createObjectStore(STORE); };