Skip to content

Conversation

@alxhotel
Copy link
Member

@alxhotel alxhotel commented May 20, 2021

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[X] Bug fix
[ ] New feature
[ ] Other, please explain:

What changes did you make? (Give an overview)

  • As noted in the comments of the code, there is still a bug in Chrome and Firefox which doesn't correctly parse UDP urls.

  • The previous code didn't handle the UDP case in the first parsing of the URL:

parsedUrl = new URL(announceUrl)

  • This PR creates a new function called parseUrl in the common file to handle the special case for UDP urls, so that the code can be reused wherever is necessary.

Which issue (if any) does this pull request address?
Fixes webtorrent/webtorrent#2066

Is there anything you'd like reviewers to focus on?
Let me know if the implementation of parseUrl can be improved

@alxhotel alxhotel requested a review from DiegoRBaquero May 20, 2021 19:48
@jimmywarting
Copy link
Contributor

How about something like this instead:

str = 'udp://github.com'
url = new URL(str.replace(/^udp:/, 'http:'))

if (str.match(/^udp:/)) {
  Object.defineProperties(url, {
    href: { value: url.href.replace(/^http/, 'udp') },
    protocol: { value: url.protocol.replace(/^http/, 'udp') },
    origin: { value: url.origin.replace(/^http/, 'udp') }
  })
}

You will keep it as a URL instances and get support for the rest of the class (serialize to string, instance check, searchParams)

@alxhotel
Copy link
Member Author

Neat, thanks @jimmywarting! I have updated the PR.

@alxhotel alxhotel requested a review from DiegoRBaquero May 21, 2021 10:17
Copy link
Member

@feross feross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

exports.parseUrl = function (str) {
const url = new URL(str.replace(/^udp:/, 'http:'))

if (str.match(/^udp:/)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since it's just being used to get a boolean, I prefer to use /^udp:/.test(str).

@feross feross merged commit 76f6d41 into master May 21, 2021
@feross feross deleted the fix_udp_url branch May 21, 2021 15:15
@DiegoRBaquero
Copy link
Member

🎉 This PR is included in version 9.17.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError: Failed to construct 'URL': Invalid URL

5 participants