Skip to content

Commit 906359d

Browse files
committed
Merge branch 'add/file-mounting-to-nodefs' into fix/lookup-path-not-following-symlink-mounts
2 parents 4bc154c + 648696c commit 906359d

File tree

3 files changed

+12
-24
lines changed

3 files changed

+12
-24
lines changed

packages/php-wasm/compile/php/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2182,7 +2182,7 @@ RUN set -euxo pipefail; \
21822182
# stream.stream_ops is sometimes undefined and the check fails. Let's adjust it to
21832183
# tolerate a null stream.stream_ops value.
21842184
/root/replace.sh "s/if\s*\(stream\.stream_ops\.poll\)/if (stream.stream_ops?.poll)/g" /root/output/php.js; \
2185-
# Emscriptend allows only directories to be mounted, but in Playground we support mounting files, directories, and symlinks.
2185+
# Emscripten allows only directories to be mounted, but in Playground we support mounting files, directories, and symlinks.
21862186
# For file mounting to work, we need to remove the directory check.
21872187
/root/replace-across-lines.sh 's/(\s+)if\s*\(\s*!FS\.isDir\(node\.mode\)\s*\)\s*\{\s+throw\s+new\s+FS\.ErrnoError\(54\)\s*;\s+\}(\s+)/$1$2/s' /root/output/php.js; \
21882188
# Make Emscripten websockets configurable

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { type ErrnoError, type MountHandler } from '@php-wasm/universal';
1+
import { FSHelpers, type MountHandler } from '@php-wasm/universal';
22
import { statSync } from 'fs';
33
import { basename } from 'path';
44

55
export function createNodeFsMountHandler(localPath: string): MountHandler {
6-
return async function (php, FS, vfsMountPoint) {
6+
return function (php, FS, vfsMountPoint) {
77
/**
88
* When Emscripten attempt to mount a local path into VFS, it looks up the path
99
* and adds the local path as a mount to the VFS Node.
@@ -17,47 +17,37 @@ export function createNodeFsMountHandler(localPath: string): MountHandler {
1717
* To work around this, the PHP-wasm compile removes the directory check.
1818
* PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/5821cee231f452d050fd337b99ad0b26ebda487e/packages/php-wasm/compile/php/Dockerfile#L2148
1919
*/
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)) {
3022
if (statSync(localPath).isSymbolicLink()) {
3123
(FS as any).createNode(
3224
FS.lookupPath(vfsMountPoint, { parent: true }).node,
3325
basename(localPath),
3426
110000
3527
);
36-
lookup = FS.lookupPath(vfsMountPoint);
3728
} else if (statSync(localPath).isFile()) {
3829
FS.writeFile(vfsMountPoint, '');
39-
unlinkPath = vfsMountPoint;
4030
} else if (statSync(localPath).isDirectory()) {
4131
FS.mkdirTree(vfsMountPoint);
42-
unlinkPath = vfsMountPoint;
4332
} else {
4433
throw new Error(
4534
'Unsupported file type. PHP-wasm supports only symlinks that link to files, directories, or symlinks.'
4635
);
4736
}
48-
lookup = FS.lookupPath(vfsMountPoint);
37+
removeVfsNode = true;
4938
}
39+
const lookup = FS.lookupPath(vfsMountPoint);
5040
if (!lookup.node) {
5141
throw new Error('Unable to access the mount point in VFS.');
5242
}
5343
FS.mount(FS.filesystems['NODEFS'], { root: localPath }, vfsMountPoint);
5444
return () => {
5545
FS!.unmount(vfsMountPoint);
56-
if (unlinkPath) {
46+
if (removeVfsNode) {
5747
if (FS.isDir(lookup.node.mode)) {
58-
FS.rmdir(unlinkPath);
48+
FS.rmdir(vfsMountPoint);
5949
} else {
60-
FS.unlink(unlinkPath);
50+
FS.unlink(vfsMountPoint);
6151
}
6252
}
6353
};

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)