Skip to content

Commit ad601c8

Browse files
mcollinaclaude
andcommitted
vfs: add RealFSProvider for mounting real directories
RealFSProvider wraps a real filesystem directory, allowing it to be mounted at a different VFS path. This is useful for: - Mounting a directory at a different path - Enabling virtualCwd support in Worker threads - Creating sandboxed views of real directories The provider prevents path traversal attacks by ensuring resolved paths stay within the configured root directory. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b82cc48 commit ad601c8

File tree

4 files changed

+698
-1
lines changed

4 files changed

+698
-1
lines changed

doc/api/vfs.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ Throws `ERR_INVALID_STATE` if `virtualCwd` was not enabled during construction.
265265
When mounted with `virtualCwd` enabled, the VFS also hooks `process.chdir()` and
266266
`process.cwd()` to support virtual paths transparently. In Worker threads,
267267
`process.chdir()` to virtual paths will work, but attempting to change to real
268-
filesystem paths will throw `ERR_WORKER_UNSUPPORTED_OPERATION`.
268+
file system paths will throw `ERR_WORKER_UNSUPPORTED_OPERATION`.
269269

270270
### `vfs.cwd()`
271271

@@ -456,6 +456,91 @@ try {
456456
}
457457
```
458458

459+
## Class: `RealFSProvider`
460+
461+
<!-- YAML
462+
added: v26.0.0
463+
-->
464+
465+
The `RealFSProvider` wraps a real file system directory, allowing it to be
466+
mounted at a different VFS path. This is useful for:
467+
468+
* Mounting a directory at a different path
469+
* Enabling `virtualCwd` support in Worker threads (by mounting the real
470+
file system through VFS)
471+
* Creating sandboxed views of real directories
472+
473+
### `new RealFSProvider(rootPath)`
474+
475+
<!-- YAML
476+
added: v26.0.0
477+
-->
478+
479+
* `rootPath` {string} The real file system path to use as the provider root.
480+
481+
Creates a new `RealFSProvider` that wraps the specified directory. All paths
482+
accessed through this provider are resolved relative to `rootPath`. Path
483+
traversal outside `rootPath` (via `..`) is prevented for security.
484+
485+
```mjs
486+
import vfs from 'node:vfs';
487+
488+
// Mount /home/user/project at /project
489+
const projectVfs = vfs.create(new vfs.RealFSProvider('/home/user/project'));
490+
projectVfs.mount('/project');
491+
492+
// Now /project/src/index.js maps to /home/user/project/src/index.js
493+
import fs from 'node:fs';
494+
const content = fs.readFileSync('/project/src/index.js', 'utf8');
495+
```
496+
497+
```cjs
498+
const vfs = require('node:vfs');
499+
500+
// Mount /home/user/project at /project
501+
const projectVfs = vfs.create(new vfs.RealFSProvider('/home/user/project'));
502+
projectVfs.mount('/project');
503+
504+
// Now /project/src/index.js maps to /home/user/project/src/index.js
505+
const fs = require('node:fs');
506+
const content = fs.readFileSync('/project/src/index.js', 'utf8');
507+
```
508+
509+
### Using `virtualCwd` in Worker threads
510+
511+
Since `process.chdir()` is not available in Worker threads, you can use
512+
`RealFSProvider` to enable virtual working directory support:
513+
514+
```cjs
515+
const { Worker, isMainThread, parentPort } = require('node:worker_threads');
516+
const vfs = require('node:vfs');
517+
518+
if (isMainThread) {
519+
new Worker(__filename);
520+
} else {
521+
// In worker: mount real file system with virtualCwd enabled
522+
const realVfs = vfs.create(
523+
new vfs.RealFSProvider('/home/user/project'),
524+
{ virtualCwd: true },
525+
);
526+
realVfs.mount('/project');
527+
528+
// Now we can use virtual chdir in the worker
529+
realVfs.chdir('/project/src');
530+
console.log(realVfs.cwd()); // '/project/src'
531+
}
532+
```
533+
534+
### `realFSProvider.rootPath`
535+
536+
<!-- YAML
537+
added: v26.0.0
538+
-->
539+
540+
* {string}
541+
542+
The real file system path that this provider wraps.
543+
459544
## Integration with `fs` module
460545

461546
When a VFS is mounted, the standard `fs` module automatically routes operations

0 commit comments

Comments
 (0)