Skip to content

Commit 938c025

Browse files
committed
remove wrtc dependency
For webtorrent#303
1 parent 9a6e854 commit 938c025

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

client.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ inherits(Client, EventEmitter)
2525
* @param {Number} opts.numWant number of peers to request
2626
* @param {Number} opts.interval announce interval (in ms)
2727
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
28+
* @param {Number} opts.wrtc custom webrtc implementation
2829
*/
2930
function Client (peerId, port, torrent, opts) {
3031
var self = this
@@ -42,6 +43,9 @@ function Client (peerId, port, torrent, opts) {
4243
: new Buffer(torrent.infoHash, 'hex')
4344
self.torrentLength = torrent.length
4445

46+
self._rtcConfig = opts.rtcConfig
47+
self._wrtc = opts.wrtc
48+
4549
// optional
4650
self._numWant = opts.numWant || common.DEFAULT_ANNOUNCE_PEERS
4751
self._intervalMs = opts.interval || common.DEFAULT_ANNOUNCE_INTERVAL
@@ -51,18 +55,19 @@ function Client (peerId, port, torrent, opts) {
5155
if (typeof torrent.announce === 'string') torrent.announce = [ torrent.announce ]
5256
if (torrent.announce == null) torrent.announce = []
5357

58+
var trackerOpts = { interval: self._intervalMs }
59+
var webrtcSupport = !!self._wrtc || typeof window !== 'undefined'
60+
5461
self._trackers = torrent.announce
5562
.map(function (announceUrl) {
56-
var trackerOpts = { interval: self._intervalMs }
5763
var protocol = url.parse(announceUrl).protocol
5864

5965
if ((protocol === 'http:' || protocol === 'https:') &&
6066
typeof HTTPTracker === 'function') {
6167
return new HTTPTracker(self, announceUrl, trackerOpts)
6268
} else if (protocol === 'udp:' && typeof UDPTracker === 'function') {
6369
return new UDPTracker(self, announceUrl, trackerOpts)
64-
} else if ((protocol === 'ws:' || protocol === 'wss:') &&
65-
WebSocketTracker.supported) {
70+
} else if ((protocol === 'ws:' || protocol === 'wss:') && webrtcSupport) {
6671
return new WebSocketTracker(self, announceUrl, trackerOpts)
6772
}
6873
return null

lib/websocket-tracker.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ var inherits = require('inherits')
99
var Peer = require('simple-peer')
1010
var Socket = require('simple-websocket')
1111

12-
var wrtc
13-
try {
14-
wrtc = require('wrtc') // WebRTC in node - empty object in browser
15-
if (!wrtc.RTCPeerConnection) wrtc = null
16-
} catch (err) {
17-
wrtc = null // optional dependency failed to install
18-
}
19-
20-
var WEBRTC_SUPPORT = !!wrtc || typeof window !== 'undefined'
21-
2212
var common = require('./common')
2313

2414
// It turns out that you can't open multiple websockets to the same server within one
@@ -48,8 +38,6 @@ function WebSocketTracker (client, announceUrl, opts) {
4838
self._socket.on('data', self._onSocketData.bind(self))
4939
}
5040

51-
WebSocketTracker.supported = WEBRTC_SUPPORT
52-
5341
WebSocketTracker.prototype.announce = function (opts) {
5442
var self = this
5543
if (!self._socket.connected) {
@@ -138,7 +126,11 @@ WebSocketTracker.prototype._onSocketData = function (data) {
138126

139127
var peer
140128
if (data.offer) {
141-
peer = new Peer({ trickle: false, config: self._opts.rtcConfig, wrtc: wrtc })
129+
peer = new Peer({
130+
trickle: false,
131+
config: self.client._rtcConfig,
132+
wrtc: self.client._wrtc
133+
})
142134
peer.id = common.binaryToHex(data.peer_id)
143135
peer.once('signal', function (answer) {
144136
var params = {
@@ -188,8 +180,8 @@ WebSocketTracker.prototype._generateOffers = function (numWant, cb) {
188180
var peer = self._peers[offerId] = new Peer({
189181
initiator: true,
190182
trickle: false,
191-
config: self._opts.rtcConfig,
192-
wrtc: wrtc
183+
config: self.client._rtcConfig,
184+
wrtc: self.client._wrtc
193185
})
194186
peer.once('signal', function (offer) {
195187
offers.push({

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
"./lib/common-node": false,
1515
"./lib/http-tracker": false,
1616
"./lib/udp-tracker": false,
17-
"./server": false,
18-
"wrtc": false
17+
"./server": false
1918
},
2019
"bugs": {
2120
"url": "https://github.com/feross/bittorrent-tracker/issues"
@@ -64,8 +63,5 @@
6463
},
6564
"scripts": {
6665
"test": "standard && tape test/*.js"
67-
},
68-
"optionalDependencies": {
69-
"wrtc": "0.0.55"
7066
}
7167
}

0 commit comments

Comments
 (0)