Skip to content

Commit 8dce57e

Browse files
committed
module: open stat/readPackage to mutations
1 parent addb726 commit 8dce57e

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

lib/internal/modules/cjs/loader.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ function stat(filename) {
162162
}
163163
return result;
164164
}
165+
Module._stat = stat;
165166

166167
function updateChildren(parent, child, scan) {
167168
const children = parent?.children;
@@ -334,6 +335,7 @@ function readPackage(requestPath) {
334335
throw e;
335336
}
336337
}
338+
Module._readPackage = readPackage;
337339

338340
function readPackageScope(checkPath) {
339341
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
@@ -343,7 +345,7 @@ function readPackageScope(checkPath) {
343345
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
344346
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
345347
return false;
346-
const pjson = readPackage(checkPath + sep);
348+
const pjson = Module._readPackage(checkPath + sep);
347349
if (pjson) return {
348350
data: pjson,
349351
path: checkPath,
@@ -353,7 +355,7 @@ function readPackageScope(checkPath) {
353355
}
354356

355357
function tryPackage(requestPath, exts, isMain, originalPath) {
356-
const pkg = readPackage(requestPath)?.main;
358+
const pkg = Module._readPackage(requestPath)?.main;
357359

358360
if (!pkg) {
359361
return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
@@ -399,7 +401,7 @@ const realpathCache = new SafeMap();
399401
// keep symlinks intact, otherwise resolve to the
400402
// absolute realpath.
401403
function tryFile(requestPath, isMain) {
402-
const rc = stat(requestPath);
404+
const rc = Module._stat(requestPath);
403405
if (rc !== 0) return;
404406
if (preserveSymlinks && !isMain) {
405407
return path.resolve(requestPath);
@@ -493,7 +495,7 @@ function resolveExports(nmPath, request) {
493495
if (!name)
494496
return;
495497
const pkgPath = path.resolve(nmPath, name);
496-
const pkg = readPackage(pkgPath);
498+
const pkg = Module._readPackage(pkgPath);
497499
if (pkg?.exports != null) {
498500
try {
499501
return finalizeEsmResolution(packageExportsResolve(
@@ -533,7 +535,7 @@ Module._findPath = function(request, paths, isMain) {
533535
for (let i = 0; i < paths.length; i++) {
534536
// Don't search further if path doesn't exist
535537
const curPath = paths[i];
536-
if (curPath && stat(curPath) < 1) continue;
538+
if (curPath && Module._stat(curPath) < 1) continue;
537539

538540
if (!absoluteRequest) {
539541
const exportsResolved = resolveExports(curPath, request);
@@ -544,7 +546,7 @@ Module._findPath = function(request, paths, isMain) {
544546
const basePath = path.resolve(curPath, request);
545547
let filename;
546548

547-
const rc = stat(basePath);
549+
const rc = Module._stat(basePath);
548550
if (!trailingSlash) {
549551
if (rc === 0) { // File.
550552
if (!isMain) {

0 commit comments

Comments
 (0)