/ lib / common.js
common.js
 1  /**
 2   * Functions/constants needed by both the client and server.
 3   */
 4  import * as common from './common-node.js'
 5  export * from './common-node.js'
 6  
 7  export const DEFAULT_ANNOUNCE_PEERS = 50
 8  export const MAX_ANNOUNCE_PEERS = 82
 9  
10  // HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
11  // 'udp:'. Just replace it with 'http:' since we only need a few properties.
12  //
13  // Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and
14  // Edge.
15  //
16  // Note: UDP trackers aren't used in the normal browser build, but they are
17  // used in a Chrome App build (i.e. by Brave Browser).
18  //
19  // Bug reports:
20  // - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
21  // - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
22  export const parseUrl = str => {
23    const url = new URL(str.replace(/^udp:/, 'http:'))
24  
25    if (str.match(/^udp:/)) {
26      Object.defineProperties(url, {
27        href: { value: url.href.replace(/^http/, 'udp') },
28        protocol: { value: url.protocol.replace(/^http/, 'udp') },
29        origin: { value: url.origin.replace(/^http/, 'udp') }
30      })
31    }
32  
33    return url
34  }
35  
36  export default {
37    DEFAULT_ANNOUNCE_PEERS,
38    MAX_ANNOUNCE_PEERS,
39    parseUrl,
40    ...common
41  }