(opts = {})
| 36 | */ |
| 37 | class Server extends EventEmitter { |
| 38 | constructor (opts = {}) { |
| 39 | super() |
| 40 | debug('new server %s', JSON.stringify(opts)) |
| 41 | |
| 42 | this.intervalMs = opts.interval |
| 43 | ? opts.interval |
| 44 | : 10 * 60 * 1000 // 10 min |
| 45 | |
| 46 | this._trustProxy = !!opts.trustProxy |
| 47 | if (typeof opts.filter === 'function') this._filter = opts.filter |
| 48 | |
| 49 | this.peersCacheLength = opts.peersCacheLength |
| 50 | this.peersCacheTtl = opts.peersCacheTtl |
| 51 | |
| 52 | this._listenCalled = false |
| 53 | this.listening = false |
| 54 | this.destroyed = false |
| 55 | this.torrents = {} |
| 56 | |
| 57 | this.http = null |
| 58 | this.udp4 = null |
| 59 | this.udp6 = null |
| 60 | this.ws = null |
| 61 | |
| 62 | // start an http tracker unless the user explictly says no |
| 63 | if (opts.http !== false) { |
| 64 | this.http = http.createServer(isObject(opts.http) ? opts.http : undefined) |
| 65 | this.http.on('error', err => { this._onError(err) }) |
| 66 | this.http.on('listening', onListening) |
| 67 | |
| 68 | // Add default http request handler on next tick to give user the chance to add |
| 69 | // their own handler first. Handle requests untouched by user's handler. |
| 70 | process.nextTick(() => { |
| 71 | this.http.on('request', (req, res) => { |
| 72 | if (res.headersSent) return |
| 73 | this.onHttpRequest(req, res) |
| 74 | }) |
| 75 | }) |
| 76 | } |
| 77 | |
| 78 | // start a udp tracker unless the user explicitly says no |
| 79 | if (opts.udp !== false) { |
| 80 | this.udp4 = this.udp = dgram.createSocket({ |
| 81 | type: 'udp4', |
| 82 | reuseAddr: true, |
| 83 | ...(isObject(opts.udp) ? opts.udp : undefined) |
| 84 | }) |
| 85 | this.udp4.on('message', (msg, rinfo) => { this.onUdpRequest(msg, rinfo) }) |
| 86 | this.udp4.on('error', err => { this._onError(err) }) |
| 87 | this.udp4.on('listening', onListening) |
| 88 | |
| 89 | this.udp6 = dgram.createSocket({ |
| 90 | type: 'udp6', |
| 91 | reuseAddr: true, |
| 92 | ...(isObject(opts.udp) ? opts.udp : undefined) |
| 93 | }) |
| 94 | this.udp6.on('message', (msg, rinfo) => { this.onUdpRequest(msg, rinfo) }) |
| 95 | this.udp6.on('error', err => { this._onError(err) }) |
nothing calls this directly
no test coverage detected