Commit 386ec3e
committed
vfs: move fs interception from monkey-patching into lib/fs.js
Instead of monkey-patching fs.readFileSync, fs.statSync, etc. at
runtime in module_hooks.js, add VFS handler guards directly inside the
fs method bodies. This fixes the destructuring problem where capturing
a reference before vfs.mount() would bypass VFS:
const { readFileSync } = require('fs');
vfs.mount('/app');
readFileSync('/app/file.txt'); // now works correctly
The approach:
- Add shared vfsState object and setVfsHandlers() in internal/fs/utils
- Add a null-check guard at the top of 9 sync methods in lib/fs.js
and 3 async methods in lib/internal/fs/promises.js
- Replace installFsPatches() with a handler object registered via
setVfsHandlers() — same logic, no monkey-patching
- Rename module_hooks.js to setup.js (no longer does monkey-patching)
When no VFS is active, the overhead is a single null comparison per
call. When the last VFS is unmounted, handlers are cleared to restore
the zero-overhead path.1 parent 453ca3b commit 386ec3e
File tree
6 files changed
+283
-216
lines changed- lib
- internal
- fs
- vfs
- test/parallel
6 files changed
+283
-216
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
| 130 | + | |
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
| |||
271 | 272 | | |
272 | 273 | | |
273 | 274 | | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
274 | 280 | | |
275 | 281 | | |
276 | 282 | | |
| |||
427 | 433 | | |
428 | 434 | | |
429 | 435 | | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
430 | 441 | | |
431 | 442 | | |
432 | 443 | | |
| |||
1541 | 1552 | | |
1542 | 1553 | | |
1543 | 1554 | | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
1544 | 1560 | | |
1545 | 1561 | | |
1546 | 1562 | | |
| |||
1678 | 1694 | | |
1679 | 1695 | | |
1680 | 1696 | | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
1681 | 1702 | | |
1682 | 1703 | | |
1683 | 1704 | | |
| |||
1707 | 1728 | | |
1708 | 1729 | | |
1709 | 1730 | | |
| 1731 | + | |
| 1732 | + | |
| 1733 | + | |
| 1734 | + | |
| 1735 | + | |
1710 | 1736 | | |
1711 | 1737 | | |
1712 | 1738 | | |
| |||
2496 | 2522 | | |
2497 | 2523 | | |
2498 | 2524 | | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
| 2528 | + | |
| 2529 | + | |
2499 | 2530 | | |
2500 | 2531 | | |
2501 | 2532 | | |
| |||
2563 | 2594 | | |
2564 | 2595 | | |
2565 | 2596 | | |
| 2597 | + | |
| 2598 | + | |
| 2599 | + | |
| 2600 | + | |
| 2601 | + | |
2566 | 2602 | | |
2567 | 2603 | | |
2568 | 2604 | | |
| |||
2605 | 2641 | | |
2606 | 2642 | | |
2607 | 2643 | | |
| 2644 | + | |
| 2645 | + | |
| 2646 | + | |
| 2647 | + | |
2608 | 2648 | | |
2609 | 2649 | | |
2610 | 2650 | | |
| |||
2682 | 2722 | | |
2683 | 2723 | | |
2684 | 2724 | | |
| 2725 | + | |
| 2726 | + | |
| 2727 | + | |
| 2728 | + | |
| 2729 | + | |
2685 | 2730 | | |
2686 | 2731 | | |
2687 | 2732 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
| 73 | + | |
73 | 74 | | |
74 | 75 | | |
75 | 76 | | |
| |||
941 | 942 | | |
942 | 943 | | |
943 | 944 | | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
944 | 950 | | |
945 | 951 | | |
946 | 952 | | |
| |||
1018 | 1024 | | |
1019 | 1025 | | |
1020 | 1026 | | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
1021 | 1032 | | |
1022 | 1033 | | |
1023 | 1034 | | |
| |||
1275 | 1286 | | |
1276 | 1287 | | |
1277 | 1288 | | |
| 1289 | + | |
| 1290 | + | |
| 1291 | + | |
| 1292 | + | |
| 1293 | + | |
| 1294 | + | |
| 1295 | + | |
| 1296 | + | |
1278 | 1297 | | |
1279 | 1298 | | |
1280 | 1299 | | |
| |||
1333 | 1352 | | |
1334 | 1353 | | |
1335 | 1354 | | |
1336 | | - | |
| 1355 | + | |
1337 | 1356 | | |
1338 | 1357 | | |
1339 | 1358 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
915 | 915 | | |
916 | 916 | | |
917 | 917 | | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
918 | 923 | | |
919 | 924 | | |
920 | 925 | | |
| |||
953 | 958 | | |
954 | 959 | | |
955 | 960 | | |
| 961 | + | |
| 962 | + | |
956 | 963 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
| 47 | + | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
52 | 52 | | |
53 | | - | |
54 | | - | |
55 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| |||
228 | 228 | | |
229 | 229 | | |
230 | 230 | | |
231 | | - | |
| 231 | + | |
232 | 232 | | |
233 | 233 | | |
234 | 234 | | |
| |||
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
247 | | - | |
| 247 | + | |
248 | 248 | | |
249 | 249 | | |
250 | 250 | | |
| |||
0 commit comments