Files
node/test/parallel/test-vfs-fs-open-callback.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

78 lines
2.5 KiB
JavaScript

// Flags: --experimental-vfs
'use strict';
// fs.open / fs.fstat / fs.read / fs.write / fs.close / fs.ftruncate callbacks
// dispatch through VFS.
const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const vfs = require('node:vfs');
const baseMountPoint = path.resolve('/tmp/vfs-open-cb-' + 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 world');
myVfs.mount(mountPoint);
return { myVfs, mountPoint };
}
// Open + fstat + read + close
{
const { myVfs, mountPoint } = mounted();
fs.open(path.join(mountPoint, 'src/hello.txt'), 'r',
common.mustSucceed((fd) => {
assert.notStrictEqual(fd & 0x40000000, 0);
fs.fstat(fd, common.mustSucceed((stats) => {
assert.strictEqual(stats.isFile(), true);
const buf = Buffer.alloc(11);
fs.read(fd, buf, 0, 11, 0,
common.mustSucceed((n, buffer) => {
assert.strictEqual(n, 11);
assert.strictEqual(buffer.toString(), 'hello world');
fs.close(fd,
common.mustSucceed(() => myVfs.unmount()));
}));
}));
}));
}
// Open + write + close
{
const { myVfs, mountPoint } = mounted();
fs.open(path.join(mountPoint, 'src/aw.txt'), 'w',
common.mustSucceed((fd) => {
const data = Buffer.from('async-fd');
fs.write(fd, data, 0, data.length, 0,
common.mustSucceed((n) => {
assert.strictEqual(n, data.length);
fs.close(fd, common.mustSucceed(() => {
assert.strictEqual(
fs.readFileSync(
path.join(mountPoint, 'src/aw.txt'), 'utf8'),
'async-fd',
);
myVfs.unmount();
}));
}));
}));
}
// ftruncate (cb)
{
const { myVfs, mountPoint } = mounted();
const fd = fs.openSync(path.join(mountPoint, 'src/hello.txt'), 'r+');
fs.ftruncate(fd, 5, common.mustSucceed(() => {
fs.closeSync(fd);
assert.strictEqual(
fs.readFileSync(path.join(mountPoint, 'src/hello.txt'), 'utf8'),
'hello',
);
myVfs.unmount();
}));
}