mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
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>
65 lines
2.0 KiB
JavaScript
65 lines
2.0 KiB
JavaScript
// Flags: --experimental-vfs
|
|
'use strict';
|
|
|
|
// The promise-based fs methods that accept `encoding: 'buffer'` must convert
|
|
// the (string) provider result into a Buffer before resolving.
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const fsp = require('fs/promises');
|
|
const path = require('path');
|
|
const vfs = require('node:vfs');
|
|
|
|
const baseMountPoint = path.resolve('/tmp/vfs-buf-enc-' + process.pid);
|
|
let counter = 0;
|
|
function mounted() {
|
|
const mountPoint = baseMountPoint + '-' + (counter++);
|
|
const myVfs = vfs.create();
|
|
myVfs.mkdirSync('/src', { recursive: true });
|
|
myVfs.writeFileSync('/src/hello.txt', 'hello');
|
|
myVfs.mount(mountPoint);
|
|
return { myVfs, mountPoint };
|
|
}
|
|
|
|
(async () => {
|
|
// readdir
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
const entries = await fsp.readdir(path.join(mountPoint, 'src'),
|
|
{ encoding: 'buffer' });
|
|
assert.ok(entries.every(Buffer.isBuffer));
|
|
assert.ok(entries.some((b) => b.toString() === 'hello.txt'));
|
|
myVfs.unmount();
|
|
}
|
|
|
|
// realpath
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
const p = path.join(mountPoint, 'src/hello.txt');
|
|
const rp = await fsp.realpath(p, { encoding: 'buffer' });
|
|
assert.ok(Buffer.isBuffer(rp));
|
|
assert.strictEqual(rp.toString(), p);
|
|
myVfs.unmount();
|
|
}
|
|
|
|
// readlink
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
await fsp.symlink('hello.txt', path.join(mountPoint, 'src/ln.txt'));
|
|
const target = await fsp.readlink(path.join(mountPoint, 'src/ln.txt'),
|
|
{ encoding: 'buffer' });
|
|
assert.ok(Buffer.isBuffer(target));
|
|
assert.strictEqual(target.toString(), 'hello.txt');
|
|
myVfs.unmount();
|
|
}
|
|
|
|
// mkdtemp
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
const dir = await fsp.mkdtemp(path.join(mountPoint, 'src/td-'),
|
|
{ encoding: 'buffer' });
|
|
assert.ok(Buffer.isBuffer(dir));
|
|
myVfs.unmount();
|
|
}
|
|
})().then(common.mustCall());
|