@@ -34,6 +34,7 @@ const {
3434 ERR_WORKER_UNSERIALIZABLE_ERROR ,
3535} = require ( 'internal/errors' ) . codes ;
3636const { URL } = require ( 'internal/url' ) ;
37+ const { canParse : urlCanParse } = internalBinding ( 'url' ) ;
3738const { receiveMessageOnPort } = require ( 'worker_threads' ) ;
3839const {
3940 isAnyArrayBuffer,
@@ -270,17 +271,17 @@ class Hooks {
270271
271272 // Avoid expensive URL instantiation for known-good URLs
272273 if ( ! this . #validatedUrls. has ( url ) ) {
273- try {
274- new URL ( url ) ;
275- this . #validatedUrls. add ( url ) ;
276- } catch {
274+ // No need to convert to string, since the type is already validated
275+ if ( ! urlCanParse ( url ) ) {
277276 throw new ERR_INVALID_RETURN_PROPERTY_VALUE (
278277 'a URL string' ,
279278 hookErrIdentifier ,
280279 'url' ,
281280 url ,
282281 ) ;
283282 }
283+
284+ this . #validatedUrls. add ( url ) ;
284285 }
285286
286287 if (
@@ -349,16 +350,16 @@ class Hooks {
349350
350351 // Avoid expensive URL instantiation for known-good URLs
351352 if ( ! this . #validatedUrls. has ( nextUrl ) ) {
352- try {
353- new URL ( nextUrl ) ;
354- this . #validatedUrls. add ( nextUrl ) ;
355- } catch {
353+ // No need to convert to string, since the type is already validated
354+ if ( ! urlCanParse ( nextUrl ) ) {
356355 throw new ERR_INVALID_ARG_VALUE (
357356 `${ hookErrIdentifier } url` ,
358357 nextUrl ,
359358 'should be a URL string' ,
360359 ) ;
361360 }
361+
362+ this . #validatedUrls. add ( nextUrl ) ;
362363 }
363364
364365 if ( ctx ) { validateObject ( ctx , `${ hookErrIdentifier } context` ) ; }
0 commit comments