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