Refactor parseUrl

This commit is contained in:
Alex 2021-05-21 12:15:34 +02:00
parent 8e24a8c97b
commit ce7dd6e175

View File

@ -32,22 +32,17 @@ exports.hexToBinary = function (str) {
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
exports.parseUrl = function (str) {
const isUDP = str.match(/^udp:/)
const parsedUrl = (isUDP) ? new URL(str.replace(/^udp:/, 'http:')) : new URL(str)
const url = new URL(str.replace(/^udp:/, 'http:'))
return {
hash: parsedUrl.hash,
host: parsedUrl.host,
hostname: parsedUrl.hostname,
href: isUDP ? parsedUrl.href.replace(/^http:/, 'udp:') : parsedUrl.href,
origin: isUDP ? parsedUrl.origin.replace(/^http:/, 'udp:') : parsedUrl.origin,
password: parsedUrl.password,
pathname: parsedUrl.pathname,
port: parsedUrl.port,
protocol: isUDP ? 'udp:' : parsedUrl.protocol,
search: parsedUrl.search,
username: parsedUrl.username
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') }
})
}
return url
}
const config = require('./common-node')