Skip to content

Commit cc80088

Browse files
committed
handle case where parsedTorrent.length is undefined
- 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 webtorrent#15.
1 parent 2983811 commit cc80088

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Tracker.prototype.complete = function (opts) {
7171
var self = this
7272
opts = opts || {}
7373
opts.event = 'completed'
74-
opts.downloaded = self._torrentLength
74+
opts.downloaded = opts.downloaded || self.torrentLength || 0
7575
self._request(opts)
7676
}
7777

@@ -126,13 +126,16 @@ Tracker.prototype._request = function (opts) {
126126
info_hash: bytewiseEncodeURIComponent(self.client._infoHash),
127127
peer_id: bytewiseEncodeURIComponent(self.client._peerId),
128128
port: self.client._port,
129-
left: self.client._torrentLength - (opts.downloaded || 0),
130129
compact: 1,
131130
numwant: self.client._numWant,
132131
uploaded: 0, // default, user should provide real value
133132
downloaded: 0 // default, user should provide real value
134133
}, opts)
135134

135+
if (self.client.torrentLength !== undefined) {
136+
opts.left = self.client.torrentLength - (opts.downloaded || 0)
137+
}
138+
136139
if (self._trackerId) {
137140
opts.trackerid = self._trackerId
138141
}
@@ -269,7 +272,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
269272
self.client._infoHash,
270273
self.client._peerId,
271274
toUInt64(opts.downloaded || 0),
272-
toUInt64(opts.left || 0),
275+
opts.left ? toUInt64(opts.left) : new Buffer('FFFFFFFFFFFFFFFF', 'hex'),
273276
toUInt64(opts.uploaded || 0),
274277
toUInt32(EVENTS[opts.event] || 0),
275278
toUInt32(0), // ip address (optional)
@@ -392,7 +395,7 @@ function Client (peerId, port, torrent, opts) {
392395
self._infoHash = Buffer.isBuffer(torrent.infoHash)
393396
? torrent.infoHash
394397
: new Buffer(torrent.infoHash, 'hex')
395-
self._torrentLength = torrent.length
398+
self.torrentLength = torrent.length
396399
self._announce = torrent.announce
397400

398401
// optional

0 commit comments

Comments
 (0)