Repository navigation
feat: Add an OPFS filesystem backend - #35
Conversation
2c5c6e6 to
00e1528
Compare
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>
00e1528 to
c4a21fb
Compare
kateinoigakukun
left a comment
There was a problem hiding this comment.
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.
Rename OPFSBackend to ExclusiveOPFSBackend and drop useOPFS.
95437ed to
d08ab31
Compare
|
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:
For real multi-tab I see two options:
I'll run some PoCs for both and let you know how it goes 🙏🏻 |
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
useFSfrom #34. The backend implementsFSBackendusing only whatuwasi/filesystemexports. The only core change is thatuseFStakes one object: the filesystem carries its backend.Commits:
wasi.test.mjsbecomes a caller ofwasi_suite.mjs, so a second backend can run the same wasi-testsuite cases.uwasi/opfsentry point. Importinguwasidoesn't load it.useFSwith one object.useFS({ withFileSystem })takes the backend from the filesystem, so theuseOPFSwrapper goes. The names say the store is exclusive:ExclusiveOPFSFileSystemandExclusiveOPFSBackend.withBackendfrom feat: Add useFS for custom filesystem backends #34 was never released.OPFSStoreBusyError, and the store doesn't change.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_syncon them returnsNOSPC.fileSystem.backendhassettle(),persistAll()andclose(), which do the asynchronous work between guest calls.Some details that follow the review of #34:
A store the backend can't read, such as one a later version wrote, is refused and left unchanged.
Limits
OPFSStoreBusyError.NOTSUP. That's the only wasi-testsuite case that fails, and the runner checks it fails for that reason.Test Plan
npm teston its own.uwasi/opfsworks from ESM, CommonJS and TypeScript.