Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2019-09-07 22:36:54 +0300
committerGitHub <noreply@github.com>2019-09-07 22:36:54 +0300
commit2e5f69d1949dbfaccd32054fe41b0583f03a5b67 (patch)
treea55c4ea306ed965aa368fdfac22b827a2c21d7cc /lib
parent6744c97064fbeb066d515285a4fde67ba2759582 (diff)
parent5f0de22a179196772efc23aa093ab9046cd007aa (diff)
Fix how the first piece's irrelevant bytes are calculated (#1738)
Fix how the first piece's irrelevant bytes are calculated
Diffstat (limited to 'lib')
-rw-r--r--lib/file.js14
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)
}