Files
node/test/parallel/test-vfs-multi-mount.js
T
Matteo Collina 569369f927 vfs: dispatch fs/promises to mounted VFS instances
Add mount/unmount lifecycle on `VirtualFileSystem`, a handler registry
that fs.js and fs/promises.js consult via `vfsState.handlers`, and a
router that maps absolute paths to the VFS that owns them. When a VFS
is mounted, the public `fs.*` and `fs/promises` APIs (including
streams, `fs.watch`, and `opendir`) dispatch to the provider for paths
under the mount point, and fall through to the real filesystem
otherwise. Includes per-method dispatch tests, error-path coverage,
multi-mount routing tests, and router unit tests.

Ref: https://github.com/nodejs/node/pull/63115

Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/63537
Refs: https://github.com/nodejs/node/pull/63115
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
2026-05-29 17:57:34 +00:00

66 lines
2.1 KiB
JavaScript

// Flags: --experimental-vfs
'use strict';
// Two concurrent non-overlapping mounts must each route to its own VFS without
// interference. Also exercises that the handler registry iterates and routes
// correctly when more than one VFS is active.
require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vfs = require('node:vfs');
const baseA = path.resolve('/tmp/vfs-multi-a-' + process.pid);
const baseB = path.resolve('/tmp/vfs-multi-b-' + process.pid);
const a = vfs.create();
a.writeFileSync('/file.txt', 'from-a');
a.mkdirSync('/dir', { recursive: true });
a.writeFileSync('/dir/inside.txt', 'a-inside');
const b = vfs.create();
b.writeFileSync('/file.txt', 'from-b');
b.mkdirSync('/dir', { recursive: true });
b.writeFileSync('/dir/inside.txt', 'b-inside');
a.mount(baseA);
b.mount(baseB);
// Each mount sees its own content
assert.strictEqual(fs.readFileSync(path.join(baseA, 'file.txt'), 'utf8'),
'from-a');
assert.strictEqual(fs.readFileSync(path.join(baseB, 'file.txt'), 'utf8'),
'from-b');
// Per-mount directory listings are isolated
assert.deepStrictEqual(
fs.readdirSync(baseA).sort(),
['dir', 'file.txt'],
);
assert.deepStrictEqual(
fs.readdirSync(baseB).sort(),
['dir', 'file.txt'],
);
// Writing to one mount doesn't bleed into the other
fs.writeFileSync(path.join(baseA, 'only-a.txt'), 'A');
assert.strictEqual(fs.existsSync(path.join(baseB, 'only-a.txt')), false);
assert.strictEqual(fs.readFileSync(path.join(baseA, 'only-a.txt'), 'utf8'),
'A');
// realpathSync returns the mount-rooted path (proves #toMountedPath on each)
assert.strictEqual(fs.realpathSync(path.join(baseA, 'file.txt')),
path.join(baseA, 'file.txt'));
assert.strictEqual(fs.realpathSync(path.join(baseB, 'file.txt')),
path.join(baseB, 'file.txt'));
// Unmount one, the other still works
a.unmount();
assert.strictEqual(a.mounted, false);
assert.strictEqual(b.mounted, true);
assert.strictEqual(fs.readFileSync(path.join(baseB, 'file.txt'), 'utf8'),
'from-b');
b.unmount();