Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohn Hiesey <john@hiesey.com>2021-02-10 00:57:21 +0300
committerJohn Hiesey <john@hiesey.com>2021-02-10 00:57:21 +0300
commitc2a37c9efa07b840c791c27984729a789202a660 (patch)
tree3c613e74e6271b55cfab5e062ed3ef49936ce765 /lib
parent17ac7fbb680206cc7467383b865811b7a485e9b3 (diff)
Support opts.preloadedStore in webtorrent.seed
Diffstat (limited to 'lib')
-rw-r--r--lib/file-stream.js5
-rw-r--r--lib/torrent.js9
2 files changed, 11 insertions, 3 deletions
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