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>
48 lines
1.4 KiB
JavaScript
48 lines
1.4 KiB
JavaScript
// Flags: --experimental-vfs
|
|
'use strict';
|
|
|
|
// fs.readdirSync dispatches to VFS, including the buffer-encoding and
|
|
// withFileTypes options.
|
|
|
|
require('../common');
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const vfs = require('node:vfs');
|
|
|
|
const mountPoint = path.resolve('/tmp/vfs-readdirSync-' + process.pid);
|
|
const myVfs = vfs.create();
|
|
myVfs.mkdirSync('/src/subdir', { recursive: true });
|
|
myVfs.writeFileSync('/src/hello.txt', 'hello');
|
|
myVfs.writeFileSync('/src/data.json', '{}');
|
|
myVfs.mount(mountPoint);
|
|
|
|
// Default (utf8 string array)
|
|
{
|
|
const entries = fs.readdirSync(path.join(mountPoint, 'src'));
|
|
assert.ok(entries.includes('hello.txt'));
|
|
assert.ok(entries.includes('data.json'));
|
|
assert.ok(entries.includes('subdir'));
|
|
}
|
|
|
|
// withFileTypes: true -> Dirent array
|
|
{
|
|
const dirents = fs.readdirSync(path.join(mountPoint, 'src'),
|
|
{ withFileTypes: true });
|
|
const hello = dirents.find((d) => d.name === 'hello.txt');
|
|
assert.ok(hello);
|
|
assert.strictEqual(hello.isFile(), true);
|
|
const subdir = dirents.find((d) => d.name === 'subdir');
|
|
assert.strictEqual(subdir.isDirectory(), true);
|
|
}
|
|
|
|
// encoding: 'buffer' -> Buffer entries
|
|
{
|
|
const entries = fs.readdirSync(path.join(mountPoint, 'src'),
|
|
{ encoding: 'buffer' });
|
|
assert.ok(entries.every(Buffer.isBuffer));
|
|
assert.ok(entries.some((b) => b.toString() === 'hello.txt'));
|
|
}
|
|
|
|
myVfs.unmount();
|