mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 00:20:08 +01:00
ESM resolution and loading is now always synchronous from a non-loader-hook thread. If no asynchrnous loader hooks are registered, the resolution/loading is entirely synchronous. If asynchronous loader hooks are registered, these would be synchronous on the non-loader-hook thread, and asynchronous on the loader hook thread. This avoids several races caused by async/sync loading sharing the same cache. In particular, asynchronous loader hooks now works with `require(esm)` - previously it tends to break due to races. In addition, when an asynchronous loader hook returns a promise that never settles, the main thread no longer silently exits with exit code 13, leaving the code below any module loading calls silently ignored without being executed. Instead, it now throws ERR_ASYNC_LOADER_REQUEST_NEVER_SETTLED which can be caught and handled by the main thread. If the module request comes from `import()`, the never-settling promise is now relayed to the result returned by `import()`. Drive-by: when annotating the error about importing undetectable named exports from CommonJS, it now no longer reload the source code of the CommonJS module, and instead reuses format information cached when the module was loaded for linking. PR-URL: https://github.com/nodejs/node/pull/60380 Fixes: https://github.com/nodejs/node/issues/59666 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jacob Smith <jacob@frende.me>
84 lines
2.3 KiB
JavaScript
84 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
StringPrototypeStartsWith,
|
|
} = primordials;
|
|
|
|
const { getOptionValue } = require('internal/options');
|
|
const {
|
|
setLazyPathHelpers,
|
|
} = internalBinding('modules');
|
|
|
|
const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta-resolve');
|
|
|
|
/**
|
|
* Generate a function to be used as import.meta.resolve for a particular module.
|
|
* @param {string} defaultParentURL The default base to use for resolution
|
|
* @param {typeof import('./loader.js').ModuleLoader} loader Reference to the current module loader
|
|
* @param {bool} allowParentURL Whether to permit parentURL second argument for contextual resolution
|
|
* @returns {(specifier: string) => string} Function to assign to import.meta.resolve
|
|
*/
|
|
function createImportMetaResolve(defaultParentURL, loader, allowParentURL) {
|
|
/**
|
|
* @param {string} specifier
|
|
* @param {URL['href']} [parentURL] When `--experimental-import-meta-resolve` is specified, a
|
|
* second argument can be provided.
|
|
* @returns {string}
|
|
*/
|
|
return function resolve(specifier, parentURL = defaultParentURL) {
|
|
let url;
|
|
|
|
if (!allowParentURL) {
|
|
parentURL = defaultParentURL;
|
|
}
|
|
|
|
try {
|
|
const request = { specifier, __proto__: null };
|
|
({ url } = loader.resolveSync(parentURL, request));
|
|
return url;
|
|
} catch (error) {
|
|
switch (error?.code) {
|
|
case 'ERR_UNSUPPORTED_DIR_IMPORT':
|
|
case 'ERR_MODULE_NOT_FOUND':
|
|
({ url } = error);
|
|
if (url) {
|
|
return url;
|
|
}
|
|
}
|
|
throw error;
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Create the `import.meta` object for a module.
|
|
* @param {object} meta
|
|
* @param {{url: string, isMain?: boolean}} context
|
|
* @param {typeof import('./loader.js').ModuleLoader} loader Reference to the current module loader
|
|
* @returns {{dirname?: string, filename?: string, url: string, resolve?: Function}}
|
|
*/
|
|
function initializeImportMeta(meta, context, loader) {
|
|
const { url, isMain } = context;
|
|
|
|
// Alphabetical
|
|
if (StringPrototypeStartsWith(url, 'file:') === true) {
|
|
// dirname
|
|
// filename
|
|
setLazyPathHelpers(meta, url);
|
|
}
|
|
|
|
meta.main = !!isMain;
|
|
|
|
if (!loader || loader.allowImportMetaResolve) {
|
|
meta.resolve = createImportMetaResolve(url, loader, experimentalImportMetaResolve);
|
|
}
|
|
|
|
meta.url = url;
|
|
|
|
return meta;
|
|
}
|
|
|
|
module.exports = {
|
|
initializeImportMeta,
|
|
};
|