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
diff options
context:
space:
mode:
-rw-r--r--docs/api.md21
-rw-r--r--index.js12
-rw-r--r--lib/torrent.js15
-rw-r--r--test/node/basic.js48
4 files changed, 80 insertions, 16 deletions
diff --git a/docs/api.md b/docs/api.md
index e178c18..1532a89 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -165,12 +165,14 @@ destroyed and all torrents are removed and cleaned up when this occurs.
Always listen for the 'error' event.
-## `client.remove(torrentId, [function callback (err) {}])`
+## `client.remove(torrentId, [opts], [function callback (err) {}])`
-Remove a torrent from the client. Destroy all connections to peers and delete all saved
-file data. If `callback` is specified, it will be called when file data is removed.
+Remove a torrent from the client. Destroy all connections to peers and delete all saved file metadata.
-*Note: This method does not currently delete torrent data (in e.g. `/tmp/webtorrent/...`, see the `path` option to `client.add`). Until this is fixed, please implement it yourself (consider using the `rimraf` npm package).
+If `opts.destroyStore` is truthy, `store.destroy()` will be called, which will delete the torrent's files from the disk.
+
+If `callback` is provided, it will be called when the torrent is fully destroyed,
+i.e. all open sockets are closed, and the storage is either closed or destroyed.
## `client.destroy([function callback (err) {}])`
@@ -321,11 +323,14 @@ Author of the torrent (string).
A comment optionnaly set by the author (string).
-## `torrent.destroy([callback])`
+## `torrent.destroy([opts], [callback])`
+
+Remove the torrent from its client. Destroy all connections to peers and delete all saved file metadata.
+
+If `opts.destroyStore` is truthy, `store.destroy()` will be called, which will delete the torrent's files from the disk.
-Alias for `client.remove(torrent)`. If `callback` is provided, it will be called when
-the torrent is fully destroyed, i.e. all open sockets are closed, and the storage is
-closed.
+If `callback` is provided, it will be called when the torrent is fully destroyed,
+i.e. all open sockets are closed, and the storage is either closed or destroyed.
## `torrent.addPeer(peer)`
diff --git a/index.js b/index.js
index a807c9e..f024ba8 100644
--- a/index.js
+++ b/index.js
@@ -335,18 +335,22 @@ class WebTorrent extends EventEmitter {
* @param {string|Buffer|Torrent} torrentId
* @param {function} cb
*/
- remove (torrentId, cb) {
+ remove (torrentId, opts, cb) {
+ if (typeof opts === 'function') return this.remove(torrentId, null, opts)
+
this._debug('remove')
const torrent = this.get(torrentId)
if (!torrent) throw new Error(`No torrent with id ${torrentId}`)
- this._remove(torrentId, cb)
+ this._remove(torrentId, opts, cb)
}
- _remove (torrentId, cb) {
+ _remove (torrentId, opts, cb) {
+ if (typeof opts === 'function') return this._remove(torrentId, null, opts)
+
const torrent = this.get(torrentId)
if (!torrent) return
this.torrents.splice(this.torrents.indexOf(torrent), 1)
- torrent.destroy(cb)
+ torrent.destroy(opts, cb)
}
address () {
diff --git a/lib/torrent.js b/lib/torrent.js
index 3be12af..caa6687 100644
--- a/lib/torrent.js
+++ b/lib/torrent.js
@@ -641,11 +641,14 @@ class Torrent extends EventEmitter {
this._updateSelections()
}
- destroy (cb) {
- this._destroy(null, cb)
+ destroy (opts, cb) {
+ if (typeof opts === 'function') return this.destroy(null, opts)
+
+ this._destroy(null, opts, cb)
}
- _destroy (err, cb) {
+ _destroy (err, opts, cb) {
+ if (typeof opts === 'function') return this._destroy(err, null, opts)
if (this.destroyed) return
this.destroyed = true
this._debug('destroy')
@@ -682,7 +685,11 @@ class Torrent extends EventEmitter {
if (this.store) {
tasks.push(cb => {
- this.store.close(cb)
+ if (opts && opts.destroyStore) {
+ this.store.destroy(cb)
+ } else {
+ this.store.close(cb)
+ }
})
}
diff --git a/test/node/basic.js b/test/node/basic.js
index f99e2ef..0d7dc64 100644
--- a/test/node/basic.js
+++ b/test/node/basic.js
@@ -1,4 +1,6 @@
var fixtures = require('webtorrent-fixtures')
+var fs = require('fs')
+var path = require('path')
var http = require('http')
var test = require('tape')
var WebTorrent = require('../../')
@@ -157,3 +159,49 @@ test('client.add: invalid torrent id: invalid filesystem path', function (t) {
client.add('/invalid/filesystem/path/123')
})
+
+test('client.remove: opts.destroyStore', function (t) {
+ t.plan(2)
+
+ var client = new WebTorrent({ dht: false, tracker: false })
+
+ client.on('error', function (err) { t.fail(err) })
+ client.on('warning', function (err) { t.fail(err) })
+
+ client.seed(fixtures.alice.content, { name: 'alice.txt', announce: [] }, function (torrent) {
+ var torrentPath = torrent.path
+ client.remove(torrent, { destroyStore: true }, function (err) {
+ if (err) t.fail(err)
+
+ fs.stat(path.join(torrentPath, 'alice.txt'), function (err) {
+ if (err && err.code === 'ENOENT') t.pass('file deleted')
+ else t.fail('file still exists')
+
+ client.destroy(function (err) { t.error(err, 'client destroyed') })
+ })
+ })
+ })
+})
+
+test('torrent.destroy: opts.destroyStore', function (t) {
+ t.plan(2)
+
+ var client = new WebTorrent({ dht: false, tracker: false })
+
+ client.on('error', function (err) { t.fail(err) })
+ client.on('warning', function (err) { t.fail(err) })
+
+ client.seed(fixtures.alice.content, { name: 'alice.txt', announce: [] }, function (torrent) {
+ var torrentPath = torrent.path
+ torrent.destroy({ destroyStore: true }, function (err) {
+ if (err) t.fail(err)
+
+ fs.stat(path.join(torrentPath, 'alice.txt'), function (err) {
+ if (err && err.code === 'ENOENT') t.pass('file deleted')
+ else t.fail('file still exists')
+
+ client.destroy(function (err) { t.error(err, 'client destroyed') })
+ })
+ })
+ })
+})