@@ -19,5 +19,36 @@ exports.hexToBinary = function (str) {
19
19
return Buffer . from ( str , 'hex' ) . toString ( 'binary' )
20
20
}
21
21
22
+ // HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
23
+ // 'udp:'. Just replace it with 'http:' since we only need a few properties.
24
+ //
25
+ // Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and
26
+ // Edge.
27
+ //
28
+ // Note: UDP trackers aren't used in the normal browser build, but they are
29
+ // used in a Chrome App build (i.e. by Brave Browser).
30
+ //
31
+ // Bug reports:
32
+ // - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
33
+ // - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
34
+ exports . parseUrl = function ( str ) {
35
+ const isUDP = str . match ( / ^ u d p : / )
36
+ const parsedUrl = ( isUDP ) ? new URL ( str . replace ( / ^ u d p : / , 'http:' ) ) : new URL ( str )
37
+
38
+ return {
39
+ hash : parsedUrl . hash ,
40
+ host : parsedUrl . host ,
41
+ hostname : parsedUrl . hostname ,
42
+ href : isUDP ? parsedUrl . href . replace ( / ^ h t t p : / , 'udp:' ) : parsedUrl . href ,
43
+ origin : isUDP ? parsedUrl . origin . replace ( / ^ h t t p : / , '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
50
+ }
51
+ }
52
+
22
53
const config = require ( './common-node' )
23
54
Object . assign ( exports , config )
0 commit comments