1
- import { type ErrnoError , type MountHandler } from '@php-wasm/universal' ;
1
+ import { FSHelpers , type MountHandler } from '@php-wasm/universal' ;
2
2
import { statSync } from 'fs' ;
3
3
import { basename } from 'path' ;
4
4
5
5
export function createNodeFsMountHandler ( localPath : string ) : MountHandler {
6
- return async function ( php , FS , vfsMountPoint ) {
6
+ return function ( php , FS , vfsMountPoint ) {
7
7
/**
8
8
* When Emscripten attempt to mount a local path into VFS, it looks up the path
9
9
* and adds the local path as a mount to the VFS Node.
@@ -17,47 +17,37 @@ export function createNodeFsMountHandler(localPath: string): MountHandler {
17
17
* To work around this, the PHP-wasm compile removes the directory check.
18
18
* PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/5821cee231f452d050fd337b99ad0b26ebda487e/packages/php-wasm/compile/php/Dockerfile#L2148
19
19
*/
20
- let lookup ;
21
- let unlinkPath : string | undefined ;
22
- try {
23
- lookup = FS . lookupPath ( vfsMountPoint ) ;
24
- } catch ( e ) {
25
- const err = e as ErrnoError ;
26
- // FS.lookupPath will throw an error with errno 44 if the path doesn't exist.
27
- if ( err . errno !== 44 ) {
28
- throw e ;
29
- }
20
+ let removeVfsNode = false ;
21
+ if ( ! FSHelpers . fileExists ( FS , vfsMountPoint ) ) {
30
22
if ( statSync ( localPath ) . isSymbolicLink ( ) ) {
31
23
( FS as any ) . createNode (
32
24
FS . lookupPath ( vfsMountPoint , { parent : true } ) . node ,
33
25
basename ( localPath ) ,
34
26
110000
35
27
) ;
36
- lookup = FS . lookupPath ( vfsMountPoint ) ;
37
28
} else if ( statSync ( localPath ) . isFile ( ) ) {
38
29
FS . writeFile ( vfsMountPoint , '' ) ;
39
- unlinkPath = vfsMountPoint ;
40
30
} else if ( statSync ( localPath ) . isDirectory ( ) ) {
41
31
FS . mkdirTree ( vfsMountPoint ) ;
42
- unlinkPath = vfsMountPoint ;
43
32
} else {
44
33
throw new Error (
45
34
'Unsupported file type. PHP-wasm supports only symlinks that link to files, directories, or symlinks.'
46
35
) ;
47
36
}
48
- lookup = FS . lookupPath ( vfsMountPoint ) ;
37
+ removeVfsNode = true ;
49
38
}
39
+ const lookup = FS . lookupPath ( vfsMountPoint ) ;
50
40
if ( ! lookup . node ) {
51
41
throw new Error ( 'Unable to access the mount point in VFS.' ) ;
52
42
}
53
43
FS . mount ( FS . filesystems [ 'NODEFS' ] , { root : localPath } , vfsMountPoint ) ;
54
44
return ( ) => {
55
45
FS ! . unmount ( vfsMountPoint ) ;
56
- if ( unlinkPath ) {
46
+ if ( removeVfsNode ) {
57
47
if ( FS . isDir ( lookup . node . mode ) ) {
58
- FS . rmdir ( unlinkPath ) ;
48
+ FS . rmdir ( vfsMountPoint ) ;
59
49
} else {
60
- FS . unlink ( unlinkPath ) ;
50
+ FS . unlink ( vfsMountPoint ) ;
61
51
}
62
52
}
63
53
} ;
0 commit comments