Skip to content

Commit c4f4f01

Browse files
committed
Add fixes and tests for PR webtorrent#179
1 parent 91cb2d3 commit c4f4f01

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ var requiredOpts = {
5959
}
6060

6161
var optionalOpts = {
62-
// RTCPeerConnection config object (only used in browser)
63-
rtcConfig: {},
64-
// custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
65-
wrtc: {},
6662
getAnnounceOpts: function () {
67-
// provide a callback that will be called whenever announce() is called
63+
// Provide a callback that will be called whenever announce() is called
6864
// internally (on timer), or by the user
6965
return {
7066
uploaded: 0,
@@ -73,6 +69,12 @@ var optionalOpts = {
7369
customParam: 'blah' // custom parameters supported
7470
}
7571
}
72+
// RTCPeerConnection config object (only used in browser)
73+
rtcConfig: {},
74+
// User-Agent header for http requests
75+
userAgent: '',
76+
// Custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
77+
wrtc: {},
7678
}
7779

7880
var client = new Client(requiredOpts)

client.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ inherits(Client, EventEmitter)
3030
* @param {number} opts.port torrent client listening port
3131
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
3232
* @param {number} opts.rtcConfig RTCPeerConnection configuration object
33+
* @param {number} opts.userAgent User-Agent header for http requests
3334
* @param {number} opts.wrtc custom webrtc impl (useful in node.js)
3435
*/
3536
function Client (opts) {
@@ -43,7 +44,6 @@ function Client (opts) {
4344
if (!opts.announce) throw new Error('Option `announce` is required')
4445
if (!process.browser && !opts.port) throw new Error('Option `port` is required')
4546

46-
// required
4747
self.peerId = typeof opts.peerId === 'string'
4848
? opts.peerId
4949
: opts.peerId.toString('hex')
@@ -56,39 +56,35 @@ function Client (opts) {
5656
self._infoHashBuffer = Buffer.from(self.infoHash, 'hex')
5757
self._infoHashBinary = self._infoHashBuffer.toString('binary')
5858

59-
self._port = opts.port
59+
debug('new client %s', self.infoHash)
6060

6161
self.destroyed = false
6262

63-
self._rtcConfig = opts.rtcConfig
63+
self._port = opts.port
6464
self._getAnnounceOpts = opts.getAnnounceOpts
65-
self._wrtc = opts.wrtc
65+
self._rtcConfig = opts.rtcConfig
66+
self._userAgent = opts.userAgent
6667

6768
// Support lazy 'wrtc' module initialization
6869
// See: https://github.com/feross/webtorrent-hybrid/issues/46
69-
if (typeof self._wrtc === 'function') self._wrtc = self._wrtc()
70-
71-
debug('new client %s', self.infoHash)
72-
73-
var webrtcSupport = self._wrtc !== false && (!!self._wrtc || Peer.WEBRTC_SUPPORT)
70+
self._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc
7471

75-
var announce = (typeof opts.announce === 'string')
72+
var announce = typeof opts.announce === 'string'
7673
? [ opts.announce ]
77-
: opts.announce == null
78-
? []
79-
: opts.announce
74+
: opts.announce == null ? [] : opts.announce
8075

76+
// Remove trailing slash from trackers to catch duplicates
8177
announce = announce.map(function (announceUrl) {
8278
announceUrl = announceUrl.toString()
8379
if (announceUrl[announceUrl.length - 1] === '/') {
84-
// remove trailing slash from trackers to catch duplicates
8580
announceUrl = announceUrl.substring(0, announceUrl.length - 1)
8681
}
8782
return announceUrl
8883
})
89-
9084
announce = uniq(announce)
9185

86+
var webrtcSupport = self._wrtc !== false && (!!self._wrtc || Peer.WEBRTC_SUPPORT)
87+
9288
self._trackers = announce
9389
.map(function (announceUrl) {
9490
var protocol = url.parse(announceUrl).protocol

lib/client/http-tracker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ HTTPTracker.prototype._request = function (requestUrl, params, cb) {
9191
common.querystringStringify(params)
9292
var opts = {
9393
url: u,
94-
header: {
94+
headers: {
9595
'user-agent': self.client._userAgent || ''
9696
}
9797
}

test/client.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,35 @@ test('http: client announce with numwant', function (t) {
380380
test('udp: client announce with numwant', function (t) {
381381
testClientAnnounceWithNumWant(t, 'udp')
382382
})
383+
384+
test('http: userAgent', function (t) {
385+
t.plan(2)
386+
387+
common.createServer(t, 'http', function (server, announceUrl) {
388+
// Confirm that user-agent header is set
389+
server.http.on('request', function (req, res) {
390+
t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1)
391+
})
392+
393+
var client = new Client({
394+
infoHash: fixtures.leaves.parsedTorrent.infoHash,
395+
announce: announceUrl,
396+
peerId: peerId1,
397+
port: port,
398+
userAgent: 'WebTorrent/0.98.0 (https://webtorrent.io)',
399+
wrtc: {}
400+
})
401+
402+
client.on('error', function (err) { t.error(err) })
403+
client.on('warning', function (err) { t.error(err) })
404+
405+
client.once('update', function (data) {
406+
t.equal(data.announce, announceUrl)
407+
408+
server.close()
409+
client.destroy()
410+
})
411+
412+
client.start()
413+
})
414+
})

0 commit comments

Comments
 (0)