Skip to content

Commit ce7dd6e

Browse files
committed
Refactor parseUrl
1 parent 8e24a8c commit ce7dd6e

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

lib/common.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,17 @@ exports.hexToBinary = function (str) {
3232
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
3333
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
3434
exports.parseUrl = function (str) {
35-
const isUDP = str.match(/^udp:/)
36-
const parsedUrl = (isUDP) ? new URL(str.replace(/^udp:/, 'http:')) : new URL(str)
35+
const url = new URL(str.replace(/^udp:/, 'http:'))
3736

38-
return {
39-
hash: parsedUrl.hash,
40-
host: parsedUrl.host,
41-
hostname: parsedUrl.hostname,
42-
href: isUDP ? parsedUrl.href.replace(/^http:/, 'udp:') : parsedUrl.href,
43-
origin: isUDP ? parsedUrl.origin.replace(/^http:/, 'udp:') : parsedUrl.origin,
44-
password: parsedUrl.password,
45-
pathname: parsedUrl.pathname,
46-
port: parsedUrl.port,
47-
protocol: isUDP ? 'udp:' : parsedUrl.protocol,
48-
search: parsedUrl.search,
49-
username: parsedUrl.username
37+
if (str.match(/^udp:/)) {
38+
Object.defineProperties(url, {
39+
href: { value: url.href.replace(/^http/, 'udp') },
40+
protocol: { value: url.protocol.replace(/^http/, 'udp') },
41+
origin: { value: url.origin.replace(/^http/, 'udp') }
42+
})
5043
}
44+
45+
return url
5146
}
5247

5348
const config = require('./common-node')

0 commit comments

Comments
 (0)