From f85ee6cb22732afc075274ed90b40b8656527bba Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 16 May 2024 21:04:24 -0400 Subject: [PATCH] lib: reduce amount of caught URL errors --- lib/internal/modules/esm/hooks.js | 8 ++------ lib/internal/modules/esm/loader.js | 8 ++++---- lib/internal/source_map/source_map_cache.js | 15 +++++++-------- lib/internal/url.js | 15 ++++++--------- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/lib/internal/modules/esm/hooks.js b/lib/internal/modules/esm/hooks.js index f5833ad61cdb754..b67aad5b824bfb1 100644 --- a/lib/internal/modules/esm/hooks.js +++ b/lib/internal/modules/esm/hooks.js @@ -33,7 +33,7 @@ const { ERR_WORKER_UNSERIALIZABLE_ERROR, } = require('internal/errors').codes; const { exitCodes: { kUnsettledTopLevelAwait } } = internalBinding('errors'); -const { URL } = require('internal/url'); +const { URLParse } = require('internal/url'); const { canParse: URLCanParse } = internalBinding('url'); const { receiveMessageOnPort, isMainThread } = require('worker_threads'); const { @@ -403,11 +403,7 @@ class Hooks { let responseURLObj; if (typeof responseURL === 'string') { - try { - responseURLObj = new URL(responseURL); - } catch { - // responseURLObj not defined will throw in next branch. - } + responseURLObj = URLParse(responseURL); } if (responseURLObj?.href !== responseURL) { diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 5eb7cd17d02fadb..4b334dd3cfc12b8 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -29,14 +29,13 @@ const { ERR_UNKNOWN_MODULE_FORMAT, } = require('internal/errors').codes; const { getOptionValue } = require('internal/options'); -const { isURL, pathToFileURL, URL } = require('internal/url'); +const { isURL, pathToFileURL, URLParse } = require('internal/url'); const { emitExperimentalWarning, kEmptyObject } = require('internal/util'); const { compileSourceTextModule, getDefaultConditions, } = require('internal/modules/esm/utils'); const { kImplicitAssertType } = require('internal/modules/esm/assert'); -const { canParse } = internalBinding('url'); const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding('module_wrap'); const { urlToFilename, @@ -323,8 +322,9 @@ class ModuleLoader { getModuleJobForRequire(specifier, parentURL, importAttributes) { assert(getOptionValue('--experimental-require-module')); - if (canParse(specifier)) { - const protocol = new URL(specifier).protocol; + const parsed = URLParse(specifier); + if (parsed != null) { + const protocol = parsed.protocol; if (protocol === 'https:' || protocol === 'http:') { throw new ERR_NETWORK_IMPORT_DISALLOWED(specifier, parentURL, 'ES modules cannot be loaded by require() from the network'); diff --git a/lib/internal/source_map/source_map_cache.js b/lib/internal/source_map/source_map_cache.js index 53c3374fc09176e..960245dd2d5b51f 100644 --- a/lib/internal/source_map/source_map_cache.js +++ b/lib/internal/source_map/source_map_cache.js @@ -42,7 +42,7 @@ const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?[^\s]+)/g; const { isAbsolute } = require('path'); -const { fileURLToPath, pathToFileURL, URL } = require('internal/url'); +const { fileURLToPath, pathToFileURL, URL, URLParse } = require('internal/url'); let SourceMap; @@ -187,8 +187,9 @@ function maybeCacheGeneratedSourceMap(content) { } function dataFromUrl(sourceURL, sourceMappingURL) { - try { - const url = new URL(sourceMappingURL); + const url = URLParse(sourceMappingURL); + + if (url != null) { switch (url.protocol) { case 'data:': return sourceMapFromDataUrl(sourceURL, url.pathname); @@ -196,12 +197,10 @@ function dataFromUrl(sourceURL, sourceMappingURL) { debug(`unknown protocol ${url.protocol}`); return null; } - } catch (err) { - debug(err); - // If no scheme is present, we assume we are dealing with a file path. - const mapURL = new URL(sourceMappingURL, sourceURL).href; - return sourceMapFromFile(mapURL); } + + const mapURL = new URL(sourceMappingURL, sourceURL).href; + return sourceMapFromFile(mapURL); } // Cache the length of each line in the file that a source map was extracted diff --git a/lib/internal/url.js b/lib/internal/url.js index 9c18aab07ff19d1..52762a643c75184 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -959,15 +959,11 @@ class URL { if (protocol === 'blob:') { const path = this.pathname; if (path.length > 0) { - try { - const out = new URL(path); - // Only return origin of scheme is `http` or `https` - // Otherwise return a new opaque origin (null). - if (out.#context.scheme_type === 0 || out.#context.scheme_type === 2) { - return `${out.protocol}//${out.host}`; - } - } catch { - // Do nothing. + const out = URL.parse(path); + // Only return origin of scheme is `http` or `https` + // Otherwise return a new opaque origin (null). + if (out?.#context.scheme_type === 0 || out?.#context.scheme_type === 2) { + return `${out.protocol}//${out.host}`; } } } @@ -1601,6 +1597,7 @@ module.exports = { installObjectURLMethods, URL, URLSearchParams, + URLParse: URL.parse, domainToASCII, domainToUnicode, urlToHttpOptions,