Skip to content

Commit bf1525c

Browse files
anonrigruyadorno
authored andcommittedAug 29, 2023
module: reduce url invocations in esm/load.js
PR-URL: #48337 Refs: nodejs/performance#92 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 0651358 commit bf1525c

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed
 

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

+18-13
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,40 @@ const {
2929

3030
const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/;
3131

32+
/**
33+
* @param {URL} url URL to the module
34+
* @param {ESModuleContext} context used to decorate error messages
35+
* @returns {{ responseURL: string, source: string | BufferView }}
36+
*/
3237
async function getSource(url, context) {
33-
const parsed = new URL(url);
34-
let responseURL = url;
38+
const { protocol, href } = url;
39+
let responseURL = href;
3540
let source;
36-
if (parsed.protocol === 'file:') {
37-
source = await readFileAsync(parsed);
38-
} else if (parsed.protocol === 'data:') {
39-
const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname);
41+
if (protocol === 'file:') {
42+
source = await readFileAsync(url);
43+
} else if (protocol === 'data:') {
44+
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
4045
if (!match) {
41-
throw new ERR_INVALID_URL(url);
46+
throw new ERR_INVALID_URL(responseURL);
4247
}
4348
const { 1: base64, 2: body } = match;
4449
source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8');
4550
} else if (experimentalNetworkImports && (
46-
parsed.protocol === 'https:' ||
47-
parsed.protocol === 'http:'
51+
protocol === 'https:' ||
52+
protocol === 'http:'
4853
)) {
49-
const res = await fetchModule(parsed, context);
54+
const res = await fetchModule(url, context);
5055
source = await res.body;
5156
responseURL = res.resolvedHREF;
5257
} else {
5358
const supportedSchemes = ['file', 'data'];
5459
if (experimentalNetworkImports) {
5560
ArrayPrototypePush(supportedSchemes, 'http', 'https');
5661
}
57-
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, supportedSchemes);
62+
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes);
5863
}
5964
if (policy?.manifest) {
60-
policy.manifest.assertIntegrity(parsed, source);
65+
policy.manifest.assertIntegrity(href, source);
6166
}
6267
return { __proto__: null, responseURL, source };
6368
}
@@ -91,7 +96,7 @@ async function defaultLoad(url, context) {
9196
) {
9297
source = null;
9398
} else if (source == null) {
94-
({ responseURL, source } = await getSource(url, context));
99+
({ responseURL, source } = await getSource(urlInstance, context));
95100
}
96101

97102
return {

0 commit comments

Comments
 (0)
Please sign in to comment.