2014-06-07 22:10:12 +00:00
|
|
|
/**
|
2014-07-13 01:44:41 +00:00
|
|
|
* Functions/constants needed by both the client and server.
|
2014-06-07 22:10:12 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-03 23:53:37 +00:00
|
|
|
exports.DEFAULT_ANNOUNCE_PEERS = 50
|
2014-12-09 23:44:45 +00:00
|
|
|
exports.MAX_ANNOUNCE_PEERS = 82
|
|
|
|
|
2014-12-12 10:02:11 +00:00
|
|
|
exports.binaryToHex = function (str) {
|
2016-05-30 06:12:23 +00:00
|
|
|
if (typeof str !== 'string') {
|
|
|
|
str = String(str)
|
|
|
|
}
|
|
|
|
return Buffer.from(str, 'binary').toString('hex')
|
2014-12-12 10:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.hexToBinary = function (str) {
|
2016-05-30 06:12:23 +00:00
|
|
|
if (typeof str !== 'string') {
|
|
|
|
str = String(str)
|
|
|
|
}
|
|
|
|
return Buffer.from(str, 'hex').toString('binary')
|
2014-07-13 01:44:41 +00:00
|
|
|
}
|
2014-07-11 04:28:37 +00:00
|
|
|
|
2021-05-20 19:38:51 +00:00
|
|
|
// HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like
|
|
|
|
// 'udp:'. Just replace it with 'http:' since we only need a few properties.
|
|
|
|
//
|
|
|
|
// Note: Only affects Chrome and Firefox. Works fine in Node.js, Safari, and
|
|
|
|
// Edge.
|
|
|
|
//
|
|
|
|
// Note: UDP trackers aren't used in the normal browser build, but they are
|
|
|
|
// used in a Chrome App build (i.e. by Brave Browser).
|
|
|
|
//
|
|
|
|
// Bug reports:
|
|
|
|
// - 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)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 04:57:47 +00:00
|
|
|
const config = require('./common-node')
|
2018-10-02 13:09:02 +00:00
|
|
|
Object.assign(exports, config)
|