module, esm: jsdoc for modules files

PR-URL: https://github.com/nodejs/node/pull/49523
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
Geoffrey Booth 2023-09-18 19:48:24 -07:00 committed by GitHub
parent d847895b8d
commit 7517c9f95b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 710 additions and 158 deletions

View file

@ -29,18 +29,28 @@ const {
const assert = require('internal/assert');
let defaultConditions;
/**
* Returns the default conditions for ES module loading.
*/
function getDefaultConditions() {
assert(defaultConditions !== undefined);
return defaultConditions;
}
/** @type {Set<string>} */
let defaultConditionsSet;
/**
* Returns the default conditions for ES module loading, as a Set.
*/
function getDefaultConditionsSet() {
assert(defaultConditionsSet !== undefined);
return defaultConditionsSet;
}
// This function is called during pre-execution, before any user code is run.
/**
* Initializes the default conditions for ESM module loading.
* This function is called during pre-execution, before any user code is run.
*/
function initializeDefaultConditions() {
const userConditions = getOptionValue('--conditions');
const noAddons = getOptionValue('--no-addons');
@ -123,7 +133,11 @@ function registerModule(referrer, registry) {
moduleRegistries.set(idSymbol, registry);
}
// The native callback
/**
* Defines the `import.meta` object for a given module.
* @param {symbol} symbol - Reference to the module.
* @param {Record<string, string | Function>} meta - The import.meta object to initialize.
*/
function initializeImportMetaObject(symbol, meta) {
if (moduleRegistries.has(symbol)) {
const { initializeImportMeta, callbackReferrer } = moduleRegistries.get(symbol);
@ -133,7 +147,14 @@ function initializeImportMetaObject(symbol, meta) {
}
}
// The native callback
/**
* Asynchronously imports a module dynamically using a callback function. The native callback.
* @param {symbol} symbol - Reference to the module.
* @param {string} specifier - The module specifier string.
* @param {Record<string, string>} assertions - The import assertions object.
* @returns {Promise<import('internal/modules/esm/loader.js').ModuleExports>} - The imported module object.
* @throws {ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING} - If the callback function is missing.
*/
async function importModuleDynamicallyCallback(symbol, specifier, assertions) {
if (moduleRegistries.has(symbol)) {
const { importModuleDynamically, callbackReferrer } = moduleRegistries.get(symbol);
@ -144,9 +165,13 @@ async function importModuleDynamicallyCallback(symbol, specifier, assertions) {
throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING();
}
// This is configured during pre-execution. Specifically it's set to true for
// the loader worker in internal/main/worker_thread.js.
let _isLoaderWorker = false;
/**
* Initializes handling of ES modules.
* This is configured during pre-execution. Specifically it's set to true for
* the loader worker in internal/main/worker_thread.js.
* @param {boolean} [isLoaderWorker=false] - A boolean indicating whether the loader is a worker or not.
*/
function initializeESM(isLoaderWorker = false) {
_isLoaderWorker = isLoaderWorker;
initializeDefaultConditions();
@ -156,10 +181,17 @@ function initializeESM(isLoaderWorker = false) {
setImportModuleDynamicallyCallback(importModuleDynamicallyCallback);
}
/**
* Determine whether the current process is a loader worker.
* @returns {boolean} Whether the current process is a loader worker.
*/
function isLoaderWorker() {
return _isLoaderWorker;
}
/**
* Register module customization hooks.
*/
async function initializeHooks() {
const customLoaderURLs = getOptionValue('--experimental-loader');