Skip to content

Commit e0d702b

Browse files
esm: sincify defaultLoad
1 parent 38390e5 commit e0d702b

4 files changed

Lines changed: 7 additions & 31 deletions

File tree

lib/internal/modules/esm/load.js

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,6 @@ const {
2424
dataURLProcessor,
2525
} = require('internal/data_url');
2626

27-
/**
28-
* @param {URL} url URL to the module
29-
* @param {ESModuleContext} context used to decorate error messages
30-
* @returns {Promise<{ responseURL: string, source: string | BufferView }>}
31-
*/
32-
async function getSource(url, context) {
33-
const { protocol, href } = url;
34-
const responseURL = href;
35-
let source;
36-
if (protocol === 'file:') {
37-
const { readFile: readFileAsync } = require('internal/fs/promises').exports;
38-
source = await readFileAsync(url);
39-
} else if (protocol === 'data:') {
40-
const result = dataURLProcessor(url);
41-
if (result === 'failure') {
42-
throw new ERR_INVALID_URL(responseURL, null);
43-
}
44-
source = BufferFrom(result.body);
45-
} else {
46-
const supportedSchemes = ['file', 'data'];
47-
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes);
48-
}
49-
return { __proto__: null, responseURL, source };
50-
}
5127

5228
/**
5329
* @param {URL} url URL to the module
@@ -80,7 +56,7 @@ function getSourceSync(url, context) {
8056
* @param {LoadContext} context
8157
* @returns {LoadReturn}
8258
*/
83-
async function defaultLoad(url, context = kEmptyObject) {
59+
function defaultLoad(url, context = kEmptyObject) {
8460
let responseURL = url;
8561
let {
8662
importAttributes,
@@ -110,13 +86,13 @@ async function defaultLoad(url, context = kEmptyObject) {
11086
source = null;
11187
} else if (format !== 'commonjs') {
11288
if (source == null) {
113-
({ responseURL, source } = await getSource(urlInstance, context));
89+
({ responseURL, source } = getSourceSync(urlInstance, context));
11490
context = { __proto__: context, source };
11591
}
11692

11793
if (format == null) {
11894
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
119-
format = await defaultGetFormat(urlInstance, context);
95+
format = defaultGetFormat(urlInstance, context);
12096

12197
if (format === 'commonjs') {
12298
// 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
@@ -804,9 +804,9 @@ class ModuleLoader {
804804
* if any.
805805
* @param {string} url The URL of the module to be loaded.
806806
* @param {object} context Metadata about the module
807-
* @returns {Promise<{ format: ModuleFormat, source: ModuleSource }>}
807+
* @returns {{ format: ModuleFormat, source: ModuleSource }}
808808
*/
809-
async load(url, context) {
809+
load(url, context) {
810810
if (loadHooks.length) {
811811
// Has module.registerHooks() hooks, use the synchronous variant that can handle both hooks.
812812
return this.#loadSync(url, context);

lib/internal/modules/esm/module_job.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class ModuleJob extends ModuleJobBase {
238238
// in the import attributes and some formats require them; but we only
239239
// care about CommonJS for the purposes of this error message.
240240
({ format } =
241-
await this.#loader.load(childFileURL));
241+
this.#loader.load(childFileURL));
242242
} catch {
243243
// Continue regardless of error.
244244
}

lib/internal/modules/esm/resolve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ function defaultResolve(specifier, context = {}) {
965965
if (protocol === 'node:') { return { __proto__: null, url: specifier }; }
966966

967967

968-
const isMain = parentURL === undefined;
968+
const isMain = parentURL === undefined || StringPrototypeStartsWith(parentURL, 'node:internal/');
969969
if (isMain) {
970970
parentURL = getCWDURL().href;
971971

0 commit comments

Comments
 (0)