Skip to content

Commit aea3c44

Browse files
committed
server: expose getSwarm()
drops capability to pass a *hex* infoHash
1 parent fee289b commit aea3c44

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

server.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,9 @@ Server.prototype.close = function (cb) {
116116
}
117117
}
118118

119-
Server.prototype.getSwarm = function (infoHash) {
120-
var self = this
121-
var binaryInfoHash = Buffer.isBuffer(infoHash)
122-
? infoHash.toString('binary')
123-
: new Buffer(infoHash, 'hex').toString('binary')
124-
return self._getSwarm(binaryInfoHash)
125-
}
126-
127-
Server.prototype._getSwarm = function (binaryInfoHash) {
119+
Server.prototype.getSwarm = function (binaryInfoHash) {
128120
var self = this
121+
if (Buffer.isBuffer(binaryInfoHash)) binaryInfoHash = binaryInfoHash.toString('binary')
129122
var swarm = self.torrents[binaryInfoHash]
130123
if (!swarm) {
131124
swarm = self.torrents[binaryInfoHash] = {
@@ -224,7 +217,7 @@ Server.prototype._onRequest = function (params, cb) {
224217
Server.prototype._onAnnounce = function (params, cb) {
225218
var self = this
226219

227-
var swarm = self._getSwarm(params.info_hash)
220+
var swarm = self.getSwarm(params.info_hash)
228221
var peer = swarm.peers[params.addr]
229222

230223
var start = function () {
@@ -335,7 +328,7 @@ Server.prototype._onScrape = function (params, cb) {
335328
}
336329

337330
params.info_hash.some(function (infoHash) {
338-
var swarm = self._getSwarm(infoHash)
331+
var swarm = self.getSwarm(infoHash)
339332

340333
response.files[infoHash] = {
341334
complete: swarm.complete,
@@ -414,12 +407,14 @@ function parseHttpRequest (req, options) {
414407
}
415408

416409
if (params.info_hash) {
417-
if (!Array.isArray(params.info_hash)) throw new Error('invalid info_hash')
410+
if (!Array.isArray(params.info_hash)) throw new Error('invalid info_hash array')
418411

419-
params.info_hash.some(function (infoHash) {
412+
params.info_hash = params.info_hash.map(function (infoHash) {
420413
if (infoHash.length !== 20) {
421414
throw new Error('invalid info_hash')
422415
}
416+
417+
return infoHash
423418
})
424419
}
425420

test/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var Client = require('../')
22
var Server = require('../').Server
33
var test = require('tape')
44

5-
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
5+
var infoHash = new Buffer('4cb67059ed6bd08362da625b3ae77f6f4a075705', 'hex')
66
var peerId = '01234567890123456789'
77
var peerId2 = '12345678901234567890'
88
var torrentLength = 50000

0 commit comments

Comments
 (0)