From 8dce57e2e08ff513b38325d067ec0b1beffc4050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Tue, 6 Sep 2022 15:10:00 +0200 Subject: [PATCH 1/3] module: open stat/readPackage to mutations --- lib/internal/modules/cjs/loader.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 102bf90953b65b..252a34d9d80d80 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -162,6 +162,7 @@ function stat(filename) { } return result; } +Module._stat = stat; function updateChildren(parent, child, scan) { const children = parent?.children; @@ -334,6 +335,7 @@ function readPackage(requestPath) { throw e; } } +Module._readPackage = readPackage; function readPackageScope(checkPath) { const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep); @@ -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, @@ -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); @@ -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); @@ -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( @@ -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); @@ -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) { From 1e221d930f3fd5034a6c6f95e49d97df3f045f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Wed, 14 Sep 2022 20:26:06 +0200 Subject: [PATCH 2/3] Adds experimental warnings when patching _stat/_readPackage --- lib/internal/modules/cjs/loader.js | 39 ++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 252a34d9d80d80..df1b5bba968354 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -84,6 +84,7 @@ const vm = require('vm'); const assert = require('internal/assert'); const fs = require('fs'); const internalFS = require('internal/fs/utils'); +const { emitExperimentalWarning } = require('internal/util'); const path = require('path'); const { sep } = path; const { internalModuleStat } = internalBinding('fs'); @@ -162,7 +163,18 @@ function stat(filename) { } return result; } -Module._stat = stat; + +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; @@ -335,7 +347,18 @@ function readPackage(requestPath) { throw e; } } -Module._readPackage = readPackage; + +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); @@ -345,7 +368,7 @@ function readPackageScope(checkPath) { checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex); if (StringPrototypeEndsWith(checkPath, sep + 'node_modules')) return false; - const pjson = Module._readPackage(checkPath + sep); + const pjson = _readPackage(checkPath + sep); if (pjson) return { data: pjson, path: checkPath, @@ -355,7 +378,7 @@ function readPackageScope(checkPath) { } function tryPackage(requestPath, exts, isMain, originalPath) { - const pkg = Module._readPackage(requestPath)?.main; + const pkg = _readPackage(requestPath)?.main; if (!pkg) { return tryExtensions(path.resolve(requestPath, 'index'), exts, isMain); @@ -401,7 +424,7 @@ const realpathCache = new SafeMap(); // keep symlinks intact, otherwise resolve to the // absolute realpath. function tryFile(requestPath, isMain) { - const rc = Module._stat(requestPath); + const rc = _stat(requestPath); if (rc !== 0) return; if (preserveSymlinks && !isMain) { return path.resolve(requestPath); @@ -495,7 +518,7 @@ function resolveExports(nmPath, request) { if (!name) return; const pkgPath = path.resolve(nmPath, name); - const pkg = Module._readPackage(pkgPath); + const pkg = _readPackage(pkgPath); if (pkg?.exports != null) { try { return finalizeEsmResolution(packageExportsResolve( @@ -535,7 +558,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 && Module._stat(curPath) < 1) continue; + if (curPath && _stat(curPath) < 1) continue; if (!absoluteRequest) { const exportsResolved = resolveExports(curPath, request); @@ -546,7 +569,7 @@ Module._findPath = function(request, paths, isMain) { const basePath = path.resolve(curPath, request); let filename; - const rc = Module._stat(basePath); + const rc = _stat(basePath); if (!trailingSlash) { if (rc === 0) { // File. if (!isMain) { From a983192a47eb6ece4dfad5983ac99d397c0fbb13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Wed, 14 Sep 2022 21:38:52 +0200 Subject: [PATCH 3/3] Update loader.js --- lib/internal/modules/cjs/loader.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index df1b5bba968354..68edd781e33a3e 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -79,12 +79,17 @@ 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'); const internalFS = require('internal/fs/utils'); -const { emitExperimentalWarning } = require('internal/util'); const path = require('path'); const { sep } = path; const { internalModuleStat } = internalBinding('fs');