Skip to content

Commit cb3c3af

Browse files
anonrigCeres6
authored andcommitted
module: reduce url invocations in esm/load.js
PR-URL: nodejs#48337 Refs: nodejs/performance#92 Reviewed-By: Stephen Belanger <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent bc38f69 commit cb3c3af

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,37 +29,42 @@ 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:') {
41+
if (protocol === 'file:') {
3742
const { readFile: readFileAsync } = require('internal/fs/promises').exports;
38-
source = await readFileAsync(parsed);
39-
} else if (parsed.protocol === 'data:') {
40-
const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname);
43+
source = await readFileAsync(url);
44+
} else if (protocol === 'data:') {
45+
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
4146
if (!match) {
42-
throw new ERR_INVALID_URL(url);
47+
throw new ERR_INVALID_URL(responseURL);
4348
}
4449
const { 1: base64, 2: body } = match;
4550
source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8');
4651
} else if (experimentalNetworkImports && (
47-
parsed.protocol === 'https:' ||
48-
parsed.protocol === 'http:'
52+
protocol === 'https:' ||
53+
protocol === 'http:'
4954
)) {
5055
const { fetchModule } = require('internal/modules/esm/fetch_module');
51-
const res = await fetchModule(parsed, context);
56+
const res = await fetchModule(url, context);
5257
source = await res.body;
5358
responseURL = res.resolvedHREF;
5459
} else {
5560
const supportedSchemes = ['file', 'data'];
5661
if (experimentalNetworkImports) {
5762
ArrayPrototypePush(supportedSchemes, 'http', 'https');
5863
}
59-
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, supportedSchemes);
64+
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes);
6065
}
6166
if (policy?.manifest) {
62-
policy.manifest.assertIntegrity(parsed, source);
67+
policy.manifest.assertIntegrity(href, source);
6368
}
6469
return { __proto__: null, responseURL, source };
6570
}
@@ -93,7 +98,7 @@ async function defaultLoad(url, context = kEmptyObject) {
9398
) {
9499
source = null;
95100
} else if (source == null) {
96-
({ responseURL, source } = await getSource(url, context));
101+
({ responseURL, source } = await getSource(urlInstance, context));
97102
}
98103

99104
return {

0 commit comments

Comments
 (0)