From bd035a6c1aa0278d412d2a42da0e2bf4db873a9b Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 4 Jun 2023 21:10:54 -0400 Subject: [PATCH] module: reduce url invocations in esm/load.js --- lib/internal/modules/esm/load.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/internal/modules/esm/load.js b/lib/internal/modules/esm/load.js index c929fcc649c9f1..bca7fa0b9f3259 100644 --- a/lib/internal/modules/esm/load.js +++ b/lib/internal/modules/esm/load.js @@ -30,25 +30,25 @@ const { const DATA_URL_PATTERN = /^[^/]+\/[^,;]+(?:[^,]*?)(;base64)?,([\s\S]*)$/; async function getSource(url, context) { - const parsed = new URL(url); - let responseURL = url; + const protocol = url.protocol; + let responseURL = url.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); + } 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 { @@ -56,10 +56,10 @@ async function getSource(url, context) { 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(url, source); } return { __proto__: null, responseURL, source }; } @@ -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 {