Skip to content

Commit 0a02db1

Browse files
committed
vfs: intercept all supported fs methods when VFS is mounted
Previously only 9 sync methods in fs.js and 3 async methods in promises.js were intercepted, causing callback-based and promise-based operations on VFS paths to fall through to the real filesystem. Add interception for ~40 fs methods across sync, callback, promise, FD-based, and stream APIs. Add VirtualWriteStream, writeSync, rmSync, and rm to the VFS class. Document all intercepted methods in vfs.md.
1 parent a737d76 commit 0a02db1

File tree

7 files changed

+1949
-9
lines changed

7 files changed

+1949
-9
lines changed

doc/api/vfs.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,74 @@ fs.createReadStream('/virtual/hello.txt'); // Stream
703703
fs.readFileSync('/etc/passwd'); // Real file
704704
```
705705

706+
### Intercepted `node:fs` methods
707+
708+
The following `node:fs` methods are intercepted when a VFS is mounted. Each
709+
method is intercepted in its synchronous, callback, and/or promise form.
710+
711+
**Path-based read operations** (synchronous, callback, and promise):
712+
713+
* `existsSync()`, `exists()`
714+
* `statSync()`, `stat()`, `fs.promises.stat()`
715+
* `lstatSync()`, `lstat()`, `fs.promises.lstat()`
716+
* `readFileSync()`, `readFile()`, `fs.promises.readFile()`
717+
* `readdirSync()`, `readdir()`, `fs.promises.readdir()`
718+
* `realpathSync()`, `realpath()`, `fs.promises.realpath()`
719+
* `accessSync()`, `access()`, `fs.promises.access()`
720+
* `readlinkSync()`, `readlink()`, `fs.promises.readlink()`
721+
722+
**Path-based write operations** (synchronous, callback, and promise):
723+
724+
* `writeFileSync()`, `writeFile()`, `fs.promises.writeFile()`
725+
* `appendFileSync()`, `appendFile()`, `fs.promises.appendFile()`
726+
* `mkdirSync()`, `mkdir()`, `fs.promises.mkdir()`
727+
* `rmdirSync()`, `rmdir()`, `fs.promises.rmdir()`
728+
* `rmSync()`, `rm()`, `fs.promises.rm()`
729+
* `unlinkSync()`, `unlink()`, `fs.promises.unlink()`
730+
* `renameSync()`, `rename()`, `fs.promises.rename()`
731+
* `copyFileSync()`, `copyFile()`, `fs.promises.copyFile()`
732+
* `symlinkSync()`, `symlink()`, `fs.promises.symlink()`
733+
734+
**File descriptor operations** (synchronous and callback):
735+
736+
* `openSync()`, `open()`
737+
* `closeSync()`, `close()`
738+
* `readSync()`, `read()`
739+
* `writeSync()`, `write()`
740+
* `fstatSync()`, `fstat()`
741+
742+
Virtual file descriptors use values starting at 10000 to avoid conflicts with
743+
real file descriptors.
744+
745+
**Stream operations**:
746+
747+
* `createReadStream()`
748+
* `createWriteStream()`
749+
750+
**Watch operations**:
751+
752+
* `watch()`, `fs.promises.watch()`
753+
* `watchFile()`
754+
* `unwatchFile()`
755+
756+
### `node:fs` methods with no VFS equivalent
757+
758+
The following `node:fs` methods are **not** intercepted and always operate on
759+
the real file system:
760+
761+
* `chmod()`, `chmodSync()`, `fchmod()`, `fchmodSync()`
762+
* `chown()`, `chownSync()`, `fchown()`, `fchownSync()`
763+
* `truncate()`, `truncateSync()`, `ftruncate()`, `ftruncateSync()`
764+
* `utimes()`, `utimesSync()`, `futimes()`, `futimesSync()`, `lutimes()`,
765+
`lutimesSync()`
766+
* `link()`, `linkSync()`
767+
* `fdatasync()`, `fdatasyncSync()`, `fsync()`, `fsyncSync()`
768+
* `mkdtemp()`, `mkdtempSync()`
769+
* `cp()`, `cpSync()`
770+
* `glob()`, `globSync()`
771+
* `statfs()`, `statfsSync()`
772+
* `opendir()`, `opendirSync()`
773+
706774
## Integration with module loading
707775

708776
Virtual files can be loaded as modules using `require()` or `import`:

0 commit comments

Comments
 (0)