Skip to content

Commit 952c6d6

Browse files
committed
Address feedback
1 parent 7fa179c commit 952c6d6

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
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';
26
import { statSync } from 'fs';
37
import { basename } from 'path';
48

59
export function createNodeFsMountHandler(localPath: string): MountHandler {
6-
return async function (php, FS, vfsMountPoint) {
10+
return function (php, FS, vfsMountPoint) {
711
/**
812
* When Emscripten attempt to mount a local path into VFS, it looks up the path
913
* and adds the local path as a mount to the VFS Node.
@@ -17,47 +21,37 @@ export function createNodeFsMountHandler(localPath: string): MountHandler {
1721
* To work around this, the PHP-wasm compile removes the directory check.
1822
* PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/5821cee231f452d050fd337b99ad0b26ebda487e/packages/php-wasm/compile/php/Dockerfile#L2148
1923
*/
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)) {
3026
if (statSync(localPath).isSymbolicLink()) {
3127
(FS as any).createNode(
3228
FS.lookupPath(vfsMountPoint, { parent: true }).node,
3329
basename(localPath),
3430
110000
3531
);
36-
lookup = FS.lookupPath(vfsMountPoint);
3732
} else if (statSync(localPath).isFile()) {
3833
FS.writeFile(vfsMountPoint, '');
39-
unlinkPath = vfsMountPoint;
4034
} else if (statSync(localPath).isDirectory()) {
4135
FS.mkdirTree(vfsMountPoint);
42-
unlinkPath = vfsMountPoint;
4336
} else {
4437
throw new Error(
4538
'Unsupported file type. PHP-wasm supports only symlinks that link to files, directories, or symlinks.'
4639
);
4740
}
48-
lookup = FS.lookupPath(vfsMountPoint);
41+
removeVfsNode = true;
4942
}
43+
const lookup = FS.lookupPath(vfsMountPoint);
5044
if (!lookup.node) {
5145
throw new Error('Unable to access the mount point in VFS.');
5246
}
5347
FS.mount(FS.filesystems['NODEFS'], { root: localPath }, vfsMountPoint);
5448
return () => {
5549
FS!.unmount(vfsMountPoint);
56-
if (unlinkPath) {
50+
if (removeVfsNode) {
5751
if (FS.isDir(lookup.node.mode)) {
58-
FS.rmdir(unlinkPath);
52+
FS.rmdir(vfsMountPoint);
5953
} else {
60-
FS.unlink(unlinkPath);
54+
FS.unlink(vfsMountPoint);
6155
}
6256
}
6357
};

packages/php-wasm/universal/src/lib/rethrow-file-system-error.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77
*/
88

99
export class ErrnoError extends Error {
10-
constructor(errno: number, message?: string, options?: { cause?: any }) {
11-
super(message);
10+
constructor(errno: number, message?: string, options?: any) {
11+
super(message, options);
1212
this.name = 'ErrnoError';
1313
this.errno = errno;
14-
this.message = message ?? '';
15-
this.cause = options?.cause;
1614
}
1715

1816
node?: any;

0 commit comments

Comments
 (0)