Skip to content

Commit 7a18822

Browse files
committed
vfs: address remaining review feedback from avivkeller
Inline VFS_FD_BASE constant since it was only used once. Replace custom getFormatFromExtension with extensionFormatMap from internal/modules/esm/formats.
1 parent 77851e9 commit 7a18822

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

lib/internal/vfs/fd.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ const kFd = Symbol('kFd');
1010
const kEntry = Symbol('kEntry');
1111

1212
// FD range: 10000+ to avoid conflicts with real fds
13-
const VFS_FD_BASE = 10_000;
14-
let nextFd = VFS_FD_BASE;
13+
let nextFd = 10_000;
1514

1615
// Global registry of open virtual file descriptors
1716
const openFDs = new SafeMap();

lib/internal/vfs/module_hooks.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ const {
55
ArrayPrototypePush,
66
ArrayPrototypeSplice,
77
FunctionPrototypeCall,
8-
StringPrototypeEndsWith,
98
StringPrototypeStartsWith,
109
} = primordials;
1110

12-
const { dirname, isAbsolute, normalize, resolve } = require('path');
11+
const { dirname, extname, isAbsolute, normalize, resolve } = require('path');
12+
const { extensionFormatMap } = require('internal/modules/esm/formats');
1313
const { isURL, pathToFileURL, fileURLToPath, toPathIfFileURL, URL } = require('internal/url');
1414
const { kEmptyObject } = require('internal/util');
1515
const { validateObject } = require('internal/validators');
@@ -321,22 +321,18 @@ function findVFSForWatch(filename) {
321321

322322
/**
323323
* Determine module format from file extension.
324-
* @param {string} url The file URL
325-
* @returns {string} The format ('module', 'commonjs', or 'json')
324+
* Uses the shared extensionFormatMap, falling back to commonjs for .js
325+
* and unknown extensions since VFS does not check package.json "type".
326+
* @param {string} filePath The file path
327+
* @returns {string} The format ('module', 'commonjs', 'json', etc.)
326328
*/
327-
function getFormatFromExtension(url) {
328-
if (StringPrototypeEndsWith(url, '.mjs')) {
329-
return 'module';
330-
}
331-
if (StringPrototypeEndsWith(url, '.cjs')) {
329+
function getFormatFromExtension(filePath) {
330+
const ext = extname(filePath);
331+
if (ext === '.js') {
332+
// TODO: Check package.json "type" field for proper detection
332333
return 'commonjs';
333334
}
334-
if (StringPrototypeEndsWith(url, '.json')) {
335-
return 'json';
336-
}
337-
// Default to commonjs for .js files
338-
// TODO: Check package.json "type" field for proper detection
339-
return 'commonjs';
335+
return extensionFormatMap[ext] ?? 'commonjs';
340336
}
341337

342338
/**

0 commit comments

Comments
 (0)