Skip to content

Commit a6cfea3

Browse files
aduh95UlisesGascon
authored andcommittedSep 10, 2023
esm: align sync and async load implementations
Refs: #48272 PR-URL: #49152 Refs: #47999 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jacob Smith <jacob@frende.me>

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed
 

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

+20-11
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,30 @@ async function getSource(url, context) {
7070
return { __proto__: null, responseURL, source };
7171
}
7272

73+
/**
74+
* @param {URL} url URL to the module
75+
* @param {ESModuleContext} context used to decorate error messages
76+
* @returns {{ responseURL: string, source: string | BufferView }}
77+
*/
7378
function getSourceSync(url, context) {
74-
const parsed = new URL(url);
75-
const responseURL = url;
79+
const { protocol, href } = url;
80+
const responseURL = href;
7681
let source;
77-
if (parsed.protocol === 'file:') {
78-
source = readFileSync(parsed);
79-
} else if (parsed.protocol === 'data:') {
80-
const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname);
82+
if (protocol === 'file:') {
83+
source = readFileSync(url);
84+
} else if (protocol === 'data:') {
85+
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
8186
if (!match) {
82-
throw new ERR_INVALID_URL(url);
87+
throw new ERR_INVALID_URL(responseURL);
8388
}
8489
const { 1: base64, 2: body } = match;
8590
source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8');
8691
} else {
8792
const supportedSchemes = ['file', 'data'];
88-
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, supportedSchemes);
93+
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes);
8994
}
9095
if (policy?.manifest) {
91-
policy.manifest.assertIntegrity(parsed, source);
96+
policy.manifest.assertIntegrity(url, source);
9297
}
9398
return { __proto__: null, responseURL, source };
9499
}
@@ -159,14 +164,18 @@ function defaultLoadSync(url, context = kEmptyObject) {
159164
source,
160165
} = context;
161166

162-
format ??= defaultGetFormat(new URL(url), context);
167+
const urlInstance = new URL(url);
168+
169+
throwIfUnsupportedURLScheme(urlInstance, false);
170+
171+
format ??= defaultGetFormat(urlInstance, context);
163172

164173
validateAssertions(url, format, importAssertions);
165174

166175
if (format === 'builtin') {
167176
source = null;
168177
} else if (source == null) {
169-
({ responseURL, source } = getSourceSync(url, context));
178+
({ responseURL, source } = getSourceSync(urlInstance, context));
170179
}
171180

172181
return {

0 commit comments

Comments
 (0)
Please sign in to comment.