Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/php-wasm/node/src/lib/node-fs-mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
type MountHandler,
} from '@php-wasm/universal';
import { isParentOf } from '@php-wasm/util';
import { lstatSync } from 'fs';
import { realpathSync, statSync } from 'fs';
import { dirname } from 'path';

export function createNodeFsMountHandler(localPath: string): MountHandler {
Expand All @@ -23,16 +23,19 @@ export function createNodeFsMountHandler(localPath: string): MountHandler {
* PHP-WASM source: https://github.com/WordPress/wordpress-playground/blob/5821cee231f452d050fd337b99ad0b26ebda487e/packages/php-wasm/compile/php/Dockerfile#L2148
*/
let removeVfsNode = false;
const mountRoot = realpathSync(localPath);
if (!FSHelpers.fileExists(FS, vfsMountPoint)) {
const lstat = lstatSync(localPath);
if (lstat.isFile() || lstat.isSymbolicLink()) {
// Resolve symlinks so both the VFS placeholder and NODEFS root
// match the target's file-or-directory shape.
const stat = statSync(mountRoot);
if (stat.isFile()) {
FS.mkdirTree(dirname(vfsMountPoint));
FS.writeFile(vfsMountPoint, '');
} else if (lstat.isDirectory()) {
} else if (stat.isDirectory()) {
FS.mkdirTree(vfsMountPoint);
} else {
throw new Error(
'Unsupported file type. PHP-wasm supports only symlinks that link to files, directories, or symlinks.'
'Unsupported file type. PHP-wasm supports mounting only files and directories, including symlinks that resolve to files or directories.'
);
}
removeVfsNode = true;
Expand All @@ -49,7 +52,7 @@ export function createNodeFsMountHandler(localPath: string): MountHandler {
}
throw e;
}
FS.mount(FS.filesystems['NODEFS'], { root: localPath }, vfsMountPoint);
FS.mount(FS.filesystems['NODEFS'], { root: mountRoot }, vfsMountPoint);
return () => {
FS!.unmount(vfsMountPoint);
if (removeVfsNode) {
Expand Down
16 changes: 13 additions & 3 deletions packages/php-wasm/node/src/test/mount.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ describe('Mounting', () => {
createNodeFsMountHandler(filePath)
);

const originalContent = await php.readFileAsText(
fileMountPoint
);
const originalContent =
await php.readFileAsText(fileMountPoint);
await php.writeFile(fileMountPoint, 'new content');

expect(await php.readFileAsText(fileMountPoint)).toBe(
Expand Down Expand Up @@ -523,6 +522,17 @@ describe('Mounting', () => {
expect(php.isDir(directoryMountPoint)).toBe(true);
});

it('Should create a directory node when mounting a directory', async () => {
await php.mount(
directoryMountPoint,
createNodeFsMountHandler(directoryPath)
);

const FS = php[__private__dont__use].FS;
const lookup = FS.lookupPath(directoryMountPoint);
expect(FS.isDir(lookup.node.mode)).toBe(true);
});

it('Should remount mounted directory after unmounting', async () => {
const unmount = await php.mount(
directoryMountPoint,
Expand Down
Loading