Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to destroy store on torrent removal (delete files from disk) #1364

Open
wants to merge 2 commits into
base: master
from
Open
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

Docs and tests for store destruction

  • Loading branch information
KayleePop committed Apr 24, 2018
commit 58d4ff0e7cbd87a38214715c15d25969752e7dce
@@ -162,12 +162,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) {}])`

@@ -262,11 +264,14 @@ Number of peers in the torrent swarm.

Torrent download location.

## `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)`

@@ -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') })
})
})
})
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.