@@ -25,23 +25,19 @@ const {
25
25
} = primordials ;
26
26
const internalFS = require ( 'internal/fs/utils' ) ;
27
27
const { BuiltinModule } = require ( 'internal/bootstrap/realm' ) ;
28
- const {
29
- realpathSync,
30
- statSync,
31
- Stats,
32
- } = require ( 'fs' ) ;
28
+ const { realpathSync } = require ( 'fs' ) ;
33
29
const { getOptionValue } = require ( 'internal/options' ) ;
34
30
// Do not eagerly grab .manifest, it may be in TDZ
35
31
const policy = getOptionValue ( '--experimental-policy' ) ?
36
32
require ( 'internal/process/policy' ) :
37
33
null ;
38
- const { sep, relative } = require ( 'path' ) ;
34
+ const { sep, relative, toNamespacedPath } = require ( 'path' ) ;
39
35
const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
40
36
const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
41
37
const experimentalNetworkImports =
42
38
getOptionValue ( '--experimental-network-imports' ) ;
43
39
const typeFlag = getOptionValue ( '--input-type' ) ;
44
- const { URL , pathToFileURL, fileURLToPath, isURL } = require ( 'internal/url' ) ;
40
+ const { URL , pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require ( 'internal/url' ) ;
45
41
const { canParse : canParseURL } = internalBinding ( 'url' ) ;
46
42
const {
47
43
ERR_INPUT_TYPE_NOT_ALLOWED ,
@@ -61,6 +57,7 @@ const {
61
57
const { Module : CJSModule } = require ( 'internal/modules/cjs/loader' ) ;
62
58
const { getPackageConfig, getPackageScopeConfig } = require ( 'internal/modules/esm/package_config' ) ;
63
59
const { getConditionsSet } = require ( 'internal/modules/esm/utils' ) ;
60
+ const { internalModuleStat } = internalBinding ( 'fs' ) ;
64
61
65
62
/**
66
63
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig } PackageConfig
@@ -137,19 +134,12 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
137
134
138
135
const realpathCache = new SafeMap ( ) ;
139
136
140
- /**
141
- * @param {string | URL } path
142
- * @returns {import('fs').Stats }
143
- */
144
- const tryStatSync =
145
- ( path ) => statSync ( path , { throwIfNoEntry : false } ) ?? new Stats ( ) ;
146
-
147
137
/**
148
138
* @param {string | URL } url
149
139
* @returns {boolean }
150
140
*/
151
141
function fileExists ( url ) {
152
- return statSync ( url , { throwIfNoEntry : false } ) ?. isFile ( ) ?? false ;
142
+ return internalModuleStat ( toNamespacedPath ( toPathIfFileURL ( url ) ) ) === 0 ;
153
143
}
154
144
155
145
/**
@@ -220,13 +210,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
220
210
221
211
const path = fileURLToPath ( resolved ) ;
222
212
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 ) {
226
218
const err = new ERR_UNSUPPORTED_DIR_IMPORT ( path , fileURLToPath ( base ) ) ;
227
219
err . url = String ( resolved ) ;
228
220
throw err ;
229
- } else if ( ! stats . isFile ( ) ) {
221
+ } else if ( stats !== 0 ) {
222
+ // Check for !stats.isFile()
230
223
if ( process . env . WATCH_REPORT_DEPENDENCIES && process . send ) {
231
224
process . send ( { 'watch:require' : [ path || resolved . pathname ] } ) ;
232
225
}
@@ -755,9 +748,10 @@ function packageResolve(specifier, base, conditions) {
755
748
let packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
756
749
let lastPath ;
757
750
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 ) {
761
755
lastPath = packageJSONPath ;
762
756
packageJSONUrl = new URL ( ( isScoped ?
763
757
'../../../../node_modules/' : '../../../node_modules/' ) +
0 commit comments