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:
parent
5457443210
commit
5e1537c30b
@ -26,32 +26,7 @@ const {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {URL} url URL to the module
|
* @param {URL} url URL to the module
|
||||||
* @param {ESModuleContext} context used to decorate error messages
|
* @param {LoadContext} 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
|
|
||||||
* @returns {{ responseURL: string, source: string | BufferView }}
|
* @returns {{ responseURL: string, source: string | BufferView }}
|
||||||
*/
|
*/
|
||||||
function getSourceSync(url, context) {
|
function getSourceSync(url, context) {
|
||||||
@ -80,7 +55,7 @@ function getSourceSync(url, context) {
|
|||||||
* @param {LoadContext} context
|
* @param {LoadContext} context
|
||||||
* @returns {LoadReturn}
|
* @returns {LoadReturn}
|
||||||
*/
|
*/
|
||||||
async function defaultLoad(url, context = kEmptyObject) {
|
function defaultLoad(url, context = kEmptyObject) {
|
||||||
let responseURL = url;
|
let responseURL = url;
|
||||||
let {
|
let {
|
||||||
importAttributes,
|
importAttributes,
|
||||||
@ -110,13 +85,13 @@ async function defaultLoad(url, context = kEmptyObject) {
|
|||||||
source = null;
|
source = null;
|
||||||
} else if (format !== 'commonjs') {
|
} else if (format !== 'commonjs') {
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
({ responseURL, source } = await getSource(urlInstance, context));
|
({ responseURL, source } = getSourceSync(urlInstance, context));
|
||||||
context = { __proto__: context, source };
|
context = { __proto__: context, source };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (format == null) {
|
if (format == null) {
|
||||||
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
|
// 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') {
|
if (format === 'commonjs') {
|
||||||
// For backward compatibility reasons, we need to discard the source in
|
// For backward compatibility reasons, we need to discard the source in
|
||||||
|
@ -806,9 +806,9 @@ class ModuleLoader {
|
|||||||
* if any.
|
* if any.
|
||||||
* @param {string} url The URL of the module to be loaded.
|
* @param {string} url The URL of the module to be loaded.
|
||||||
* @param {object} context Metadata about the module
|
* @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) {
|
if (loadHooks.length) {
|
||||||
// Has module.registerHooks() hooks, use the synchronous variant that can handle both hooks.
|
// Has module.registerHooks() hooks, use the synchronous variant that can handle both hooks.
|
||||||
return this.#loadSync(url, context);
|
return this.#loadSync(url, context);
|
||||||
|
@ -264,7 +264,7 @@ function normalizeReferrerURL(referrerName) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (StringPrototypeStartsWith(referrerName, 'file://') ||
|
if (StringPrototypeStartsWith(referrerName, 'file://') ||
|
||||||
URLCanParse(referrerName)) {
|
URLCanParse(referrerName)) {
|
||||||
return referrerName;
|
return referrerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user