Skip to content

Commit 42d4077

Browse files
committed
fix(PeerGroup): handle lack of WebRTC support
When WebRTC is not supported (old browsers, or in Node.js without the "wrtc" package installed, don't try to get WebTC peers from a web seed. Also, throw an error if we try to listen for web connections.
1 parent 0f575da commit 42d4077

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

lib/peerGroup.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ var u = require('./utils.js')
1313

1414
var webSeeds = require('./constants.js').webSeeds
1515

16+
var supportsWebRTC = false
17+
if (process.browser) {
18+
var RTCPC = window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.RTCPeerConnection
19+
supportsWebRTC = !!RTCPC
20+
} else {
21+
try {
22+
require('wrtc')
23+
supportsWebRTC = true
24+
} catch(e) {}
25+
}
26+
1627
// HACK: suppress warnings from Buffer#get()
1728
Buffer.prototype.get = function get (offset) {
1829
return this.readUInt8(offset)
@@ -151,14 +162,16 @@ PeerGroup.prototype.connect = function (opts, cb) {
151162
})
152163
}
153164

154-
webSeeds.forEach(function (uri) {
155-
var client = new PeerhubClient(uri, function () {
156-
self.webSeeds.push(client)
157-
self.emit('seedconnect', client)
158-
self._connectToWebPeers()
159-
if (self.acceptWeb) self._acceptFromPeerhub(client)
165+
if (supportsWebRTC) {
166+
webSeeds.forEach(function (uri) {
167+
var client = new PeerhubClient(uri, function () {
168+
self.webSeeds.push(client)
169+
self.emit('seedconnect', client)
170+
self._connectToWebPeers()
171+
if (self.acceptWeb) self._acceptFromPeerhub(client)
172+
})
160173
})
161-
})
174+
}
162175
}
163176

164177
PeerGroup.prototype._connectToLocalhost = function (cb) {
@@ -231,6 +244,7 @@ PeerGroup.prototype._onWebPeerConnect = function (conn, incoming) {
231244
}
232245

233246
PeerGroup.prototype.acceptWebPeers = function () {
247+
if (!supportsWebRTC) throw new Error('WebRTC is not supported')
234248
this.webSeeds.forEach(this._acceptFromPeerhub.bind(this))
235249
this.acceptWeb = true
236250
}

0 commit comments

Comments
 (0)