Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: reduce url invocations in esm/load.js #48337

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 18 additions & 13 deletions lib/internal/modules/esm/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,42 @@ const {

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

/**
* @param {URL} url URL to the module
* @param {ESModuleContext} context used to decorate error messages
* @returns {{ responseURL: string, source: string | BufferView }}
*/
async function getSource(url, context) {
anonrig marked this conversation as resolved.
Show resolved Hide resolved
const parsed = new URL(url);
let responseURL = url;
const { protocol, href } = url;
let responseURL = href;
let source;
if (parsed.protocol === 'file:') {
if (protocol === 'file:') {
const { readFile: readFileAsync } = require('internal/fs/promises').exports;
source = await readFileAsync(parsed);
} else if (parsed.protocol === 'data:') {
const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname);
source = await readFileAsync(url);
GeoffreyBooth marked this conversation as resolved.
Show resolved Hide resolved
} else if (protocol === 'data:') {
const match = RegExpPrototypeExec(DATA_URL_PATTERN, url.pathname);
if (!match) {
throw new ERR_INVALID_URL(url);
throw new ERR_INVALID_URL(responseURL);
}
const { 1: base64, 2: body } = match;
source = BufferFrom(decodeURIComponent(body), base64 ? 'base64' : 'utf8');
} else if (experimentalNetworkImports && (
parsed.protocol === 'https:' ||
parsed.protocol === 'http:'
protocol === 'https:' ||
protocol === 'http:'
)) {
const { fetchModule } = require('internal/modules/esm/fetch_module');
const res = await fetchModule(parsed, context);
const res = await fetchModule(url, context);
source = await res.body;
responseURL = res.resolvedHREF;
} else {
const supportedSchemes = ['file', 'data'];
if (experimentalNetworkImports) {
ArrayPrototypePush(supportedSchemes, 'http', 'https');
}
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed, supportedSchemes);
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(url, supportedSchemes);
}
if (policy?.manifest) {
policy.manifest.assertIntegrity(parsed, source);
policy.manifest.assertIntegrity(href, source);
}
return { __proto__: null, responseURL, source };
}
Expand Down Expand Up @@ -93,7 +98,7 @@ async function defaultLoad(url, context = kEmptyObject) {
) {
source = null;
} else if (source == null) {
({ responseURL, source } = await getSource(url, context));
({ responseURL, source } = await getSource(urlInstance, context));
}

return {
Expand Down