Skip to content

Commit 5375959

Browse files
committed
esm: avoid try/catch when validating urls
1 parent 1323992 commit 5375959

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

lib/internal/modules/esm/hooks.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,16 @@ class Hooks {
272272

273273
// Avoid expensive URL instantiation for known-good URLs
274274
if (!this.#validatedUrls.has(url)) {
275-
try {
276-
new URL(url);
277-
this.#validatedUrls.add(url);
278-
} catch {
275+
if (!URL.canParse(url)) {
279276
throw new ERR_INVALID_RETURN_PROPERTY_VALUE(
280277
'a URL string',
281278
hookErrIdentifier,
282279
'url',
283280
url,
284281
);
285282
}
283+
284+
this.#validatedUrls.add(url);
286285
}
287286

288287
if (
@@ -352,16 +351,15 @@ class Hooks {
352351

353352
// Avoid expensive URL instantiation for known-good URLs
354353
if (!this.#validatedUrls.has(nextUrl)) {
355-
try {
356-
new URL(nextUrl);
357-
this.#validatedUrls.add(nextUrl);
358-
} catch {
354+
if (!URL.canParse(nextUrl)) {
359355
throw new ERR_INVALID_ARG_VALUE(
360356
`${hookErrIdentifier} url`,
361357
nextUrl,
362358
'should be a URL string',
363359
);
364360
}
361+
362+
this.#validatedUrls.add(nextUrl);
365363
}
366364

367365
if (ctx) { validateObject(ctx, `${hookErrIdentifier} context`); }

lib/internal/modules/esm/resolve.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,7 @@ function resolvePackageTargetString(
333333
if (!StringPrototypeStartsWith(target, './')) {
334334
if (internal && !StringPrototypeStartsWith(target, '../') &&
335335
!StringPrototypeStartsWith(target, '/')) {
336-
let isURL = false;
337-
try {
338-
new URL(target);
339-
isURL = true;
340-
} catch {
341-
// Continue regardless of error.
342-
}
343-
if (!isURL) {
336+
if (!URL.canParse(target)) {
344337
const exportTarget = pattern ?
345338
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
346339
target + subpath;

0 commit comments

Comments
 (0)