Skip to content

Commit ce171f1

Browse files
committed
move common encode/decode fns to common.js
1 parent 2c34583 commit ce171f1

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

client.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Tracker.prototype.scrape = function (opts) {
192192
debug('sent `scrape` to ' + self._announceUrl)
193193

194194
opts = extend({
195-
info_hash: bytewiseEncodeURIComponent(self.client._infoHash)
195+
info_hash: common.bytewiseEncodeURIComponent(self.client._infoHash)
196196
}, opts)
197197

198198
self._requestImpl(self._scrapeUrl, opts)
@@ -214,8 +214,8 @@ Tracker.prototype.setInterval = function (intervalMs) {
214214
Tracker.prototype._request = function (opts) {
215215
var self = this
216216
opts = extend({
217-
info_hash: bytewiseEncodeURIComponent(self.client._infoHash),
218-
peer_id: bytewiseEncodeURIComponent(self.client._peerId),
217+
info_hash: common.bytewiseEncodeURIComponent(self.client._infoHash),
218+
peer_id: common.bytewiseEncodeURIComponent(self.client._peerId),
219219
port: self.client._port,
220220
compact: 1,
221221
numwant: self.client._numWant,
@@ -461,7 +461,7 @@ Tracker.prototype._handleResponse = function (requestUrl, data) {
461461
} else if (requestUrl === self._scrapeUrl) {
462462
// NOTE: the unofficial spec says to use the 'files' key but i've seen 'host' in practice
463463
data = data.files || data.host || {}
464-
data = data[bytewiseEncodeURIComponent(self.client._infoHash)]
464+
data = data[common.bytewiseEncodeURIComponent(self.client._infoHash)]
465465

466466
if (!data) {
467467
self.client.emit('error', new Error('invalid scrape response'))
@@ -499,7 +499,3 @@ function toUInt64 (n) {
499499
}
500500
return Buffer.concat([common.toUInt32(0), common.toUInt32(n)])
501501
}
502-
503-
function bytewiseEncodeURIComponent (buf) {
504-
return encodeURIComponent(buf.toString('binary'))
505-
}

lib/common.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ exports.toUInt32 = toUInt32
1212
exports.CONNECTION_ID = Buffer.concat([ toUInt32(0x417), toUInt32(0x27101980) ])
1313
exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 }
1414
exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3 }
15+
16+
exports.bytewiseDecodeURIComponent = function (str) {
17+
return new Buffer(decodeURIComponent(str), 'binary')
18+
}
19+
20+
exports.bytewiseEncodeURIComponent = function (buf) {
21+
return encodeURIComponent(buf.toString('binary'))
22+
}

server.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ Server.prototype._onHttpRequest = function (req, res) {
116116

117117
if (s[0] === '/announce') {
118118
var infoHash = typeof params.info_hash === 'string' &&
119-
bytewiseDecodeURIComponent(params.info_hash).toString('hex')
119+
common.bytewiseDecodeURIComponent(params.info_hash).toString('hex')
120120
var port = Number(params.port)
121121
var peerId = typeof params.peer_id === 'string' &&
122-
bytewiseDecodeURIComponent(params.peer_id).toString('utf8')
122+
common.bytewiseDecodeURIComponent(params.peer_id).toString('utf8')
123123

124124
if (!infoHash) return error('invalid info_hash')
125125
if (infoHash.length !== 40) return error('invalid info_hash')
@@ -231,7 +231,7 @@ Server.prototype._onHttpRequest = function (req, res) {
231231
}
232232

233233
params.info_hash.some(function (infoHash) {
234-
var infoHashHex = bytewiseDecodeURIComponent(infoHash).toString('hex')
234+
var infoHashHex = common.bytewiseDecodeURIComponent(infoHash).toString('hex')
235235
if (infoHashHex.length !== 40) {
236236
error('invalid info_hash')
237237
return true // early return
@@ -473,7 +473,3 @@ function fromUInt64 (buf) {
473473

474474
return high * TWO_PWR_32 + lowUnsigned
475475
}
476-
477-
function bytewiseDecodeURIComponent (str) {
478-
return new Buffer(decodeURIComponent(str), 'binary')
479-
}

0 commit comments

Comments
 (0)