Skip to content

feat: Add an OPFS filesystem backend - #35

Merged
kateinoigakukun merged 5 commits into
swiftwasm:mainfrom
PassiveLogic:feat/opfs-backend
Oct 27, 2026
Merged

kateinoigakukun merged 5 commits into
swiftwasm:mainfrom
PassiveLogic:feat/opfs-backend

Conversation

@krodak

@krodak krodak commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Summary

Add an OPFS backend, so a browser guest can keep its files across page reloads and worker restarts. This is the use case behind useFS from #34. The backend implements FSBackend using only what uwasi/filesystem exports. The only core change is that useFS takes one object: the filesystem carries its backend.

import { WASI, useFS } from "uwasi";
import { ExclusiveOPFSFileSystem } from "uwasi/opfs";

const fileSystem = await ExclusiveOPFSFileSystem.create(
  await navigator.storage.getDirectory(),
);
const wasi = new WASI({ features: [useFS({ withFileSystem: fileSystem })] });
// ... run the guest ...
await fileSystem.backend.close();

Commits:

  1. Share the WASI conformance runner. Test-only: wasi.test.mjs becomes a caller of wasi_suite.mjs, so a second backend can run the same wasi-testsuite cases.
  2. Add the OPFS backend. From a new uwasi/opfs entry point. Importing uwasi doesn't load it.
  3. Plug a filesystem into useFS with one object. useFS({ withFileSystem }) takes the backend from the filesystem, so the useOPFS wrapper goes. The names say the store is exclusive: ExclusiveOPFSFileSystem and ExclusiveOPFSBackend. withBackend from feat: Add useFS for custom filesystem backends #34 was never released.
  4. Report a busy store. Another tab or worker gets OPFSStoreBusyError, and the store doesn't change.
  5. Reopen a store at the browser's handle limit. Safari allows about 250 sync access handles per worker. There the spare pool shrinks, and a store whose files alone need more handles is refused with a clear error.

How it works

The backend owns the directory it's given. Each guest file is one data file, kept open through a sync access handle, because getting a handle is asynchronous and syscalls aren't. A small binary record maps guest names to data files: two alternating snapshots plus an append-only log. Each name change appends and flushes one record before its syscall returns, so its cost doesn't grow with the number of files.

A pool of open spare files makes creating a file synchronous. A call that creates more files than the pool holds still works, but those files are only durable after the event loop gets a turn; until then fd_sync on them returns NOSPC. fileSystem.backend has settle(), persistAll() and close(), which do the asynchronous work between guest calls.

Some details that follow the review of #34:

  • Unlink and replacing rename save the change before they delete any bytes, so a failed one leaves the file as it was. An unlinked file's bytes go when its last fd closes.
  • The backend never changes the tree itself. When it writes a full snapshot during a change, it encodes the tree as the change will leave it.
  • Opening a file writes nothing. A file the host added is stored on its first write or sync.
  • A short write is reported as a short write.

A store the backend can't read, such as one a later version wrote, is refused and left unchanged.

Limits

  • Workers only, and one live backend per store (sync access handles are exclusive locks). Another tab or worker gets OPFSStoreBusyError.
  • Each file holds a sync access handle, so Safari's limit of about 250 per worker also limits the files in a store.
  • Hard links and changes to device nodes return NOTSUP. That's the only wasi-testsuite case that fails, and the runner checks it fails for that reason.
  • Inode numbers and timestamps aren't persisted.
  • Tests run against an in-memory mock of OPFS, so they check the backend's logic and crash recovery, not a real browser's durability.

Test Plan

  • Each commit builds and passes npm test on its own.
  • The full wasi-testsuite runs over OPFS; only the hard-link case fails, for the documented reason.
  • Backend tests cover the handle and namespace contract, saving without touching the tree, refusing hard links and device nodes, a busy store, and a store at the handle limit.
  • Persistence tests cover reopening, sync across a simulated crash, the spare-pool overflow path, files the host adds, renames and unlinks, failed and short writes, opening without writing, an unlinked file read and written through an open fd, compaction, an unreadable store, and recovery from a failed log reset, a torn log record and a failed unlink.
  • uwasi/opfs works from ESM, CommonJS and TypeScript.

krodak and others added 2 commits October 15, 2026 12:14
Move the wasi-testsuite runner from wasi.test.mjs to wasi-suite.mjs, and
let each caller supply the features for a test case. wasi.test.mjs runs
it with useAll over a MemoryFileSystem, as before. A caller can also list
cases that must fail for a documented reason; the runner checks that they
do fail, and for that reason.
Add OPFSBackend, an FSBackend that stores guest files in the browser's
Origin Private File System, and useOPFS, which is useFS with that backend.
Both are exported from a new uwasi/opfs entry point; the root does not
load them. The core is unchanged: the backend uses only what
uwasi/filesystem exports.

The backend owns the directory it is given: one data file per guest file,
held open through a sync access handle, and a binary namespace record (two
alternating snapshots and a change log) that maps names to data files.
Namespace changes are durable when the syscall returns. Unlink and
replacing rename record the change before they destroy any content, so a
failed one leaves the file as it was. A pool of open spare files makes
file creation synchronous; settle(), persistAll() and close() run the
asynchronous work between guest calls. Opening a file writes nothing; a
host-seeded file is stored on its first write or sync. Hard links and
changes to device nodes return NOTSUP. A store whose record it cannot
read is refused, unchanged.

Tests run the backend over an in-memory OPFS mock: backend-level checks,
persistence and crash recovery, and the full wasi-testsuite, where only
the hard-link case fails, for the documented reason.

Co-authored-by: Peter Grayson <pete@jpgrayson.net>
@krodak
krodak force-pushed the feat/opfs-backend branch from 00e1528 to c4a21fb Compare October 15, 2026 10:14

@kateinoigakukun kateinoigakukun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like it does not support accessing the same OPFS directory in multiple tabs at all? Not sure if we have any good option to implement it unless we rely on SharedWorker though.

If you are already aware of that and it's fine, I think we can merge this with a different name indicating that this backend is exclusive to a single browsing context.

Comment thread src/opfs/index.ts Resolved
@krodak
krodak force-pushed the feat/opfs-backend branch from 95437ed to d08ab31 Compare October 26, 2026 07:51
@krodak

krodak commented Oct 26, 2026

Copy link
Copy Markdown
Member Author

Hey @kateinoigakukun Yuta, thanks! Yeah, for now we're focused on the single-tab flow, as it fits our use case. For Wwe plan one owner per store, so an exclusive backend is what we need, plus a clear error when a second tab opens the same store. On our larger graphs this backend also turned out faster than SQLite's own OPFS VFS (510k nodes: save 33.1 s vs 42.7 s, load 36.5 s vs 48.7 s), and it doesn't need COOP/COEP

Butyeah, I should have named it better 😅 🙇🏼‍♂️ Fixed in the new commits:

  • It's now ExclusiveOPFSFileSystem, plugged in with one object as you suggested: useFS({ withFileSystem: await ExclusiveOPFSFileSystem.create(dir) }). The useOPFS wrapper and withBackend are gone (withBackend wasn't released yet).
  • A second tab or worker that opens the same store gets OPFSStoreBusyError, and the store stays untouched.
  • While testing in Chromium and WebKit I found a bug, also fixed: a store at Safari's limit of ~250 open handles couldn't be reopened.

For real multi-tab I see two options:

  1. Your SharedWorker idea: one owner holds the store and the other tabs go through it. Chromium doesn't allow sync access handles in a SharedWorker, so it would route requests to a dedicated worker in the owner tab.
  2. JSPI: a separate backend where a syscall can wait for a lock or a file handle that another tab holds, without cross-origin isolation. It needs async-capable fs calls in the core.

I'll run some PoCs for both and let you know how it goes 🙏🏻

@kateinoigakukun
kateinoigakukun merged commit 11b4ebf into swiftwasm:main Oct 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants