Skip to content

Commit b6760b3

Browse files
JakobJingleheimertargos
authored andcommitted
esm: syncify default path of ModuleLoader.load
PR-URL: #57419 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent 6390f70 commit b6760b3

3 files changed

Lines changed: 7 additions & 32 deletions

File tree

‎lib/internal/modules/esm/load.js‎

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

2727
/**
2828
* @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-
asyncfunctiongetSource(url,context){
33-
const{ protocol, href }=url;
34-
constresponseURL=href;
35-
letsource;
36-
if(protocol==='file:'){
37-
const{readFile: readFileAsync}=require('internal/fs/promises').exports;
38-
source=awaitreadFileAsync(url);
39-
}elseif(protocol==='data:'){
40-
constresult=dataURLProcessor(url);
41-
if(result==='failure'){
42-
thrownewERR_INVALID_URL(responseURL,null);
43-
}
44-
source=BufferFrom(result.body);
45-
}else{
46-
constsupportedSchemes=['file','data'];
47-
thrownewERR_UNSUPPORTED_ESM_URL_SCHEME(url,supportedSchemes);
48-
}
49-
return{__proto__: null, responseURL, source };
50-
}
51-
52-
/**
53-
* @param {URL} url URL to the module
54-
* @param {ESModuleContext} context used to decorate error messages
29+
* @param {LoadContext} context used to decorate error messages
5530
* @returns {{ responseURL: string, source: string | BufferView }}
5631
*/
5732
functiongetSourceSync(url,context){
@@ -80,7 +55,7 @@ function getSourceSync(url, context) {
8055
* @param {LoadContext} context
8156
* @returns {LoadReturn}
8257
*/
83-
asyncfunctiondefaultLoad(url,context=kEmptyObject){
58+
functiondefaultLoad(url,context=kEmptyObject){
8459
letresponseURL=url;
8560
let{
8661
importAttributes,
@@ -110,13 +85,13 @@ async function defaultLoad(url, context = kEmptyObject) {
11085
source=null;
11186
}elseif(format!=='commonjs'){
11287
if(source==null){
113-
({ responseURL, source }=awaitgetSource(urlInstance,context));
88+
({ responseURL, source }=getSourceSync(urlInstance,context));
11489
context={__proto__: context, source };
11590
}
11691

11792
if(format==null){
11893
// Now that we have the source for the module, run `defaultGetFormat` to detect its format.
119-
format=awaitdefaultGetFormat(urlInstance,context);
94+
format=defaultGetFormat(urlInstance,context);
12095

12196
if(format==='commonjs'){
12297
// 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
@@ -806,9 +806,9 @@ class ModuleLoader {
806806
* if any.
807807
* @param {string} url The URL of the module to be loaded.
808808
* @param {object} context Metadata about the module
809-
* @returns {Promise<{ format: ModuleFormat, source: ModuleSource }>}
809+
* @returns {Promise<{ format: ModuleFormat, source: ModuleSource }> | { format: ModuleFormat, source: ModuleSource }}
810810
*/
811-
asyncload(url,context){
811+
load(url,context){
812812
if(loadHooks.length){
813813
// Has module.registerHooks() hooks, use the synchronous variant that can handle both hooks.
814814
returnthis.#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
returnreferrerName;
269269
}
270270

0 commit comments

Comments
 (0)