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