@@ -56,7 +56,6 @@ const {
56
56
StringPrototypeCharAt,
57
57
StringPrototypeCharCodeAt,
58
58
StringPrototypeEndsWith,
59
- StringPrototypeLastIndexOf,
60
59
StringPrototypeIndexOf,
61
60
StringPrototypeRepeat,
62
61
StringPrototypeSlice,
@@ -69,7 +68,7 @@ const cjsParseCache = new SafeWeakMap();
69
68
70
69
// Set first due to cycle with ESM loader functions.
71
70
module . exports = {
72
- wrapSafe, Module, toRealPath , readPackageScope , cjsParseCache,
71
+ wrapSafe, Module, cjsParseCache,
73
72
get hasLoadedAnyUserCJSModule ( ) { return hasLoadedAnyUserCJSModule ; } ,
74
73
initializeCJS,
75
74
} ;
@@ -89,9 +88,7 @@ const {
89
88
const { internalCompileFunction } = require ( 'internal/vm' ) ;
90
89
const assert = require ( 'internal/assert' ) ;
91
90
const fs = require ( 'fs' ) ;
92
- const internalFS = require ( 'internal/fs/utils' ) ;
93
91
const path = require ( 'path' ) ;
94
- const { sep } = path ;
95
92
const { internalModuleStat } = internalBinding ( 'fs' ) ;
96
93
const { safeGetenv } = internalBinding ( 'credentials' ) ;
97
94
const {
@@ -107,6 +104,7 @@ const {
107
104
makeRequireFunction,
108
105
normalizeReferrerURL,
109
106
stripBOM,
107
+ toRealPath,
110
108
} = require ( 'internal/modules/helpers' ) ;
111
109
const packageJsonReader = require ( 'internal/modules/package_json_reader' ) ;
112
110
const { getOptionValue, getEmbedderOptions } = require ( 'internal/options' ) ;
@@ -402,15 +400,7 @@ function initializeCJS() {
402
400
// -> a.<ext>
403
401
// -> a/index.<ext>
404
402
405
- /**
406
- * @param {string } requestPath
407
- * @return {PackageConfig }
408
- */
409
- function readPackage ( requestPath ) {
410
- return packageJsonReader . read ( path . resolve ( requestPath , 'package.json' ) ) ;
411
- }
412
-
413
- let _readPackage = readPackage ;
403
+ let _readPackage = packageJsonReader . readPackage ;
414
404
ObjectDefineProperty ( Module , '_readPackage' , {
415
405
__proto__ : null ,
416
406
get ( ) { return _readPackage ; } ,
@@ -422,31 +412,6 @@ ObjectDefineProperty(Module, '_readPackage', {
422
412
configurable : true ,
423
413
} ) ;
424
414
425
- /**
426
- * Get the nearest parent package.json file from a given path.
427
- * Return the package.json data and the path to the package.json file, or false.
428
- * @param {string } checkPath The path to start searching from.
429
- */
430
- function readPackageScope ( checkPath ) {
431
- const rootSeparatorIndex = StringPrototypeIndexOf ( checkPath , sep ) ;
432
- let separatorIndex ;
433
- do {
434
- separatorIndex = StringPrototypeLastIndexOf ( checkPath , sep ) ;
435
- checkPath = StringPrototypeSlice ( checkPath , 0 , separatorIndex ) ;
436
- if ( StringPrototypeEndsWith ( checkPath , sep + 'node_modules' ) ) {
437
- return false ;
438
- }
439
- const pjson = _readPackage ( checkPath + sep ) ;
440
- if ( pjson . exists ) {
441
- return {
442
- data : pjson ,
443
- path : checkPath ,
444
- } ;
445
- }
446
- } while ( separatorIndex > rootSeparatorIndex ) ;
447
- return false ;
448
- }
449
-
450
415
/**
451
416
* Try to load a specifier as a package.
452
417
* @param {string } requestPath The path to what we are trying to load
@@ -491,14 +456,6 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
491
456
return actual ;
492
457
}
493
458
494
- /**
495
- * Cache for storing resolved real paths of modules.
496
- * In order to minimize unnecessary lstat() calls, this cache is a list of known-real paths.
497
- * Set to an empty Map to reset.
498
- * @type {Map<string, string> }
499
- */
500
- const realpathCache = new SafeMap ( ) ;
501
-
502
459
/**
503
460
* Check if the file exists and is not a directory if using `--preserve-symlinks` and `isMain` is false, keep symlinks
504
461
* intact, otherwise resolve to the absolute realpath.
@@ -514,17 +471,6 @@ function tryFile(requestPath, isMain) {
514
471
return toRealPath ( requestPath ) ;
515
472
}
516
473
517
-
518
- /**
519
- * Resolves the path of a given `require` specifier, following symlinks.
520
- * @param {string } requestPath The `require` specifier
521
- */
522
- function toRealPath ( requestPath ) {
523
- return fs . realpathSync ( requestPath , {
524
- [ internalFS . realpathCacheKey ] : realpathCache ,
525
- } ) ;
526
- }
527
-
528
474
/**
529
475
* Given a path, check if the file exists with any of the set extensions.
530
476
* @param {string } basePath The path and filename without extension
@@ -586,7 +532,7 @@ function trySelfParentPath(parent) {
586
532
function trySelf ( parentPath , request ) {
587
533
if ( ! parentPath ) { return false ; }
588
534
589
- const { data : pkg , path : pkgPath } = readPackageScope ( parentPath ) ;
535
+ const { data : pkg , path : pkgPath } = packageJsonReader . readPackageScope ( parentPath ) ;
590
536
if ( ! pkg || pkg . exports == null || pkg . name === undefined ) {
591
537
return false ;
592
538
}
@@ -1143,7 +1089,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
1143
1089
1144
1090
if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
1145
1091
const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1146
- const pkg = readPackageScope ( parentPath ) || { __proto__ : null } ;
1092
+ const pkg = packageJsonReader . readPackageScope ( parentPath ) || { __proto__ : null } ;
1147
1093
if ( pkg . data ?. imports != null ) {
1148
1094
try {
1149
1095
const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1431,7 +1377,7 @@ Module._extensions['.js'] = function(module, filename) {
1431
1377
content = fs . readFileSync ( filename , 'utf8' ) ;
1432
1378
}
1433
1379
if ( StringPrototypeEndsWith ( filename , '.js' ) ) {
1434
- const pkg = readPackageScope ( filename ) || { __proto__ : null } ;
1380
+ const pkg = packageJsonReader . readPackageScope ( filename ) || { __proto__ : null } ;
1435
1381
// Function require shouldn't be used in ES modules.
1436
1382
if ( pkg . data ?. type === 'module' ) {
1437
1383
const parent = moduleParentCache . get ( module ) ;
0 commit comments