Skip to content

Commit 710cf77

Browse files
anonrigtargos
authored andcommitted
lib: reduce amount of caught URL errors
PR-URL: #52658 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me>
1 parent 7d87877 commit 710cf77

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

lib/internal/modules/esm/hooks.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const {
3333
ERR_WORKER_UNSERIALIZABLE_ERROR,
3434
} = require('internal/errors').codes;
3535
const { exitCodes: { kUnsettledTopLevelAwait } } = internalBinding('errors');
36-
const { URL } = require('internal/url');
36+
const { URLParse } = require('internal/url');
3737
const { canParse: URLCanParse } = internalBinding('url');
3838
const { receiveMessageOnPort } = require('worker_threads');
3939
const {
@@ -403,11 +403,7 @@ class Hooks {
403403

404404
let responseURLObj;
405405
if (typeof responseURL === 'string') {
406-
try {
407-
responseURLObj = new URL(responseURL);
408-
} catch {
409-
// responseURLObj not defined will throw in next branch.
410-
}
406+
responseURLObj = URLParse(responseURL);
411407
}
412408

413409
if (responseURLObj?.href !== responseURL) {

lib/internal/modules/esm/loader.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ const {
2929
ERR_UNKNOWN_MODULE_FORMAT,
3030
} = require('internal/errors').codes;
3131
const { getOptionValue } = require('internal/options');
32-
const { isURL, pathToFileURL, URL } = require('internal/url');
32+
const { isURL, pathToFileURL, URLParse } = require('internal/url');
3333
const { emitExperimentalWarning, kEmptyObject } = require('internal/util');
3434
const {
3535
compileSourceTextModule,
3636
getDefaultConditions,
3737
} = require('internal/modules/esm/utils');
3838
const { kImplicitTypeAttribute } = require('internal/modules/esm/assert');
39-
const { canParse } = internalBinding('url');
4039
const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding('module_wrap');
4140
const {
4241
urlToFilename,
@@ -322,8 +321,9 @@ class ModuleLoader {
322321
getModuleJobForRequire(specifier, parentURL, importAttributes) {
323322
assert(getOptionValue('--experimental-require-module'));
324323

325-
if (canParse(specifier)) {
326-
const protocol = new URL(specifier).protocol;
324+
const parsed = URLParse(specifier);
325+
if (parsed != null) {
326+
const protocol = parsed.protocol;
327327
if (protocol === 'https:' || protocol === 'http:') {
328328
throw new ERR_NETWORK_IMPORT_DISALLOWED(specifier, parentURL,
329329
'ES modules cannot be loaded by require() from the network');

lib/internal/source_map/source_map_cache.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappi
3939
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g;
4040

4141
const { isAbsolute } = require('path');
42-
const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
42+
const { fileURLToPath, pathToFileURL, URL, URLParse } = require('internal/url');
4343

4444
let SourceMap;
4545

@@ -209,21 +209,20 @@ function maybeCacheGeneratedSourceMap(content) {
209209
* @returns {object} deserialized source map JSON object
210210
*/
211211
function dataFromUrl(sourceURL, sourceMappingURL) {
212-
try {
213-
const url = new URL(sourceMappingURL);
212+
const url = URLParse(sourceMappingURL);
213+
214+
if (url != null) {
214215
switch (url.protocol) {
215216
case 'data:':
216217
return sourceMapFromDataUrl(sourceURL, url.pathname);
217218
default:
218219
debug(`unknown protocol ${url.protocol}`);
219220
return null;
220221
}
221-
} catch (err) {
222-
debug(err);
223-
// If no scheme is present, we assume we are dealing with a file path.
224-
const mapURL = new URL(sourceMappingURL, sourceURL).href;
225-
return sourceMapFromFile(mapURL);
226222
}
223+
224+
const mapURL = new URL(sourceMappingURL, sourceURL).href;
225+
return sourceMapFromFile(mapURL);
227226
}
228227

229228
// Cache the length of each line in the file that a source map was extracted

lib/internal/url.js

+1
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,7 @@ module.exports = {
16011601
installObjectURLMethods,
16021602
URL,
16031603
URLSearchParams,
1604+
URLParse: URL.parse,
16041605
domainToASCII,
16051606
domainToUnicode,
16061607
urlToHttpOptions,

0 commit comments

Comments
 (0)