-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace URL.canParse()
with better supported code
#80
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,13 @@ function decodeHtmlCharacters(str: string) { | |
} | ||
|
||
function isValidUrl(url: string): boolean { | ||
return URL.canParse(url); | ||
try { | ||
// We can replace this with URL.canParse() once it's more widely available | ||
new URL(url); // eslint-disable-line no-new -- We're only checking for exceptions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return URL.canParse?.(url) ?? (new URL(url) ? true : false) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won't work, since But, we could do something like: if (URL.canParse) {
return URL.canParse(url);
}
try {
// We can replace this with URL.canParse() once it's more widely available
new URL(url); // eslint-disable-line no-new -- We're only checking for exceptions
return true;
} catch (error) {
return false;
} However, I'm not a fan of this. Sure, it's slightly faster in environments that do support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be clear, I didn't suggest to remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's a useless suggestion though, when the documentation says:
If that holds true in perpetuity, then don't worry about my suggestion |
||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
function decodeURI(uri: string): string { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you pin it ? I have read your explanation, but I don't understand more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because
URL.canParse
doesn't exist in Node.JS v18.0, since it was only added in Node.JS v18.17.So this test will make sure that we don't accidentally add anything that breaks with old versions of Node.JS v18. (if we do, we'd ideally want to make a new MAJOR change and state that the minimum supported of Node.JS has changed in the release notes).
For example, Amazon Linux 2023 only comes with Node.JS v18.12.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's a clever move. Thanks for explanation.