From cc800880384766ae51112130d63374338333cf86 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 14 May 2014 12:49:15 -0700 Subject: [PATCH] handle case where parsedTorrent.length is undefined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Expose `torrentLength` so the user can set it when they know the torrent length. (With a magnet uri, they won’t know the length at the time the Client is instantiated) - UDP Client: Send FFFFFFFFFFFFFFFF for ‘left’ param when we don’t know the size. (This is what Transmission does) Fixes #15. --- index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 21867dc..0ce3cd5 100644 --- a/index.js +++ b/index.js @@ -71,7 +71,7 @@ Tracker.prototype.complete = function (opts) { var self = this opts = opts || {} opts.event = 'completed' - opts.downloaded = self._torrentLength + opts.downloaded = opts.downloaded || self.torrentLength || 0 self._request(opts) } @@ -126,13 +126,16 @@ Tracker.prototype._request = function (opts) { info_hash: bytewiseEncodeURIComponent(self.client._infoHash), peer_id: bytewiseEncodeURIComponent(self.client._peerId), port: self.client._port, - left: self.client._torrentLength - (opts.downloaded || 0), compact: 1, numwant: self.client._numWant, uploaded: 0, // default, user should provide real value downloaded: 0 // default, user should provide real value }, opts) + if (self.client.torrentLength !== undefined) { + opts.left = self.client.torrentLength - (opts.downloaded || 0) + } + if (self._trackerId) { opts.trackerid = self._trackerId } @@ -269,7 +272,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) { self.client._infoHash, self.client._peerId, toUInt64(opts.downloaded || 0), - toUInt64(opts.left || 0), + opts.left ? toUInt64(opts.left) : new Buffer('FFFFFFFFFFFFFFFF', 'hex'), toUInt64(opts.uploaded || 0), toUInt32(EVENTS[opts.event] || 0), toUInt32(0), // ip address (optional) @@ -392,7 +395,7 @@ function Client (peerId, port, torrent, opts) { self._infoHash = Buffer.isBuffer(torrent.infoHash) ? torrent.infoHash : new Buffer(torrent.infoHash, 'hex') - self._torrentLength = torrent.length + self.torrentLength = torrent.length self._announce = torrent.announce // optional