Skip to content

Commit ab5a304

Browse files
arcanisRafaelGSS
authored andcommitted
module: open stat/readPackage to mutations
PR-URL: #44537 Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 7571c3a commit ab5a304

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

lib/internal/modules/cjs/loader.js

+37-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ const {
7979
maybeCacheSourceMap,
8080
} = require('internal/source_map/source_map_cache');
8181
const { pathToFileURL, fileURLToPath, isURLInstance } = require('internal/url');
82-
const { deprecate, kEmptyObject, filterOwnProperties, setOwnProperty } = require('internal/util');
82+
const {
83+
deprecate,
84+
emitExperimentalWarning,
85+
kEmptyObject,
86+
filterOwnProperties,
87+
setOwnProperty,
88+
} = require('internal/util');
8389
const vm = require('vm');
8490
const assert = require('internal/assert');
8591
const fs = require('fs');
@@ -163,6 +169,18 @@ function stat(filename) {
163169
return result;
164170
}
165171

172+
let _stat = stat;
173+
ObjectDefineProperty(Module, '_stat', {
174+
__proto__: null,
175+
get() { return _stat; },
176+
set(stat) {
177+
emitExperimentalWarning('Module._stat');
178+
_stat = stat;
179+
return true;
180+
},
181+
configurable: true,
182+
});
183+
166184
function updateChildren(parent, child, scan) {
167185
const children = parent?.children;
168186
if (children && !(scan && ArrayPrototypeIncludes(children, child)))
@@ -335,6 +353,18 @@ function readPackage(requestPath) {
335353
}
336354
}
337355

356+
let _readPackage = readPackage;
357+
ObjectDefineProperty(Module, '_readPackage', {
358+
__proto__: null,
359+
get() { return _readPackage; },
360+
set(readPackage) {
361+
emitExperimentalWarning('Module._readPackage');
362+
_readPackage = readPackage;
363+
return true;
364+
},
365+
configurable: true,
366+
});
367+
338368
function readPackageScope(checkPath) {
339369
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
340370
let separatorIndex;
@@ -343,7 +373,7 @@ function readPackageScope(checkPath) {
343373
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
344374
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
345375
return false;
346-
const pjson = readPackage(checkPath + sep);
376+
const pjson = _readPackage(checkPath + sep);
347377
if (pjson) return {
348378
data: pjson,
349379
path: checkPath,
@@ -353,7 +383,7 @@ function readPackageScope(checkPath) {
353383
}
354384

355385
function tryPackage(requestPath, exts, isMain, originalPath) {
356-
const pkg = readPackage(requestPath)?.main;
386+
const pkg = _readPackage(requestPath)?.main;
357387

358388
if (!pkg) {
359389
return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
@@ -399,7 +429,7 @@ const realpathCache = new SafeMap();
399429
// keep symlinks intact, otherwise resolve to the
400430
// absolute realpath.
401431
function tryFile(requestPath, isMain) {
402-
const rc = stat(requestPath);
432+
const rc = _stat(requestPath);
403433
if (rc !== 0) return;
404434
if (preserveSymlinks && !isMain) {
405435
return path.resolve(requestPath);
@@ -493,7 +523,7 @@ function resolveExports(nmPath, request) {
493523
if (!name)
494524
return;
495525
const pkgPath = path.resolve(nmPath, name);
496-
const pkg = readPackage(pkgPath);
526+
const pkg = _readPackage(pkgPath);
497527
if (pkg?.exports != null) {
498528
try {
499529
return finalizeEsmResolution(packageExportsResolve(
@@ -533,7 +563,7 @@ Module._findPath = function(request, paths, isMain) {
533563
for (let i = 0; i < paths.length; i++) {
534564
// Don't search further if path doesn't exist
535565
const curPath = paths[i];
536-
if (curPath && stat(curPath) < 1) continue;
566+
if (curPath && _stat(curPath) < 1) continue;
537567

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

547-
const rc = stat(basePath);
577+
const rc = _stat(basePath);
548578
if (!trailingSlash) {
549579
if (rc === 0) { // File.
550580
if (!isMain) {

0 commit comments

Comments
 (0)