From ede2119f78b2f7b714c33cc2b8535391fd767b76 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 15 Mar 2016 21:33:43 -0700 Subject: [PATCH] Fixes for PR #128 --- client.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/client.js b/client.js index d9aa6b4..3b52376 100644 --- a/client.js +++ b/client.js @@ -87,24 +87,25 @@ function Client (peerId, port, torrent, opts) { } else if (protocol === 'udp:' && typeof UDPTracker === 'function') { return new UDPTracker(self, announceUrl) } else if ((protocol === 'ws:' || protocol === 'wss:') && webrtcSupport) { - // Don't try to add http tracker on an https website - if (protocol === 'ws:' && location && location.protocol && location.protocol === 'https:') { - process.nextTick(function () { - var err = new Error('unsupported http tracker on https: ' + announceUrl) - self.emit('warning', err) - }) + // Skip ws:// trackers on https:// sites because they throw SecurityError + if (protocol === 'ws:' && typeof window !== 'undefined' && + window.location.protocol === 'https:') { + nextTickWarn(new Error('Unsupported tracker protocol: ' + announceUrl)) return null } return new WebSocketTracker(self, announceUrl) } else { - process.nextTick(function () { - var err = new Error('unsupported tracker protocol for ' + announceUrl) - self.emit('warning', err) - }) + nextTickWarn(new Error('Unsupported tracker protocol: ' + announceUrl)) + return null } - return null }) .filter(Boolean) + + function nextTickWarn (err) { + process.nextTick(function () { + self.emit('warning', err) + }) + } } /**