Skip to content

Commit

Permalink
module: reduce url invocations in esm/load.js
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jun 5, 2023
1 parent 5c27cc2 commit cadc77b
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/internal/modules/esm/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ const {

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

async function getSource(url, context) {
const parsed = new URL(url);
let responseURL = url;
async function getSource(parsed, context) {
const protocol = parsed.protocol;
let responseURL = parsed.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:') {
} else if (protocol === 'data:') {
const match = RegExpPrototypeExec(DATA_URL_PATTERN, parsed.pathname);
if (!match) {
throw new ERR_INVALID_URL(url);
throw new ERR_INVALID_URL(parsed.href);
}
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);
Expand Down Expand Up @@ -93,7 +93,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

0 comments on commit cadc77b

Please sign in to comment.