diff --git a/lib/torrent.js b/lib/torrent.js index 48001ac2..ee8dfb14 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -251,14 +251,46 @@ Torrent.prototype._onSwarmListening = function () { /** * Called when the metadata is received. + * Does setup on torrent, if metadata is not already set. + * Sets: + * Torrent.metadata + * If the passed metadata is a decoded torrent file encodes + * the passed metadata into a torrent file + * otherwise uses the passed metadata + * {@link https://github.com/feross/parse-torrent} + * Torrent.parsedTorrent + * (note: parsedTorrent is synonym for decoded torrent file) + * If the passed metadata is already a decoded torrent file + * sets it as the parsedTorrent + * otherwise attempts to parse it + * {@link https://en.wikipedia.org/wiki/Torrent_file} + * Torrent.name + * Preliminary update to the name + * uses the torrent file's name property + * Torrent.discovery + * calls setTorrent + * Updates discovery module with full torrent metadata + * Torrent.rarityMap + * Creates a new RarityMap using the current swarm and parsedTorrent + * Torrent.storage + * If _storageImpl is default + * Creates a new Storage using parsedTorrent and self.opts + * otherwise + * uses the provided _storageImpl + * Torrent.storage.on + * Does setup on storage, setting event handlers for + * 'piece','file', 'done', 'select', 'deselect', 'critical' + * Torrent.files + * Gets all files from Torrent.storage and pushes them to Torrent.files + * + * */ Torrent.prototype._onMetadata = function (metadata) { var self = this if (self.metadata || self.destroyed) return debug('got metadata') - if (metadata && metadata.infoHash) { - // `metadata` is a parsed torrent (from parse-torrent module) + if (metadata.infoHash) { self.metadata = parseTorrent.toTorrentFile(metadata) self.parsedTorrent = metadata } else { @@ -272,10 +304,8 @@ Torrent.prototype._onMetadata = function (metadata) { self.parsedTorrent.announce = announce } - // update preliminary torrent name self.name = self.parsedTorrent.name - // update discovery module with full torrent metadata self.discovery.setTorrent(self.parsedTorrent) self.rarityMap = new RarityMap(self.swarm, self.parsedTorrent.pieces.length)