From c4f4f012dddcf70b57fe8c4d79825d519a566a34 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 16 Jan 2017 15:43:47 -0800 Subject: [PATCH] Add fixes and tests for PR #179 --- README.md | 12 +++++++----- client.js | 26 +++++++++++--------------- lib/client/http-tracker.js | 2 +- test/client.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index a65a849..d834889 100644 --- a/README.md +++ b/README.md @@ -59,12 +59,8 @@ var requiredOpts = { } var optionalOpts = { - // RTCPeerConnection config object (only used in browser) - rtcConfig: {}, - // custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc) - wrtc: {}, getAnnounceOpts: function () { - // provide a callback that will be called whenever announce() is called + // Provide a callback that will be called whenever announce() is called // internally (on timer), or by the user return { uploaded: 0, @@ -73,6 +69,12 @@ var optionalOpts = { customParam: 'blah' // custom parameters supported } } + // RTCPeerConnection config object (only used in browser) + rtcConfig: {}, + // User-Agent header for http requests + userAgent: '', + // Custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc) + wrtc: {}, } var client = new Client(requiredOpts) diff --git a/client.js b/client.js index 7265f54..c72f8d5 100644 --- a/client.js +++ b/client.js @@ -30,6 +30,7 @@ inherits(Client, EventEmitter) * @param {number} opts.port torrent client listening port * @param {function} opts.getAnnounceOpts callback to provide data to tracker * @param {number} opts.rtcConfig RTCPeerConnection configuration object + * @param {number} opts.userAgent User-Agent header for http requests * @param {number} opts.wrtc custom webrtc impl (useful in node.js) */ function Client (opts) { @@ -43,7 +44,6 @@ function Client (opts) { if (!opts.announce) throw new Error('Option `announce` is required') if (!process.browser && !opts.port) throw new Error('Option `port` is required') - // required self.peerId = typeof opts.peerId === 'string' ? opts.peerId : opts.peerId.toString('hex') @@ -56,39 +56,35 @@ function Client (opts) { self._infoHashBuffer = Buffer.from(self.infoHash, 'hex') self._infoHashBinary = self._infoHashBuffer.toString('binary') - self._port = opts.port + debug('new client %s', self.infoHash) self.destroyed = false - self._rtcConfig = opts.rtcConfig + self._port = opts.port self._getAnnounceOpts = opts.getAnnounceOpts - self._wrtc = opts.wrtc + self._rtcConfig = opts.rtcConfig + self._userAgent = opts.userAgent // Support lazy 'wrtc' module initialization // See: https://github.com/feross/webtorrent-hybrid/issues/46 - if (typeof self._wrtc === 'function') self._wrtc = self._wrtc() + self._wrtc = typeof opts.wrtc === 'function' ? opts.wrtc() : opts.wrtc - debug('new client %s', self.infoHash) - - var webrtcSupport = self._wrtc !== false && (!!self._wrtc || Peer.WEBRTC_SUPPORT) - - var announce = (typeof opts.announce === 'string') + var announce = typeof opts.announce === 'string' ? [ opts.announce ] - : opts.announce == null - ? [] - : opts.announce + : opts.announce == null ? [] : opts.announce + // Remove trailing slash from trackers to catch duplicates announce = announce.map(function (announceUrl) { announceUrl = announceUrl.toString() if (announceUrl[announceUrl.length - 1] === '/') { - // remove trailing slash from trackers to catch duplicates announceUrl = announceUrl.substring(0, announceUrl.length - 1) } return announceUrl }) - announce = uniq(announce) + var webrtcSupport = self._wrtc !== false && (!!self._wrtc || Peer.WEBRTC_SUPPORT) + self._trackers = announce .map(function (announceUrl) { var protocol = url.parse(announceUrl).protocol diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index 383a666..570a910 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -91,7 +91,7 @@ HTTPTracker.prototype._request = function (requestUrl, params, cb) { common.querystringStringify(params) var opts = { url: u, - header: { + headers: { 'user-agent': self.client._userAgent || '' } } diff --git a/test/client.js b/test/client.js index 9f1c397..29a2897 100644 --- a/test/client.js +++ b/test/client.js @@ -380,3 +380,35 @@ test('http: client announce with numwant', function (t) { test('udp: client announce with numwant', function (t) { testClientAnnounceWithNumWant(t, 'udp') }) + +test('http: userAgent', function (t) { + t.plan(2) + + common.createServer(t, 'http', function (server, announceUrl) { + // Confirm that user-agent header is set + server.http.on('request', function (req, res) { + t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1) + }) + + var client = new Client({ + infoHash: fixtures.leaves.parsedTorrent.infoHash, + announce: announceUrl, + peerId: peerId1, + port: port, + userAgent: 'WebTorrent/0.98.0 (https://webtorrent.io)', + wrtc: {} + }) + + client.on('error', function (err) { t.error(err) }) + client.on('warning', function (err) { t.error(err) }) + + client.once('update', function (data) { + t.equal(data.announce, announceUrl) + + server.close() + client.destroy() + }) + + client.start() + }) +})