From 29dfb97ec06ce5e76314829977a2fc71f41c10c1 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 13 Jun 2024 14:32:59 -0400 Subject: [PATCH] lib: reduce amount of caught URL errors PR-URL: https://github.com/nodejs/node/pull/52658 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Daniel Lemire --- lib/internal/modules/esm/hooks.js | 8 ++------ lib/internal/modules/esm/loader.js | 8 ++++---- lib/internal/url.js | 1 + test/parallel/test-source-map-cjs-require-cache.js | 5 +++-- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/internal/modules/esm/hooks.js b/lib/internal/modules/esm/hooks.js index 88c66f89a83c66..c13d7c8ad3321c 100644 --- a/lib/internal/modules/esm/hooks.js +++ b/lib/internal/modules/esm/hooks.js @@ -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 { @@ -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) { diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 0878e257ade491..f94696fa804759 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -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, @@ -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'); diff --git a/lib/internal/url.js b/lib/internal/url.js index 42debfc20005b0..91976bceb0db19 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1624,6 +1624,7 @@ module.exports = { installObjectURLMethods, URL, URLSearchParams, + URLParse: URL.parse, domainToASCII, domainToUnicode, urlToHttpOptions, diff --git a/test/parallel/test-source-map-cjs-require-cache.js b/test/parallel/test-source-map-cjs-require-cache.js index 42ec0b229526fa..155b7a66efab1b 100644 --- a/test/parallel/test-source-map-cjs-require-cache.js +++ b/test/parallel/test-source-map-cjs-require-cache.js @@ -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'); @@ -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.