Commit 310bc02
committed
vfs: mount inside a reserved namespace
Move VFS mounts into `${os.devNull}/vfs/layer-<id>/<prefix>`, a
reserved location that cannot exist on the real file system because
`os.devNull` is a character device on POSIX and a device-namespace
path on Windows - neither can have child filesystem entries. Virtual
paths never conflate with real paths, and the owning layer of a path
is decidable by inspecting the path itself.
The prefix passed to `vfs.mount(prefix)` becomes a purely logical name
inside the namespace - it is never resolved against `process.cwd()`.
`mount()` returns the resulting absolute mount point. Two instances
using the same prefix cannot collide, so overlap detection is gone.
Because the namespace makes ownership self-describing, this removes:
- the per-VFS `ownedFilenames` / `ownedPathCacheKeys` sets;
- the `Module._pathCache` write recorder;
- the `?vfs-layer=<id>` search-param tagging in the ESM resolver;
- the Windows drive-less-URL fixup in finalizeResolution.
Dispatch collapses to one normalization, one prefix check against the
namespace root, and a map lookup on the layer id - constant time in the
number of mounted layers. Unmount purges loader caches by a prefix scan
of the mount point, which fixes the two correctness gaps flagged in
review: symlink realpaths inside a VFS always resolve under the mount
point, so cached entries under any symlinked realpath are purged; and
resolved URLs are plain file URLs under the mount point, so
`import(import.meta.resolve(x))` hits the same module job on repeat.
`os.devNull` was chosen over `process.execPath` because it is
ASCII-clean by definition. This keeps mount points safe to embed in
dynamic-`import()` specifiers even when Node.js itself is installed
under a directory whose name contains URL-reserved characters (a case
exercised by the "linux-arm" CI job that runs from a path containing
`?` and other unusual characters).
`fs.statSync` / `fs.lstatSync` now catch ENOENT from the VFS handler
when `throwIfNoEntry` is false, matching the native binding contract.
The VFS handler surfaces ENOENT unconditionally for missing entries;
the fall-through to the real file system would otherwise traverse
`os.devNull` and produce ENOTDIR.
Docs, tests and benchmark adapted. `benchmark/vfs/bench-fs-dispatch.js`
now reports flat per-call latency across 1..10 mounted layers, versus
~80 ns/layer for stat/exists/access and ~200 ns/layer for readFile
before this change.
Refs: https://github.com/nodejs/single-executable/blob/main/docs/virtual-file-system-requirements.md#no-interference-with-valid-paths-in-the-file-system
Signed-off-by: Matteo Collina <hello@matteocollina.com>1 parent 133a8a1 commit 310bc02
61 files changed
Lines changed: 860 additions & 1099 deletions
File tree
- benchmark/vfs
- doc/api
- lib
- internal
- modules
- cjs
- esm
- vfs
- test/parallel
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | | - | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | | - | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
115 | | - | |
| 115 | + | |
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
120 | 120 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
132 | 141 | | |
133 | 142 | | |
134 | 143 | | |
135 | 144 | | |
136 | 145 | | |
137 | 146 | | |
138 | 147 | | |
139 | | - | |
| 148 | + | |
| 149 | + | |
140 | 150 | | |
141 | | - | |
| 151 | + | |
142 | 152 | | |
143 | 153 | | |
144 | 154 | | |
145 | 155 | | |
146 | | - | |
147 | | - | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
148 | 159 | | |
149 | 160 | | |
150 | 161 | | |
| |||
153 | 164 | | |
154 | 165 | | |
155 | 166 | | |
| 167 | + | |
156 | 168 | | |
157 | 169 | | |
158 | 170 | | |
159 | | - | |
| 171 | + | |
160 | 172 | | |
161 | | - | |
| 173 | + | |
162 | 174 | | |
163 | 175 | | |
164 | | - | |
| 176 | + | |
165 | 177 | | |
166 | 178 | | |
167 | 179 | | |
| |||
196 | 208 | | |
197 | 209 | | |
198 | 210 | | |
199 | | - | |
200 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
201 | 214 | | |
202 | 215 | | |
203 | 216 | | |
| |||
212 | 225 | | |
213 | 226 | | |
214 | 227 | | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
225 | 232 | | |
226 | 233 | | |
227 | 234 | | |
| |||
322 | 329 | | |
323 | 330 | | |
324 | 331 | | |
325 | | - | |
| 332 | + | |
326 | 333 | | |
327 | 334 | | |
328 | 335 | | |
| |||
339 | 346 | | |
340 | 347 | | |
341 | 348 | | |
342 | | - | |
| 349 | + | |
343 | 350 | | |
344 | | - | |
| 351 | + | |
345 | 352 | | |
346 | 353 | | |
347 | 354 | | |
348 | 355 | | |
349 | 356 | | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
376 | 363 | | |
377 | | - | |
378 | | - | |
379 | | - | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
380 | 369 | | |
381 | 370 | | |
382 | 371 | | |
| |||
507 | 496 | | |
508 | 497 | | |
509 | 498 | | |
510 | | - | |
511 | 499 | | |
512 | 500 | | |
513 | 501 | | |
514 | 502 | | |
515 | 503 | | |
516 | 504 | | |
517 | 505 | | |
518 | | - | |
| 506 | + | |
519 | 507 | | |
520 | 508 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2044 | 2044 | | |
2045 | 2045 | | |
2046 | 2046 | | |
2047 | | - | |
2048 | | - | |
| 2047 | + | |
| 2048 | + | |
| 2049 | + | |
| 2050 | + | |
| 2051 | + | |
| 2052 | + | |
| 2053 | + | |
2049 | 2054 | | |
2050 | 2055 | | |
2051 | 2056 | | |
| |||
2078 | 2083 | | |
2079 | 2084 | | |
2080 | 2085 | | |
2081 | | - | |
2082 | | - | |
| 2086 | + | |
| 2087 | + | |
| 2088 | + | |
| 2089 | + | |
| 2090 | + | |
| 2091 | + | |
| 2092 | + | |
2083 | 2093 | | |
2084 | 2094 | | |
2085 | 2095 | | |
| |||
0 commit comments