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

error handling for duplicate torrents #351

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

error handling for duplicate torrents

  • Loading branch information
Tristan Davies
Tristan Davies committed Jun 7, 2015
commit 2cc2ab46195866ac60ef604a98793b53845680cb
@@ -168,8 +168,17 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) {
if (ontorrent) self.on('torrent', clientOnTorrent)

torrent.on('error', function (err) {
self.emit('error', err, torrent)
self.remove(torrent)
var alreadyExistsMessage = 'There is already a swarm with info hash'
if (ontorrent && err.message && err.message.substr(0, alreadyExistsMessage.length) === alreadyExistsMessage) {

This comment has been minimized.

Copy link
@madd512

madd512 Jun 7, 2015

Author

Is it okay for this line to be this long?

self.torrents.splice(self.torrents.indexOf(torrent), 1)

This comment has been minimized.

Copy link
@madd512

madd512 Jun 7, 2015

Author

can't call self.remove because it does other stuff like destroying the torrent

torrent = self.torrents.filter(function (_torrent) {
return torrent.infoHash === _torrent.infoHash
})[0]
process.nextTick(clientOnTorrent.bind(null, torrent))

This comment has been minimized.

Copy link
@madd512

madd512 Jun 7, 2015

Author

calling this immediately causes this test to fail

} else {
self.emit('error', err, torrent)
self.remove(torrent)
}
})

torrent.on('listening', function (port) {
@@ -0,0 +1,45 @@
var fs = require('fs')
var test = require('tape')
var WebTorrent = require('../')

var leavesBook = fs.readFileSync(__dirname + '/content/Leaves of Grass by Walt Whitman.epub')

test('client.seed followed by duplicate client.add', function (t) {
t.plan(2)

var opts = {
name: 'Leaves of Grass by Walt Whitman.epub'
}

var client = new WebTorrent({ dht: false, tracker: false })
client.seed(leavesBook, opts, function (torrent1) {
client.add(torrent1.infoHash, function (torrent2) {
t.equal(torrent1.infoHash, torrent2.infoHash)
t.equal(client.torrents.length, 1)
})
})
})

test('client.seed followed by duplicate client.add, twice', function (t) {
t.plan(4)

var opts = {
name: 'Leaves of Grass by Walt Whitman.epub'
}

var client = new WebTorrent({ dht: false, tracker: false })

client.seed(leavesBook, opts, function (torrent1) {
client.add(torrent1.infoHash, function (torrent2) {
t.equal(torrent1.infoHash, torrent2.infoHash)
t.equal(client.torrents.length, 1)
})
})

client.seed(leavesBook, opts, function (torrent1) {
client.add(torrent1.infoHash, function (torrent2) {
t.equal(torrent1.infoHash, torrent2.infoHash)
t.equal(client.torrents.length, 1)
})
})
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.