server: unify compact serialization

This commit is contained in:
Astro 2014-12-10 16:51:35 +01:00
parent b72960dee3
commit 8439300df9
2 changed files with 7 additions and 18 deletions

View File

@ -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,

View File

@ -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)
})