Skip to content

Commit 1e38f50

Browse files
committed
Create directory node before mounting
1 parent 6e093a7 commit 1e38f50

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

packages/php-wasm/node/src/lib/node-fs-mount.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,31 @@ import type { MountHandler } from '@php-wasm/universal';
22

33
export function createNodeFsMountHandler(localPath: string): MountHandler {
44
return async function (php, FS, vfsMountPoint) {
5+
/**
6+
* DON'T MERGE THIS.
7+
* This is a temporary workaround to demonstrate how mounting requires the mount point to be a directory.
8+
*
9+
* Emscripten requires the mount point to be a directory.
10+
* By creating a directory we can even mount a file over it as long as the paths match.
11+
* PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/trunk/packages/php-wasm/node/asyncify/php_8_0.js#L2679
12+
*
13+
* When Emscripten attempt to mount a local path into VFS, it looks up the path
14+
* and adds the local path as a mount to the VFS Node.
15+
* PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/trunk/packages/php-wasm/node/asyncify/php_8_0.js#L2700
16+
*
17+
* For mounting to work, the Node must exist in VFS and be a directory.
18+
* If the Node doesn't exist, the mount fails with error 44 (MEMFS.doesNotExistError).
19+
* PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/trunk/packages/php-wasm/node/asyncify/php_8_0.js#L1201
20+
*/
21+
try {
22+
FS.lookupPath(vfsMountPoint);
23+
} catch (e) {
24+
// FS.lookupPath will throw an error if the path doesn't exist.
25+
FS.mkdirTree(vfsMountPoint);
26+
}
527
FS.mount(FS.filesystems['NODEFS'], { root: localPath }, vfsMountPoint);
628
return () => {
29+
// TODO: Delete the mount point if was created during the mount.
730
FS!.unmount(vfsMountPoint);
831
};
932
};

0 commit comments

Comments
 (0)