diff options
Diffstat (limited to 'lib/torrent.js')
-rw-r--r-- | lib/torrent.js | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index ce5abb8..5d2b313 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -31,6 +31,7 @@ const File = require('./file') const Peer = require('./peer') const RarityMap = require('./rarity-map') const Server = require('./server') // browser exclude +const WebConn = require('./webconn') const MAX_BLOCK_LENGTH = 128 * 1024 const PIECE_TIMEOUT = 30000 @@ -820,28 +821,49 @@ class Torrent extends EventEmitter { return newPeer } - addWebSeed (url) { + addWebSeed (urlOrConnection) { if (this.destroyed) throw new Error('torrent is destroyed') - if (!/^https?:\/\/.+/.test(url)) { - this.emit('warning', new Error(`ignoring invalid web seed: ${url}`)) - this.emit('invalidPeer', url) - return - } + let id + let conn + if (typeof urlOrConnection === 'string') { + const url = urlOrConnection + id = urlOrConnection + + if (!/^https?:\/\/.+/.test(url)) { + this.emit('warning', new Error(`ignoring invalid web seed: ${id}`)) + this.emit('invalidPeer', id) + return + } + + if (this._peers[id]) { + this.emit('warning', new Error(`ignoring duplicate web seed: ${id}`)) + this.emit('invalidPeer', id) + return + } + + conn = new WebConn(url, this) + } else if (urlOrConnection && typeof urlOrConnection.connId === 'string') { + conn = urlOrConnection + id = conn.connId - if (this._peers[url]) { - this.emit('warning', new Error(`ignoring duplicate web seed: ${url}`)) - this.emit('invalidPeer', url) + if (this._peers[id]) { + this.emit('warning', new Error(`ignoring duplicate web seed: ${id}`)) + this.emit('invalidPeer', id) + return + } + } else { + this.emit('warning', new Error('addWebSeed must be passed a string or connection object with id property')) return } - this._debug('add web seed %s', url) + this._debug('add web seed %s', id) - const newPeer = Peer.createWebSeedPeer(url, this) + const newPeer = Peer.createWebSeedPeer(conn, id, this) this._peers[newPeer.id] = newPeer this._peersLength += 1 - this.emit('peer', url) + this.emit('peer', id) } /** |