Skip to content

Commit 9a4b57d

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 89ed24b commit 9a4b57d

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,24 +25,20 @@ 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
const pendingDeprecation = getOptionValue('--pending-deprecation');
3531
// Do not eagerly grab .manifest, it may be in TDZ
3632
const policy = getOptionValue('--experimental-policy') ?
3733
require('internal/process/policy') :
3834
null;
39-
const { sep, relative, resolve } = require('path');
35+
const { sep, relative, resolve, toNamespacedPath } = require('path');
4036
const preserveSymlinks = getOptionValue('--preserve-symlinks');
4137
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
4238
const experimentalNetworkImports =
4339
getOptionValue('--experimental-network-imports');
4440
const typeFlag = getOptionValue('--input-type');
45-
const { URL, pathToFileURL, fileURLToPath } = require('internal/url');
41+
const { URL, pathToFileURL, fileURLToPath, toPathIfFileURL } = require('internal/url');
4642
const {
4743
ERR_INPUT_TYPE_NOT_ALLOWED,
4844
ERR_INVALID_MODULE_SPECIFIER,
@@ -60,6 +56,7 @@ const { Module: CJSModule } = require('internal/modules/cjs/loader');
6056
const packageJsonReader = require('internal/modules/package_json_reader');
6157
const { getPackageConfig, getPackageScopeConfig } = require('internal/modules/esm/package_config');
6258
const { getConditionsSet } = require('internal/modules/esm/utils');
59+
const { internalModuleStat } = internalBinding('fs');
6360

6461
/**
6562
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig} PackageConfig
@@ -138,19 +135,12 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
138135

139136
const realpathCache = new SafeMap();
140137

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

156146
/**
@@ -285,13 +275,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
285275
path = fileURLToPath(resolved);
286276
}
287277

288-
const stats = tryStatSync(StringPrototypeEndsWith(path, '/') ?
289-
StringPrototypeSlice(path, -1) : path);
290-
if (stats.isDirectory()) {
278+
const stats = internalModuleStat(toNamespacedPath(StringPrototypeEndsWith(path, '/') ?
279+
StringPrototypeSlice(path, -1) : path));
280+
281+
// Check for stats.isDirectory()
282+
if (stats === 1) {
291283
const err = new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base));
292284
err.url = String(resolved);
293285
throw err;
294-
} else if (!stats.isFile()) {
286+
} else if (stats !== 0) {
287+
// Check for !stats.isFile()
295288
if (process.env.WATCH_REPORT_DEPENDENCIES && process.send) {
296289
process.send({ 'watch:require': [path || resolved.pathname] });
297290
}
@@ -826,9 +819,10 @@ function packageResolve(specifier, base, conditions) {
826819
let packageJSONPath = fileURLToPath(packageJSONUrl);
827820
let lastPath;
828821
do {
829-
const stat = tryStatSync(StringPrototypeSlice(packageJSONPath, 0,
830-
packageJSONPath.length - 13));
831-
if (!stat.isDirectory()) {
822+
const stat = internalModuleStat(toNamespacedPath(StringPrototypeSlice(packageJSONPath, 0,
823+
packageJSONPath.length - 13)));
824+
// Check for !stat.isDirectory()
825+
if (stat !== 1) {
832826
lastPath = packageJSONPath;
833827
packageJSONUrl = new URL((isScoped ?
834828
'../../../../node_modules/' : '../../../node_modules/') +

0 commit comments

Comments
 (0)