@@ -27,8 +27,6 @@ const internalFS = require('internal/fs/utils');
27
27
const { BuiltinModule } = require ( 'internal/bootstrap/loaders' ) ;
28
28
const {
29
29
realpathSync,
30
- statSync,
31
- Stats,
32
30
} = require ( 'fs' ) ;
33
31
const { getOptionValue } = require ( 'internal/options' ) ;
34
32
// Do not eagerly grab .manifest, it may be in TDZ
@@ -41,7 +39,7 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
41
39
const experimentalNetworkImports =
42
40
getOptionValue ( '--experimental-network-imports' ) ;
43
41
const typeFlag = getOptionValue ( '--input-type' ) ;
44
- const { URL , pathToFileURL, fileURLToPath } = require ( 'internal/url' ) ;
42
+ const { URL , pathToFileURL, fileURLToPath, toPathIfFileURL } = require ( 'internal/url' ) ;
45
43
const {
46
44
ERR_INPUT_TYPE_NOT_ALLOWED ,
47
45
ERR_INVALID_MODULE_SPECIFIER ,
@@ -59,6 +57,7 @@ const {
59
57
const { Module : CJSModule } = require ( 'internal/modules/cjs/loader' ) ;
60
58
const { getPackageConfig, getPackageScopeConfig } = require ( 'internal/modules/esm/package_config' ) ;
61
59
const { getConditionsSet } = require ( 'internal/modules/esm/utils' ) ;
60
+ const { internalModuleStat } = internalBinding ( 'fs' ) ;
62
61
63
62
/**
64
63
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig } PackageConfig
@@ -135,19 +134,12 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
135
134
136
135
const realpathCache = new SafeMap ( ) ;
137
136
138
- /**
139
- * @param {string | URL } path
140
- * @returns {import('fs').Stats }
141
- */
142
- const tryStatSync =
143
- ( path ) => statSync ( path , { throwIfNoEntry : false } ) ?? new Stats ( ) ;
144
-
145
137
/**
146
138
* @param {string | URL } url
147
139
* @returns {boolean }
148
140
*/
149
141
function fileExists ( url ) {
150
- return statSync ( url , { throwIfNoEntry : false } ) ?. isFile ( ) ?? false ;
142
+ return internalModuleStat ( toPathIfFileURL ( url ) ) === 0 ;
151
143
}
152
144
153
145
/**
@@ -218,13 +210,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
218
210
219
211
const path = fileURLToPath ( resolved ) ;
220
212
221
- const stats = tryStatSync ( StringPrototypeEndsWith ( path , '/' ) ?
213
+ const stats = internalModuleStat ( StringPrototypeEndsWith ( path , '/' ) ?
222
214
StringPrototypeSlice ( path , - 1 ) : path ) ;
223
- if ( stats . isDirectory ( ) ) {
215
+
216
+ // Check for stats.isDirectory()
217
+ if ( stats === 1 ) {
224
218
const err = new ERR_UNSUPPORTED_DIR_IMPORT ( path , fileURLToPath ( base ) ) ;
225
219
err . url = String ( resolved ) ;
226
220
throw err ;
227
- } else if ( ! stats . isFile ( ) ) {
221
+ } else if ( stats !== 0 ) {
222
+ // Check for !stats.isFile()
228
223
if ( process . env . WATCH_REPORT_DEPENDENCIES && process . send ) {
229
224
process . send ( { 'watch:require' : [ path || resolved . pathname ] } ) ;
230
225
}
@@ -760,9 +755,11 @@ function packageResolve(specifier, base, conditions) {
760
755
let packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
761
756
let lastPath ;
762
757
do {
763
- const stat = tryStatSync ( StringPrototypeSlice ( packageJSONPath , 0 ,
764
- packageJSONPath . length - 13 ) ) ;
765
- if ( ! stat . isDirectory ( ) ) {
758
+ const stat = internalModuleStat ( StringPrototypeSlice ( packageJSONPath , 0 ,
759
+ packageJSONPath . length - 13 ) ) ;
760
+
761
+ // Check for !stat.isDirectory
762
+ if ( stat !== 1 ) {
766
763
lastPath = packageJSONPath ;
767
764
packageJSONUrl = new URL ( ( isScoped ?
768
765
'../../../../node_modules/' : '../../../node_modules/' ) +
0 commit comments