Skip to content

Commit

Permalink
module: Open stat/readPackage to mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanis committed Sep 6, 2022
1 parent addb726 commit cb887ec
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/internal/modules/cjs/loader.js
Expand Up @@ -162,6 +162,7 @@ function stat(filename) {
}
return result;
}
Module._stat = stat;

function updateChildren(parent, child, scan) {
const children = parent?.children;
Expand Down Expand Up @@ -334,6 +335,7 @@ function readPackage(requestPath) {
throw e;
}
}
Module._readPackage = readPackage;

function readPackageScope(checkPath) {
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
Expand All @@ -343,7 +345,7 @@ function readPackageScope(checkPath) {
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
return false;
const pjson = readPackage(checkPath + sep);
const pjson = Module._readPackage(checkPath + sep);
if (pjson) return {
data: pjson,
path: checkPath,
Expand All @@ -353,7 +355,7 @@ function readPackageScope(checkPath) {
}

function tryPackage(requestPath, exts, isMain, originalPath) {
const pkg = readPackage(requestPath)?.main;
const pkg = Module._readPackage(requestPath)?.main;

if (!pkg) {
return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
Expand Down Expand Up @@ -399,7 +401,7 @@ const realpathCache = new SafeMap();
// keep symlinks intact, otherwise resolve to the
// absolute realpath.
function tryFile(requestPath, isMain) {
const rc = stat(requestPath);
const rc = Module._stat(requestPath);
if (rc !== 0) return;
if (preserveSymlinks && !isMain) {
return path.resolve(requestPath);
Expand Down Expand Up @@ -493,7 +495,7 @@ function resolveExports(nmPath, request) {
if (!name)
return;
const pkgPath = path.resolve(nmPath, name);
const pkg = readPackage(pkgPath);
const pkg = Module._readPackage(pkgPath);
if (pkg?.exports != null) {
try {
return finalizeEsmResolution(packageExportsResolve(
Expand Down Expand Up @@ -533,7 +535,7 @@ Module._findPath = function(request, paths, isMain) {
for (let i = 0; i < paths.length; i++) {
// Don't search further if path doesn't exist
const curPath = paths[i];
if (curPath && stat(curPath) < 1) continue;
if (curPath && Module._stat(curPath) < 1) continue;

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

const rc = stat(basePath);
const rc = Module._stat(basePath);
if (!trailingSlash) {
if (rc === 0) { // File.
if (!isMain) {
Expand Down

0 comments on commit cb887ec

Please sign in to comment.