@@ -55,7 +55,6 @@ const {
5555 StringPrototypeCharAt,
5656 StringPrototypeCharCodeAt,
5757 StringPrototypeEndsWith,
58- StringPrototypeLastIndexOf,
5958 StringPrototypeIndexOf,
6059 StringPrototypeRepeat,
6160 StringPrototypeSlice,
@@ -68,7 +67,7 @@ const cjsParseCache = new SafeWeakMap();
6867
6968// Set first due to cycle with ESM loader functions.
7069module . exports = {
71- wrapSafe, Module, readPackageScope , cjsParseCache,
70+ wrapSafe, Module, cjsParseCache,
7271 get hasLoadedAnyUserCJSModule ( ) { return hasLoadedAnyUserCJSModule ; } ,
7372 initializeCJS,
7473} ;
@@ -89,7 +88,6 @@ const { internalCompileFunction } = require('internal/vm');
8988const assert = require ( 'internal/assert' ) ;
9089const fs = require ( 'fs' ) ;
9190const path = require ( 'path' ) ;
92- const { sep } = path ;
9391const { internalModuleStat } = internalBinding ( 'fs' ) ;
9492const { safeGetenv } = internalBinding ( 'credentials' ) ;
9593const {
@@ -403,15 +401,7 @@ function initializeCJS() {
403401// -> a.<ext>
404402// -> a/index.<ext>
405403
406- /**
407- * @param {string } requestPath
408- * @return {PackageConfig }
409- */
410- function readPackage ( requestPath ) {
411- return packageJsonReader . read ( path . resolve ( requestPath , 'package.json' ) ) ;
412- }
413-
414- let _readPackage = readPackage ;
404+ let _readPackage = packageJsonReader . readPackage ;
415405ObjectDefineProperty ( Module , '_readPackage' , {
416406 __proto__ : null ,
417407 get ( ) { return _readPackage ; } ,
@@ -423,37 +413,6 @@ ObjectDefineProperty(Module, '_readPackage', {
423413 configurable : true ,
424414} ) ;
425415
426- /**
427- * Get the nearest parent package.json file from a given path.
428- * Return the package.json data and the path to the package.json file, or false.
429- * @param {string } checkPath The path to start searching from.
430- */
431- function readPackageScope ( checkPath ) {
432- const rootSeparatorIndex = StringPrototypeIndexOf ( checkPath , sep ) ;
433- let separatorIndex ;
434- const enabledPermission = permission . isEnabled ( ) ;
435- do {
436- separatorIndex = StringPrototypeLastIndexOf ( checkPath , sep ) ;
437- checkPath = StringPrototypeSlice ( checkPath , 0 , separatorIndex ) ;
438- // Stop the search when the process doesn't have permissions
439- // to walk upwards
440- if ( enabledPermission && ! permission . has ( 'fs.read' , checkPath + sep ) ) {
441- return false ;
442- }
443- if ( StringPrototypeEndsWith ( checkPath , sep + 'node_modules' ) ) {
444- return false ;
445- }
446- const pjson = _readPackage ( checkPath + sep ) ;
447- if ( pjson . exists ) {
448- return {
449- data : pjson ,
450- path : checkPath ,
451- } ;
452- }
453- } while ( separatorIndex > rootSeparatorIndex ) ;
454- return false ;
455- }
456-
457416/**
458417 * Try to load a specifier as a package.
459418 * @param {string } requestPath The path to what we are trying to load
@@ -574,7 +533,7 @@ function trySelfParentPath(parent) {
574533function trySelf ( parentPath , request ) {
575534 if ( ! parentPath ) { return false ; }
576535
577- const { data : pkg , path : pkgPath } = readPackageScope ( parentPath ) ;
536+ const { data : pkg , path : pkgPath } = packageJsonReader . readPackageScope ( parentPath ) ;
578537 if ( ! pkg || pkg . exports == null || pkg . name === undefined ) {
579538 return false ;
580539 }
@@ -1134,7 +1093,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
11341093
11351094 if ( request [ 0 ] === '#' && ( parent ?. filename || parent ?. id === '<repl>' ) ) {
11361095 const parentPath = parent ?. filename ?? process . cwd ( ) + path . sep ;
1137- const pkg = readPackageScope ( parentPath ) || { __proto__ : null } ;
1096+ const pkg = packageJsonReader . readPackageScope ( parentPath ) || { __proto__ : null } ;
11381097 if ( pkg . data ?. imports != null ) {
11391098 try {
11401099 const { packageImportsResolve } = require ( 'internal/modules/esm/resolve' ) ;
@@ -1431,7 +1390,7 @@ Module._extensions['.js'] = function(module, filename) {
14311390 content = fs . readFileSync ( filename , 'utf8' ) ;
14321391 }
14331392 if ( StringPrototypeEndsWith ( filename , '.js' ) ) {
1434- const pkg = readPackageScope ( filename ) || { __proto__ : null } ;
1393+ const pkg = packageJsonReader . readPackageScope ( filename ) || { __proto__ : null } ;
14351394 // Function require shouldn't be used in ES modules.
14361395 if ( pkg . data ?. type === 'module' ) {
14371396 const parent = moduleParentCache . get ( module ) ;
0 commit comments