// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The page's data disk jobs that run in a worker of their own (data.ts // runDataJob()): copying the IndexedDB data disk to OPFS (the SDK's // migrateDataDisk(): batches by key range, the IndexedDB copy fenced) and // importing a .kwsparse image into the OPFS disk, which need a dedicated // worker's synchronous file handles; and removing the blocks of the fenced // IndexedDB copy afterwards (clearMovedDataDisk(): a transaction per batch, // the meta kept as a tombstone), which runs in the background beside the // kernel worker and touches no OPFS file. One job per worker: it posts its // progress and log lines, then "done" or "error", and the page ends it. For // a migration or an import the kernel worker must not have the OPFS disk // open (before the boot, or after detachDisk()); the page holds the one-tab // lock for all of them. import { clearMovedDataDisk, importDataDisk, migrateDataDisk } from "../../js/web/disk.ts"; export type DataJob = { op: "migrate" } | { op: "import"; bytes: Uint8Array } | { op: "clear-moved" }; export type DataJobMessage = | { t: "progress"; done: number; total: number } | { t: "log"; msg: string } | { t: "done"; value: unknown } | { t: "error"; name: string; message: string }; /** The dedicated worker's global (the page's tsconfig has no webworker lib). */ const scope = globalThis as unknown as { postMessage(msg: DataJobMessage): void; addEventListener(type: "message", fn: (e: MessageEvent) => void): void; }; async function run(job: DataJob): Promise { const log = (msg: string) => scope.postMessage({ t: "log", msg }); if (job.op === "migrate") return migrateDataDisk({ log, onProgress: ({ done, total }) => scope.postMessage({ t: "progress", done, total }) }); if (job.op === "clear-moved") return clearMovedDataDisk({ log, onProgress: (done) => scope.postMessage({ t: "progress", done, total: 0 }) }); return importDataDisk(job.bytes, { backend: "opfs", log }); } scope.addEventListener("message", (e) => { run(e.data).then((value) => scope.postMessage({ t: "done", value }), (err: unknown) => { const x = err as Error | undefined; scope.postMessage({ t: "error", name: x?.name ?? "Error", message: x?.message ?? String(err) }); }); });