diff options
author | John Hiesey <john@hiesey.com> | 2021-03-08 23:58:50 +0300 |
---|---|---|
committer | John Hiesey <john@hiesey.com> | 2021-03-09 00:14:19 +0300 |
commit | 4cbce24f24005ddab216ce94da7716400baafff9 (patch) | |
tree | 9e6876270127c94aa83ea976212af0b8166a4884 /lib | |
parent | a860773ec4d379cb10922b60de3489559c6913cd (diff) |
Only pass length to store.get when defined
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 |