Skip to content

Commit b437e54

Browse files
committed
doc: document overlay mode behavior for fs operations
1 parent c083f03 commit b437e54

1 file changed

Lines changed: 48 additions & 5 deletions

File tree

doc/api/vfs.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,40 @@ is useful for:
3737
* Creating virtual module systems
3838
* Embedding configuration or data files in applications
3939

40-
## Mount mode
40+
## Operating modes
4141

42-
The VFS operates in **mount mode only**. When mounted at a path prefix (e.g.,
43-
`/virtual`), the VFS handles all operations for paths starting with that
44-
prefix. There is no overlay mode that would merge virtual and real file system
45-
contents at the same paths.
42+
The VFS supports two operating modes:
43+
44+
### Standard mode (default)
45+
46+
When mounted at a path prefix (e.g., `/virtual`), the VFS handles **all**
47+
operations for paths starting with that prefix. The VFS completely shadows
48+
any real file system paths under the mount point.
49+
50+
### Overlay mode
51+
52+
When created with `{ overlay: true }`, the VFS selectively intercepts only
53+
paths that exist within the VFS. Paths that don't exist in the VFS fall through
54+
to the real file system. This is useful for mocking specific files while leaving
55+
others unchanged.
56+
57+
```cjs
58+
const vfs = require('node:vfs');
59+
const fs = require('node:fs');
60+
61+
// Overlay mode: only intercept files that exist in VFS
62+
const myVfs = vfs.create({ overlay: true });
63+
myVfs.writeFileSync('/etc/config.json', '{"mocked": true}');
64+
myVfs.mount('/');
65+
66+
// This reads from VFS (file exists in VFS)
67+
fs.readFileSync('/etc/config.json', 'utf8'); // '{"mocked": true}'
68+
69+
// This reads from real FS (file doesn't exist in VFS)
70+
fs.readFileSync('/etc/hostname', 'utf8'); // Real file content
71+
```
72+
73+
See [Security considerations][] for important warnings about overlay mode.
4674

4775
## Basic usage
4876

@@ -318,6 +346,21 @@ All paths are relative to the VFS root (not the mount point).
318346
These methods accept the same argument types as their `fs` counterparts,
319347
including `string`, `Buffer`, `TypedArray`, and `DataView` where applicable.
320348

349+
#### Overlay mode behavior
350+
351+
When overlay mode is enabled, the following behavior applies to `fs` operations
352+
on mounted paths:
353+
354+
* **Read operations** (`readFile`, `readdir`, `stat`, `lstat`, `access`,
355+
`exists`, `realpath`, `readlink`): Check VFS first. If the path doesn't exist
356+
in VFS, fall through to the real file system.
357+
* **Write operations** (`writeFile`, `appendFile`, `mkdir`, `rename`, `unlink`,
358+
`rmdir`, `symlink`, `copyFile`): Always operate on VFS. New files are created
359+
in VFS, and attempting to modify a real file that doesn't exist in VFS will
360+
create a new VFS file instead.
361+
* **File descriptors**: Once a file is opened, all subsequent operations on that
362+
descriptor stay within the same layer (VFS or real FS) where it was opened.
363+
321364
#### Synchronous Methods
322365

323366
* `vfs.accessSync(path[, mode])` - Check file accessibility

0 commit comments

Comments
 (0)