Skip to content

Commit

Permalink
esm: avoid try/catch when validating urls
Browse files Browse the repository at this point in the history
PR-URL: #47541
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Jacob Smith <jacob@frende.me>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
  • Loading branch information
anonrig authored Apr 15, 2023
1 parent 40d808f commit 17570c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
17 changes: 9 additions & 8 deletions lib/internal/modules/esm/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
} = require('internal/errors').codes;
const { exitCodes: { kUnfinishedTopLevelAwait } } = internalBinding('errors');
const { URL } = require('internal/url');
const { canParse: urlCanParse } = internalBinding('url');
const { receiveMessageOnPort } = require('worker_threads');
const {
isAnyArrayBuffer,
Expand Down Expand Up @@ -272,17 +273,17 @@ class Hooks {

// Avoid expensive URL instantiation for known-good URLs
if (!this.#validatedUrls.has(url)) {
try {
new URL(url);
this.#validatedUrls.add(url);
} catch {
// No need to convert to string, since the type is already validated
if (!urlCanParse(url)) {
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
'a URL string',
hookErrIdentifier,
'url',
url,
);
}

this.#validatedUrls.add(url);
}

if (
Expand Down Expand Up @@ -352,16 +353,16 @@ class Hooks {

// Avoid expensive URL instantiation for known-good URLs
if (!this.#validatedUrls.has(nextUrl)) {
try {
new URL(nextUrl);
this.#validatedUrls.add(nextUrl);
} catch {
// No need to convert to string, since the type is already validated
if (!urlCanParse(nextUrl)) {
throw new ERR_INVALID_ARG_VALUE(
`${hookErrIdentifier} url`,
nextUrl,
'should be a URL string',
);
}

this.#validatedUrls.add(nextUrl);
}

if (ctx) { validateObject(ctx, `${hookErrIdentifier} context`); }
Expand Down
11 changes: 3 additions & 8 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const experimentalNetworkImports =
getOptionValue('--experimental-network-imports');
const typeFlag = getOptionValue('--input-type');
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
const { canParse: canParseURL } = internalBinding('url');
const {
ERR_INPUT_TYPE_NOT_ALLOWED,
ERR_INVALID_ARG_TYPE,
Expand Down Expand Up @@ -333,14 +334,8 @@ function resolvePackageTargetString(
if (!StringPrototypeStartsWith(target, './')) {
if (internal && !StringPrototypeStartsWith(target, '../') &&
!StringPrototypeStartsWith(target, '/')) {
let isURL = false;
try {
new URL(target);
isURL = true;
} catch {
// Continue regardless of error.
}
if (!isURL) {
// No need to convert target to string, since it's already presumed to be
if (!canParseURL(target)) {
const exportTarget = pattern ?
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
target + subpath;
Expand Down

0 comments on commit 17570c0

Please sign in to comment.