diff options
author | John Hiesey <john@hiesey.com> | 2021-02-10 00:57:21 +0300 |
---|---|---|
committer | John Hiesey <john@hiesey.com> | 2021-02-10 00:57:21 +0300 |
commit | c2a37c9efa07b840c791c27984729a789202a660 (patch) | |
tree | 3c613e74e6271b55cfab5e062ed3ef49936ce765 | |
parent | 17ac7fbb680206cc7467383b865811b7a485e9b3 (diff) |
Support opts.preloadedStore in webtorrent.seed
-rw-r--r-- | index.js | 6 | ||||
-rw-r--r-- | lib/file-stream.js | 5 | ||||
-rw-r--r-- | lib/torrent.js | 9 |
3 files changed, 14 insertions, 6 deletions
@@ -273,8 +273,8 @@ class WebTorrent extends EventEmitter { const onTorrent = torrent => { const tasks = [ cb => { - // when a filesystem path is specified, files are already in the FS store - if (isFilePath) return cb() + // when a filesystem path is specified or the store is preloaded, files are already in the FS store + if (isFilePath || opts.preloadedStore) return cb() torrent.load(streams, cb) } ] @@ -304,7 +304,7 @@ class WebTorrent extends EventEmitter { else if (!Array.isArray(input)) input = [input] parallel(input.map(item => cb => { - if (isReadable(item)) { + if (!opts.preloadedStore && isReadable(item)) { concat(item, (err, buf) => { if (err) return cb(err) buf.name = item.name 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 |