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>
41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
// Flags: --experimental-vfs
|
|
'use strict';
|
|
|
|
// fs.writeFile and fs.appendFile 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-writeFile-cb-' + process.pid);
|
|
let counter = 0;
|
|
function mounted() {
|
|
const mountPoint = baseMountPoint + '-' + (counter++);
|
|
const myVfs = vfs.create();
|
|
myVfs.mkdirSync('/src', { recursive: true });
|
|
myVfs.mount(mountPoint);
|
|
return { myVfs, mountPoint };
|
|
}
|
|
|
|
// writeFile (cb)
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
const target = path.join(mountPoint, 'src/cb-w.txt');
|
|
fs.writeFile(target, 'cbw', common.mustSucceed(() => {
|
|
assert.strictEqual(fs.readFileSync(target, 'utf8'), 'cbw');
|
|
myVfs.unmount();
|
|
}));
|
|
}
|
|
|
|
// appendFile (cb)
|
|
{
|
|
const { myVfs, mountPoint } = mounted();
|
|
const target = path.join(mountPoint, 'src/cb-a.txt');
|
|
fs.writeFileSync(target, 'base');
|
|
fs.appendFile(target, ' more', common.mustSucceed(() => {
|
|
assert.strictEqual(fs.readFileSync(target, 'utf8'), 'base more');
|
|
myVfs.unmount();
|
|
}));
|
|
}
|