mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
"useCodeCache" was rejected together with "useVfs" because the SEA code cache is only consumed by the embedder compile paths, while a main script in the VFS runs through the regular loaders and a main-only cache would not cover the modules it depends on anyway. Compile every JavaScript (and, with --strip-types, TypeScript) module in the VFS when the preparation blob is generated, without executing them, through a portable CompileCacheHandler keyed by the path relative to the mount point. The entries are serialized in the cache file layout and embedded as reserved node:sea:compile-cache/<key> assets, which the SEA provider hides from the mount. Modules that fail to compile are skipped. CompileCacheHandler gains a key root for portable caches and in-memory serialization of its entries, shared with Persist(). The build runs in a Node.js instance whose environment ignores NODE_COMPILE_CACHE. Reading the embedded entries at startup is left to a follow-up. Tests skip up front when building a single executable application on Windows without signtool, so the async VFS code cache test is skipped cleanly instead of failing on an unmatched mustCall. Signed-off-by: Matteo Collina <hello@matteocollina.com> Assisted-by: pi PR-URL: https://github.com/nodejs/node/pull/66768 Reviewed-By: Paolo Insogna <paolo@cowtech.it>
846 lines
27 KiB
Markdown
846 lines
27 KiB
Markdown
# Virtual File System
|
|
|
|
<!--introduced_in=v26.4.0-->
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
> Stability: 1 - Experimental
|
|
|
|
<!-- source_link=lib/vfs.js -->
|
|
|
|
The `node:vfs` module provides a virtual file system with a `node:fs`-like API.
|
|
It is useful for tests, fixtures, embedded assets, and other scenarios where you
|
|
need a self-contained file system without touching the actual file-system.
|
|
|
|
To access it:
|
|
|
|
```mjs
|
|
import vfs from 'node:vfs';
|
|
```
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
```
|
|
|
|
This module is only available under the `node:` scheme. Loading it emits an
|
|
`ExperimentalWarning`.
|
|
|
|
## Security
|
|
|
|
The VFS API is not a sandbox, permission system, or access-control mechanism.
|
|
It does not isolate untrusted code from the host file system or from other
|
|
Node.js capabilities. Code that can access a [`VirtualFileSystem`][] instance,
|
|
mount it, select its provider, or pass paths to it is trusted application code.
|
|
|
|
Mounting a VFS only redirects supported [`node:fs`][] calls whose resolved paths
|
|
are under the mount point. It does not prevent code from using other paths or
|
|
other Node.js APIs to access resources available to the process.
|
|
[`RealFSProvider`][] maps VFS paths under its configured root and rejects paths
|
|
that resolve outside that root, but that check is not a security boundary.
|
|
[`ZipProvider`][] has no real file-system paths of its own to escape; its
|
|
entries only ever exist within the archive's own namespace. Do not rely on VFS
|
|
to run untrusted code; use operating-system-level isolation, such as separate
|
|
users, containers, or platform sandboxes, when a security boundary is
|
|
required.
|
|
|
|
## Basic usage
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
|
|
const myVfs = vfs.create();
|
|
myVfs.mkdirSync('/dir', { recursive: true });
|
|
myVfs.writeFileSync('/dir/hello.txt', 'Hello, VFS!');
|
|
|
|
console.log(myVfs.readFileSync('/dir/hello.txt', 'utf8')); // 'Hello, VFS!'
|
|
```
|
|
|
|
`vfs.create()` returns a [`VirtualFileSystem`][] instance backed by a
|
|
[`MemoryProvider`][] by default. The instance exposes synchronous,
|
|
callback-based, and promise-based file system methods that mirror the
|
|
shape of the [`node:fs`][] API. All paths are POSIX-style and absolute
|
|
(starting with `/`).
|
|
|
|
By default, the file tree is private to the VFS instance. To expose
|
|
it through the global `node:fs` module, `require()`, and `import`,
|
|
call [`vfs.mount()`][]; call [`vfs.unmount()`][] (or rely on a
|
|
`using` declaration) to detach again.
|
|
|
|
## `vfs.create([provider][, options])`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
* `provider` {VirtualProvider} The provider to use. **Default:**
|
|
`new MemoryProvider()`.
|
|
* `options` {Object}
|
|
* `emitExperimentalWarning` {boolean} Whether to emit an experimental warning
|
|
when an instance is created. **Default:** `true`. Loading `node:vfs` emits
|
|
the warning regardless of this option.
|
|
* Returns: {VirtualFileSystem}
|
|
|
|
Convenience factory equivalent to `new VirtualFileSystem(provider, options)`.
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
|
|
// Default in-memory provider
|
|
const memoryVfs = vfs.create();
|
|
|
|
// Explicit provider
|
|
const realVfs = vfs.create(new vfs.RealFSProvider('/tmp/vfs-root'));
|
|
```
|
|
|
|
## `vfs.registerProvider(entry)`
|
|
|
|
<!-- YAML
|
|
added: v26.10.0
|
|
-->
|
|
|
|
* `entry` {Object}
|
|
* `name` {string} A short identifier, used in diagnostics.
|
|
* `canHandle` {Function} Called with the resolved path and its
|
|
[`fs.Stats`][]. Returns `true` if this provider should back the source.
|
|
* `create` {Function} Called with the resolved path and its [`fs.Stats`][].
|
|
Returns the {VirtualProvider} backing the source.
|
|
|
|
Registers a provider that [`--vfs-load`][] can select for a source it
|
|
recognizes, so a file format Node.js has no built-in provider for can still be
|
|
mounted.
|
|
|
|
A source is claimed by the first provider whose `canHandle()` returns `true`.
|
|
Registered providers are consulted before the built-in ones, newest
|
|
registration first, and are offered directories as well as files, so a
|
|
registered provider can back, wrap, or vet any source. If none claims the
|
|
source, the built-in providers handle it: a directory with
|
|
[`RealFSProvider`][], and a file whose bytes are a ZIP archive with
|
|
[`ZipProvider`][].
|
|
|
|
Providers must be registered before the source is mounted. Register from a
|
|
module preloaded with [`--require`][] or [`--import`][]:
|
|
|
|
```cjs
|
|
// provider.js, preloaded with --require
|
|
const fs = require('node:fs');
|
|
const vfs = require('node:vfs');
|
|
|
|
const MAGIC = Buffer.from('CUSTOMFMT');
|
|
|
|
vfs.registerProvider({
|
|
name: 'customfmt',
|
|
canHandle(path, stats) {
|
|
if (!stats.isFile()) return false;
|
|
const head = Buffer.alloc(MAGIC.length);
|
|
const fd = fs.openSync(path, 'r');
|
|
try {
|
|
fs.readSync(fd, head, 0, MAGIC.length, 0);
|
|
} finally {
|
|
fs.closeSync(fd);
|
|
}
|
|
return head.equals(MAGIC);
|
|
},
|
|
create(path) {
|
|
return new MyCustomProvider(path);
|
|
},
|
|
});
|
|
```
|
|
|
|
```console
|
|
$ node --require ./provider.js \
|
|
--vfs-load archive.customfmt
|
|
```
|
|
|
|
## `vfs.vfsBase()`
|
|
|
|
<!-- YAML
|
|
added: v26.11.0
|
|
-->
|
|
|
|
* Returns: {string} The absolute path of the [reserved root directory][].
|
|
|
|
Returns the directory that holds the mount points of every mounted virtual file
|
|
system, which is `path.join(os.devNull, 'vfs')`. Reading it lists what is
|
|
mounted; see [The reserved root directory][reserved root directory].
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
const fs = require('node:fs');
|
|
|
|
const myVfs = vfs.create();
|
|
const mountPoint = myVfs.mount();
|
|
|
|
fs.readdirSync(vfs.vfsBase()); // The name of every mount point in it
|
|
mountPoint.startsWith(vfs.vfsBase()); // true
|
|
```
|
|
|
|
## Class: `VirtualFileSystem`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
A `VirtualFileSystem` wraps a [`VirtualProvider`][] and exposes a
|
|
`node:fs`-like API. Each instance maintains its own file tree.
|
|
|
|
### `new VirtualFileSystem([provider][, options])`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
* `provider` {VirtualProvider} The provider to use. **Default:**
|
|
`new MemoryProvider()`.
|
|
* `options` {Object}
|
|
* `emitExperimentalWarning` {boolean} Whether to emit an experimental warning
|
|
when an instance is created. **Default:** `true`. Loading `node:vfs` emits
|
|
the warning regardless of this option.
|
|
|
|
### `vfs.mount()`
|
|
|
|
<!-- YAML
|
|
added: v26.9.0
|
|
-->
|
|
|
|
* Returns: {string} The absolute mount point.
|
|
|
|
Mounts the virtual file system and returns the resulting mount point.
|
|
After mounting, files in the VFS can be accessed through the
|
|
`node:fs` module and resolved through `require()` and `import`
|
|
using paths under the returned mount point.
|
|
|
|
Mount points always live inside a reserved namespace that cannot have child file system entries,
|
|
so virtual paths never conflate with (or shadow) real paths. A mount point is obtained from what
|
|
`vfs.mount()` returns or from [`vfs.mountPoint`][], and the mount points of all mounted file
|
|
systems can be listed by reading the [reserved root directory][], whose path [`vfs.vfsBase()`][]
|
|
returns. The name of a mount point within that directory is assigned at runtime, so it is not
|
|
something to construct or hard-code.
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
const fs = require('node:fs');
|
|
|
|
const myVfs = vfs.create();
|
|
myVfs.writeFileSync('/data.txt', 'Hello');
|
|
const mountPoint = myVfs.mount();
|
|
// e.g. '/dev/null/vfs/1'
|
|
|
|
fs.readFileSync(`${mountPoint}/data.txt`, 'utf8'); // 'Hello'
|
|
```
|
|
|
|
Like any mount point, the mount point cannot be removed or renamed, nor
|
|
replaced by renaming something else onto it: [`fs.rmdir()`][] and
|
|
[`fs.rename()`][] fail with `EBUSY`. A recursive [`fs.rm()`][] of the mount
|
|
point empties the file system before failing the same way.
|
|
|
|
Each `VirtualFileSystem` instance may be mounted at most once at a
|
|
time. Attempting to mount an already-mounted instance throws
|
|
`ERR_INVALID_STATE`. Because each instance mounts inside its own
|
|
per-layer namespace, mounts from different instances can never
|
|
overlap.
|
|
|
|
The VFS supports the [Explicit Resource Management][] proposal. Use
|
|
a `using` declaration to unmount automatically when leaving scope:
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
const fs = require('node:fs');
|
|
|
|
let mountPoint;
|
|
{
|
|
using myVfs = vfs.create();
|
|
myVfs.writeFileSync('/data.txt', 'Hello');
|
|
mountPoint = myVfs.mount();
|
|
|
|
fs.readFileSync(`${mountPoint}/data.txt`, 'utf8'); // 'Hello'
|
|
} // VFS is automatically unmounted here
|
|
|
|
fs.existsSync(`${mountPoint}/data.txt`); // false
|
|
```
|
|
|
|
### `vfs.unmount()`
|
|
|
|
<!-- YAML
|
|
added: v26.9.0
|
|
-->
|
|
|
|
Unmounts the virtual file system. After unmounting, virtual files
|
|
are no longer reachable through `node:fs`, `require()`, or `import`.
|
|
The same instance may be mounted again by calling `mount()`.
|
|
|
|
This method is idempotent: calling `unmount()` on a VFS that is not
|
|
currently mounted has no effect.
|
|
|
|
### `vfs.mounted`
|
|
|
|
<!-- YAML
|
|
added: v26.9.0
|
|
-->
|
|
|
|
* {boolean}
|
|
|
|
`true` while the VFS is mounted; `false` otherwise.
|
|
|
|
### `vfs.mountPoint`
|
|
|
|
<!-- YAML
|
|
added: v26.9.0
|
|
-->
|
|
|
|
* {string | null}
|
|
|
|
The current mount point as an absolute string (the value returned by
|
|
the last [`vfs.mount()`][] call), or `null` when the VFS is not
|
|
mounted.
|
|
|
|
### `vfs.mountPointURL`
|
|
|
|
<!-- YAML
|
|
added: v26.9.0
|
|
-->
|
|
|
|
* {string | null}
|
|
|
|
The current mount point as a `file:` URL string (the [`vfs.mountPoint`][]
|
|
path converted with [`url.pathToFileURL()`][]), or `null` when the VFS
|
|
is not mounted.
|
|
|
|
This is a convenience for addressing mounted files with URL-based
|
|
APIs such as dynamic `import()`:
|
|
|
|
```mjs
|
|
import vfs from 'node:vfs';
|
|
|
|
const myVfs = vfs.create();
|
|
myVfs.writeFileSync('/mod.mjs', 'export const value = 42;');
|
|
myVfs.mount();
|
|
|
|
const { value } = await import(`${myVfs.mountPointURL}/mod.mjs`);
|
|
console.log(value); // 42
|
|
|
|
myVfs.unmount();
|
|
```
|
|
|
|
### `vfs.provider`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
* {VirtualProvider}
|
|
|
|
The provider backing this VFS instance.
|
|
|
|
### `vfs.readonly`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
* {boolean}
|
|
|
|
`true` when the underlying provider is read-only.
|
|
|
|
### APIs
|
|
|
|
`VirtualFileSystem` implements the following methods, with the same
|
|
signatures as their [`node:fs`][] counterparts:
|
|
|
|
#### Synchronous API
|
|
|
|
* `existsSync(path)`
|
|
* `statSync(path[, options])`
|
|
* `lstatSync(path[, options])`
|
|
* `readFileSync(path[, options])`
|
|
* `writeFileSync(path, data[, options])`
|
|
* `appendFileSync(path, data[, options])`
|
|
* `readdirSync(path[, options])`
|
|
* `mkdirSync(path[, options])`
|
|
* `rmdirSync(path)`
|
|
* `unlinkSync(path)`
|
|
* `renameSync(oldPath, newPath)`
|
|
* `copyFileSync(src, dest[, mode])`
|
|
* `realpathSync(path[, options])`
|
|
* `readlinkSync(path[, options])`
|
|
* `symlinkSync(target, path[, type])`
|
|
* `accessSync(path[, mode])`
|
|
* `rmSync(path[, options])`
|
|
* `truncateSync(path[, len])`
|
|
* `ftruncateSync(fd[, len])`
|
|
* `linkSync(existingPath, newPath)`
|
|
* `chmodSync(path, mode)`
|
|
* `chownSync(path, uid, gid)`
|
|
* `lchownSync(path, uid, gid)`
|
|
* `utimesSync(path, atime, mtime)`
|
|
* `lutimesSync(path, atime, mtime)`
|
|
* `mkdtempSync(prefix)`
|
|
* `mkstempSync(prefix)`
|
|
* `opendirSync(path[, options])`
|
|
* `openAsBlob(path[, options])`
|
|
* File-descriptor ops: `openSync`, `closeSync`, `readSync`, `writeSync`,
|
|
`fstatSync`
|
|
* Streams: `createReadStream`, `createWriteStream`
|
|
* Watchers: `watch`, `watchFile`, `unwatchFile`
|
|
|
|
#### Callback API
|
|
|
|
`readFile`, `writeFile`, `stat`, `lstat`, `readdir`, `realpath`, `readlink`,
|
|
`access`, `open`, `close`, `read`, `write`, `rm`, `fstat`, `truncate`,
|
|
`ftruncate`, `link`, `mkdtemp`, `mkstemp`, `opendir`. Each takes a
|
|
Node.js-style callback `(err, ...result) => {}`.
|
|
|
|
#### Promise API
|
|
|
|
`vfs.promises` exposes the promise-based variants:
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
|
|
async function example() {
|
|
const myVfs = vfs.create();
|
|
await myVfs.promises.writeFile('/file.txt', 'hello');
|
|
const data = await myVfs.promises.readFile('/file.txt', 'utf8');
|
|
return data;
|
|
}
|
|
example();
|
|
```
|
|
|
|
The promise namespace mirrors `fs.promises` and includes `readFile`,
|
|
`writeFile`, `appendFile`, `stat`, `lstat`, `readdir`, `mkdir`, `rmdir`,
|
|
`unlink`, `rename`, `copyFile`, `realpath`, `readlink`, `symlink`,
|
|
`access`, `rm`, `truncate`, `link`, `mkdtemp`, `mkstemp`, `chmod`, `chown`,
|
|
`lchown`, `utimes`, `lutimes`, `open`, `lchmod`, and `watch`.
|
|
|
|
## The reserved root directory
|
|
|
|
While any virtual file system is mounted, the directory that holds the mount
|
|
points can be read through [`node:fs`][]. [`vfs.vfsBase()`][] returns its path,
|
|
`path.join(os.devNull, 'vfs')`. It contains a directory for every mounted file
|
|
system, named like the last segment of its [`vfs.mountPoint`][].
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const root = vfs.vfsBase();
|
|
const assets = vfs.create();
|
|
assets.writeFileSync('/logo.svg', '<svg/>');
|
|
const mountPoint = assets.mount();
|
|
|
|
const name = path.basename(mountPoint);
|
|
fs.readdirSync(root); // [ name ]
|
|
fs.readdirSync(root, { recursive: true }); // [ name, `${name}/logo.svg` ]
|
|
```
|
|
|
|
The root directory itself is read-only. Creating, removing, or changing its
|
|
entries fails with `EROFS`, while the file systems its entries lead to can be
|
|
written to as usual. When nothing is mounted, the root directory does not exist.
|
|
|
|
## Module loader integration
|
|
|
|
Once a `VirtualFileSystem` is mounted, paths under the mount point
|
|
participate in module resolution and loading. The [CommonJS
|
|
resolution algorithm][] used by [`require()`][] and
|
|
[`require.resolve()`][] and the [ES modules resolution algorithm][]
|
|
used by `import` and [`import.meta.resolve()`][] are unchanged;
|
|
instead, every file system operation those algorithms perform is
|
|
dispatched on the path being probed: paths under a mount point are
|
|
served by the owning VFS, and all other paths are served by the real
|
|
file system. Files served from the VFS therefore behave as
|
|
first-class modules.
|
|
|
|
Because mounted paths live in a reserved namespace that cannot exist
|
|
on disk, any given path is served either by exactly one VFS or by
|
|
the real file system, never both. There is no search order or
|
|
fallback between the two: if a path under a mount point does not
|
|
exist in the VFS, resolution fails with `ENOENT` without consulting
|
|
the disk, and a mounted layer never shadows a real directory.
|
|
|
|
For resolution purposes the mount point behaves as a file system
|
|
root: `package.json` scope lookups and [loading from `node_modules`
|
|
folders][] stop at the mount point. For example, when
|
|
`${mountPoint}/foo/bar/main.cjs` calls `require('baz')`, the lookup
|
|
goes through:
|
|
|
|
* `${mountPoint}/foo/bar/node_modules/baz`
|
|
* `${mountPoint}/foo/node_modules/baz`
|
|
* `${mountPoint}/node_modules/baz`
|
|
* If `$NODE_PATH` is set, the folders listed in `$NODE_PATH`
|
|
* `$HOME/.node_modules/baz`
|
|
* `$HOME/.node_libraries/baz`
|
|
* `$PREFIX/lib/node/baz`
|
|
|
|
The last four entries are [the global folders][], which are legacy
|
|
CommonJS behavior and do not apply to `import`. Absolute specifiers
|
|
may cross the boundary in either direction: a module on the real
|
|
file system can `require()` a mounted path, and a virtual module can
|
|
`require()` a real one.
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
|
|
const myVfs = vfs.create();
|
|
myVfs.mkdirSync('/lib');
|
|
myVfs.writeFileSync('/lib/greet.js', 'module.exports = () => "hi";');
|
|
myVfs.writeFileSync(
|
|
'/lib/package.json', '{"main": "./greet.js"}');
|
|
const mountPoint = myVfs.mount();
|
|
|
|
const greet = require(`${mountPoint}/lib`);
|
|
console.log(greet()); // 'hi'
|
|
|
|
myVfs.unmount();
|
|
```
|
|
|
|
For ECMAScript modules, use `file:` URLs when passing mounted paths
|
|
to dynamic `import()`. [`vfs.mountPointURL`][] provides the mount
|
|
point in that form; this keeps VFS imports portable on Windows,
|
|
where mounted paths use Windows path syntax.
|
|
|
|
```mjs
|
|
import vfs from 'node:vfs';
|
|
|
|
const myVfs = vfs.create();
|
|
myVfs.writeFileSync('/mod.mjs', 'export const value = 42;');
|
|
myVfs.mount();
|
|
|
|
const { value } = await import(`${myVfs.mountPointURL}/mod.mjs`);
|
|
console.log(value); // 42
|
|
|
|
myVfs.unmount();
|
|
```
|
|
|
|
CommonJS modules loaded from a mounted VFS are identified by their VFS paths
|
|
that start with the mount point. This is reflected in, for example, `__filename` and
|
|
`__dirname` in the module, or the errors stack traces involving functions from
|
|
the VFS modules. ES modules in the VFS are similarly identified by the `file:` URL of
|
|
their VFS paths and this is reflected in e.g. `import.meta.url`.
|
|
|
|
Like modules loaded from the real file system, modules loaded from the VFS are
|
|
cached on the first load. When `require()` or `import()` is used to load an absolute
|
|
path or URL that falls under the mounted VFS multiple times, the module is only loaded
|
|
once and subsequent calls return the same instance.
|
|
|
|
Calling [`vfs.unmount()`][] invalidates the modules that were loaded
|
|
from the mount point: a subsequent `require()` or `import` of a path
|
|
under a re-created mount re-reads the file from the newly mounted
|
|
VFS rather than returning a stale module. Modules loaded from other
|
|
VFS instances or from the real file system are unaffected.
|
|
|
|
Mounting and unmounting do not stop any module execution that is
|
|
already started, or invalidate any objects materialized from VFS
|
|
modules that are already executed. As with modules in the real file
|
|
system, the callers are responsible for avoiding removal or
|
|
invalidation of modules in the virtual file system while they are
|
|
being loaded.
|
|
|
|
Native addons (`.node` files) stored in a mounted VFS can be `require()`d as
|
|
well. The operating system's dynamic loader cannot open a virtual path, so the
|
|
addon's bytes are read from the VFS and loaded from a private, self-cleaning
|
|
temporary image instead. Addons on the real file system are unaffected and
|
|
load directly.
|
|
|
|
Shared libraries opened through [`ffi.dlopen()`][] (or
|
|
[`new ffi.DynamicLibrary()`][]) work the same way: a library path inside a
|
|
mounted VFS is detected, its bytes are read from the VFS, and the library is
|
|
loaded from a private, self-cleaning image while `library.path` keeps
|
|
reporting the virtual path. Libraries on the real file system load directly.
|
|
|
|
## Use with Single Executable Applications
|
|
|
|
When running as a [Single Executable Application][] built with
|
|
`"useVfs": true` in the SEA configuration, the bundled assets are
|
|
automatically mounted as a read-only virtual file system and the injected
|
|
main script is executed from the root of the mount. No additional setup is
|
|
required. Since the mount point is reserved and chosen at runtime, bundled
|
|
code accesses the assets through `__dirname`-relative paths and relative
|
|
`require()` calls rather than through a fixed path:
|
|
|
|
```cjs
|
|
// In the SEA main script, __dirname is the root of the mounted assets.
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const config = JSON.parse(
|
|
fs.readFileSync(path.join(__dirname, 'config.json'), 'utf8'));
|
|
const template = fs.readFileSync(
|
|
path.join(__dirname, 'templates/index.html'), 'utf8');
|
|
```
|
|
|
|
ESM entry points (`"mainFormat": "module"`) are supported: the main module
|
|
is loaded from inside the mount through the ESM loader, and
|
|
`import.meta.dirname` points at the mount root.
|
|
|
|
`"useVfs"` cannot be used together with `"useSnapshot"`; the SEA
|
|
configuration parser will error if the combination is detected. With
|
|
`"useCodeCache"`, every module in the mount is compiled at build time and its
|
|
code cache is embedded into the executable.
|
|
|
|
Instead of listing individual `"assets"`, the SEA configuration can point
|
|
`"vfsArchive"` at a prebuilt ZIP archive; the mount is then backed by a
|
|
[`ZipProvider`][] over the embedded archive, and each file is inflated when
|
|
it is read. See [Serving the assets from a ZIP archive][] for details.
|
|
|
|
See the [Single Executable Application][] documentation for more information
|
|
on creating SEA builds with assets.
|
|
|
|
## Class: `VirtualProvider`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
The base class for all VFS providers. Subclasses implement the essential
|
|
primitives (such as `open`, `stat`, `readdir`, `mkdir`, `rmdir`, `unlink`,
|
|
`rename`, etc.) and inherit default implementations of the derived
|
|
methods (such as `readFile`, `writeFile`, `exists`, `copyFile`, `access`, etc.).
|
|
|
|
### Capability flags
|
|
|
|
* `provider.readonly` {boolean} **Default:** `false`.
|
|
* `provider.supportsSymlinks` {boolean} **Default:** `false`.
|
|
* `provider.supportsWatch` {boolean} **Default:** `false`.
|
|
|
|
### Creating custom providers
|
|
|
|
```cjs
|
|
const { VirtualProvider } = require('node:vfs');
|
|
|
|
class StaticProvider extends VirtualProvider {
|
|
get readonly() { return true; }
|
|
|
|
statSync(path) { /* ... */ }
|
|
openSync(path, flags) { /* ... */ }
|
|
readdirSync(path, options) { /* ... */ }
|
|
// ...
|
|
}
|
|
```
|
|
|
|
The base class throws `ERR_METHOD_NOT_IMPLEMENTED` for any primitive
|
|
that has not been overridden, and rejects writes from a `readonly`
|
|
provider with `EROFS`.
|
|
|
|
## Class: `MemoryProvider`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
The default in-memory provider. Stores files, directories, and symbolic
|
|
links in a `Map`-backed tree, supports symlinks (`supportsSymlinks ===
|
|
true`), and supports watching (`supportsWatch === true`).
|
|
|
|
### `memoryProvider.setReadOnly()`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
Locks the provider into read-only mode. Subsequent writes through any
|
|
[`VirtualFileSystem`][] using this provider throw `EROFS`. There is no
|
|
way to revert the provider to writable.
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
|
|
const provider = new vfs.MemoryProvider();
|
|
const myVfs = vfs.create(provider);
|
|
myVfs.writeFileSync('/seed.txt', 'initial');
|
|
|
|
provider.setReadOnly();
|
|
|
|
myVfs.writeFileSync('/x.txt', 'fail'); // throws EROFS
|
|
```
|
|
|
|
## Class: `ComposableProvider`
|
|
|
|
<!-- YAML
|
|
added: v26.12.0
|
|
-->
|
|
|
|
[`ComposableProvider`][] combines one or more providers in priority order. The first
|
|
provider is the writable layer; reads search from first to last. Directories
|
|
are merged, with entries in higher-priority layers shadowing entries with the
|
|
same name in lower layers. Writes to a lower file copy it to the first provider
|
|
before changing it. Removing a file hides lower copies without deleting them.
|
|
The first provider must be writable to change the composed file system.
|
|
|
|
### `new ComposableProvider(providers)`
|
|
|
|
<!-- YAML
|
|
added: v26.12.0
|
|
-->
|
|
|
|
* `providers` {VirtualProvider\[]} Non-empty array of providers, ordered from
|
|
highest to lowest priority.
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
|
|
const memory = new vfs.MemoryProvider();
|
|
const disk = new vfs.RealFSProvider('/tmp/vfs-root');
|
|
const combined = vfs.create(new vfs.ComposableProvider([memory, disk]));
|
|
combined.writeFileSync('/config.json', '{"debug":true}');
|
|
// The file in memory shadows /tmp/vfs-root/config.json.
|
|
```
|
|
|
|
### `composableProvider.providers`
|
|
|
|
<!-- YAML
|
|
added: v26.12.0
|
|
-->
|
|
|
|
* {VirtualProvider\[]}
|
|
|
|
A copy of the ordered provider list. Changes to this array do not affect the
|
|
composition. File handles opened before a write continue to refer to the layer
|
|
on which they were opened. Watching a path watches only its currently selected
|
|
provider, not changes across the entire composition. Symbolic links are
|
|
resolved by the provider containing them, not across providers. Traversal
|
|
through a symbolic-link directory is not supported by the composition. Renaming
|
|
a directory over a directory that exists only in a lower layer is not supported.
|
|
Layer selection and copy-up use synchronous provider operations, including
|
|
when invoked through the asynchronous VFS API.
|
|
|
|
## Class: `RealFSProvider`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
A provider that wraps a directory (i.e. one on the actual file system) and
|
|
exposes its contents through the VFS API. All VFS paths are resolved relative to
|
|
the root and verified to stay inside it; symbolic links resolving outside the
|
|
root are rejected. This path mapping is not a sandbox or access-control
|
|
mechanism.
|
|
|
|
### `new RealFSProvider(rootPath)`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
* `rootPath` {string} The absolute file-system path to use as the root.
|
|
Must be a non-empty string.
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
|
|
const realVfs = vfs.create(new vfs.RealFSProvider('/tmp/vfs-root'));
|
|
realVfs.writeFileSync('/file.txt', 'hello'); // writes /tmp/vfs-root/file.txt
|
|
```
|
|
|
|
### `realFSProvider.rootPath`
|
|
|
|
<!-- YAML
|
|
added: v26.4.0
|
|
-->
|
|
|
|
* {string}
|
|
|
|
The resolved absolute path used as the root.
|
|
|
|
## Class: `ZipProvider`
|
|
|
|
<!-- YAML
|
|
added: v26.9.0
|
|
-->
|
|
|
|
A provider that exposes the entries of a ZIP archive - either a
|
|
[`zlib.ZipBuffer`][] (in memory) or a [`zlib.ZipFile`][] (on disk) - through
|
|
the VFS API. `provider.readonly` reflects the archive's own
|
|
[`zipFile.writable`][] flag: a `ZipBuffer` is always writable, and a
|
|
`ZipFile` is writable only when opened with `{ writable: true }`.
|
|
|
|
Directories are recognized both explicitly (an entry whose name ends in `/`)
|
|
and implicitly (any entry name starting with `"<dir>/"`), and are listed by
|
|
`readdir()`, including with `{ recursive: true }`, either way. Because a ZIP
|
|
member cannot be edited or read in place - only fully written or fully
|
|
decompressed - a file opened for writing only commits its content (as a new
|
|
archive entry) when the handle is closed.
|
|
|
|
Every method has a synchronous counterpart (`openSync()`, `statSync()`,
|
|
`readdirSync()`, and so on), backed by the equally complete synchronous
|
|
surface [`zlib.ZipBuffer`][]/[`zlib.ZipFile`][] expose. As with those, the
|
|
synchronous methods here block the Node.js event loop and further JavaScript
|
|
execution until the operation - including any deflate/inflate pass -
|
|
completes.
|
|
|
|
```cjs
|
|
const vfs = require('node:vfs');
|
|
const zlib = require('node:zlib');
|
|
const { readFileSync } = require('node:fs');
|
|
|
|
async function main() {
|
|
const zip = new zlib.ZipBuffer(readFileSync('archive.zip'));
|
|
const archiveVfs = vfs.create(new vfs.ZipProvider(zip));
|
|
|
|
console.log(await archiveVfs.promises.readdir('/'));
|
|
await archiveVfs.promises.writeFile('/new.txt', 'hello');
|
|
}
|
|
main();
|
|
```
|
|
|
|
### `new ZipProvider(source)`
|
|
|
|
<!-- YAML
|
|
added: v26.9.0
|
|
-->
|
|
|
|
* `source` {zlib.ZipBuffer|zlib.ZipFile} An already-open archive.
|
|
|
|
## Implementation details
|
|
|
|
### `Stats` objects
|
|
|
|
VFS `Stats` objects are real instances of [`fs.Stats`][] (or
|
|
[`fs.BigIntStats`][] when `{ bigint: true }` is requested). Their
|
|
fields use synthetic but stable values:
|
|
|
|
* `dev` is `4085` (the VFS device id).
|
|
* `ino` is monotonically increasing per process.
|
|
* `blksize` is `4096`.
|
|
* `blocks` is `Math.ceil(size / 512)`.
|
|
* Times default to the moment the entry was created/last modified.
|
|
|
|
[CommonJS resolution algorithm]: modules.md#all-together
|
|
[ES modules resolution algorithm]: esm.md#resolution-algorithm
|
|
[Explicit Resource Management]: https://github.com/tc39/proposal-explicit-resource-management
|
|
[Serving the assets from a ZIP archive]: single-executable-applications.md#serving-the-assets-from-a-zip-archive-with-vfsarchive
|
|
[Single Executable Application]: single-executable-applications.md
|
|
[`--import`]: cli.md#--importmodule
|
|
[`--require`]: cli.md#-r---require-module
|
|
[`--vfs-load`]: cli.md#--vfs-loadsource
|
|
[`ComposableProvider`]: #class-composableprovider
|
|
[`MemoryProvider`]: #class-memoryprovider
|
|
[`RealFSProvider`]: #class-realfsprovider
|
|
[`VirtualFileSystem`]: #class-virtualfilesystem
|
|
[`VirtualProvider`]: #class-virtualprovider
|
|
[`ZipProvider`]: #class-zipprovider
|
|
[`ffi.dlopen()`]: ffi.md#ffidlopenpath-definitions
|
|
[`fs.BigIntStats`]: fs.md#class-fsstats
|
|
[`fs.Stats`]: fs.md#class-fsstats
|
|
[`fs.rename()`]: fs.md#fsrenameoldpath-newpath-callback
|
|
[`fs.rm()`]: fs.md#fsrmpath-options-callback
|
|
[`fs.rmdir()`]: fs.md#fsrmdirpath-options-callback
|
|
[`import.meta.resolve()`]: esm.md#importmetaresolvespecifier
|
|
[`new ffi.DynamicLibrary()`]: ffi.md#new-dynamiclibrarypath
|
|
[`node:fs`]: fs.md
|
|
[`require()`]: modules.md#requireid
|
|
[`require.resolve()`]: modules.md#requireresolverequest-options
|
|
[`url.pathToFileURL()`]: url.md#urlpathtofileurlpath-options
|
|
[`vfs.mount()`]: #vfsmount
|
|
[`vfs.mountPointURL`]: #vfsmountpointurl
|
|
[`vfs.mountPoint`]: #vfsmountpoint
|
|
[`vfs.unmount()`]: #vfsunmount
|
|
[`vfs.vfsBase()`]: #vfsvfsbase
|
|
[`zipFile.writable`]: zlib.md#zipfilewritable
|
|
[`zlib.ZipBuffer`]: zlib.md#class-zlibzipbuffer
|
|
[`zlib.ZipFile`]: zlib.md#class-zlibzipfile
|
|
[loading from `node_modules` folders]: modules.md#loading-from-node_modules-folders
|
|
[reserved root directory]: #the-reserved-root-directory
|
|
[the global folders]: modules.md#loading-from-the-global-folders
|