Skip to content

Commit

Permalink
lib: reduce amount of caught URL errors
Browse files Browse the repository at this point in the history
PR-URL: #52658
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Daniel Lemire <[email protected]>
  • Loading branch information
anonrig authored and joyeecheung committed Jan 23, 2025
1 parent ee32d0b commit a51b482
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
8 changes: 2 additions & 6 deletions lib/internal/modules/esm/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const {
ERR_WORKER_UNSERIALIZABLE_ERROR,
} = require('internal/errors').codes;
const { exitCodes: { kUnfinishedTopLevelAwait } } = internalBinding('errors');
const { URL } = require('internal/url');
const { URLParse } = require('internal/url');
const { canParse: URLCanParse } = internalBinding('url');
const { receiveMessageOnPort } = require('worker_threads');
const {
Expand Down Expand Up @@ -471,11 +471,7 @@ class Hooks {

let responseURLObj;
if (typeof responseURL === 'string') {
try {
responseURLObj = new URL(responseURL);
} catch {
// responseURLObj not defined will throw in next branch.
}
responseURLObj = URLParse(responseURL);
}

if (responseURLObj?.href !== responseURL) {
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ const {
ERR_UNKNOWN_MODULE_FORMAT,
} = require('internal/errors').codes;
const { getOptionValue } = require('internal/options');
const { isURL, pathToFileURL, URL } = require('internal/url');
const { isURL, pathToFileURL, URLParse } = require('internal/url');
const { emitExperimentalWarning, kEmptyObject } = require('internal/util');
const {
compileSourceTextModule,
getDefaultConditions,
} = require('internal/modules/esm/utils');
const { kImplicitAssertType } = require('internal/modules/esm/assert');
const { canParse } = internalBinding('url');
const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding('module_wrap');
const {
urlToFilename,
Expand Down Expand Up @@ -321,8 +320,9 @@ class ModuleLoader {
getModuleJobForRequire(specifier, parentURL, importAttributes) {
assert(getOptionValue('--experimental-require-module'));

if (canParse(specifier)) {
const protocol = new URL(specifier).protocol;
const parsed = URLParse(specifier);
if (parsed != null) {
const protocol = parsed.protocol;
if (protocol === 'https:' || protocol === 'http:') {
throw new ERR_NETWORK_IMPORT_DISALLOWED(specifier, parentURL,
'ES modules cannot be loaded by require() from the network');
Expand Down
1 change: 1 addition & 0 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,7 @@ module.exports = {
installObjectURLMethods,
URL,
URLSearchParams,
URLParse: URL.parse,
domainToASCII,
domainToUnicode,
urlToHttpOptions,
Expand Down
5 changes: 3 additions & 2 deletions test/parallel/test-source-map-cjs-require-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

'use strict';
const common = require('../common');
require('../common');
const { gcUntil } = require('../common/gc');
const assert = require('node:assert');
const { findSourceMap } = require('node:module');

Expand All @@ -28,7 +29,7 @@ function run(moduleId) {
run(moduleId);

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

0 comments on commit a51b482

Please sign in to comment.