Skip to content

feat(p2-shim): add opfs filesystem adapter - #2095

Merged
vados-cosmonic merged 8 commits into
bytecodealliance:mainfrom
eduardomourar:feat/p2-shim-opfs-adapter
Sep 11, 2026
Merged

feat(p2-shim): add opfs filesystem adapter#2095
vados-cosmonic merged 8 commits into
bytecodealliance:mainfrom
eduardomourar:feat/p2-shim-opfs-adapter

Conversation

@eduardomourar

@eduardomourar eduardomourar commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Adds a first-party OPFS-backed filesystem adapter for preview2-shim's browser wasi:filesystem implementation, plus two non-JSPI improvement candidates (advisory locking, in-memory symlink support).

  • OpfsFilesystemAdapter (src/browser/opfs-filesystem.ts) - loads a preopened OPFS directory into an in-memory tree once (loadOpfsCapability()), so every Descriptor op stays fully synchronous - no JSPI required. Mutations flush back to OPFS automatically (debounced to a microtask); flush()/dispose() force it explicitly.
  • Symlinks survive a flush + reload via a hidden sidecar file (.__wasi_symlinks__.json) per root, since OPFS has no native symlink concept.
  • Advisory locking (lockShared/lockExclusive/tryLock*/unlock) on BrowserFilesystemDescriptor, backed by a same-process reader/writer lock in InMemoryFilesystemAdapter. new OpfsFilesystemAdapter({ lockManager: navigator.locks }) opts into best-effort cross-tab coordination via the Web Locks API - off by default. lockManager takes a narrow BrowserLockManager interface rather than the global navigator directly, so it's easy to fake in tests.
  • _addPreopenWithAdapter (src/browser/filesystem.ts) lets a host wire a custom adapter into the top-level wasi:filesystem/preopens singleton that transpiled components import statically.

Related to: #144

vados-cosmonic
vados-cosmonic previously approved these changes Sep 10, 2026

@vados-cosmonic vados-cosmonic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is awesome! Thanks for working on this -- most of my nits are more about testing and reserving some future optionality.

One more thing I'd really love to have is an E2E test in the browser! We have machinery and browser automation already set up, but for some reason we don't actually use it from any of the tests... I think you should be able to copy the setup in other projects (jco, primarily), but also happy to leave that as a follow up (we can make a new issue to track that separately, it would be very easy for anyone else to pick up), if you'd rather get this in as-is!

Comment thread packages/preview2-shim/test/browser/opfs-filesystem.ts
Comment thread packages/preview2-shim/test/browser/filesystem.ts Outdated
Comment thread packages/preview2-shim/src/browser/opfs-filesystem.ts
@eduardomourar
eduardomourar force-pushed the feat/p2-shim-opfs-adapter branch from b398726 to 2e47e5e Compare September 11, 2026 01:52
@eduardomourar

Copy link
Copy Markdown
Contributor Author

Added e2e coverage using the harness in test/common.ts (startTestServer/runBasicHarnessPageTest), in opfs-filesystem-e2e.ts:

  • one suite componentizes/transpiles a small wasm component, runs it against a live OpfsFilesystemAdapter, flushes, reloads the same OPFS directory into a fresh adapter, and re-runs the component — confirming writes/symlinks made across the component boundary actually survive in real OPFS storage, not just in one adapter's in-memory tree.
  • a second suite exercises cross-tab locking directly against the browser's real navigator.locks (no wasm component needed there) — verifying two independent adapters actually queue/grant through the browser's own Web Locks manager, not a mock.

@vados-cosmonic vados-cosmonic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM 🚀

I think some of the new tests could do without the beforeAll (since they are testing results from a single run), but that can definitely get fixed in a follow up!)

@vados-cosmonic
vados-cosmonic added this pull request to the merge queue Sep 11, 2026
Merged via the queue into bytecodealliance:main with commit e2f0547 Sep 11, 2026
111 of 113 checks passed
@eduardomourar
eduardomourar deleted the feat/p2-shim-opfs-adapter branch September 11, 2026 09:29
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