diff --git a/client.js b/client.js index 7254ea4..4f68eb1 100644 --- a/client.js +++ b/client.js @@ -192,7 +192,7 @@ Tracker.prototype.scrape = function (opts) { debug('sent `scrape` to ' + self._announceUrl) opts = extend({ - info_hash: bytewiseEncodeURIComponent(self.client._infoHash) + info_hash: common.bytewiseEncodeURIComponent(self.client._infoHash) }, opts) self._requestImpl(self._scrapeUrl, opts) @@ -214,8 +214,8 @@ Tracker.prototype.setInterval = function (intervalMs) { Tracker.prototype._request = function (opts) { var self = this opts = extend({ - info_hash: bytewiseEncodeURIComponent(self.client._infoHash), - peer_id: bytewiseEncodeURIComponent(self.client._peerId), + info_hash: common.bytewiseEncodeURIComponent(self.client._infoHash), + peer_id: common.bytewiseEncodeURIComponent(self.client._peerId), port: self.client._port, compact: 1, numwant: self.client._numWant, @@ -461,7 +461,7 @@ Tracker.prototype._handleResponse = function (requestUrl, data) { } else if (requestUrl === self._scrapeUrl) { // NOTE: the unofficial spec says to use the 'files' key but i've seen 'host' in practice data = data.files || data.host || {} - data = data[bytewiseEncodeURIComponent(self.client._infoHash)] + data = data[common.bytewiseEncodeURIComponent(self.client._infoHash)] if (!data) { self.client.emit('error', new Error('invalid scrape response')) @@ -499,7 +499,3 @@ function toUInt64 (n) { } return Buffer.concat([common.toUInt32(0), common.toUInt32(n)]) } - -function bytewiseEncodeURIComponent (buf) { - return encodeURIComponent(buf.toString('binary')) -} diff --git a/lib/common.js b/lib/common.js index fe936b1..a281f83 100644 --- a/lib/common.js +++ b/lib/common.js @@ -12,3 +12,11 @@ exports.toUInt32 = toUInt32 exports.CONNECTION_ID = Buffer.concat([ toUInt32(0x417), toUInt32(0x27101980) ]) exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 } exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3 } + +exports.bytewiseDecodeURIComponent = function (str) { + return new Buffer(decodeURIComponent(str), 'binary') +} + +exports.bytewiseEncodeURIComponent = function (buf) { + return encodeURIComponent(buf.toString('binary')) +} diff --git a/server.js b/server.js index a7db9eb..8d3a0eb 100644 --- a/server.js +++ b/server.js @@ -116,10 +116,10 @@ Server.prototype._onHttpRequest = function (req, res) { if (s[0] === '/announce') { var infoHash = typeof params.info_hash === 'string' && - bytewiseDecodeURIComponent(params.info_hash).toString('hex') + common.bytewiseDecodeURIComponent(params.info_hash).toString('hex') var port = Number(params.port) var peerId = typeof params.peer_id === 'string' && - bytewiseDecodeURIComponent(params.peer_id).toString('utf8') + common.bytewiseDecodeURIComponent(params.peer_id).toString('utf8') if (!infoHash) return error('invalid info_hash') if (infoHash.length !== 40) return error('invalid info_hash') @@ -231,7 +231,7 @@ Server.prototype._onHttpRequest = function (req, res) { } params.info_hash.some(function (infoHash) { - var infoHashHex = bytewiseDecodeURIComponent(infoHash).toString('hex') + var infoHashHex = common.bytewiseDecodeURIComponent(infoHash).toString('hex') if (infoHashHex.length !== 40) { error('invalid info_hash') return true // early return @@ -473,7 +473,3 @@ function fromUInt64 (buf) { return high * TWO_PWR_32 + lowUnsigned } - -function bytewiseDecodeURIComponent (str) { - return new Buffer(decodeURIComponent(str), 'binary') -}