Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 533afe8

Browse filesBrowse files
anonrigmarco-ippolito
authored andcommitted
lib: reduce amount of caught URL errors
PR-URL: #52658 Backport-PR-URL: #56927 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniel Lemire <daniel@lemire.me> Refs: #52697
1 parent 34221a1 commit 533afe8
Copy full SHA for 533afe8

File tree

Expand file treeCollapse file tree

4 files changed

+14
-18
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+14
-18
lines changed
Open diff view settings
Collapse file

‎lib/internal/modules/esm/hooks.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/hooks.js
+2-6Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const {
3838
ERR_WORKER_UNSERIALIZABLE_ERROR,
3939
} = require('internal/errors').codes;
4040
const { exitCodes: { kUnfinishedTopLevelAwait } } = internalBinding('errors');
41-
const { URL } = require('internal/url');
41+
const { URLParse } = require('internal/url');
4242
const { canParse: URLCanParse } = internalBinding('url');
4343
const { receiveMessageOnPort } = require('worker_threads');
4444
const {
@@ -471,11 +471,7 @@ class Hooks {
471471

472472
let responseURLObj;
473473
if (typeof responseURL === 'string') {
474-
try {
475-
responseURLObj = new URL(responseURL);
476-
} catch {
477-
// responseURLObj not defined will throw in next branch.
478-
}
474+
responseURLObj = URLParse(responseURL);
479475
}
480476

481477
if (responseURLObj?.href !== responseURL) {
Collapse file

‎lib/internal/modules/esm/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ const {
2828
ERR_UNKNOWN_MODULE_FORMAT,
2929
} = require('internal/errors').codes;
3030
const { getOptionValue } = require('internal/options');
31-
const { isURL, pathToFileURL, URL } = require('internal/url');
31+
const { isURL, pathToFileURL, URLParse } = require('internal/url');
3232
const { emitExperimentalWarning, kEmptyObject } = require('internal/util');
3333
const {
3434
compileSourceTextModule,
3535
getDefaultConditions,
3636
} = require('internal/modules/esm/utils');
3737
const { kImplicitAssertType } = require('internal/modules/esm/assert');
38-
const { canParse } = internalBinding('url');
3938
const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding('module_wrap');
4039
const {
4140
urlToFilename,
@@ -321,8 +320,9 @@ class ModuleLoader {
321320
getModuleJobForRequire(specifier, parentURL, importAttributes) {
322321
assert(getOptionValue('--experimental-require-module'));
323322

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

‎lib/internal/source_map/source_map_cache.js‎

Copy file name to clipboardExpand all lines: lib/internal/source_map/source_map_cache.js
+7-8Lines changed: 7 additions & 8 deletions
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
Collapse file

‎lib/internal/url.js‎

Copy file name to clipboardExpand all lines: lib/internal/url.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,7 @@ module.exports = {
16241624
installObjectURLMethods,
16251625
URL,
16261626
URLSearchParams,
1627+
URLParse: URL.parse,
16271628
domainToASCII,
16281629
domainToUnicode,
16291630
urlToHttpOptions,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.