diff options
Diffstat (limited to 'lib/torrent.js')
-rw-r--r-- | lib/torrent.js | 157 |
1 files changed, 70 insertions, 87 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index acb4c3e..97ee244 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -5,8 +5,6 @@ var createTorrent = require('create-torrent') var debug = require('debug')('webtorrent:torrent') var Discovery = require('torrent-discovery') var EventEmitter = require('events').EventEmitter -var fs = require('fs') // browser exclude -var get = require('simple-get') // browser exclude var inherits = require('inherits') var parallel = require('run-parallel') var parseTorrent = require('parse-torrent') @@ -77,91 +75,7 @@ function Torrent (torrentId, opts) { self._storageImpl = opts.storage || Storage this._torrentFileURL = null - var parsedTorrent - try { - parsedTorrent = (torrentId && torrentId.parsedTorrent) || parseTorrent(torrentId) - } catch (err) { - // If torrent fails to parse, it could be an http/https URL or filesystem path, so - // don't consider it an error yet. - } - - if (parsedTorrent && parsedTorrent.infoHash) { - onTorrentId(parsedTorrent) - } else if (typeof get === 'function' && /^https?:/.test(torrentId)) { - // http or https url to torrent file - get.concat({ - url: torrentId, - headers: { 'user-agent': 'WebTorrent (http://webtorrent.io)' } - }, function (err, data) { - if (err) { - err = new Error('Error downloading torrent: ' + err.message) - return self._onError(err) - } - onTorrentId(data) - }) - } else if (typeof fs.readFile === 'function') { - // assume it's a filesystem path - fs.readFile(torrentId, function (err, torrent) { - if (err) return self._onError(new Error('Invalid torrent identifier')) - onTorrentId(torrent) - }) - } else throw new Error('Invalid torrent identifier') - - function onTorrentId (torrentId) { - try { - self.parsedTorrent = parseTorrent(torrentId) - } catch (err) { - return self._onError(new Error('Malformed torrent data: ' + err.message)) - } - - self.infoHash = self.parsedTorrent.infoHash - - if (!self.infoHash) { - return self._onError(new Error('Malformed torrent data: Missing info hash.')) - } - - if (self.parsedTorrent.name) self.name = self.parsedTorrent.name // preliminary name - - if (opts.announce) { - self.parsedTorrent.announce.push.apply(self.parsedTorrent.announce, opts.announce) - } - - // When no trackers specified, use some reasonable defaults - if (self.parsedTorrent.announce.length === 0) { - self.parsedTorrent.announce = createTorrent.announceList.map(function (list) { - return list[0] - }) - } - - // So `webtorrent-hybrid` can force specific trackers to be used - if (global.WEBTORRENT_ANNOUNCE) { - global.WEBTORRENT_ANNOUNCE.forEach(function (url) { - self.parsedTorrent.announce.push(url) - }) - } - - uniq(self.parsedTorrent.announce) - - // create swarm - self.swarm = new Swarm(self.infoHash, self.client.peerId, { - handshake: { dht: !!self.client.dht } - }) - self.swarm.on('error', self._onError.bind(self)) - reemit(self.swarm, self, ['warning']) - self.swarm.on('wire', self._onWire.bind(self)) - - // update overall client stats - self.swarm.on('download', self.client.downloadSpeed.bind(self.client)) - self.swarm.on('upload', self.client.uploadSpeed.bind(self.client)) - - // listen for peers (note: in the browser, this is a no-op and callback is called on - // next tick) - self.swarm.listen(self.client.torrentPort, self._onSwarmListening.bind(self)) - - process.nextTick(function () { - self.emit('infoHash') - }) - } + if (torrentId) self._onTorrentId(torrentId) } // torrent size (in bytes) @@ -230,6 +144,75 @@ Object.defineProperty(Torrent.prototype, 'torrentFileURL', { } }) +Torrent.prototype._onTorrentId = function (torrentId) { + var self = this + var parsedTorrent = torrentId && torrentId.parsedTorrent + if (parsedTorrent) { + self._onParsedTorrent(parsedTorrent) + } else { + parseTorrent.remote(torrentId, function (err, parsedTorrent) { + if (self.destroyed) return + if (err) return self._onError(err) + self._onParsedTorrent(parsedTorrent) + }) + } +} + +Torrent.prototype._onParsedTorrent = function (parsedTorrent) { + var self = this + self.parsedTorrent = parsedTorrent + self.infoHash = parsedTorrent.infoHash + + if (!self.infoHash) { + return self._onError(new Error('Malformed torrent data: Missing info hash.')) + } + + if (self.parsedTorrent.name) self.name = self.parsedTorrent.name // preliminary name + + if (self.opts.announce) { + self.parsedTorrent.announce.push.apply( + self.parsedTorrent.announce, self.opts.announce + ) + } + + // When no trackers specified, use some reasonable defaults + if (self.parsedTorrent.announce.length === 0) { + self.parsedTorrent.announce = createTorrent.announceList.map(function (list) { + return list[0] + }) + } + + // So `webtorrent-hybrid` can force specific trackers to be used + if (global.WEBTORRENT_ANNOUNCE) { + global.WEBTORRENT_ANNOUNCE.forEach(function (url) { + self.parsedTorrent.announce.push(url) + }) + } + + uniq(self.parsedTorrent.announce) + + // create swarm + self.swarm = new Swarm(self.infoHash, self.client.peerId, { + handshake: { dht: !!self.client.dht } + }) + self.swarm.on('error', self._onError.bind(self)) + reemit(self.swarm, self, ['warning']) + self.swarm.on('wire', self._onWire.bind(self)) + + // update overall client stats + self.swarm.on('download', self.client.downloadSpeed.bind(self.client)) + self.swarm.on('upload', self.client.uploadSpeed.bind(self.client)) + + // listen for peers (note: in the browser, this is a no-op and callback is called on + // next tick) + self.swarm.listen(self.client.torrentPort, self._onSwarmListening.bind(self)) + + process.nextTick(function () { + if (self.destroyed) return + self.emit('infoHash', self.infoHash) + }) +} + Torrent.prototype._onSwarmListening = function () { var self = this if (self.destroyed) return |