server: expose getSwarm()

drops capability to pass a *hex* infoHash
This commit is contained in:
Astro 2014-12-09 23:18:47 +01:00
parent fee289b074
commit aea3c44c08
2 changed files with 9 additions and 14 deletions

View File

@ -116,16 +116,9 @@ Server.prototype.close = function (cb) {
}
}
Server.prototype.getSwarm = function (infoHash) {
var self = this
var binaryInfoHash = Buffer.isBuffer(infoHash)
? infoHash.toString('binary')
: new Buffer(infoHash, 'hex').toString('binary')
return self._getSwarm(binaryInfoHash)
}
Server.prototype._getSwarm = function (binaryInfoHash) {
Server.prototype.getSwarm = function (binaryInfoHash) {
var self = this
if (Buffer.isBuffer(binaryInfoHash)) binaryInfoHash = binaryInfoHash.toString('binary')
var swarm = self.torrents[binaryInfoHash]
if (!swarm) {
swarm = self.torrents[binaryInfoHash] = {
@ -224,7 +217,7 @@ Server.prototype._onRequest = function (params, cb) {
Server.prototype._onAnnounce = function (params, cb) {
var self = this
var swarm = self._getSwarm(params.info_hash)
var swarm = self.getSwarm(params.info_hash)
var peer = swarm.peers[params.addr]
var start = function () {
@ -335,7 +328,7 @@ Server.prototype._onScrape = function (params, cb) {
}
params.info_hash.some(function (infoHash) {
var swarm = self._getSwarm(infoHash)
var swarm = self.getSwarm(infoHash)
response.files[infoHash] = {
complete: swarm.complete,
@ -414,12 +407,14 @@ function parseHttpRequest (req, options) {
}
if (params.info_hash) {
if (!Array.isArray(params.info_hash)) throw new Error('invalid info_hash')
if (!Array.isArray(params.info_hash)) throw new Error('invalid info_hash array')
params.info_hash.some(function (infoHash) {
params.info_hash = params.info_hash.map(function (infoHash) {
if (infoHash.length !== 20) {
throw new Error('invalid info_hash')
}
return infoHash
})
}

View File

@ -2,7 +2,7 @@ var Client = require('../')
var Server = require('../').Server
var test = require('tape')
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
var infoHash = new Buffer('4cb67059ed6bd08362da625b3ae77f6f4a075705', 'hex')
var peerId = '01234567890123456789'
var peerId2 = '12345678901234567890'
var torrentLength = 50000