From 82386b9d13a25c64a2de8bf89d0109b73a1ed0c7 Mon Sep 17 00:00:00 2001 From: Geoffrey Booth Date: Wed, 27 Sep 2023 15:22:02 -0700 Subject: [PATCH] module: move helpers out of cjs loader --- lib/internal/modules/cjs/loader.js | 23 ++--------------------- lib/internal/modules/helpers.js | 20 ++++++++++++++++++++ lib/internal/modules/run_main.js | 3 ++- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index db31feb93ee724..3fb223ba0a7e5e 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -68,7 +68,7 @@ const cjsParseCache = new SafeWeakMap(); // Set first due to cycle with ESM loader functions. module.exports = { - wrapSafe, Module, toRealPath, readPackageScope, cjsParseCache, + wrapSafe, Module, readPackageScope, cjsParseCache, get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; }, initializeCJS, }; @@ -88,7 +88,6 @@ const { const { internalCompileFunction } = require('internal/vm'); const assert = require('internal/assert'); const fs = require('fs'); -const internalFS = require('internal/fs/utils'); const path = require('path'); const { sep } = path; const { internalModuleStat } = internalBinding('fs'); @@ -106,6 +105,7 @@ const { makeRequireFunction, normalizeReferrerURL, stripBOM, + toRealPath, } = require('internal/modules/helpers'); const packageJsonReader = require('internal/modules/package_json_reader'); const { getOptionValue, getEmbedderOptions } = require('internal/options'); @@ -498,14 +498,6 @@ function tryPackage(requestPath, exts, isMain, originalPath) { return actual; } -/** - * Cache for storing resolved real paths of modules. - * In order to minimize unnecessary lstat() calls, this cache is a list of known-real paths. - * Set to an empty Map to reset. - * @type {Map} - */ -const realpathCache = new SafeMap(); - /** * Check if the file exists and is not a directory if using `--preserve-symlinks` and `isMain` is false, keep symlinks * intact, otherwise resolve to the absolute realpath. @@ -521,17 +513,6 @@ function tryFile(requestPath, isMain) { return toRealPath(requestPath); } - -/** - * Resolves the path of a given `require` specifier, following symlinks. - * @param {string} requestPath The `require` specifier - */ -function toRealPath(requestPath) { - return fs.realpathSync(requestPath, { - [internalFS.realpathCacheKey]: realpathCache, - }); -} - /** * Given a path, check if the file exists with any of the set extensions. * @param {string} basePath The path and filename without extension diff --git a/lib/internal/modules/helpers.js b/lib/internal/modules/helpers.js index cc32e95c4eb413..7f2959cc469dc1 100644 --- a/lib/internal/modules/helpers.js +++ b/lib/internal/modules/helpers.js @@ -21,6 +21,8 @@ const { const { BuiltinModule } = require('internal/bootstrap/realm'); const { validateString } = require('internal/validators'); +const fs = require('fs'); // Import all of `fs` so that it can be monkey-patched. +const internalFS = require('internal/fs/utils'); const path = require('path'); const { pathToFileURL, fileURLToPath, URL } = require('internal/url'); @@ -39,6 +41,23 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => { /** @typedef {import('internal/modules/cjs/loader.js').Module} Module */ +/** + * Cache for storing resolved real paths of modules. + * In order to minimize unnecessary lstat() calls, this cache is a list of known-real paths. + * Set to an empty Map to reset. + * @type {Map} + */ +const realpathCache = new SafeMap(); +/** + * Resolves the path of a given `require` specifier, following symlinks. + * @param {string} requestPath The `require` specifier + */ +function toRealPath(requestPath) { + return fs.realpathSync(requestPath, { + [internalFS.realpathCacheKey]: realpathCache, + }); +} + /** @type {Set} */ let cjsConditions; /** @@ -310,4 +329,5 @@ module.exports = { makeRequireFunction, normalizeReferrerURL, stripBOM, + toRealPath, }; diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js index ac1ffef0412b17..e6fd1c323efbee 100644 --- a/lib/internal/modules/run_main.js +++ b/lib/internal/modules/run_main.js @@ -15,12 +15,13 @@ function resolveMainPath(main) { // Note extension resolution for the main entry point can be deprecated in a // future major. // Module._findPath is monkey-patchable here. - const { Module, toRealPath } = require('internal/modules/cjs/loader'); + const { Module } = require('internal/modules/cjs/loader'); let mainPath = Module._findPath(path.resolve(main), null, true); if (!mainPath) { return; } const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); if (!preserveSymlinksMain) { + const { toRealPath } = require('internal/modules/helpers'); mainPath = toRealPath(mainPath); }