Skip to content

Commit 82386b9

Browse files
committed
module: move helpers out of cjs loader
1 parent 3838b57 commit 82386b9

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const cjsParseCache = new SafeWeakMap();
6868

6969
// Set first due to cycle with ESM loader functions.
7070
module.exports = {
71-
wrapSafe, Module, toRealPath, readPackageScope, cjsParseCache,
71+
wrapSafe, Module, readPackageScope, cjsParseCache,
7272
get hasLoadedAnyUserCJSModule() { return hasLoadedAnyUserCJSModule; },
7373
initializeCJS,
7474
};
@@ -88,7 +88,6 @@ const {
8888
const { internalCompileFunction } = require('internal/vm');
8989
const assert = require('internal/assert');
9090
const fs = require('fs');
91-
const internalFS = require('internal/fs/utils');
9291
const path = require('path');
9392
const { sep } = path;
9493
const { internalModuleStat } = internalBinding('fs');
@@ -106,6 +105,7 @@ const {
106105
makeRequireFunction,
107106
normalizeReferrerURL,
108107
stripBOM,
108+
toRealPath,
109109
} = require('internal/modules/helpers');
110110
const packageJsonReader = require('internal/modules/package_json_reader');
111111
const { getOptionValue, getEmbedderOptions } = require('internal/options');
@@ -498,14 +498,6 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
498498
return actual;
499499
}
500500

501-
/**
502-
* Cache for storing resolved real paths of modules.
503-
* In order to minimize unnecessary lstat() calls, this cache is a list of known-real paths.
504-
* Set to an empty Map to reset.
505-
* @type {Map<string, string>}
506-
*/
507-
const realpathCache = new SafeMap();
508-
509501
/**
510502
* Check if the file exists and is not a directory if using `--preserve-symlinks` and `isMain` is false, keep symlinks
511503
* intact, otherwise resolve to the absolute realpath.
@@ -521,17 +513,6 @@ function tryFile(requestPath, isMain) {
521513
return toRealPath(requestPath);
522514
}
523515

524-
525-
/**
526-
* Resolves the path of a given `require` specifier, following symlinks.
527-
* @param {string} requestPath The `require` specifier
528-
*/
529-
function toRealPath(requestPath) {
530-
return fs.realpathSync(requestPath, {
531-
[internalFS.realpathCacheKey]: realpathCache,
532-
});
533-
}
534-
535516
/**
536517
* Given a path, check if the file exists with any of the set extensions.
537518
* @param {string} basePath The path and filename without extension

lib/internal/modules/helpers.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const {
2121
const { BuiltinModule } = require('internal/bootstrap/realm');
2222

2323
const { validateString } = require('internal/validators');
24+
const fs = require('fs'); // Import all of `fs` so that it can be monkey-patched.
25+
const internalFS = require('internal/fs/utils');
2426
const path = require('path');
2527
const { pathToFileURL, fileURLToPath, URL } = require('internal/url');
2628

@@ -39,6 +41,23 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => {
3941

4042
/** @typedef {import('internal/modules/cjs/loader.js').Module} Module */
4143

44+
/**
45+
* Cache for storing resolved real paths of modules.
46+
* In order to minimize unnecessary lstat() calls, this cache is a list of known-real paths.
47+
* Set to an empty Map to reset.
48+
* @type {Map<string, string>}
49+
*/
50+
const realpathCache = new SafeMap();
51+
/**
52+
* Resolves the path of a given `require` specifier, following symlinks.
53+
* @param {string} requestPath The `require` specifier
54+
*/
55+
function toRealPath(requestPath) {
56+
return fs.realpathSync(requestPath, {
57+
[internalFS.realpathCacheKey]: realpathCache,
58+
});
59+
}
60+
4261
/** @type {Set<string>} */
4362
let cjsConditions;
4463
/**
@@ -310,4 +329,5 @@ module.exports = {
310329
makeRequireFunction,
311330
normalizeReferrerURL,
312331
stripBOM,
332+
toRealPath,
313333
};

lib/internal/modules/run_main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ function resolveMainPath(main) {
1515
// Note extension resolution for the main entry point can be deprecated in a
1616
// future major.
1717
// Module._findPath is monkey-patchable here.
18-
const { Module, toRealPath } = require('internal/modules/cjs/loader');
18+
const { Module } = require('internal/modules/cjs/loader');
1919
let mainPath = Module._findPath(path.resolve(main), null, true);
2020
if (!mainPath) { return; }
2121

2222
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
2323
if (!preserveSymlinksMain) {
24+
const { toRealPath } = require('internal/modules/helpers');
2425
mainPath = toRealPath(mainPath);
2526
}
2627

0 commit comments

Comments
 (0)