Skip to content

Commit a66a1c1

Browse files
committed
lib: reduce amount of caught URL errors
1 parent 9790ddf commit a66a1c1

File tree

5 files changed

+31
-40
lines changed

5 files changed

+31
-40
lines changed

lib/internal/modules/esm/hooks.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = URL.parse(responseURL);
411407
}
412408

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

lib/internal/modules/esm/resolve.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -907,16 +907,15 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
907907
} else if (protocol === 'file:' && specifier[0] === '#') {
908908
resolved = packageImportsResolve(specifier, base, conditions);
909909
} else {
910-
try {
911-
resolved = new URL(specifier);
912-
} catch (cause) {
913-
if (isRemote && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
914-
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
915-
setOwnProperty(error, 'cause', cause);
916-
throw error;
917-
}
918-
resolved = packageResolve(specifier, base, conditions);
910+
resolved = URL.parse(specifier);
911+
912+
if (resolved == null && isRemote && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
913+
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
914+
setOwnProperty(error, 'cause', cause);
915+
throw error;
919916
}
917+
918+
resolved ??= packageResolve(specifier, base, conditions);
920919
}
921920
if (resolved.protocol !== 'file:') {
922921
return resolved;
@@ -1082,11 +1081,7 @@ function defaultResolve(specifier, context = {}) {
10821081

10831082
let parsedParentURL;
10841083
if (parentURL) {
1085-
try {
1086-
parsedParentURL = new URL(parentURL);
1087-
} catch {
1088-
// Ignore exception
1089-
}
1084+
parsedParentURL = URL.parse(parentURL);
10901085
}
10911086

10921087
let parsed, protocol;

lib/internal/modules/esm/translators.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,17 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
350350
}
351351
} : loadCJSModule;
352352

353-
try {
354-
// We still need to read the FS to detect the exports.
355-
source ??= readFileSync(new URL(url), 'utf8');
356-
} catch {
357-
// Continue regardless of error.
353+
const urlPath = URL.parse(url);
354+
355+
if (urlPath != null) {
356+
try {
357+
// We still need to read the FS to detect the exports.
358+
source ??= readFileSync(urlPath, 'utf8');
359+
} catch {
360+
// Continue regardless of error.
361+
}
358362
}
363+
359364
return createCJSModuleWrap(url, source, isMain, cjsLoader);
360365

361366
});

lib/internal/source_map/source_map_cache.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = URL.parse(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: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -958,15 +958,11 @@ class URL {
958958
if (protocol === 'blob:') {
959959
const path = this.pathname;
960960
if (path.length > 0) {
961-
try {
962-
const out = new URL(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}`;
967-
}
968-
} catch {
969-
// Do nothing.
961+
const out = URL.parse(path);
962+
// Only return origin of scheme is `http` or `https`
963+
// Otherwise return a new opaque origin (null).
964+
if (out && (out.#context.scheme_type === 0 || out.#context.scheme_type === 2)) {
965+
return `${out.protocol}//${out.host}`;
970966
}
971967
}
972968
}

0 commit comments

Comments
 (0)