esm: syncify default path of ModuleLoader.load

PR-URL: https://github.com/nodejs/node/pull/57419
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Jacob Smith 2025-06-11 23:53:56 +02:00 committed by GitHub
parent 5457443210
commit 5e1537c30b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 32 deletions

View File

@ -26,32 +26,7 @@ const {
/**
* @param {URL} url URL to the module
* @param {ESModuleContext} context used to decorate error messages
* @returns {Promise<{ responseURL: string, source: string | BufferView }>}
*/
async function getSource(url, context) {
const { protocol, href } = url;
const responseURL = href;
let source;
if (protocol === 'file:') {
const { readFile: readFileAsync } = require('internal/fs/promises').exports;
source = await readFileAsync(url);
} else if (protocol === 'data:') {
const result = dataURLProcessor(url);
if (result === 'failure') {
throw new ERR_INVALID_URL(responseURL, null);
}
source = BufferFrom(result.body);
} else {
const supportedSchemes = ['file', 'data'];
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes);
}
return { __proto__: null, responseURL, source };
}
/**
* @param {URL} url URL to the module
* @param {ESModuleContext} context used to decorate error messages
* @param {LoadContext} context used to decorate error messages
* @returns {{ responseURL: string, source: string | BufferView }}
*/
function getSourceSync(url, context) {
@ -80,7 +55,7 @@ function getSourceSync(url, context) {
* @param {LoadContext} context
* @returns {LoadReturn}
*/
async function defaultLoad(url, context = kEmptyObject) {
function defaultLoad(url, context = kEmptyObject) {
let responseURL = url;
let {
importAttributes,
@ -110,13 +85,13 @@ async function defaultLoad(url, context = kEmptyObject) {
source = null;
} else if (format !== 'commonjs') {
if (source == null) {
({ responseURL, source } = await getSource(urlInstance, context));
({ responseURL, source } = getSourceSync(urlInstance, context));
context = { __proto__: context, source };
}
if (format == null) {
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
format = await defaultGetFormat(urlInstance, context);
format = defaultGetFormat(urlInstance, context);
if (format === 'commonjs') {
// For backward compatibility reasons, we need to discard the source in

View File

@ -806,9 +806,9 @@ class ModuleLoader {
* if any.
* @param {string} url The URL of the module to be loaded.
* @param {object} context Metadata about the module
* @returns {Promise<{ format: ModuleFormat, source: ModuleSource }>}
* @returns {Promise<{ format: ModuleFormat, source: ModuleSource }> | { format: ModuleFormat, source: ModuleSource }}
*/
async load(url, context) {
load(url, context) {
if (loadHooks.length) {
// Has module.registerHooks() hooks, use the synchronous variant that can handle both hooks.
return this.#loadSync(url, context);

View File

@ -264,7 +264,7 @@ function normalizeReferrerURL(referrerName) {
}
if (StringPrototypeStartsWith(referrerName, 'file://') ||
URLCanParse(referrerName)) {
URLCanParse(referrerName)) {
return referrerName;
}