Skip to content

Commit 42c4ca6

Browse files
JakobJingleheimeraduh95
authored andcommitted
esm: syncify default path of ModuleLoader.load
PR-URL: #57419 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
1 parent a7406f5 commit 42c4ca6

File tree

3 files changed

+7
-32
lines changed

3 files changed

+7
-32
lines changed

lib/internal/modules/esm/load.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,7 @@ const {
3030

3131
/**
3232
* @param {URL} url URL to the module
33-
* @param {ESModuleContext} context used to decorate error messages
34-
* @returns {Promise<{ responseURL: string, source: string | BufferView }>}
35-
*/
36-
async function getSource(url, context) {
37-
const { protocol, href } = url;
38-
const responseURL = href;
39-
let source;
40-
if (protocol === 'file:') {
41-
const { readFile: readFileAsync } = require('internal/fs/promises').exports;
42-
source = await readFileAsync(url);
43-
} else if (protocol === 'data:') {
44-
const result = dataURLProcessor(url);
45-
if (result === 'failure') {
46-
throw new ERR_INVALID_URL(responseURL, null);
47-
}
48-
source = BufferFrom(result.body);
49-
} else {
50-
const supportedSchemes = ['file', 'data'];
51-
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes);
52-
}
53-
return { __proto__: null, responseURL, source };
54-
}
55-
56-
/**
57-
* @param {URL} url URL to the module
58-
* @param {ESModuleContext} context used to decorate error messages
33+
* @param {LoadContext} context used to decorate error messages
5934
* @returns {{ responseURL: string, source: string | BufferView }}
6035
*/
6136
function getSourceSync(url, context) {
@@ -84,7 +59,7 @@ function getSourceSync(url, context) {
8459
* @param {LoadContext} context
8560
* @returns {LoadReturn}
8661
*/
87-
async function defaultLoad(url, context = kEmptyObject) {
62+
function defaultLoad(url, context = kEmptyObject) {
8863
let responseURL = url;
8964
let {
9065
importAttributes,
@@ -111,13 +86,13 @@ async function defaultLoad(url, context = kEmptyObject) {
11186
format ??= 'builtin';
11287
} else if (format !== 'commonjs' || defaultType === 'module') {
11388
if (source == null) {
114-
({ responseURL, source } = await getSource(urlInstance, context));
89+
({ responseURL, source } = getSourceSync(urlInstance, context));
11590
context = { __proto__: context, source };
11691
}
11792

11893
if (format == null) {
11994
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
120-
format = await defaultGetFormat(urlInstance, context);
95+
format = defaultGetFormat(urlInstance, context);
12196

12297
if (format === 'commonjs') {
12398
// For backward compatibility reasons, we need to discard the source in

lib/internal/modules/esm/loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,9 +785,9 @@ class ModuleLoader {
785785
* if any.
786786
* @param {string} url The URL of the module to be loaded.
787787
* @param {object} context Metadata about the module
788-
* @returns {Promise<{ format: ModuleFormat, source: ModuleSource }>}
788+
* @returns {Promise<{ format: ModuleFormat, source: ModuleSource }> | { format: ModuleFormat, source: ModuleSource }}
789789
*/
790-
async load(url, context) {
790+
load(url, context) {
791791
if (loadHooks.length) {
792792
// Has module.registerHooks() hooks, use the synchronous variant that can handle both hooks.
793793
return this.#loadSync(url, context);

lib/internal/modules/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ function normalizeReferrerURL(referrerName) {
264264
}
265265

266266
if (StringPrototypeStartsWith(referrerName, 'file://') ||
267-
URLCanParse(referrerName)) {
267+
URLCanParse(referrerName)) {
268268
return referrerName;
269269
}
270270

0 commit comments

Comments
 (0)