Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: open stat/readPackage to mutations #44537

Merged
merged 3 commits into from
Sep 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ const {
maybeCacheSourceMap,
} = require('internal/source_map/source_map_cache');
const { pathToFileURL, fileURLToPath, isURLInstance } = require('internal/url');
const { deprecate, kEmptyObject, filterOwnProperties, setOwnProperty } = require('internal/util');
const {
deprecate,
emitExperimentalWarning,
kEmptyObject,
filterOwnProperties,
setOwnProperty,
} = require('internal/util');
const vm = require('vm');
const assert = require('internal/assert');
const fs = require('fs');
Expand Down Expand Up @@ -163,6 +169,18 @@ function stat(filename) {
return result;
}

let _stat = stat;
ObjectDefineProperty(Module, '_stat', {
__proto__: null,
get() { return _stat; },
set(stat) {
emitExperimentalWarning('Module._stat');
_stat = stat;
return true;
},
configurable: true,
});

function updateChildren(parent, child, scan) {
const children = parent?.children;
if (children && !(scan && ArrayPrototypeIncludes(children, child)))
Expand Down Expand Up @@ -335,6 +353,18 @@ function readPackage(requestPath) {
}
}

let _readPackage = readPackage;
ObjectDefineProperty(Module, '_readPackage', {
__proto__: null,
get() { return _readPackage; },
set(readPackage) {
emitExperimentalWarning('Module._readPackage');
_readPackage = readPackage;
return true;
},
configurable: true,
});

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

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

if (!pkg) {
return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
Expand Down Expand Up @@ -399,7 +429,7 @@ const realpathCache = new SafeMap();
// keep symlinks intact, otherwise resolve to the
// absolute realpath.
function tryFile(requestPath, isMain) {
const rc = stat(requestPath);
const rc = _stat(requestPath);
if (rc !== 0) return;
if (preserveSymlinks && !isMain) {
return path.resolve(requestPath);
Expand Down Expand Up @@ -493,7 +523,7 @@ function resolveExports(nmPath, request) {
if (!name)
return;
const pkgPath = path.resolve(nmPath, name);
const pkg = readPackage(pkgPath);
const pkg = _readPackage(pkgPath);
if (pkg?.exports != null) {
try {
return finalizeEsmResolution(packageExportsResolve(
Expand Down Expand Up @@ -533,7 +563,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 && _stat(curPath) < 1) continue;

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

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