From cd43d50b15e17abd575b2aa1ac0b07010c84324d Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 11 Aug 2019 19:11:38 +0200 Subject: [PATCH 1/4] Fix 'left' parameter on seed --- lib/torrent.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/torrent.js b/lib/torrent.js index ef541eab..0254b87b 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -300,6 +300,14 @@ class Torrent extends EventEmitter { }) } + if (this.info) { + // if full metadata was included in initial torrent id, use it immediately. Otherwise, + // wait for torrent-discovery to find peers and ut_metadata to get the metadata. + this._onMetadata(this) + } else if (this.xs) { + this._getMetadataFromServer() + } + // begin discovering peers via DHT and trackers this.discovery = new Discovery({ infoHash: this.infoHash, @@ -334,14 +342,6 @@ class Torrent extends EventEmitter { this.discovery.on('warning', (err) => { this.emit('warning', err) }) - - if (this.info) { - // if full metadata was included in initial torrent id, use it immediately. Otherwise, - // wait for torrent-discovery to find peers and ut_metadata to get the metadata. - this._onMetadata(this) - } else if (this.xs) { - this._getMetadataFromServer() - } } _getMetadataFromServer () { From 26dcc3fb29e3d9fa4b94fe195aae7ba028684178 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 12 Aug 2019 14:06:27 +0200 Subject: [PATCH 2/4] Fix left parameter for asynchronous behaviour --- lib/torrent.js | 24 ++++++++++++++++-------- test/common.js | 13 +++++++++++++ test/node/blocklist-dht.js | 9 +++++++-- test/node/blocklist-tracker.js | 9 +++++++-- 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 test/common.js diff --git a/lib/torrent.js b/lib/torrent.js index 0254b87b..abf439b0 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -277,6 +277,19 @@ class Torrent extends EventEmitter { } _onListening () { + if (this.destroyed) return + + if (this.info) { + // if full metadata was included in initial torrent id, use it immediately. Otherwise, + // wait for torrent-discovery to find peers and ut_metadata to get the metadata. + this._onMetadata(this) + } else { + if (this.xs) this._getMetadataFromServer() + this._startDiscovery() + } + } + + _startDiscovery () { if (this.discovery || this.destroyed) return let trackerOpts = this.client.tracker @@ -300,14 +313,6 @@ class Torrent extends EventEmitter { }) } - if (this.info) { - // if full metadata was included in initial torrent id, use it immediately. Otherwise, - // wait for torrent-discovery to find peers and ut_metadata to get the metadata. - this._onMetadata(this) - } else if (this.xs) { - this._getMetadataFromServer() - } - // begin discovering peers via DHT and trackers this.discovery = new Discovery({ infoHash: this.infoHash, @@ -611,6 +616,9 @@ class Torrent extends EventEmitter { if (this.destroyed) return this._debug('on store') + // Start discovery before emitting 'ready' + this._startDiscovery() + this.ready = true this.emit('ready') diff --git a/test/common.js b/test/common.js new file mode 100644 index 00000000..9ac99a18 --- /dev/null +++ b/test/common.js @@ -0,0 +1,13 @@ +const os = require('os') +const fs = require('fs') +const path = require('path') + +exports.getTestPath = function (infix, infoHash) { + let testPath + try { + testPath = path.join(fs.statSync('/tmp') && '/tmp', 'webtorrent', 'test') + } catch (err) { + testPath = path.join(typeof os.tmpdir === 'function' ? os.tmpdir() : '/', 'webtorrent', 'test') + } + return path.join(testPath, infix, infoHash) +} diff --git a/test/node/blocklist-dht.js b/test/node/blocklist-dht.js index e9567310..d2ce9bc0 100644 --- a/test/node/blocklist-dht.js +++ b/test/node/blocklist-dht.js @@ -3,6 +3,7 @@ var fixtures = require('webtorrent-fixtures') var series = require('run-series') var test = require('tape') var WebTorrent = require('../../') +var common = require('../common') test('blocklist blocks peers discovered via DHT', function (t) { t.plan(8) @@ -25,7 +26,9 @@ test('blocklist blocks peers discovered via DHT', function (t) { client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - var torrent1 = client1.add(fixtures.leaves.parsedTorrent) + var torrent1 = client1.add(fixtures.leaves.parsedTorrent, { + path: common.getTestPath('client_1', fixtures.leaves.parsedTorrent.infoHash) + }) torrent1.on('peer', function () { t.fail('client1 should not find any peers') @@ -63,7 +66,9 @@ test('blocklist blocks peers discovered via DHT', function (t) { client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) - var torrent2 = client2.add(fixtures.leaves.parsedTorrent) + var torrent2 = client2.add(fixtures.leaves.parsedTorrent, { + path: common.getTestPath('client_2', fixtures.leaves.parsedTorrent.infoHash) + }) torrent2.on('blockedPeer', function (addr) { t.pass('client2 blocked connection to client1: ' + addr) diff --git a/test/node/blocklist-tracker.js b/test/node/blocklist-tracker.js index 302bd8f8..665ad481 100644 --- a/test/node/blocklist-tracker.js +++ b/test/node/blocklist-tracker.js @@ -3,6 +3,7 @@ var series = require('run-series') var test = require('tape') var TrackerServer = require('bittorrent-tracker/server') var WebTorrent = require('../../') +var common = require('../common') test('blocklist blocks peers discovered via tracker', function (t) { t.plan(9) @@ -38,7 +39,9 @@ test('blocklist blocks peers discovered via tracker', function (t) { client1.on('error', function (err) { t.fail(err) }) client1.on('warning', function (err) { t.fail(err) }) - var torrent1 = client1.add(parsedTorrent) + var torrent1 = client1.add(parsedTorrent, { + path: common.getTestPath('client_1', parsedTorrent.infoHash) + }) torrent1.on('invalidPeer', function () { t.pass('client1 found itself') @@ -58,7 +61,9 @@ test('blocklist blocks peers discovered via tracker', function (t) { client2.on('error', function (err) { t.fail(err) }) client2.on('warning', function (err) { t.fail(err) }) - var torrent2 = client2.add(parsedTorrent) + var torrent2 = client2.add(parsedTorrent, { + path: common.getTestPath('client_2', parsedTorrent.infoHash) + }) torrent2.once('blockedPeer', function () { t.pass('client2 blocked first peer') From 35fc44f9b98d6970244a517a5a9ca6175a3b40f5 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 12 Aug 2019 15:41:54 +0200 Subject: [PATCH 3/4] Rename util function for tests --- test/common.js | 10 +++++----- test/node/blocklist-dht.js | 4 ++-- test/node/blocklist-tracker.js | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/common.js b/test/common.js index 9ac99a18..8c306861 100644 --- a/test/common.js +++ b/test/common.js @@ -2,12 +2,12 @@ const os = require('os') const fs = require('fs') const path = require('path') -exports.getTestPath = function (infix, infoHash) { - let testPath +exports.getDownloadPath = function (infix, infoHash) { + let downloadPath try { - testPath = path.join(fs.statSync('/tmp') && '/tmp', 'webtorrent', 'test') + downloadPath = path.join(fs.statSync('/tmp') && '/tmp', 'webtorrent', 'test') } catch (err) { - testPath = path.join(typeof os.tmpdir === 'function' ? os.tmpdir() : '/', 'webtorrent', 'test') + downloadPath = path.join(typeof os.tmpdir === 'function' ? os.tmpdir() : '/', 'webtorrent', 'test') } - return path.join(testPath, infix, infoHash) + return path.join(downloadPath, infix, infoHash) } diff --git a/test/node/blocklist-dht.js b/test/node/blocklist-dht.js index d2ce9bc0..fa3915a2 100644 --- a/test/node/blocklist-dht.js +++ b/test/node/blocklist-dht.js @@ -27,7 +27,7 @@ test('blocklist blocks peers discovered via DHT', function (t) { client1.on('warning', function (err) { t.fail(err) }) var torrent1 = client1.add(fixtures.leaves.parsedTorrent, { - path: common.getTestPath('client_1', fixtures.leaves.parsedTorrent.infoHash) + path: common.getDownloadPath('client_1', fixtures.leaves.parsedTorrent.infoHash) }) torrent1.on('peer', function () { @@ -67,7 +67,7 @@ test('blocklist blocks peers discovered via DHT', function (t) { client2.on('warning', function (err) { t.fail(err) }) var torrent2 = client2.add(fixtures.leaves.parsedTorrent, { - path: common.getTestPath('client_2', fixtures.leaves.parsedTorrent.infoHash) + path: common.getDownloadPath('client_2', fixtures.leaves.parsedTorrent.infoHash) }) torrent2.on('blockedPeer', function (addr) { diff --git a/test/node/blocklist-tracker.js b/test/node/blocklist-tracker.js index 665ad481..e877d240 100644 --- a/test/node/blocklist-tracker.js +++ b/test/node/blocklist-tracker.js @@ -40,7 +40,7 @@ test('blocklist blocks peers discovered via tracker', function (t) { client1.on('warning', function (err) { t.fail(err) }) var torrent1 = client1.add(parsedTorrent, { - path: common.getTestPath('client_1', parsedTorrent.infoHash) + path: common.getDownloadPath('client_1', parsedTorrent.infoHash) }) torrent1.on('invalidPeer', function () { @@ -62,7 +62,7 @@ test('blocklist blocks peers discovered via tracker', function (t) { client2.on('warning', function (err) { t.fail(err) }) var torrent2 = client2.add(parsedTorrent, { - path: common.getTestPath('client_2', parsedTorrent.infoHash) + path: common.getDownloadPath('client_2', parsedTorrent.infoHash) }) torrent2.once('blockedPeer', function () { From 3ddb63c874c7ea24f27ff521f0ceef818f92084f Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 12 Aug 2019 15:52:33 +0200 Subject: [PATCH 4/4] Small refactor common test function --- test/common.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/common.js b/test/common.js index 8c306861..6f2ce1e1 100644 --- a/test/common.js +++ b/test/common.js @@ -3,11 +3,11 @@ const fs = require('fs') const path = require('path') exports.getDownloadPath = function (infix, infoHash) { - let downloadPath + let tmpPath try { - downloadPath = path.join(fs.statSync('/tmp') && '/tmp', 'webtorrent', 'test') + tmpPath = path.join(fs.statSync('/tmp') && '/tmp') } catch (err) { - downloadPath = path.join(typeof os.tmpdir === 'function' ? os.tmpdir() : '/', 'webtorrent', 'test') + tmpPath = path.join(typeof os.tmpdir === 'function' ? os.tmpdir() : '/') } - return path.join(downloadPath, infix, infoHash) + return path.join(tmpPath, 'webtorrent', 'test', infix, infoHash) }