mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2024-12-13 19:06:27 +00:00
server: unify compact serialization
This commit is contained in:
parent
b72960dee3
commit
8439300df9
19
lib/swarm.js
19
lib/swarm.js
@ -1,5 +1,4 @@
|
||||
var debug = require('debug')('bittorrent-tracker')
|
||||
var string2compact = require('string2compact')
|
||||
|
||||
module.exports = Swarm
|
||||
|
||||
@ -85,9 +84,7 @@ Swarm.prototype.announce = function (params, cb) {
|
||||
if (params.left === 0 && peer) peer.complete = true
|
||||
|
||||
// send peers
|
||||
var peers = params.compact === 1
|
||||
? self._getPeersCompact(params.numwant)
|
||||
: self._getPeers(params.numwant)
|
||||
var peers = self._getPeers(params.numwant)
|
||||
|
||||
cb(null, {
|
||||
complete: this.complete,
|
||||
@ -111,20 +108,6 @@ Swarm.prototype._getPeers = function (numwant) {
|
||||
return peers
|
||||
}
|
||||
|
||||
Swarm.prototype._getPeersCompact = function (numwant) {
|
||||
var peers = []
|
||||
|
||||
for (var peerId in this.peers) {
|
||||
if (peers.length >= numwant) break
|
||||
var peer = this.peers[peerId]
|
||||
if (!peer) continue // ignore null values
|
||||
peers.push(peer.ip + ':' + peer.port)
|
||||
}
|
||||
|
||||
return string2compact(peers)
|
||||
}
|
||||
|
||||
|
||||
Swarm.prototype.scrape = function (infoHash, params, cb) {
|
||||
cb(null, {
|
||||
complete: this.complete,
|
||||
|
@ -9,6 +9,7 @@ var inherits = require('inherits')
|
||||
var ipLib = require('ip')
|
||||
var portfinder = require('portfinder')
|
||||
var series = require('run-series')
|
||||
var string2compact = require('string2compact')
|
||||
|
||||
var common = require('./lib/common')
|
||||
var Swarm = require('./lib/swarm')
|
||||
@ -216,6 +217,11 @@ Server.prototype._onAnnounce = function (params, cb) {
|
||||
if (response) {
|
||||
if (!response.action) response.action = common.ACTIONS.ANNOUNCE
|
||||
if (!response.intervalMs) response.intervalMs = self._intervalMs
|
||||
if (params.compact === 1) {
|
||||
response.peers = string2compact(response.peers.map(function (peer) {
|
||||
return peer.ip + ':' + peer.port // TODO: ipv6 brackets
|
||||
}))
|
||||
}
|
||||
}
|
||||
cb(err, response)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user