@@ -5,69 +5,57 @@ import {
5
5
} from '@php-wasm/universal' ;
6
6
import { statSync } from 'fs' ;
7
7
import { basename } from 'path' ;
8
- import type { Emscripten } from '@php-wasm/universal' ;
9
-
10
- export function mountNODEFSMountPoint (
11
- FS : Emscripten . RootFS ,
12
- vfsMountPoint : string ,
13
- localPath : string
14
- ) {
15
- /**
16
- * When Emscripten attempt to mount a local path into VFS, it looks up the path
17
- * and adds the local path as a mount to the VFS Node.
18
- * PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/trunk/packages/php-wasm/node/asyncify/php_8_0.js#L2700
19
- *
20
- * For mounting to work, the Node must exist in VFS.
21
- * If the Node doesn't exist, the mount fails with error 44 (MEMFS.doesNotExistError).
22
- * PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/trunk/packages/php-wasm/node/asyncify/php_8_0.js#L1201
23
- *
24
- * Emscripten requires the mount point to be a directory.
25
- * To work around this, the PHP-wasm compile removes the directory check.
26
- * PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/5821cee231f452d050fd337b99ad0b26ebda487e/packages/php-wasm/compile/php/Dockerfile#L2148
27
- */
28
- try {
29
- FS . lookupPath ( vfsMountPoint ) ;
30
- } catch ( e ) {
31
- const err = e as ErrnoError ;
32
- // FS.lookupPath will throw an error with errno 44 if the path doesn't exist.
33
- if ( err . errno !== 44 ) {
34
- throw e ;
35
- }
36
- if ( statSync ( localPath ) . isSymbolicLink ( ) ) {
37
- ( FS as any ) . createNode (
38
- FS . lookupPath ( vfsMountPoint , { parent : true } ) . node ,
39
- basename ( localPath ) ,
40
- 110000
41
- ) ;
42
- } else if ( statSync ( localPath ) . isFile ( ) ) {
43
- FS . writeFile ( vfsMountPoint , '' ) ;
44
- } else if ( statSync ( localPath ) . isDirectory ( ) ) {
45
- FS . mkdirTree ( vfsMountPoint ) ;
46
- } else {
47
- throw new Error (
48
- 'Unsupported file type. PHP-wasm supports only symlinks that link to files, directories, or symlinks.'
49
- ) ;
50
- }
51
- }
52
- FS . mount ( FS . filesystems [ 'NODEFS' ] , { root : localPath } , vfsMountPoint ) ;
53
- }
54
8
55
9
export function createNodeFsMountHandler ( localPath : string ) : MountHandler {
56
- return function ( php , FS , vfsMountPoint ) {
10
+ return async function ( php , FS , vfsMountPoint ) {
11
+ /**
12
+ * When Emscripten attempt to mount a local path into VFS, it looks up the path
13
+ * and adds the local path as a mount to the VFS Node.
14
+ * PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/trunk/packages/php-wasm/node/asyncify/php_8_0.js#L2700
15
+ *
16
+ * For mounting to work, the Node must exist in VFS.
17
+ * If the Node doesn't exist, the mount fails with error 44 (MEMFS.doesNotExistError).
18
+ * PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/trunk/packages/php-wasm/node/asyncify/php_8_0.js#L1201
19
+ *
20
+ * Emscripten requires the mount point to be a directory.
21
+ * To work around this, the PHP-wasm compile removes the directory check.
22
+ * PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/5821cee231f452d050fd337b99ad0b26ebda487e/packages/php-wasm/compile/php/Dockerfile#L2148
23
+ */
24
+ let lookup ;
57
25
let unlinkPath : string | undefined ;
58
- if ( statSync ( localPath ) . isDirectory ( ) ) {
59
- try {
60
- FS . lookupPath ( vfsMountPoint ) ;
61
- } catch {
26
+ try {
27
+ lookup = FS . lookupPath ( vfsMountPoint ) ;
28
+ } catch ( e ) {
29
+ const err = e as ErrnoError ;
30
+ // FS.lookupPath will throw an error with errno 44 if the path doesn't exist.
31
+ if ( err . errno !== 44 ) {
32
+ throw e ;
33
+ }
34
+ if ( statSync ( localPath ) . isSymbolicLink ( ) ) {
35
+ ( FS as any ) . createNode (
36
+ FS . lookupPath ( vfsMountPoint , { parent : true } ) . node ,
37
+ basename ( localPath ) ,
38
+ 110000
39
+ ) ;
40
+ lookup = FS . lookupPath ( vfsMountPoint ) ;
41
+ } else if ( statSync ( localPath ) . isFile ( ) ) {
42
+ FS . writeFile ( vfsMountPoint , '' ) ;
62
43
unlinkPath = vfsMountPoint ;
44
+ } else if ( statSync ( localPath ) . isDirectory ( ) ) {
45
+ FS . mkdirTree ( vfsMountPoint ) ;
46
+ unlinkPath = vfsMountPoint ;
47
+ } else {
48
+ throw new Error (
49
+ 'Unsupported file type. PHP-wasm supports only symlinks that link to files, directories, or symlinks.'
50
+ ) ;
63
51
}
64
- } else {
65
- unlinkPath = vfsMountPoint ;
52
+ lookup = FS . lookupPath ( vfsMountPoint ) ;
66
53
}
67
-
68
- mountNODEFSMountPoint ( FS , vfsMountPoint , localPath ) ;
69
- const lookup = FS . lookupPath ( vfsMountPoint ) ;
70
-
54
+ if ( ! lookup . node ) {
55
+ // TODO: Improve error once I understand the limitations.
56
+ throw new Error ( 'Unable to access the mount point in VFS.' ) ;
57
+ }
58
+ FS . mount ( FS . filesystems [ 'NODEFS' ] , { root : localPath } , vfsMountPoint ) ;
71
59
return ( ) => {
72
60
FS ! . unmount ( vfsMountPoint ) ;
73
61
if ( unlinkPath ) {
0 commit comments