Skip to content

Commit 21ff331

Browse files
anonrigtargos
authored andcommitted
lib: improve esm resolve performance
PR-URL: #46652 Refs: nodejs/performance#39 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Erick Wendel <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 5a9b71a commit 21ff331

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

lib/internal/modules/esm/resolve.js

+16-22
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,19 @@ const {
2525
} = primordials;
2626
const internalFS = require('internal/fs/utils');
2727
const { BuiltinModule } = require('internal/bootstrap/realm');
28-
const {
29-
realpathSync,
30-
statSync,
31-
Stats,
32-
} = require('fs');
28+
const { realpathSync } = require('fs');
3329
const { getOptionValue } = require('internal/options');
3430
// Do not eagerly grab .manifest, it may be in TDZ
3531
const policy = getOptionValue('--experimental-policy') ?
3632
require('internal/process/policy') :
3733
null;
38-
const { sep, relative } = require('path');
34+
const { sep, relative, toNamespacedPath } = require('path');
3935
const preserveSymlinks = getOptionValue('--preserve-symlinks');
4036
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
4137
const experimentalNetworkImports =
4238
getOptionValue('--experimental-network-imports');
4339
const typeFlag = getOptionValue('--input-type');
44-
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
40+
const { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url');
4541
const { canParse: canParseURL } = internalBinding('url');
4642
const {
4743
ERR_INPUT_TYPE_NOT_ALLOWED,
@@ -61,6 +57,7 @@ const {
6157
const { Module: CJSModule } = require('internal/modules/cjs/loader');
6258
const { getPackageConfig, getPackageScopeConfig } = require('internal/modules/esm/package_config');
6359
const { getConditionsSet } = require('internal/modules/esm/utils');
60+
const { internalModuleStat } = internalBinding('fs');
6461

6562
/**
6663
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig} PackageConfig
@@ -137,19 +134,12 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
137134

138135
const realpathCache = new SafeMap();
139136

140-
/**
141-
* @param {string | URL} path
142-
* @returns {import('fs').Stats}
143-
*/
144-
const tryStatSync =
145-
(path) => statSync(path, { throwIfNoEntry: false }) ?? new Stats();
146-
147137
/**
148138
* @param {string | URL} url
149139
* @returns {boolean}
150140
*/
151141
function fileExists(url) {
152-
return statSync(url, { throwIfNoEntry: false })?.isFile() ?? false;
142+
return internalModuleStat(toNamespacedPath(toPathIfFileURL(url))) === 0;
153143
}
154144

155145
/**
@@ -220,13 +210,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
220210

221211
const path = fileURLToPath(resolved);
222212

223-
const stats = tryStatSync(StringPrototypeEndsWith(path, '/') ?
224-
StringPrototypeSlice(path, -1) : path);
225-
if (stats.isDirectory()) {
213+
const stats = internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
214+
StringPrototypeSlice(path, -1) : path));
215+
216+
// Check for stats.isDirectory()
217+
if (stats === 1) {
226218
const err = new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base));
227219
err.url = String(resolved);
228220
throw err;
229-
} else if (!stats.isFile()) {
221+
} else if (stats !== 0) {
222+
// Check for !stats.isFile()
230223
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
231224
process.send({ 'watch:require': [path || resolved.pathname] });
232225
}
@@ -755,9 +748,10 @@ function packageResolve(specifier, base, conditions) {
755748
let packageJSONPath = fileURLToPath(packageJSONUrl);
756749
let lastPath;
757750
do {
758-
const stat = tryStatSync(StringPrototypeSlice(packageJSONPath, 0,
759-
packageJSONPath.length - 13));
760-
if (!stat.isDirectory()) {
751+
const stat = internalModuleStat(toNamespacedPath(StringPrototypeSlice(packageJSONPath, 0,
752+
packageJSONPath.length - 13)));
753+
// Check for !stat.isDirectory()
754+
if (stat !== 1) {
761755
lastPath = packageJSONPath;
762756
packageJSONUrl = new URL((isScoped ?
763757
'../../../../node_modules/' : '../../../node_modules/') +

0 commit comments

Comments
 (0)