node/lib/internal/vfs/fd.js
Matteo Collina c9562ddb82
vfs: add minimal node:vfs subsystem
Adds the node:vfs builtin module with VirtualFileSystem and
provider classes. No integration with fs, modules, or SEA.

Assisted-by: Claude-Opus4.7
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/63115
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2026-05-23 18:43:31 +00:00

87 lines
1.8 KiB
JavaScript

'use strict';
const {
SafeMap,
Symbol,
} = primordials;
// Private symbols
const kFd = Symbol('kFd');
const kEntry = Symbol('kEntry');
// VFS FDs use bit 30 set to avoid conflicts with real OS fds.
// Real fds are small non-negative integers; VFS fds start at 0x40000000.
const VFS_FD_MASK = 0x40000000;
let nextFd = 0;
// Global registry of open virtual file descriptors
const openFDs = new SafeMap();
/**
* Represents an open virtual file descriptor.
* Wraps a VirtualFileHandle from the provider.
*/
class VirtualFD {
/**
* @param {number} fd The file descriptor number
* @param {VirtualFileHandle} entry The virtual file handle
*/
constructor(fd, entry) {
this[kFd] = fd;
this[kEntry] = entry;
}
/**
* Gets the file descriptor number.
* @returns {number}
*/
get fd() {
return this[kFd];
}
/**
* Gets the file handle.
* @returns {VirtualFileHandle}
*/
get entry() {
return this[kEntry];
}
}
/**
* Opens a virtual file and returns its file descriptor.
* @param {VirtualFileHandle} entry The virtual file handle
* @returns {number} The file descriptor
*/
function openVirtualFd(entry) {
const fd = VFS_FD_MASK | nextFd++;
const vfd = new VirtualFD(fd, entry);
openFDs.set(fd, vfd);
return fd;
}
/**
* Gets a VirtualFD by its file descriptor number.
* @param {number} fd The file descriptor number
* @returns {VirtualFD|undefined}
*/
function getVirtualFd(fd) {
return openFDs.get(fd);
}
/**
* Closes a virtual file descriptor.
* @param {number} fd The file descriptor number
* @returns {boolean} True if the fd was found and closed
*/
function closeVirtualFd(fd) {
return openFDs.delete(fd);
}
module.exports = {
VFS_FD_MASK,
VirtualFD,
openVirtualFd,
getVirtualFd,
closeVirtualFd,
};