mirror of
https://github.com/nodejs/node.git
synced 2026-10-28 10:15:07 +08:00
Enable node:vfs by default. --no-experimental-vfs disables node:vfs exposure and --vfs-load. Assisted-by: pi Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: https://github.com/nodejs/node/pull/66318 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
// Flags: --disable-warning=ExperimentalWarning
|
|
'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 myVfs = vfs.create();
|
|
myVfs.mkdirSync('/src/subdir', { recursive: true });
|
|
myVfs.writeFileSync('/src/hello.txt', 'hello');
|
|
myVfs.writeFileSync('/src/data.json', '{}');
|
|
const mountPoint = myVfs.mount();
|
|
|
|
// 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();
|