Skip to content

Commit

Permalink
module: reduce the number of URL initializations
Browse files Browse the repository at this point in the history
PR-URL: #48272
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
anonrig authored and danielleadams committed Jul 6, 2023
1 parent 253f8c4 commit e816cd2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// If user passed `-c` or `--check` arguments to Node, check its syntax
// instead of actually running the file.

const { URL } = require('internal/url');
const {
prepareMainThreadExecution,
markBootstrapComplete,
Expand Down Expand Up @@ -55,7 +56,7 @@ async function checkSyntax(source, filename) {
const { defaultResolve } = require('internal/modules/esm/resolve');
const { defaultGetFormat } = require('internal/modules/esm/get_format');
const { url } = await defaultResolve(pathToFileURL(filename).toString());
const format = await defaultGetFormat(url);
const format = await defaultGetFormat(new URL(url));
isModule = format === 'module';
}
if (isModule) {
Expand Down
22 changes: 12 additions & 10 deletions lib/internal/modules/esm/get_format.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const experimentalNetworkImports =
const experimentalSpecifierResolution =
getOptionValue('--experimental-specifier-resolution');
const { getPackageType, getPackageScopeConfig } = require('internal/modules/esm/resolve');
const { URL, fileURLToPath } = require('internal/url');
const { fileURLToPath } = require('internal/url');
const { ERR_UNKNOWN_FILE_EXTENSION } = require('internal/errors').codes;

const protocolHandlers = {
Expand Down Expand Up @@ -99,27 +99,29 @@ function getHttpProtocolModuleFormat(url, context) {
}

/**
* @param {URL | URL['href']} url
* @param {URL} url
* @param {{parentURL: string}} context
* @returns {Promise<string> | string | undefined} only works when enabled
*/
function defaultGetFormatWithoutErrors(url, context) {
const parsed = new URL(url);
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol))
const protocol = url.protocol;
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
return null;
return protocolHandlers[parsed.protocol](parsed, context, true);
}
return protocolHandlers[protocol](url, context, true);
}

/**
* @param {URL | URL['href']} url
* @param {URL} url
* @param {{parentURL: string}} context
* @returns {Promise<string> | string | undefined} only works when enabled
*/
function defaultGetFormat(url, context) {
const parsed = new URL(url);
return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ?
protocolHandlers[parsed.protocol](parsed, context, false) :
null;
const protocol = url.protocol;
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, protocol)) {
return null;
}
return protocolHandlers[protocol](url, context, false);
}

module.exports = {
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/modules/esm/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ async function defaultLoad(url, context) {
source,
} = context;

throwIfUnsupportedURLScheme(new URL(url), experimentalNetworkImports);
const urlInstance = new URL(url);

if (format == null) {
format = await defaultGetFormat(url, context);
}
throwIfUnsupportedURLScheme(urlInstance, experimentalNetworkImports);

format ??= await defaultGetFormat(urlInstance, context);

validateAssertions(url, format, importAssertions);

Expand Down

0 comments on commit e816cd2

Please sign in to comment.