@@ -25,24 +25,20 @@ 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
const pendingDeprecation = getOptionValue ( '--pending-deprecation' ) ;
35
31
// Do not eagerly grab .manifest, it may be in TDZ
36
32
const policy = getOptionValue ( '--experimental-policy' ) ?
37
33
require ( 'internal/process/policy' ) :
38
34
null ;
39
- const { sep, relative, resolve } = require ( 'path' ) ;
35
+ const { sep, relative, resolve, toNamespacedPath } = require ( 'path' ) ;
40
36
const preserveSymlinks = getOptionValue ( '--preserve-symlinks' ) ;
41
37
const preserveSymlinksMain = getOptionValue ( '--preserve-symlinks-main' ) ;
42
38
const experimentalNetworkImports =
43
39
getOptionValue ( '--experimental-network-imports' ) ;
44
40
const typeFlag = getOptionValue ( '--input-type' ) ;
45
- const { URL , pathToFileURL, fileURLToPath } = require ( 'internal/url' ) ;
41
+ const { URL , pathToFileURL, fileURLToPath, toPathIfFileURL } = require ( 'internal/url' ) ;
46
42
const {
47
43
ERR_INPUT_TYPE_NOT_ALLOWED ,
48
44
ERR_INVALID_MODULE_SPECIFIER ,
@@ -60,6 +56,7 @@ const { Module: CJSModule } = require('internal/modules/cjs/loader');
60
56
const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
61
57
const { getPackageConfig, getPackageScopeConfig } = require ( 'internal/modules/esm/package_config' ) ;
62
58
const { getConditionsSet } = require ( 'internal/modules/esm/utils' ) ;
59
+ const { internalModuleStat } = internalBinding ( 'fs' ) ;
63
60
64
61
/**
65
62
* @typedef {import('internal/modules/esm/package_config.js').PackageConfig } PackageConfig
@@ -138,19 +135,12 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
138
135
139
136
const realpathCache = new SafeMap ( ) ;
140
137
141
- /**
142
- * @param {string | URL } path
143
- * @returns {import('fs').Stats }
144
- */
145
- const tryStatSync =
146
- ( path ) => statSync ( path , { throwIfNoEntry : false } ) ?? new Stats ( ) ;
147
-
148
138
/**
149
139
* @param {string | URL } url
150
140
* @returns {boolean }
151
141
*/
152
142
function fileExists ( url ) {
153
- return statSync ( url , { throwIfNoEntry : false } ) ?. isFile ( ) ?? false ;
143
+ return internalModuleStat ( toNamespacedPath ( toPathIfFileURL ( url ) ) ) === 0 ;
154
144
}
155
145
156
146
/**
@@ -285,13 +275,16 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
285
275
path = fileURLToPath ( resolved ) ;
286
276
}
287
277
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 ) {
291
283
const err = new ERR_UNSUPPORTED_DIR_IMPORT ( path , fileURLToPath ( base ) ) ;
292
284
err . url = String ( resolved ) ;
293
285
throw err ;
294
- } else if ( ! stats . isFile ( ) ) {
286
+ } else if ( stats !== 0 ) {
287
+ // Check for !stats.isFile()
295
288
if ( process . env . WATCH_REPORT_DEPENDENCIES && process . send ) {
296
289
process . send ( { 'watch:require' : [ path || resolved . pathname ] } ) ;
297
290
}
@@ -826,9 +819,10 @@ function packageResolve(specifier, base, conditions) {
826
819
let packageJSONPath = fileURLToPath ( packageJSONUrl ) ;
827
820
let lastPath ;
828
821
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 ) {
832
826
lastPath = packageJSONPath ;
833
827
packageJSONUrl = new URL ( ( isScoped ?
834
828
'../../../../node_modules/' : '../../../node_modules/' ) +
0 commit comments