@@ -6,6 +6,7 @@ const runInThisContext = require('vm').runInThisContext;
6
6
const assert = require ( 'assert' ) . ok ;
7
7
const fs = require ( 'fs' ) ;
8
8
const path = require ( 'path' ) ;
9
+ const internalModuleStat = process . binding ( 'fs' ) . internalModuleStat ;
9
10
10
11
11
12
// If obj.hasOwnProperty has been overridden, then calling
@@ -56,13 +57,6 @@ const debug = Module._debug;
56
57
// -> a.<ext>
57
58
// -> a/index.<ext>
58
59
59
- function statPath ( path ) {
60
- try {
61
- return fs . statSync ( path ) ;
62
- } catch ( ex ) { }
63
- return false ;
64
- }
65
-
66
60
// check if the directory is a package.json dir
67
61
const packageMainCache = { } ;
68
62
@@ -94,7 +88,7 @@ function tryPackage(requestPath, exts) {
94
88
if ( ! pkg ) return false ;
95
89
96
90
var filename = path . resolve ( requestPath , pkg ) ;
97
- return tryFile ( filename , null ) || tryExtensions ( filename , exts ) ||
91
+ return tryFile ( filename ) || tryExtensions ( filename , exts ) ||
98
92
tryExtensions ( path . resolve ( filename , 'index' ) , exts ) ;
99
93
}
100
94
@@ -104,18 +98,19 @@ function tryPackage(requestPath, exts) {
104
98
Module . _realpathCache = { } ;
105
99
106
100
// check if the file exists and is not a directory
107
- function tryFile ( requestPath , stats ) {
108
- stats = stats || statPath ( requestPath ) ;
109
- if ( stats && ! stats . isDirectory ( ) ) {
110
- return fs . realpathSync ( requestPath , Module . _realpathCache ) ;
111
- }
112
- return false ;
101
+ function tryFile ( requestPath ) {
102
+ const rc = internalModuleStat ( requestPath ) ;
103
+ return rc === 0 && toRealPath ( requestPath ) ;
104
+ }
105
+
106
+ function toRealPath ( requestPath ) {
107
+ return fs . realpathSync ( requestPath , Module . _realpathCache ) ;
113
108
}
114
109
115
110
// given a path check a the file exists with any of the set extensions
116
111
function tryExtensions ( p , exts ) {
117
112
for ( var i = 0 , EL = exts . length ; i < EL ; i ++ ) {
118
- var filename = tryFile ( p + exts [ i ] , null ) ;
113
+ var filename = tryFile ( p + exts [ i ] ) ;
119
114
120
115
if ( filename ) {
121
116
return filename ;
@@ -150,11 +145,10 @@ Module._findPath = function(request, paths) {
150
145
var filename ;
151
146
152
147
if ( ! trailingSlash ) {
153
- var stats = statPath ( basePath ) ;
154
- // try to join the request to the path
155
- filename = tryFile ( basePath , stats ) ;
156
-
157
- if ( ! filename && stats && stats . isDirectory ( ) ) {
148
+ const rc = internalModuleStat ( basePath ) ;
149
+ if ( rc === 0 ) { // File.
150
+ filename = toRealPath ( basePath ) ;
151
+ } else if ( rc === 1 ) { // Directory.
158
152
filename = tryPackage ( basePath , exts ) ;
159
153
}
160
154
0 commit comments