Skip to content

Commit 9517bc5

Browse files
committed
Revert mountNODEFSMountPoint
1 parent 8f637e9 commit 9517bc5

File tree

2 files changed

+51
-63
lines changed

2 files changed

+51
-63
lines changed

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

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,57 @@ import {
55
} from '@php-wasm/universal';
66
import { statSync } from 'fs';
77
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-
}
548

559
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;
5725
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, '');
6243
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+
);
6351
}
64-
} else {
65-
unlinkPath = vfsMountPoint;
52+
lookup = FS.lookupPath(vfsMountPoint);
6653
}
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);
7159
return () => {
7260
FS!.unmount(vfsMountPoint);
7361
if (unlinkPath) {

packages/php-wasm/node/src/test/mounting.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe('Mounting', () => {
150150

151151
expect(php.isFile(fileMountPoint)).toBe(true);
152152

153-
await unmount();
153+
unmount();
154154
expect(php.isFile(fileMountPoint)).toBe(false);
155155
});
156156

@@ -160,7 +160,7 @@ describe('Mounting', () => {
160160
createNodeFsMountHandler(filePath)
161161
);
162162

163-
await unmount();
163+
unmount();
164164
await php.mount(
165165
fileMountPoint,
166166
createNodeFsMountHandler(filePath)
@@ -184,7 +184,7 @@ describe('Mounting', () => {
184184

185185
expect(php.isFile(mountPoint)).toBe(true);
186186

187-
await unmount();
187+
unmount();
188188
expect(php.isDir(dirname(mountPoint))).toBe(true);
189189
});
190190
});
@@ -498,7 +498,7 @@ describe('Mounting', () => {
498498

499499
expect(php.isDir(directoryMountPoint)).toBe(true);
500500

501-
await unmount();
501+
unmount();
502502
expect(php.isDir(directoryMountPoint)).toBe(false);
503503
});
504504

@@ -511,7 +511,7 @@ describe('Mounting', () => {
511511

512512
expect(php.isDir(directoryMountPoint)).toBe(true);
513513

514-
await unmount();
514+
unmount();
515515
expect(php.isDir(directoryMountPoint)).toBe(true);
516516
});
517517

@@ -521,7 +521,7 @@ describe('Mounting', () => {
521521
createNodeFsMountHandler(directoryPath)
522522
);
523523

524-
await unmount();
524+
unmount();
525525
await php.mount(
526526
directoryMountPoint,
527527
createNodeFsMountHandler(directoryPath)

0 commit comments

Comments
 (0)