Skip to content

Commit 5d03fce

Browse files
anonrigjoyeecheung
authored andcommitted
lib: reduce amount of caught URL errors
PR-URL: #52658 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daniel Lemire <[email protected]>
1 parent 49aa68e commit 5d03fce

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

lib/internal/modules/esm/hooks.js

+2-6
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) {

lib/internal/modules/esm/loader.js

+4-4
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');

lib/internal/url.js

+1
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,

test/parallel/test-source-map-cjs-require-cache.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*/
77

88
'use strict';
9-
const common = require('../common');
9+
require('../common');
10+
const { gcUntil } = require('../common/gc');
1011
const assert = require('node:assert');
1112
const { findSourceMap } = require('node:module');
1213

@@ -28,7 +29,7 @@ function run(moduleId) {
2829
run(moduleId);
2930

3031
// Run until the source map is cleared by GC, or fail the test after determined iterations.
31-
common.gcUntil('SourceMap of deleted CJS module is cleared', () => {
32+
gcUntil('SourceMap of deleted CJS module is cleared', () => {
3233
// Repetitively load a second module with --max-old-space-size=10 to make GC more aggressive.
3334
run(moduleIdRepeat);
3435
// Verify that the source map is cleared.

0 commit comments

Comments
 (0)