diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2014-12-30 06:52:01 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2014-12-30 06:52:01 +0300 |
commit | ffcb0700e73a2fb21ff7d2a1bd3a19340823fcfa (patch) | |
tree | e5c6be77a5f84d7fa5b7711e08c5ef43ba654b15 /lib | |
parent | 3db656bd9b427339bcbd1ec38b0fe2e50d5588e8 (diff) |
add `torrent.magnetURI` getter function
Depends on https://github.com/feross/parse-torrent/pull/8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/torrent.js | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index 131c092..db8c33f 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -95,9 +95,9 @@ function Torrent (torrentId, opts) { } else throw new Error('invalid torrent id') function onTorrentId (torrentId) { - parsedTorrent = parseTorrent(torrentId) - self.infoHash = parsedTorrent.infoHash - if (parsedTorrent.name) self.name = parsedTorrent.name // preliminary name + self.parsedTorrent = parseTorrent(torrentId) + self.infoHash = self.parsedTorrent.infoHash + if (self.parsedTorrent.name) self.name = self.parsedTorrent.name // preliminary name // create swarm self.swarm = new Swarm(self.infoHash, self.client.peerId, { @@ -112,10 +112,10 @@ function Torrent (torrentId, opts) { if (process.browser) { // in browser, swarm does not listen - self._onSwarmListening(parsedTorrent) + self._onSwarmListening() } else { // listen for peers - self.swarm.listen(self.client.torrentPort, self._onSwarmListening.bind(self, parsedTorrent)) + self.swarm.listen(self.client.torrentPort, self._onSwarmListening.bind(self)) } process.nextTick(function () { self.emit('infoHash') @@ -166,7 +166,13 @@ Object.defineProperty(Torrent.prototype, 'ratio', { } }) -Torrent.prototype._onSwarmListening = function (parsed, port) { +Object.defineProperty(Torrent.prototype, 'magnetURI', { + get: function () { + return parseTorrent.toMagnetURI(this.parsedTorrent) + } +}) + +Torrent.prototype._onSwarmListening = function (port) { var self = this if (self._destroyed) return @@ -174,7 +180,7 @@ Torrent.prototype._onSwarmListening = function (parsed, port) { // begin discovering peers via the DHT and tracker servers self.discovery = new Discovery({ - announce: parsed.announce, + announce: self.parsedTorrent.announce, dht: self.client.dht, tracker: self.client.tracker, peerId: self.client.peerId, @@ -187,7 +193,7 @@ Torrent.prototype._onSwarmListening = function (parsed, port) { reemit(self.discovery, self, ['dhtAnnounce', 'warning', 'error']) // if full metadata was included in initial torrent id, use it - if (parsed.info) self._onMetadata(parsed) + if (self.parsedTorrent.info) self._onMetadata(self.parsedTorrent) self.emit('listening', port) } @@ -202,7 +208,7 @@ Torrent.prototype._onMetadata = function (metadata) { if (metadata && metadata.infoHash) { // `metadata` is a parsed torrent (from parse-torrent module) - self.metadata = parseTorrent.toBuffer(metadata) + self.metadata = parseTorrent.toTorrentFile(metadata) self.parsedTorrent = metadata } else { self.metadata = metadata |