Skip to content

Commit 1bb394e

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

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');
@@ -162,6 +168,18 @@ function stat(filename) {
162168
return result;
163169
}
164170

171+
let _stat = stat;
172+
ObjectDefineProperty(Module, '_stat', {
173+
__proto__: null,
174+
get() { return _stat; },
175+
set(stat) {
176+
emitExperimentalWarning('Module._stat');
177+
_stat = stat;
178+
return true;
179+
},
180+
configurable: true,
181+
});
182+
165183
function updateChildren(parent, child, scan) {
166184
const children = parent?.children;
167185
if (children && !(scan && ArrayPrototypeIncludes(children, child)))
@@ -328,6 +346,18 @@ function readPackage(requestPath) {
328346
}
329347
}
330348

349+
let _readPackage = readPackage;
350+
ObjectDefineProperty(Module, '_readPackage', {
351+
__proto__: null,
352+
get() { return _readPackage; },
353+
set(readPackage) {
354+
emitExperimentalWarning('Module._readPackage');
355+
_readPackage = readPackage;
356+
return true;
357+
},
358+
configurable: true,
359+
});
360+
331361
function readPackageScope(checkPath) {
332362
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
333363
let separatorIndex;
@@ -336,7 +366,7 @@ function readPackageScope(checkPath) {
336366
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
337367
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
338368
return false;
339-
const pjson = readPackage(checkPath + sep);
369+
const pjson = _readPackage(checkPath + sep);
340370
if (pjson) return {
341371
data: pjson,
342372
path: checkPath,
@@ -346,7 +376,7 @@ function readPackageScope(checkPath) {
346376
}
347377

348378
function tryPackage(requestPath, exts, isMain, originalPath) {
349-
const pkg = readPackage(requestPath)?.main;
379+
const pkg = _readPackage(requestPath)?.main;
350380

351381
if (!pkg) {
352382
return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain);
@@ -392,7 +422,7 @@ const realpathCache = new SafeMap();
392422
// keep symlinks intact, otherwise resolve to the
393423
// absolute realpath.
394424
function tryFile(requestPath, isMain) {
395-
const rc = stat(requestPath);
425+
const rc = _stat(requestPath);
396426
if (rc !== 0) return;
397427
if (preserveSymlinks && !isMain) {
398428
return path.resolve(requestPath);
@@ -486,7 +516,7 @@ function resolveExports(nmPath, request) {
486516
if (!name)
487517
return;
488518
const pkgPath = path.resolve(nmPath, name);
489-
const pkg = readPackage(pkgPath);
519+
const pkg = _readPackage(pkgPath);
490520
if (pkg?.exports != null) {
491521
try {
492522
return finalizeEsmResolution(packageExportsResolve(
@@ -526,7 +556,7 @@ Module._findPath = function(request, paths, isMain) {
526556
for (let i = 0; i < paths.length; i++) {
527557
// Don't search further if path doesn't exist
528558
const curPath = paths[i];
529-
if (curPath && stat(curPath) < 1) continue;
559+
if (curPath && _stat(curPath) < 1) continue;
530560

531561
if (!absoluteRequest) {
532562
const exportsResolved = resolveExports(curPath, request);
@@ -537,7 +567,7 @@ Module._findPath = function(request, paths, isMain) {
537567
const basePath = path.resolve(curPath, request);
538568
let filename;
539569

540-
const rc = stat(basePath);
570+
const rc = _stat(basePath);
541571
if (!trailingSlash) {
542572
if (rc === 0) { // File.
543573
if (!isMain) {

0 commit comments

Comments
 (0)