diff options
author | John Hiesey <john@hiesey.com> | 2021-02-26 07:04:49 +0300 |
---|---|---|
committer | John Hiesey <john@hiesey.com> | 2021-02-26 07:04:49 +0300 |
commit | 7a74efc60dbc8a17be4fb7324855ae49c6f78e82 (patch) | |
tree | be14a90db86826863de6f35a27872cad92cedc3e /lib | |
parent | 91b53a2fdbf923bba5ef73d0f99979a0a28cba45 (diff) |
Allow passing a custom connection object to torrent.addWebSeed
Diffstat (limited to 'lib')
-rw-r--r-- | lib/peer.js | 8 | ||||
-rw-r--r-- | lib/torrent.js | 46 |
2 files changed, 37 insertions, 17 deletions
diff --git a/lib/peer.js b/lib/peer.js index 0d0b807..2f291d8 100644 --- a/lib/peer.js +++ b/lib/peer.js @@ -2,8 +2,6 @@ const arrayRemove = require('unordered-array-remove') const debug = require('debug')('webtorrent:peer') const Wire = require('bittorrent-protocol') -const WebConn = require('./webconn') - const CONNECT_TIMEOUT_TCP = 5000 const CONNECT_TIMEOUT_UTP = 5000 const CONNECT_TIMEOUT_WEBRTC = 25000 @@ -86,10 +84,10 @@ const _createOutgoingPeer = (addr, swarm, type) => { /** * Peer that represents a Web Seed (BEP17 / BEP19). */ -exports.createWebSeedPeer = (url, swarm) => { - const peer = new Peer(url, 'webSeed') +exports.createWebSeedPeer = (conn, id, swarm) => { + const peer = new Peer(id, 'webSeed') peer.swarm = swarm - peer.conn = new WebConn(url, swarm) + peer.conn = conn peer.onConnect() 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) } /** |