Skip to content

Commit 2cadb5e

Browse files
committed
JSDoc for top-level modules files
1 parent 396cf41 commit 2cadb5e

File tree

2 files changed

+75
-12
lines changed

2 files changed

+75
-12
lines changed

lib/internal/modules/helpers.js

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => {
3737
debug = fn;
3838
});
3939

40+
/** @typedef {import('internal/modules/cjs/loader.js').Module} Module */
41+
42+
/** @type {Set<string>} */
4043
let cjsConditions;
44+
/**
45+
* Define the conditions that apply to the CommonJS loader.
46+
*/
4147
function initializeCjsConditions() {
4248
const userConditions = getOptionValue('--conditions');
4349
const noAddons = getOptionValue('--no-addons');
@@ -51,41 +57,62 @@ function initializeCjsConditions() {
5157
]);
5258
}
5359

60+
/**
61+
* Get the conditions that apply to the CommonJS loader.
62+
*/
5463
function getCjsConditions() {
5564
if (cjsConditions === undefined) {
5665
initializeCjsConditions();
5766
}
5867
return cjsConditions;
5968
}
6069

61-
function loadBuiltinModule(filename, request) {
62-
if (!BuiltinModule.canBeRequiredByUsers(filename)) {
70+
/**
71+
* Provide one of Node.js' public modules to user code.
72+
* @param {string} id The identifier/specifier of the builtin module to load
73+
* @param {string} request The module requiring or importing the builtin module
74+
*/
75+
function loadBuiltinModule(id, request) {
76+
if (!BuiltinModule.canBeRequiredByUsers(id)) {
6377
return;
6478
}
65-
const mod = BuiltinModule.map.get(filename);
79+
/** @type {import('internal/bootstrap/realm.js').BuiltinModule} */
80+
const mod = BuiltinModule.map.get(id);
6681
debug('load built-in module %s', request);
6782
// compileForPublicLoader() throws if canBeRequiredByUsers is false:
6883
mod.compileForPublicLoader();
6984
return mod;
7085
}
7186

87+
/** @type {Module} */
7288
let $Module = null;
89+
/**
90+
* Import the Module class on first use.
91+
*/
7392
function lazyModule() {
7493
$Module = $Module || require('internal/modules/cjs/loader').Module;
7594
return $Module;
7695
}
7796

78-
// Invoke with makeRequireFunction(module) where |module| is the Module object
79-
// to use as the context for the require() function.
80-
// Use redirects to set up a mapping from a policy and restrict dependencies
97+
/**
98+
* Invoke with `makeRequireFunction(module)` where `module` is the `Module` object to use as the context for the
99+
* `require()` function.
100+
* Use redirects to set up a mapping from a policy and restrict dependencies.
101+
*/
81102
const urlToFileCache = new SafeMap();
103+
/**
104+
* Create the module-scoped `require` function to pass into CommonJS modules.
105+
* @param {Module} mod The module to create the `require` function for.
106+
* @param {ReturnType<import('internal/policy/manifest.js').Manifest['getDependencyMapper']>} redirects
107+
*/
82108
function makeRequireFunction(mod, redirects) {
83109
// lazy due to cycle
84110
const Module = lazyModule();
85111
if (mod instanceof Module !== true) {
86112
throw new ERR_INVALID_ARG_TYPE('mod', 'Module', mod);
87113
}
88114

115+
/** @type {(specifier: string) => unknown} */
89116
let require;
90117
if (redirects) {
91118
const id = mod.filename || mod.id;
@@ -131,13 +158,22 @@ function makeRequireFunction(mod, redirects) {
131158
};
132159
}
133160

161+
/**
162+
* The `resolve` method that gets attached to module-scope `require`.
163+
* @param {string} request
164+
* @param {Parameters<Module['_resolveFilename']>[3]} options
165+
*/
134166
function resolve(request, options) {
135167
validateString(request, 'request');
136168
return Module._resolveFilename(request, mod, false, options);
137169
}
138170

139171
require.resolve = resolve;
140172

173+
/**
174+
* The `paths` method that gets attached to module-scope `require`.
175+
* @param {string} request
176+
*/
141177
function paths(request) {
142178
validateString(request, 'request');
143179
return Module._resolveLookupPaths(request, mod);
@@ -159,6 +195,7 @@ function makeRequireFunction(mod, redirects) {
159195
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
160196
* because the buffer-to-string conversion in `fs.readFileSync()`
161197
* translates it to FEFF, the UTF-16 BOM.
198+
* @param {string} content
162199
*/
163200
function stripBOM(content) {
164201
if (StringPrototypeCharCodeAt(content) === 0xFEFF) {
@@ -167,6 +204,11 @@ function stripBOM(content) {
167204
return content;
168205
}
169206

207+
/**
208+
* Add built-in modules to a global or REPL scope object.
209+
* @param {object} object The object such as `globalThis` to add the built-in modules to.
210+
* @param {string} dummyModuleName The label representing the set of built-in modules to add.
211+
*/
170212
function addBuiltinLibsToObject(object, dummyModuleName) {
171213
// Make built-in modules available directly (loaded lazily).
172214
const Module = require('internal/modules/cjs/loader').Module;
@@ -227,9 +269,8 @@ function addBuiltinLibsToObject(object, dummyModuleName) {
227269
}
228270

229271
/**
230-
*
272+
* If a referrer is an URL instance or absolute path, convert it into an URL string.
231273
* @param {string | URL} referrer
232-
* @returns {string}
233274
*/
234275
function normalizeReferrerURL(referrer) {
235276
if (typeof referrer === 'string' && path.isAbsolute(referrer)) {
@@ -238,7 +279,10 @@ function normalizeReferrerURL(referrer) {
238279
return new URL(referrer).href;
239280
}
240281

241-
// For error messages only - used to check if ESM syntax is in use.
282+
/**
283+
* For error messages only, check if ESM syntax is in use.
284+
* @param {string} code
285+
*/
242286
function hasEsmSyntax(code) {
243287
debug('Checking for ESM syntax');
244288
const parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;

lib/internal/modules/run_main.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const {
77
const { getOptionValue } = require('internal/options');
88
const path = require('path');
99

10+
/**
11+
* Get the absolute path to the main entry point.
12+
* @param {string} main Entry point path
13+
*/
1014
function resolveMainPath(main) {
1115
// Note extension resolution for the main entry point can be deprecated in a
1216
// future major.
@@ -21,6 +25,10 @@ function resolveMainPath(main) {
2125
return mainPath;
2226
}
2327

28+
/**
29+
* Determine whether the main entry point should be loaded through the ESM Loader.
30+
* @param {string} mainPath Absolute path to the main entry point
31+
*/
2432
function shouldUseESMLoader(mainPath) {
2533
/**
2634
* @type {string[]} userLoaders A list of custom loaders registered by the user
@@ -41,6 +49,10 @@ function shouldUseESMLoader(mainPath) {
4149
return pkg && pkg.data.type === 'module';
4250
}
4351

52+
/**
53+
* Run the main entry point through the ESM Loader.
54+
* @param {string} mainPath Absolute path to the main entry point
55+
*/
4456
function runMainESM(mainPath) {
4557
const { loadESM } = require('internal/process/esm_loader');
4658
const { pathToFileURL } = require('internal/url');
@@ -52,6 +64,10 @@ function runMainESM(mainPath) {
5264
}));
5365
}
5466

67+
/**
68+
* Handle process exit events around the main entry point promise.
69+
* @param {Promise} promise Main entry point promise
70+
*/
5571
async function handleMainPromise(promise) {
5672
const {
5773
handleProcessExit,
@@ -64,9 +80,12 @@ async function handleMainPromise(promise) {
6480
}
6581
}
6682

67-
// For backwards compatibility, we have to run a bunch of
68-
// monkey-patchable code that belongs to the CJS loader (exposed by
69-
// `require('module')`) even when the entry point is ESM.
83+
/**
84+
* Parse the CLI main entry point string and run it.
85+
* For backwards compatibility, we have to run a bunch of monkey-patchable code that belongs to the CJS loader (exposed
86+
* by `require('module')`) even when the entry point is ESM.
87+
* @param {string} main CLI main entry point string
88+
*/
7089
function executeUserEntryPoint(main = process.argv[1]) {
7190
const resolvedMain = resolveMainPath(main);
7291
const useESMLoader = shouldUseESMLoader(resolvedMain);

0 commit comments

Comments
 (0)