Skip to content

Commit 38be5ca

Browse files
committed
lib: reduce amount of caught URL errors
1 parent 075853e commit 38be5ca

File tree

4 files changed

+19
-27
lines changed

4 files changed

+19
-27
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, isMainThread } = 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 { kImplicitAssertType } = require('internal/modules/esm/assert');
39-
const { canParse } = internalBinding('url');
4039
const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding('module_wrap');
4140
const {
4241
urlToFilename,
@@ -323,8 +322,9 @@ class ModuleLoader {
323322
getModuleJobForRequire(specifier, parentURL, importAttributes) {
324323
assert(getOptionValue('--experimental-require-module'));
325324

326-
if (canParse(specifier)) {
327-
const protocol = new URL(specifier).protocol;
325+
const parsed = URLParse(specifier);
326+
if (parsed != null) {
327+
const protocol = parsed.protocol;
328328
if (protocol === 'https:' || protocol === 'http:') {
329329
throw new ERR_NETWORK_IMPORT_DISALLOWED(specifier, parentURL,
330330
'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
@@ -42,7 +42,7 @@ const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappi
4242
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g;
4343

4444
const { isAbsolute } = require('path');
45-
const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
45+
const { fileURLToPath, pathToFileURL, URL, URLParse } = require('internal/url');
4646

4747
let SourceMap;
4848

@@ -187,21 +187,20 @@ function maybeCacheGeneratedSourceMap(content) {
187187
}
188188

189189
function dataFromUrl(sourceURL, sourceMappingURL) {
190-
try {
191-
const url = new URL(sourceMappingURL);
190+
const url = URLParse(sourceMappingURL);
191+
192+
if (url != null) {
192193
switch (url.protocol) {
193194
case 'data:':
194195
return sourceMapFromDataUrl(sourceURL, url.pathname);
195196
default:
196197
debug(`unknown protocol ${url.protocol}`);
197198
return null;
198199
}
199-
} catch (err) {
200-
debug(err);
201-
// If no scheme is present, we assume we are dealing with a file path.
202-
const mapURL = new URL(sourceMappingURL, sourceURL).href;
203-
return sourceMapFromFile(mapURL);
204200
}
201+
202+
const mapURL = new URL(sourceMappingURL, sourceURL).href;
203+
return sourceMapFromFile(mapURL);
205204
}
206205

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

lib/internal/url.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -959,15 +959,11 @@ class URL {
959959
if (protocol === 'blob:') {
960960
const path = this.pathname;
961961
if (path.length > 0) {
962-
try {
963-
const out = new URL(path);
964-
// Only return origin of scheme is `http` or `https`
965-
// Otherwise return a new opaque origin (null).
966-
if (out.#context.scheme_type === 0 || out.#context.scheme_type === 2) {
967-
return `${out.protocol}//${out.host}`;
968-
}
969-
} catch {
970-
// Do nothing.
962+
const out = URL.parse(path);
963+
// Only return origin of scheme is `http` or `https`
964+
// Otherwise return a new opaque origin (null).
965+
if (out?.#context.scheme_type === 0 || out?.#context.scheme_type === 2) {
966+
return `${out.protocol}//${out.host}`;
971967
}
972968
}
973969
}
@@ -1601,6 +1597,7 @@ module.exports = {
16011597
installObjectURLMethods,
16021598
URL,
16031599
URLSearchParams,
1600+
URLParse: URL.parse,
16041601
domainToASCII,
16051602
domainToUnicode,
16061603
urlToHttpOptions,

0 commit comments

Comments
 (0)