Skip to content

Commit a69d20c

Browse files
committed
lib: reduce amount of caught URL errors
1 parent 9807ede commit a69d20c

File tree

6 files changed

+33
-49
lines changed

6 files changed

+33
-49
lines changed

lib/internal/modules/esm/hooks.js

Lines changed: 2 additions & 6 deletions
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 { URL, 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

Lines changed: 4 additions & 4 deletions
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, URL, 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/modules/esm/resolve.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
3333
const experimentalNetworkImports =
3434
getOptionValue('--experimental-network-imports');
3535
const inputTypeFlag = getOptionValue('--input-type');
36-
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
37-
const { getCWDURL, setOwnProperty } = require('internal/util');
36+
const { URL, pathToFileURL, fileURLToPath, isURL, URLParse } = require('internal/url');
37+
const { getCWDURL } = require('internal/util');
3838
const { canParse: URLCanParse } = internalBinding('url');
3939
const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs');
4040
const {
@@ -893,26 +893,21 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
893893
// Ok since relative URLs cannot parse as URLs.
894894
let resolved;
895895
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
896-
try {
897-
resolved = new URL(specifier, base);
898-
} catch (cause) {
899-
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
900-
setOwnProperty(error, 'cause', cause);
901-
throw error;
896+
resolved = URL.parse(specifier, base);
897+
898+
if (resolved == null) {
899+
throw new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
902900
}
903901
} else if (protocol === 'file:' && specifier[0] === '#') {
904902
resolved = packageImportsResolve(specifier, base, conditions);
905903
} else {
906-
try {
907-
resolved = new URL(specifier);
908-
} catch (cause) {
909-
if (isRemote && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
910-
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
911-
setOwnProperty(error, 'cause', cause);
912-
throw error;
913-
}
914-
resolved = packageResolve(specifier, base, conditions);
904+
resolved = URL.parse(specifier);
905+
906+
if (resolved == null && isRemote && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
907+
throw new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
915908
}
909+
910+
resolved ??= packageResolve(specifier, base, conditions);
916911
}
917912
if (resolved.protocol !== 'file:') {
918913
return resolved;
@@ -1053,11 +1048,7 @@ function defaultResolve(specifier, context = {}) {
10531048

10541049
let parsedParentURL;
10551050
if (parentURL) {
1056-
try {
1057-
parsedParentURL = new URL(parentURL);
1058-
} catch {
1059-
// Ignore exception
1060-
}
1051+
parsedParentURL = URL.parse(parentURL);
10611052
}
10621053

10631054
let parsed, protocol;

lib/internal/modules/esm/translators.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
324324
} catch {
325325
// Continue regardless of error.
326326
}
327+
327328
return createCJSModuleWrap(url, source, isMain, cjsLoader);
328329

329330
});

lib/internal/source_map/source_map_cache.js

Lines changed: 7 additions & 8 deletions
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

Lines changed: 6 additions & 9 deletions
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)