Skip to content

Browser: php.mount() support for OPFS/Native FS handles#1470

Merged
adamziel merged 11 commits intotrunkfrom
dirhandle-mount
May 29, 2024
Merged

Browser: php.mount() support for OPFS/Native FS handles#1470
adamziel merged 11 commits intotrunkfrom
dirhandle-mount

Conversation

@adamziel
Copy link
Copy Markdown
Collaborator

@adamziel adamziel commented May 28, 2024

Context of this PR

It enables mounting OPFS and local directory handles in the browser version of Playground using the same abstraction as we use for mounting local directories in Node.js. This, in turn, enables using OPFS mounts in the boot protocol and relying on the same general code paths and abstractions.

What's the technical change?

Ships a DirectoryHandleMount class that can be used to mount OPFS using the php.mount() method:

await php.mount(
	php.documentRoot,
	createDirectoryHandleMountHandler(opfsHandle);
);

Implementation details

DirectoryHandleMount is not a "real" mount in that it doesn't actually plug in an Emscripten FS implementation into the PHP Emscripten module. Instead, it copies all the OPFS files into Playground MEMFS (or the other way around). After that initial sync, it journals all the MEMFS filesystem operations and replays them in OPFS. This is good enough for the in-browser directory handle, since the underlying files aren't going to change on their own, but for the Local Filesystem directory handle we also have an explicit "Synchronize files" button to bring any local changes back into Playground.

Once Emscripten supports asynchronous filesystem operations via wasmfs, we'll be about to get rid of the journal and mount an OPFS implementation to directly delegate writes, reads, etc to the underlying filesystem.

Other changes

This PR removes the Mountable interface in favor of functional API to simplify the implementation and remove any state management concerns.

Before:

php.mount(dir, new NodeFSMount(dir));

After:

php.mount(dir, createNodeFsMountHandler(dir));

Testing Instructions

Open local Playground and change the storage option to "browser", make some visible changes, refresh Playground a few times, confirm the site still loads and the changes are still around. Close the browser, reopen it, confirm it still works.

Them repeat the test with the local directory storage option.

Also, confirm the following Playground CLI command works and sets up a site in a local new-wordpress-site directory:

bun packages/playground/cli/src/cli.ts server --mount-before-install=`pwd`/new-wordpress-site:/wordpress

Base automatically changed from fs-methods to trunk May 29, 2024 06:54
@adamziel adamziel marked this pull request as ready for review May 29, 2024 06:57
@adamziel adamziel changed the title First stab at DirectoryHandleMount Browser: DirectoryHandleMount to support mounting OPFS/local FS handles May 29, 2024
@adamziel adamziel changed the title Browser: DirectoryHandleMount to support mounting OPFS/local FS handles Browser: php.mount() support for OPFS/Native FS handles May 29, 2024
@adamziel adamziel merged commit 85bb66a into trunk May 29, 2024
@adamziel adamziel deleted the dirhandle-mount branch May 29, 2024 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant