Skip to content

Commit bd035a6

Browse files
committed
module: reduce url invocations in esm/load.js
1 parent 5c27cc2 commit bd035a6

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lib/internal/modules/esm/load.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,36 @@ const {
3030
const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/;
3131

3232
async function getSource(url, context) {
33-
const parsed = new URL(url);
34-
let responseURL = url;
33+
const protocol = url.protocol;
34+
let responseURL = url.href;
3535
let source;
36-
if (parsed.protocol === 'file:') {
36+
if (protocol === 'file:') {
3737
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);
38+
source = await readFileAsync(url);
39+
} else if (protocol === 'data:') {
40+
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
4141
if (!match) {
42-
throw new ERR_INVALID_URL(url);
42+
throw new ERR_INVALID_URL(responseURL);
4343
}
4444
const { 1: base64, 2: body } = match;
4545
source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8');
4646
} else if (experimentalNetworkImports && (
47-
parsed.protocol === 'https:' ||
48-
parsed.protocol === 'http:'
47+
protocol === 'https:' ||
48+
protocol === 'http:'
4949
)) {
5050
const { fetchModule } = require('internal/modules/esm/fetch_module');
51-
const res = await fetchModule(parsed, context);
51+
const res = await fetchModule(url, context);
5252
source = await res.body;
5353
responseURL = res.resolvedHREF;
5454
} else {
5555
const supportedSchemes = ['file', 'data'];
5656
if (experimentalNetworkImports) {
5757
ArrayPrototypePush(supportedSchemes, 'http', 'https');
5858
}
59-
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, supportedSchemes);
59+
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes);
6060
}
6161
if (policy?.manifest) {
62-
policy.manifest.assertIntegrity(parsed, source);
62+
policy.manifest.assertIntegrity(url, source);
6363
}
6464
return { __proto__: null, responseURL, source };
6565
}
@@ -93,7 +93,7 @@ async function defaultLoad(url, context = kEmptyObject) {
9393
) {
9494
source = null;
9595
} else if (source == null) {
96-
({ responseURL, source } = await getSource(url, context));
96+
({ responseURL, source } = await getSource(urlInstance, context));
9797
}
9898

9999
return {

0 commit comments

Comments
 (0)