move common encode/decode fns to common.js

This commit is contained in:
Feross Aboukhadijeh 2014-07-10 21:28:37 -07:00
parent 2c34583c5f
commit ce171f196c
3 changed files with 15 additions and 15 deletions

View File

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

View File

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

View File

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