diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/file-stream.js | 11 | ||||
-rw-r--r-- | lib/torrent.js | 11 |
2 files changed, 13 insertions, 9 deletions
diff --git a/lib/file-stream.js b/lib/file-stream.js index e83383f..4345806 100644 --- a/lib/file-stream.js +++ b/lib/file-stream.js @@ -53,10 +53,13 @@ class FileStream extends stream.Readable { if (this._torrent.destroyed) return this._destroy(new Error('Torrent removed')) const p = this._piece - const length = p === this._torrent.pieces.length - 1 ? this._torrent.lastPieceLength : undefined - this._torrent.store.get(p, { - length - }, (err, buffer) => { + + const getOpts = {} + // Specify length for the last piece in case it is zero-padded + if (p === this._torrent.pieces.length - 1) { + getOpts.length = this._torrent.lastPieceLength + } + this._torrent.store.get(p, getOpts, (err, buffer) => { this._notifying = false if (this.destroyed) return debug('read %s (length %s) (err %s)', p, buffer.length, err && err.message) diff --git a/lib/torrent.js b/lib/torrent.js index 3760db2..970eab4 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -586,11 +586,12 @@ class Torrent extends EventEmitter { parallelLimit(this.pieces.map((piece, index) => cb => { if (this.destroyed) return cb(new Error('torrent is destroyed')) - const length = index === this.pieces.length - 1 ? this.lastPieceLength : undefined - - this.store.get(index, { - length - }, (err, buf) => { + const getOpts = {} + // Specify length for the last piece in case it is zero-padded + if (index === this.pieces.length - 1) { + getOpts.length = this.lastPieceLength + } + this.store.get(index, getOpts, (err, buf) => { if (this.destroyed) return cb(new Error('torrent is destroyed')) if (err) return queueMicrotask(() => cb(null)) // ignore error |