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
Backport-PR-URL: #50669
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 and targos committed Nov 23, 2023
1 parent bac9b17 commit 2a528b7
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 @@ -34,6 +34,7 @@ const {
ERR_WORKER_UNSERIALIZABLE_ERROR,
} = require('internal/errors').codes;
const { URL } = require('internal/url');
const { canParse: urlCanParse } = internalBinding('url');
const { receiveMessageOnPort } = require('worker_threads');
const {
isAnyArrayBuffer,
Expand Down Expand Up @@ -270,17 +271,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 @@ -349,16 +350,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 @@ -39,6 +39,7 @@ const experimentalNetworkImports =
getOptionValue('--experimental-network-imports');
const typeFlag = getOptionValue('--input-type');
const { URL, pathToFileURL, fileURLToPath, toPathIfFileURL, isURL } = require('internal/url');
const { canParse: canParseURL } = internalBinding('url');
const {
ERR_INPUT_TYPE_NOT_ALLOWED,
ERR_INVALID_ARG_TYPE,
Expand Down Expand Up @@ -393,14 +394,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 2a528b7

Please sign in to comment.