|
1 | 1 | import type { MountHandler } from '@php-wasm/universal';
|
| 2 | +import { statSync } from 'fs'; |
| 3 | +import { dirname } from 'path'; |
2 | 4 |
|
3 | 5 | export function createNodeFsMountHandler(localPath: string): MountHandler {
|
4 | 6 | return async function (php, FS, vfsMountPoint) {
|
5 | 7 | /**
|
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 | 8 | * When Emscripten attempt to mount a local path into VFS, it looks up the path
|
14 | 9 | * and adds the local path as a mount to the VFS Node.
|
15 | 10 | * PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/trunk/packages/php-wasm/node/asyncify/php_8_0.js#L2700
|
16 | 11 | *
|
17 |
| - * For mounting to work, the Node must exist in VFS and be a directory. |
| 12 | + * For mounting to work, the Node must exist in VFS. |
18 | 13 | * If the Node doesn't exist, the mount fails with error 44 (MEMFS.doesNotExistError).
|
19 | 14 | * PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/trunk/packages/php-wasm/node/asyncify/php_8_0.js#L1201
|
| 15 | + * |
| 16 | + * Emscripten requires the mount point to be a directory. |
| 17 | + * To work around this, the PHP-wasm compile removes the directory check. |
| 18 | + * PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/5821cee231f452d050fd337b99ad0b26ebda487e/packages/php-wasm/compile/php/Dockerfile#L2148 |
20 | 19 | */
|
| 20 | + let lookup; |
21 | 21 | try {
|
22 |
| - FS.lookupPath(vfsMountPoint); |
| 22 | + lookup = FS.lookupPath(vfsMountPoint); |
23 | 23 | } catch (e) {
|
24 | 24 | // FS.lookupPath will throw an error if the path doesn't exist.
|
25 |
| - FS.mkdirTree(vfsMountPoint); |
| 25 | + if (statSync(localPath).isFile()) { |
| 26 | + FS.writeFile(vfsMountPoint, ''); |
| 27 | + } else if (statSync(localPath).isDirectory()) { |
| 28 | + FS.mkdirTree(dirname(vfsMountPoint)); |
| 29 | + FS.mkdirTree(vfsMountPoint); |
| 30 | + } else { |
| 31 | + throw new Error( |
| 32 | + 'Unsupported file type. PHP-wasm supports only symlinks that link to files, directories, or symlinks.' |
| 33 | + ); |
| 34 | + } |
| 35 | + lookup = FS.lookupPath(vfsMountPoint); |
| 36 | + } |
| 37 | + if (!lookup.node) { |
| 38 | + // TODO: Improve error once I understand the limitations. |
| 39 | + throw new Error('Unable to access the mount point in VFS.'); |
26 | 40 | }
|
27 | 41 | FS.mount(FS.filesystems['NODEFS'], { root: localPath }, vfsMountPoint);
|
28 | 42 | return () => {
|
|
0 commit comments