Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4ec2d92

Browse filesBrowse files
anonrigRafaelGSS
authored andcommitted
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 e10a4cd commit 4ec2d92
Copy full SHA for 4ec2d92

File tree

Expand file treeCollapse file tree

1 file changed

+18
-13
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+18
-13
lines changed
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/load.js
+18-13Lines changed: 18 additions & 13 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.