@@ -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> } */
4043let cjsConditions ;
44+ /**
45+ * Define the conditions that apply to the CommonJS loader.
46+ */
4147function 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+ */
5463function 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 } */
7288let $Module = null ;
89+ /**
90+ * Import the Module class on first use.
91+ */
7392function 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+ */
81102const 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+ */
82108function 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 */
163200function 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+ */
170212function 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 */
234275function 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+ */
242286function hasEsmSyntax ( code ) {
243287 debug ( 'Checking for ESM syntax' ) ;
244288 const parser = require ( 'internal/deps/acorn/acorn/dist/acorn' ) . Parser ;
0 commit comments