diff --git a/docs/api.md b/docs/api.md index 70046e4d..3bf772af 100644 --- a/docs/api.md +++ b/docs/api.md @@ -162,17 +162,27 @@ 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. +file data. -*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` is specified, then the default options (shown below) will be overridden. + +```js +{ + remove: Boolean // Delete torrent data (default to false) +} +``` + +If `callback` is specified, it will be called when file data is removed. ## `client.destroy([function callback (err) {}])` Destroy the client, including all torrents and connections to peers. If `callback` is specified, it will be called when the client has gracefully closed. +*Note: Destroying the client does not result in the torrent data suppression. You need to explicitly call `client.remove` or implement it yourself. + ## `client.torrents[...]` An array of all torrents in the client. @@ -262,11 +272,20 @@ Number of peers in the torrent swarm. Torrent download location. -## `torrent.destroy([callback])` +## `torrent.destroy([opts], [callback])` + +Alias for `client.remove(torrent)`. + +If `opts` is specified, then the default options (shown below) will be overridden. + +```js +{ + remove: Boolean // Delete torrent data (default to false) +} +``` -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 closed. ## `torrent.addPeer(peer)` diff --git a/index.js b/index.js index ee9a8916..aa58b2c5 100644 --- a/index.js +++ b/index.js @@ -378,18 +378,20 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { * @param {string|Buffer|Torrent} torrentId * @param {function} cb */ -WebTorrent.prototype.remove = function (torrentId, cb) { +WebTorrent.prototype.remove = function (torrentId, opts, cb) { this._debug('remove') + if (typeof opts === 'function') return this.remove(torrentId, null, opts) var torrent = this.get(torrentId) if (!torrent) throw new Error('No torrent with id ' + torrentId) - this._remove(torrentId, cb) + this._remove(torrentId, opts, cb) } -WebTorrent.prototype._remove = function (torrentId, cb) { +WebTorrent.prototype._remove = function (torrentId, opts, cb) { + if (typeof opts === 'function') return this._remove(torrentId, null, opts) var torrent = this.get(torrentId) if (!torrent) return this.torrents.splice(this.torrents.indexOf(torrent), 1) - torrent.destroy(cb) + torrent.destroy(opts, cb) } WebTorrent.prototype.address = function () { diff --git a/lib/torrent.js b/lib/torrent.js index 661c30c6..db4424fe 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -71,7 +71,6 @@ function Torrent (torrentId, client, opts) { this.announce = opts.announce this.urlList = opts.urlList - this.path = opts.path this._store = opts.store || FSChunkStore this._getAnnounceOpts = opts.getAnnounceOpts @@ -637,17 +636,21 @@ Torrent.prototype._onStore = function () { self._updateSelections() } -Torrent.prototype.destroy = function (cb) { +Torrent.prototype.destroy = function (opts, cb) { var self = this - self._destroy(null, cb) + if (typeof opts === 'function') return self.destroy(null, opts) + self._destroy(null, opts, cb) } -Torrent.prototype._destroy = function (err, cb) { +Torrent.prototype._destroy = function (err, opts, cb) { var self = this + if (typeof opts === 'function') return self._destroy(err, null, opts) if (self.destroyed) return self.destroyed = true self._debug('destroy') + opts = opts ? extend(opts) : {} + self.client._remove(self) clearInterval(self._rechokeIntervalId) @@ -682,7 +685,11 @@ Torrent.prototype._destroy = function (err, cb) { if (self.store) { tasks.push(function (cb) { - self.store.close(cb) + if (self._store === FSChunkStore && opts.remove) { + self.store.destroy(cb) + } else { + self.store.close(cb) + } }) } diff --git a/test/torrent-destroy.js b/test/torrent-destroy.js index ceae2050..284ee8de 100644 --- a/test/torrent-destroy.js +++ b/test/torrent-destroy.js @@ -1,8 +1,10 @@ +var fs = require('fs') +var path = require('path') var fixtures = require('webtorrent-fixtures') var test = require('tape') var WebTorrent = require('../') -test('torrent.destroy: destroy and remove torrent', function (t) { +test('torrent.destroy: destroy torrent', function (t) { t.plan(5) var client = new WebTorrent({ dht: false, tracker: false }) @@ -22,3 +24,39 @@ test('torrent.destroy: destroy and remove torrent', function (t) { client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) + +test('torrent.destroy: seed torrent and remove it', function (t) { + t.plan(7) + + 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.leaves.content, { + name: 'Leaves of Grass by Walt Whitman.epub', + announce: [] + }, function (torrent) { + t.equal(client.torrents.length, 1) + t.equal(torrent.infoHash, fixtures.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, fixtures.leaves.magnetURI) + + var completeFileName = path.join(torrent.path, torrent.files[0].name) + + client.remove(torrent, {'remove': true}, function (err) { + t.error(err, 'torrent removed') + // Check if stat is available + if (fs.stat) { + fs.stat(completeFileName, function (err) { + if (err && err.code === 'ENOENT') return t.pass('File deleted') + t.fail('File not deleted') + }) + } else { + t.pass('File deleted') + } + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) + }) +})