diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index c929fcc649c9f11..1b78466352a8ee1 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -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); @@ -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 {