Skip to content

Commit 929c80f

Browse files
committed
vfs: replace custom path helpers with standard path module
Remove normalizePath and joinMountPath from router.js in favor of path.normalize, path.resolve, and path.join. Remove unused VFS_FD_BASE export from fd.js.
1 parent e8e9e8a commit 929c80f

File tree

4 files changed

+23
-75
lines changed

4 files changed

+23
-75
lines changed

lib/internal/vfs/fd.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ function closeVirtualFd(fd) {
7979

8080
module.exports = {
8181
VirtualFD,
82-
VFS_FD_BASE,
8382
openVirtualFd,
8483
getVirtualFd,
8584
closeVirtualFd,

lib/internal/vfs/file_system.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ const {
1313
} = require('internal/errors');
1414
const { validateBoolean } = require('internal/validators');
1515
const { MemoryProvider } = require('internal/vfs/providers/memory');
16+
const { normalize, resolve, join, isAbsolute } = require('path');
1617
const {
17-
normalizePath,
1818
isUnderMountPoint,
1919
getRelativePath,
20-
joinMountPath,
21-
isAbsolutePath,
2220
} = require('internal/vfs/router');
2321
const {
2422
openVirtualFd,
@@ -201,18 +199,18 @@ class VirtualFileSystem {
201199
*/
202200
resolvePath(inputPath) {
203201
// If path is absolute, return as-is
204-
if (isAbsolutePath(inputPath)) {
205-
return normalizePath(inputPath);
202+
if (isAbsolute(inputPath)) {
203+
return normalize(inputPath);
206204
}
207205

208206
// If virtual cwd is enabled and set, resolve relative to it
209207
if (this[kVirtualCwdEnabled] && this[kVirtualCwd] !== null) {
210208
const resolved = `${this[kVirtualCwd]}/${inputPath}`;
211-
return normalizePath(resolved);
209+
return normalize(resolved);
212210
}
213211

214-
// Fall back to normalizing the path (will use real cwd)
215-
return normalizePath(inputPath);
212+
// Fall back to resolving the path (will use real cwd)
213+
return resolve(inputPath);
216214
}
217215

218216
// ==================== Mount ====================
@@ -226,7 +224,7 @@ class VirtualFileSystem {
226224
if (this[kMounted]) {
227225
throw new ERR_INVALID_STATE('VFS is already mounted');
228226
}
229-
this[kMountPoint] = normalizePath(prefix);
227+
this[kMountPoint] = normalize(prefix);
230228
this[kMounted] = true;
231229
if (this[kModuleHooks]) {
232230
loadModuleHooks();
@@ -277,7 +275,7 @@ class VirtualFileSystem {
277275
this[kOriginalCwd] = process.cwd;
278276

279277
process.chdir = function chdir(directory) {
280-
const normalized = normalizePath(directory);
278+
const normalized = resolve(directory);
281279

282280
if (vfs.shouldHandle(normalized)) {
283281
vfs.chdir(normalized);
@@ -341,7 +339,7 @@ class VirtualFileSystem {
341339
*/
342340
_toMountedPath(providerPath) {
343341
if (this[kMounted] && this[kMountPoint]) {
344-
return joinMountPath(this[kMountPoint], providerPath);
342+
return join(this[kMountPoint], providerPath);
345343
}
346344
return providerPath;
347345
}
@@ -358,7 +356,7 @@ class VirtualFileSystem {
358356
return false;
359357
}
360358

361-
const normalized = normalizePath(inputPath);
359+
const normalized = normalize(inputPath);
362360
if (!isUnderMountPoint(normalized, this[kMountPoint])) {
363361
return false;
364362
}

lib/internal/vfs/module_hooks.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ const {
99
StringPrototypeStartsWith,
1010
} = primordials;
1111

12-
const { dirname, isAbsolute, resolve } = require('path');
13-
const { normalizePath } = require('internal/vfs/router');
12+
const { dirname, isAbsolute, normalize, resolve } = require('path');
1413
const { isURL, pathToFileURL, fileURLToPath, toPathIfFileURL, URL } = require('internal/url');
1514
const { kEmptyObject } = require('internal/util');
1615
const { validateObject } = require('internal/validators');
@@ -80,7 +79,7 @@ function unregisterVFS(vfs) {
8079
* @returns {{ vfs: VirtualFileSystem, result: number }|null}
8180
*/
8281
function findVFSForStat(filename) {
83-
const normalized = normalizePath(filename);
82+
const normalized = normalize(filename);
8483
for (let i = 0; i < activeVFSList.length; i++) {
8584
const vfs = activeVFSList[i];
8685
if (vfs.shouldHandle(normalized)) {
@@ -102,7 +101,7 @@ function findVFSForStat(filename) {
102101
* @returns {{ vfs: VirtualFileSystem, content: Buffer|string }|null}
103102
*/
104103
function findVFSForRead(filename, options) {
105-
const normalized = normalizePath(filename);
104+
const normalized = normalize(filename);
106105
for (let i = 0; i < activeVFSList.length; i++) {
107106
const vfs = activeVFSList[i];
108107
if (vfs.shouldHandle(normalized)) {
@@ -141,7 +140,7 @@ function findVFSForRead(filename, options) {
141140
* @returns {{ vfs: VirtualFileSystem, exists: boolean }|null}
142141
*/
143142
function findVFSForExists(filename) {
144-
const normalized = normalizePath(filename);
143+
const normalized = normalize(filename);
145144
for (let i = 0; i < activeVFSList.length; i++) {
146145
const vfs = activeVFSList[i];
147146
if (vfs.shouldHandle(normalized)) {
@@ -162,7 +161,7 @@ function findVFSForExists(filename) {
162161
* @returns {{ vfs: VirtualFileSystem, realpath: string }|null}
163162
*/
164163
function findVFSForRealpath(filename) {
165-
const normalized = normalizePath(filename);
164+
const normalized = normalize(filename);
166165
for (let i = 0; i < activeVFSList.length; i++) {
167166
const vfs = activeVFSList[i];
168167
if (vfs.shouldHandle(normalized)) {
@@ -189,7 +188,7 @@ function findVFSForRealpath(filename) {
189188
* @returns {{ vfs: VirtualFileSystem, stats: Stats }|null}
190189
*/
191190
function findVFSForFsStat(filename) {
192-
const normalized = normalizePath(filename);
191+
const normalized = normalize(filename);
193192
for (let i = 0; i < activeVFSList.length; i++) {
194193
const vfs = activeVFSList[i];
195194
if (vfs.shouldHandle(normalized)) {
@@ -217,7 +216,7 @@ function findVFSForFsStat(filename) {
217216
* @returns {{ vfs: VirtualFileSystem, entries: string[]|Dirent[] }|null}
218217
*/
219218
function findVFSForReaddir(dirname, options) {
220-
const normalized = normalizePath(dirname);
219+
const normalized = normalize(dirname);
221220
for (let i = 0; i < activeVFSList.length; i++) {
222221
const vfs = activeVFSList[i];
223222
if (vfs.shouldHandle(normalized)) {
@@ -245,7 +244,7 @@ function findVFSForReaddir(dirname, options) {
245244
* @returns {Promise<{ vfs: VirtualFileSystem, entries: string[]|Dirent[] }|null>}
246245
*/
247246
async function findVFSForReaddirAsync(dirname, options) {
248-
const normalized = normalizePath(dirname);
247+
const normalized = normalize(dirname);
249248
for (let i = 0; i < activeVFSList.length; i++) {
250249
const vfs = activeVFSList[i];
251250
if (vfs.shouldHandle(normalized)) {
@@ -272,7 +271,7 @@ async function findVFSForReaddirAsync(dirname, options) {
272271
* @returns {Promise<{ vfs: VirtualFileSystem, stats: Stats }|null>}
273272
*/
274273
async function findVFSForLstatAsync(filename) {
275-
const normalized = normalizePath(filename);
274+
const normalized = normalize(filename);
276275
for (let i = 0; i < activeVFSList.length; i++) {
277276
const vfs = activeVFSList[i];
278277
if (vfs.shouldHandle(normalized)) {
@@ -300,7 +299,7 @@ async function findVFSForLstatAsync(filename) {
300299
* @returns {{ vfs: VirtualFileSystem }|null}
301300
*/
302301
function findVFSForWatch(filename) {
303-
const normalized = normalizePath(filename);
302+
const normalized = normalize(filename);
304303
for (let i = 0; i < activeVFSList.length; i++) {
305304
const vfs = activeVFSList[i];
306305
if (vfs.shouldHandle(normalized)) {
@@ -387,7 +386,7 @@ function vfsResolveHook(specifier, context, nextResolve) {
387386
}
388387

389388
// Check if any VFS handles this path
390-
const normalized = normalizePath(checkPath);
389+
const normalized = normalize(checkPath);
391390
for (let i = 0; i < activeVFSList.length; i++) {
392391
const vfs = activeVFSList[i];
393392
if (vfs.shouldHandle(normalized) && vfs.existsSync(normalized)) {
@@ -431,7 +430,7 @@ function vfsLoadHook(url, context, nextLoad) {
431430
}
432431

433432
const filePath = fileURLToPath(url);
434-
const normalized = normalizePath(filePath);
433+
const normalized = normalize(filePath);
435434

436435
// Check if any VFS handles this path
437436
for (let i = 0; i < activeVFSList.length; i++) {

lib/internal/vfs/router.js

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,12 @@
11
'use strict';
22

33
const {
4-
StringPrototypeEndsWith,
5-
StringPrototypeReplaceAll,
64
StringPrototypeSlice,
75
StringPrototypeSplit,
86
StringPrototypeStartsWith,
97
} = primordials;
108

11-
const { isAbsolute, posix: pathPosix, resolve, sep } = require('path');
12-
13-
/**
14-
* Normalizes a path for VFS lookup.
15-
* - Resolves to absolute path
16-
* - Removes trailing slashes (except for root)
17-
* - Normalizes separators to forward slashes (VFS uses '/' internally)
18-
* @param {string} inputPath The path to normalize
19-
* @returns {string} The normalized path
20-
*/
21-
function normalizePath(inputPath) {
22-
let normalized = resolve(inputPath);
23-
24-
// On Windows, convert backslashes to forward slashes for consistent VFS lookup.
25-
// VFS uses forward slashes internally regardless of platform.
26-
if (sep === '\\') {
27-
normalized = StringPrototypeReplaceAll(normalized, '\\', '/');
28-
}
29-
30-
// Remove trailing slash (except for root)
31-
if (normalized.length > 1 && StringPrototypeEndsWith(normalized, '/')) {
32-
normalized = StringPrototypeSlice(normalized, 0, -1);
33-
}
34-
35-
return normalized;
36-
}
9+
const { isAbsolute, posix: pathPosix } = require('path');
3710

3811
/**
3912
* Splits a path into segments.
@@ -110,32 +83,11 @@ function getRelativePath(normalizedPath, mountPoint) {
11083
return StringPrototypeSlice(normalizedPath, mountPoint.length);
11184
}
11285

113-
/**
114-
* Joins a mount point with a relative path.
115-
* Note: We don't use path.join here because VFS relative paths start with /
116-
* and path.join would treat them as absolute paths.
117-
* @param {string} mountPoint A normalized mount point path
118-
* @param {string} relativePath A relative path (starting with /)
119-
* @returns {string} The joined absolute path
120-
*/
121-
function joinMountPath(mountPoint, relativePath) {
122-
if (relativePath === '/') {
123-
return mountPoint;
124-
}
125-
// Special case: root mount point - the relative path is already the full path
126-
if (mountPoint === '/') {
127-
return relativePath;
128-
}
129-
return mountPoint + relativePath;
130-
}
131-
13286
module.exports = {
133-
normalizePath,
13487
splitPath,
13588
getParentPath,
13689
getBaseName,
13790
isUnderMountPoint,
13891
getRelativePath,
139-
joinMountPath,
14092
isAbsolutePath: isAbsolute,
14193
};

0 commit comments

Comments
 (0)