diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2019-09-07 19:56:12 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2019-09-07 20:01:55 +0300 |
commit | 5f0de22a179196772efc23aa093ab9046cd007aa (patch) | |
tree | 52a10bbd01d515da220ec36f8a56f2d97494ffa6 /lib | |
parent | 9dac9cfdef56d2268c28c78c92e956914d2c9593 (diff) |
Fix how the first piece's irrelevant bytes are calculated
Diffstat (limited to 'lib')
-rw-r--r-- | lib/file.js | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/file.js b/lib/file.js index 381ae5c..3e75091 100644 --- a/lib/file.js +++ b/lib/file.js @@ -41,10 +41,12 @@ class File extends EventEmitter { const { _startPiece: start, _endPiece: end } = this const piece = pieces[start] - // Calculate first piece diffrently, it sometimes have a offset + // First piece may have an offset, e.g. irrelevant bytes from the end of + // the previous file + const irrelevantFirstPieceBytes = this.offset % pieceLength let downloaded = bitfield.get(start) - ? pieceLength - (this.offset % pieceLength) - : Math.max(piece.length - piece.missing - this.offset, 0) + ? pieceLength - irrelevantFirstPieceBytes + : Math.max(pieceLength - irrelevantFirstPieceBytes - piece.missing, 0) for (let index = start + 1; index <= end; ++index) { if (bitfield.get(index)) { @@ -53,12 +55,12 @@ class File extends EventEmitter { } else { // "in progress" data const piece = pieces[index] - downloaded += piece.length - piece.missing + downloaded += pieceLength - piece.missing } } - // We don't have a end-offset and one small file can fith in the middle - // of one chunk, so return this.length if it's oversized + // We don't know the end offset, so return this.length if it's oversized. + // e.g. One small file can fit in the middle of a piece. return Math.min(downloaded, this.length) } |