From c2a37c9efa07b840c791c27984729a789202a660 Mon Sep 17 00:00:00 2001 From: John Hiesey Date: Tue, 9 Feb 2021 13:57:21 -0800 Subject: Support opts.preloadedStore in webtorrent.seed --- lib/file-stream.js | 5 ++++- lib/torrent.js | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/file-stream.js b/lib/file-stream.js index f6a8402..e83383f 100644 --- a/lib/file-stream.js +++ b/lib/file-stream.js @@ -53,7 +53,10 @@ class FileStream extends stream.Readable { if (this._torrent.destroyed) return this._destroy(new Error('Torrent removed')) const p = this._piece - this._torrent.store.get(p, (err, buffer) => { + const length = p === this._torrent.pieces.length - 1 ? this._torrent.lastPieceLength : undefined + this._torrent.store.get(p, { + length + }, (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 abb3b31..5d30057 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -71,6 +71,7 @@ class Torrent extends EventEmitter { this.path = opts.path this.skipVerify = !!opts.skipVerify this._store = opts.store || FSChunkStore + this._preloadedStore = opts.preloadedStore || null this._getAnnounceOpts = opts.getAnnounceOpts // if defined, `opts.private` overrides default privacy of torrent @@ -470,7 +471,7 @@ class Torrent extends EventEmitter { this._rarityMap = new RarityMap(this) this.store = new ImmediateChunkStore( - new this._store(this.pieceLength, { + this._preloadedStore || new this._store(this.pieceLength, { torrent: { infoHash: this.infoHash }, @@ -584,7 +585,11 @@ class Torrent extends EventEmitter { parallelLimit(this.pieces.map((piece, index) => cb => { if (this.destroyed) return cb(new Error('torrent is destroyed')) - this.store.get(index, (err, buf) => { + const length = index === this.pieces.length - 1 ? this.lastPieceLength : undefined + + this.store.get(index, { + length + }, (err, buf) => { if (this.destroyed) return cb(new Error('torrent is destroyed')) if (err) return queueMicrotask(() => cb(null)) // ignore error -- cgit v1.2.3