◐ Shell
clean mode source ↗

lib: reduce amount of caught URL errors · nodejs/node@533afe8

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) {

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

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

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,