Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix how the first piece's irrelevant bytes are calculated #1738

Merged
merged 1 commit into from Sep 7, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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)
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.