Skip to content

Commit

Permalink
lib: reduce amount of caught URL errors
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Apr 24, 2024
1 parent 9790ddf commit 9cfdd53
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 47 deletions.
6 changes: 1 addition & 5 deletions lib/internal/modules/esm/hooks.js
Expand Up @@ -403,11 +403,7 @@ class Hooks {

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

if (responseURLObj?.href !== responseURL) {
Expand Down
33 changes: 12 additions & 21 deletions lib/internal/modules/esm/resolve.js
Expand Up @@ -37,7 +37,7 @@ const experimentalNetworkImports =
getOptionValue('--experimental-network-imports');
const inputTypeFlag = getOptionValue('--input-type');
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
const { getCWDURL, setOwnProperty } = require('internal/util');
const { getCWDURL } = require('internal/util');
const { canParse: URLCanParse } = internalBinding('url');
const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs');
const {
Expand Down Expand Up @@ -897,26 +897,21 @@ function moduleResolve(specifier, base, conditions, preserveSymlinks) {
// Ok since relative URLs cannot parse as URLs.
let resolved;
if (shouldBeTreatedAsRelativeOrAbsolutePath(specifier)) {
try {
resolved = new URL(specifier, base);
} catch (cause) {
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
setOwnProperty(error, 'cause', cause);
throw error;
resolved = URL.parse(specifier, base);

if (resolved == null) {
throw new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
}
} else if (protocol === 'file:' && specifier[0] === '#') {
resolved = packageImportsResolve(specifier, base, conditions);
} else {
try {
resolved = new URL(specifier);
} catch (cause) {
if (isRemote && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
const error = new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
setOwnProperty(error, 'cause', cause);
throw error;
}
resolved = packageResolve(specifier, base, conditions);
resolved = URL.parse(specifier);

if (resolved == null && isRemote && !BuiltinModule.canBeRequiredWithoutScheme(specifier)) {
throw new ERR_UNSUPPORTED_RESOLVE_REQUEST(specifier, base);
}

resolved ??= packageResolve(specifier, base, conditions);
}
if (resolved.protocol !== 'file:') {
return resolved;
Expand Down Expand Up @@ -1082,11 +1077,7 @@ function defaultResolve(specifier, context = {}) {

let parsedParentURL;
if (parentURL) {
try {
parsedParentURL = new URL(parentURL);
} catch {
// Ignore exception
}
parsedParentURL = URL.parse(parentURL);
}

let parsed, protocol;
Expand Down
15 changes: 10 additions & 5 deletions lib/internal/modules/esm/translators.js
Expand Up @@ -350,12 +350,17 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
}
} : loadCJSModule;

try {
// We still need to read the FS to detect the exports.
source ??= readFileSync(new URL(url), 'utf8');
} catch {
// Continue regardless of error.
const urlPath = URL.parse(url);

if (urlPath != null) {
try {
// We still need to read the FS to detect the exports.
source ??= readFileSync(urlPath, 'utf8');
} catch {
// Continue regardless of error.
}
}

return createCJSModuleWrap(url, source, isMain, cjsLoader);

});
Expand Down
13 changes: 6 additions & 7 deletions lib/internal/source_map/source_map_cache.js
Expand Up @@ -187,21 +187,20 @@ function maybeCacheGeneratedSourceMap(content) {
}

function dataFromUrl(sourceURL, sourceMappingURL) {
try {
const url = new URL(sourceMappingURL);
const url = URL.parse(sourceMappingURL);

if (url != null) {
switch (url.protocol) {
case 'data:':
return sourceMapFromDataUrl(sourceURL, url.pathname);
default:
debug(`unknown protocol ${url.protocol}`);
return null;
}
} catch (err) {
debug(err);
// If no scheme is present, we assume we are dealing with a file path.
const mapURL = new URL(sourceMappingURL, sourceURL).href;
return sourceMapFromFile(mapURL);
}

const mapURL = new URL(sourceMappingURL, sourceURL).href;
return sourceMapFromFile(mapURL);
}

// Cache the length of each line in the file that a source map was extracted
Expand Down
14 changes: 5 additions & 9 deletions lib/internal/url.js
Expand Up @@ -958,15 +958,11 @@ class URL {
if (protocol === 'blob:') {
const path = this.pathname;
if (path.length > 0) {
try {
const out = new URL(path);
// Only return origin of scheme is `http` or `https`
// Otherwise return a new opaque origin (null).
if (out.#context.scheme_type === 0 || out.#context.scheme_type === 2) {
return `${out.protocol}//${out.host}`;
}
} catch {
// Do nothing.
const out = URL.parse(path);
// Only return origin of scheme is `http` or `https`
// Otherwise return a new opaque origin (null).
if (out && (out.#context.scheme_type === 0 || out.#context.scheme_type === 2)) {
return `${out.protocol}//${out.host}`;
}
}
}
Expand Down

0 comments on commit 9cfdd53

Please sign in to comment.