From 4d6fa88a9ec5731fb649d14490123b0e421caa74 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 9 Dec 2015 22:00:34 -0800 Subject: [PATCH 1/9] Improve torrent.createServer() index page MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Download link uses correct filename - List shows file paths, not just name - Add file size - Show torrent name at top, instead of generic “WebTorrent” title --- lib/server.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/server.js b/lib/server.js index 540794e1..dd9a5292 100644 --- a/lib/server.js +++ b/lib/server.js @@ -3,6 +3,7 @@ module.exports = Server var debug = require('debug')('webtorrent:server') var http = require('http') var mime = require('mime') +var prettyBytes = require('pretty-bytes') var pump = require('pump') var rangeParser = require('range-parser') var url = require('url') @@ -58,9 +59,12 @@ function Server (torrent, opts) { if (pathname === '/') { res.setHeader('Content-Type', 'text/html') var listHtml = torrent.files.map(function (file, i) { - return '
  • ' + file.name + '
  • ' + return '
  • ' + file.path + ' ' + + '(' + prettyBytes(file.length) + ')
  • ' }).join('
    ') - return res.end('

    WebTorrent

      ' + listHtml + '
    ') + + var html = '

    ' + torrent.name + '

      ' + listHtml + '
    ' + return res.end(html) } var index = Number(pathname.slice(1)) From 992caf9650a1f6821006387dfaf58f4004b17005 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 9 Dec 2015 22:01:17 -0800 Subject: [PATCH 2/9] improve torrent.createServer() test - check that index page works - wait for destroy --- test/server.js | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/test/server.js b/test/server.js index dfa1730c..bc82cdcc 100644 --- a/test/server.js +++ b/test/server.js @@ -1,41 +1,49 @@ -var path = require('path') var fs = require('fs') var get = require('simple-get') +var path = require('path') var test = require('tape') var WebTorrent = require('../') var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -test('start http server programmatically', function (t) { - t.plan(4) +test('torrent.createServer(): programmatic http server', function (t) { + t.plan(9) var client = new WebTorrent({ tracker: false, dht: false }) - client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add(leavesTorrent, { dht: false, tracker: false }, function (torrent) { + client.add(leavesTorrent, function (torrent) { t.pass('got "torrent" event') - var server = torrent.createServer() + server.listen(0, function () { - t.pass('server is listening') var port = server.address().port - get.concat('http://localhost:' + port + '/0', function (err, data) { - if (err) throw err - // Verify data for first (and only file) - t.deepEqual(data, fs.readFileSync(leavesPath)) + t.pass('server is listening on ' + port) - server.close() - client.destroy() + // Seeding after server is created should work + torrent.load(fs.createReadStream(leavesPath), function (err) { + t.error(err, 'loaded seed content into torrent') + }) + + var host = 'http://localhost:' + port + + // Index page should list files in the torrent + get.concat(host + '/', function (err, data) { + t.error(err) + data = data.toString() + t.ok(data.indexOf('Leaves of Grass by Walt Whitman.epub') !== -1) + + // Verify file content for first (and only) file + get.concat(host + '/0', function (err, data) { + t.error(err) + t.deepEqual(data, fs.readFileSync(leavesPath)) + + server.close(function () { t.pass('server closed') }) + client.destroy(function () { t.pass('client destroyed') }) + }) }) - }) - }) - torrent.on('ready', function () { - torrent.load(fs.createReadStream(leavesPath), function (err) { - if (err) throw err - t.pass('loaded seed content into torrent') }) }) }) From dfdf52b747201cdb9f616a2b1f0831e96b250ab6 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 9 Dec 2015 22:31:19 -0800 Subject: [PATCH 3/9] test: put fixtures into single location --- test/basic-node.js | 36 +++++++------------- test/basic.js | 84 +++++++++++++++++++++------------------------- test/common.js | 34 +++++++++++++++++++ test/server.js | 11 +++--- 4 files changed, 89 insertions(+), 76 deletions(-) create mode 100644 test/common.js diff --git a/test/basic-node.js b/test/basic-node.js index cff40ebd..d776a52d 100644 --- a/test/basic-node.js +++ b/test/basic-node.js @@ -1,24 +1,14 @@ -var WebTorrent = require('../') -var fs = require('fs') +var common = require('./common') var http = require('http') -var path = require('path') -var parseTorrent = require('parse-torrent') var test = require('tape') - -var leavesPath = path.resolve(__dirname, 'torrents', 'leaves.torrent') -var leaves = fs.readFileSync(leavesPath) -var leavesTorrent = parseTorrent(leaves) -var leavesBookPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') -var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80' -var numbersPath = path.resolve(__dirname, 'content', 'numbers') -var folderPath = path.resolve(__dirname, 'content', 'folder') +var WebTorrent = require('../') test('client.add: http url to a torrent file, string', function (t) { t.plan(3) var server = http.createServer(function (req, res) { t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1) - res.end(leaves) + res.end(common.leaves.torrent) }) server.listen(0, function () { @@ -30,8 +20,8 @@ test('client.add: http url to a torrent file, string', function (t) { client.on('warning', function (err) { t.fail(err) }) client.add(url, function (torrent) { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.equal(torrent.magnetURI, leavesMagnetURI) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) client.destroy() server.close() }) @@ -46,9 +36,9 @@ test('client.add: filesystem path to a torrent file, string', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.add(leavesPath, function (torrent) { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.equal(torrent.magnetURI, leavesMagnetURI) + client.add(common.leaves.torrentPath, function (torrent) { + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) client.destroy() }) }) @@ -73,9 +63,9 @@ test('client.seed: filesystem path to file, string', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.seed(leavesBookPath, opts, function (torrent) { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.equal(torrent.magnetURI, leavesMagnetURI) + client.seed(common.leaves.contentPath, opts, function (torrent) { + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) client.destroy() }) }) @@ -99,7 +89,7 @@ test('client.seed: filesystem path to folder with one file, string', function (t client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.seed(folderPath, opts, function (torrent) { + client.seed(common.folder.contentPath, opts, function (torrent) { t.equal(torrent.infoHash, '3a686c32404af0a66913dd5f8d2b40673f8d4490') t.equal(torrent.magnetURI, 'magnet:?xt=urn:btih:3a686c32404af0a66913dd5f8d2b40673f8d4490&dn=folder&tr=udp%3A%2F%2Ftracker.webtorrent.io%3A80') client.destroy() @@ -125,7 +115,7 @@ test('client.seed: filesystem path to folder with multiple files, string', funct client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.seed(numbersPath, opts, function (torrent) { + client.seed(common.numbers.contentPath, opts, function (torrent) { t.equal(torrent.infoHash, '80562f38656b385ea78959010e51a2cc9db41ea0') t.equal(torrent.magnetURI, 'magnet:?xt=urn:btih:80562f38656b385ea78959010e51a2cc9db41ea0&dn=numbers&tr=udp%3A%2F%2Ftracker.webtorrent.io%3A80') client.destroy() diff --git a/test/basic.js b/test/basic.js index 4fedcb88..3a19e4c7 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,28 +1,20 @@ -var path = require('path') -var fs = require('fs') +var common = require('./common') var extend = require('xtend') -var parseTorrent = require('parse-torrent') var test = require('tape') var WebTorrent = require('../') -var leaves = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesTorrent = parseTorrent(leaves) -var leavesBook = fs.readFileSync(path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')) - -var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80' - test('client.add/remove: magnet uri, utf-8 string', function (t) { var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) + var torrent = client.add('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) torrent.on('infoHash', function () { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) === 0) - client.remove('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) === 0) + client.remove('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 0) client.destroy() t.end() @@ -35,12 +27,12 @@ test('client.add/remove: torrent file, buffer', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add(leaves) + var torrent = client.add(common.leaves.torrent) t.equal(client.torrents.length, 1) torrent.on('infoHash', function () { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.equal(torrent.magnetURI, leavesMagnetURI) - client.remove(leaves) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) + client.remove(common.leaves.torrent) t.equal(client.torrents.length, 0) client.destroy() t.end() @@ -53,12 +45,12 @@ test('client.add/remove: info hash, hex string', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add(leavesTorrent.infoHash) + var torrent = client.add(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) torrent.on('infoHash', function () { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) === 0) - client.remove(leavesTorrent.infoHash) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) === 0) + client.remove(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 0) client.destroy() t.end() @@ -71,12 +63,12 @@ test('client.add/remove: info hash, buffer', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add(new Buffer(leavesTorrent.infoHash, 'hex')) + var torrent = client.add(common.leaves.parsedTorrent.infoHashBuffer) t.equal(client.torrents.length, 1) torrent.on('infoHash', function () { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) === 0) - client.remove(new Buffer(leavesTorrent.infoHash, 'hex')) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) === 0) + client.remove(new Buffer(common.leaves.parsedTorrent.infoHash, 'hex')) t.equal(client.torrents.length, 0) client.destroy() t.end() @@ -89,12 +81,12 @@ test('client.add/remove: parsed torrent, from `parse-torrent`', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add(leavesTorrent) + var torrent = client.add(common.leaves.parsedTorrent) t.equal(client.torrents.length, 1) torrent.on('infoHash', function () { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.equal(torrent.magnetURI, leavesMagnetURI) - client.remove(leavesTorrent) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) + client.remove(common.leaves.parsedTorrent) t.equal(client.torrents.length, 0) client.destroy() t.end() @@ -107,14 +99,14 @@ test('client.add/remove: parsed torrent, with string type announce property', fu client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var modifiedParsedTorrent = extend(leavesTorrent, { - announce: leavesTorrent.announce[0] + var modifiedParsedTorrent = extend(common.leaves.parsedTorrent, { + announce: common.leaves.parsedTorrent.announce[0] }) var torrent = client.add(modifiedParsedTorrent) t.equal(client.torrents.length, 1) torrent.on('infoHash', function () { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - client.remove(leavesTorrent) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + client.remove(common.leaves.parsedTorrent) t.equal(client.torrents.length, 0) client.destroy() t.end() @@ -127,10 +119,10 @@ test('client.remove: remove by Torrent object', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add(leavesTorrent.infoHash) + var torrent = client.add(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) torrent.on('infoHash', function () { - t.equal(torrent.infoHash, leavesTorrent.infoHash) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) client.remove(torrent) t.equal(client.torrents.length, 0) client.destroy() @@ -144,10 +136,10 @@ test('torrent.destroy: destroy and remove torrent', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add(leavesTorrent.infoHash) + var torrent = client.add(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) torrent.on('infoHash', function () { - t.equal(torrent.infoHash, leavesTorrent.infoHash) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) torrent.destroy() t.equal(client.torrents.length, 0) client.destroy() @@ -175,10 +167,10 @@ test('client.seed: torrent file (Buffer)', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.seed(leavesBook, opts, function (torrent) { + client.seed(common.leaves.content, opts, function (torrent) { t.equal(client.torrents.length, 1) - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.equal(torrent.magnetURI, leavesMagnetURI) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) client.remove(torrent) t.equal(client.torrents.length, 0) client.destroy() @@ -205,10 +197,10 @@ test('client.seed: torrent file (Blob)', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.seed(new global.Blob([ leavesBook ]), opts, function (torrent) { + client.seed(new global.Blob([ common.leaves.content ]), opts, function (torrent) { t.equal(client.torrents.length, 1) - t.equal(torrent.infoHash, leavesTorrent.infoHash) - t.equal(torrent.magnetURI, leavesMagnetURI) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) client.remove(torrent) t.equal(client.torrents.length, 0) client.destroy() @@ -231,7 +223,7 @@ test('after client.destroy(), throw on client.add() or client.seed()', function t.pass('client destroyed') }) t.throws(function () { - client.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) + client.add('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) }) t.throws(function () { client.seed(new Buffer('sup')) @@ -246,10 +238,10 @@ test('after client.destroy(), no "torrent" or "ready" events emitted', function client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.add(leaves, { name: 'leaves' }, function () { + client.add(common.leaves.torrent, { name: 'leaves' }, function () { t.fail('unexpected "torrent" event (from add)') }) - client.seed(leavesBook, { name: 'leavesBook' }, function () { + client.seed(common.leaves.content, { name: 'leaves' }, function () { t.fail('unexpected "torrent" event (from seed)') }) client.on('ready', function () { diff --git a/test/common.js b/test/common.js new file mode 100644 index 00000000..691d1785 --- /dev/null +++ b/test/common.js @@ -0,0 +1,34 @@ +var fs = require('fs') +var path = require('path') +var parseTorrent = require('parse-torrent') + +var content = path.join(__dirname, 'content') +var torrents = path.join(__dirname, 'torrents') + +module.exports = { + // Leaves of Grass by Walt Whitman.epub + leaves: { + contentPath: path.join(content, 'Leaves of Grass by Walt Whitman.epub'), + torrentPath: path.join(torrents, 'leaves.torrent'), + content: fs.readFileSync( + path.join(content, 'Leaves of Grass by Walt Whitman.epub') + ), + torrent: fs.readFileSync(path.join(torrents, 'leaves.torrent')), + parsedTorrent: parseTorrent( + fs.readFileSync(path.join(torrents, 'leaves.torrent')) + ), + magnetURI: parseTorrent.toMagnetURI( + parseTorrent(fs.readFileSync(path.join(torrents, 'leaves.torrent'))) + ) + }, + + // Folder which contains single file + folder: { + contentPath: path.join(content, 'folder') + }, + + // Folder which contains multiple files + numbers: { + contentPath: path.join(content, 'numbers') + } +} diff --git a/test/server.js b/test/server.js index bc82cdcc..8aae2681 100644 --- a/test/server.js +++ b/test/server.js @@ -1,12 +1,9 @@ +var common = require('./common') var fs = require('fs') var get = require('simple-get') -var path = require('path') var test = require('tape') var WebTorrent = require('../') -var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) - test('torrent.createServer(): programmatic http server', function (t) { t.plan(9) @@ -14,7 +11,7 @@ test('torrent.createServer(): programmatic http server', function (t) { client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.add(leavesTorrent, function (torrent) { + client.add(common.leaves.torrent, function (torrent) { t.pass('got "torrent" event') var server = torrent.createServer() @@ -23,7 +20,7 @@ test('torrent.createServer(): programmatic http server', function (t) { t.pass('server is listening on ' + port) // Seeding after server is created should work - torrent.load(fs.createReadStream(leavesPath), function (err) { + torrent.load(fs.createReadStream(common.leaves.contentPath), function (err) { t.error(err, 'loaded seed content into torrent') }) @@ -38,7 +35,7 @@ test('torrent.createServer(): programmatic http server', function (t) { // Verify file content for first (and only) file get.concat(host + '/0', function (err, data) { t.error(err) - t.deepEqual(data, fs.readFileSync(leavesPath)) + t.deepEqual(data, common.leaves.content) server.close(function () { t.pass('server closed') }) client.destroy(function () { t.pass('client destroyed') }) From e47b9a2a86db54fc3db7dd00c964d25b8203f71e Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 9 Dec 2015 22:56:40 -0800 Subject: [PATCH 4/9] Change default "created by" to match uTorrent style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of “WebTorrent/0.63.0”, it’s now “WebTorrent/0063”. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 72d6f7ab..edb4b595 100644 --- a/index.js +++ b/index.js @@ -205,7 +205,7 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { // When seeding from filesystem, initialize store from that path (avoids a copy) if (typeof input === 'string') opts.path = path.dirname(input) - if (!opts.createdBy) opts.createdBy = 'WebTorrent/' + VERSION + if (!opts.createdBy) opts.createdBy = 'WebTorrent/' + VERSION_STR var streams var torrent = self.add(undefined, opts, function (torrent) { From 4a759a59e2f127e0cded044dcd647084d1b8c26f Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 9 Dec 2015 23:57:35 -0800 Subject: [PATCH 5/9] don't add default trackers when { tracker: false } MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the user sets `{ tracker: false }` on the WebTorrent client, don’t add trackers to the torrent by default. --- index.js | 1 + lib/torrent.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index edb4b595..b6cf7334 100644 --- a/index.js +++ b/index.js @@ -206,6 +206,7 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { // When seeding from filesystem, initialize store from that path (avoids a copy) if (typeof input === 'string') opts.path = path.dirname(input) if (!opts.createdBy) opts.createdBy = 'WebTorrent/' + VERSION_STR + if (!self.tracker) opts.announce = [] var streams var torrent = self.add(undefined, opts, function (torrent) { diff --git a/lib/torrent.js b/lib/torrent.js index ccf77820..a54bd1dc 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -234,12 +234,12 @@ Torrent.prototype._processParsedTorrent = function (parsedTorrent) { parsedTorrent.announce = parsedTorrent.announce.concat(this.announce) } - if (global.WEBTORRENT_ANNOUNCE) { + if (this.client.tracker && global.WEBTORRENT_ANNOUNCE) { // So `webtorrent-hybrid` can force specific trackers to be used parsedTorrent.announce = parsedTorrent.announce.concat(global.WEBTORRENT_ANNOUNCE) } - if (parsedTorrent.announce.length === 0) { + if (this.client.tracker && parsedTorrent.announce.length === 0) { // When no trackers specified, use some reasonable defaults parsedTorrent.announce = createTorrent.announceList.map(function (list) { return list[0] From e31197d5cd9cd1b923cd56bc59d45a51b671ae1c Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 9 Dec 2015 23:59:52 -0800 Subject: [PATCH 6/9] tests: lots of cleanup --- index.js | 2 +- test/basic-node.js | 97 +++++++------- test/basic.js | 218 ++++++++++++++++++------------- test/blocklist-dht.js | 2 +- test/blocklist-tracker.js | 2 +- test/blocklist.js | 2 +- test/cmd.js | 4 +- test/common.js | 26 +++- test/download-dht-magnet.js | 2 +- test/download-dht-torrent.js | 2 +- test/download-private-dht.js | 2 +- test/download-tracker-magnet.js | 2 +- test/download-tracker-torrent.js | 2 +- test/download-webseed-torrent.js | 9 +- test/duplicates.js | 2 +- test/extensions.js | 2 +- test/metadata.js | 2 +- test/server.js | 3 +- test/torrents/folder.torrent | 1 + test/torrents/leaves.torrent | Bin 994 -> 639 bytes test/torrents/numbers.torrent | 1 + test/torrents/pride.torrent | Bin 3666 -> 2150 bytes test/torrents/sintel-5gb.torrent | Bin 26727 -> 26474 bytes 23 files changed, 216 insertions(+), 167 deletions(-) create mode 100644 test/torrents/folder.torrent create mode 100644 test/torrents/numbers.torrent diff --git a/index.js b/index.js index b6cf7334..c61a82af 100644 --- a/index.js +++ b/index.js @@ -10,9 +10,9 @@ var inherits = require('inherits') var loadIPSet = require('load-ip-set') // browser exclude var parallel = require('run-parallel') var parseTorrent = require('parse-torrent') +var path = require('path') var speedometer = require('speedometer') var zeroFill = require('zero-fill') -var path = require('path') var Torrent = require('./lib/torrent') diff --git a/test/basic-node.js b/test/basic-node.js index d776a52d..a33cde14 100644 --- a/test/basic-node.js +++ b/test/basic-node.js @@ -4,7 +4,7 @@ var test = require('tape') var WebTorrent = require('../') test('client.add: http url to a torrent file, string', function (t) { - t.plan(3) + t.plan(8) var server = http.createServer(function (req, res) { t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1) @@ -20,16 +20,21 @@ test('client.add: http url to a torrent file, string', function (t) { client.on('warning', function (err) { t.fail(err) }) client.add(url, function (torrent) { + t.equal(client.torrents.length, 1) t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.destroy() - server.close() + + client.remove(torrent, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + server.close(function () { t.pass('http server closed') }) + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) }) test('client.add: filesystem path to a torrent file, string', function (t) { - t.plan(2) + t.plan(6) var client = new WebTorrent({ dht: false, tracker: false }) @@ -37,87 +42,75 @@ test('client.add: filesystem path to a torrent file, string', function (t) { client.on('warning', function (err) { t.fail(err) }) client.add(common.leaves.torrentPath, function (torrent) { + t.equal(client.torrents.length, 1) t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.destroy() + + client.remove(torrent, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('client.seed: filesystem path to file, string', function (t) { - t.plan(2) - - var opts = { - name: 'Leaves of Grass by Walt Whitman.epub', - announce: [ - 'http://tracker.thepiratebay.org/announce', - 'udp://tracker.openbittorrent.com:80', - 'udp://tracker.ccc.de:80', - 'udp://tracker.publicbt.com:80', - 'udp://fr33domtracker.h33t.com:3310/announce', - 'http://tracker.bittorrent.am/announce' - ] - } + t.plan(6) 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(common.leaves.contentPath, opts, function (torrent) { + client.seed(common.leaves.contentPath, { + name: 'Leaves of Grass by Walt Whitman.epub' + }, function (torrent) { + t.equal(client.torrents.length, 1) t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.destroy() + + client.remove(torrent, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('client.seed: filesystem path to folder with one file, string', function (t) { - t.plan(2) - - var opts = { - pieceLength: 32768, // force piece length to 32KB so info-hash will - // match what transmission generated, since we use - // a different algo for picking piece length - - private: false, // also force `private: false` to match transmission - announce: [ - 'udp://tracker.webtorrent.io:80' - ] - } + t.plan(6) 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(common.folder.contentPath, opts, function (torrent) { - t.equal(torrent.infoHash, '3a686c32404af0a66913dd5f8d2b40673f8d4490') - t.equal(torrent.magnetURI, 'magnet:?xt=urn:btih:3a686c32404af0a66913dd5f8d2b40673f8d4490&dn=folder&tr=udp%3A%2F%2Ftracker.webtorrent.io%3A80') - client.destroy() + client.seed(common.folder.contentPath, function (torrent) { + t.equal(client.torrents.length, 1) + t.equal(torrent.infoHash, common.folder.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.folder.magnetURI) + + client.remove(torrent, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('client.seed: filesystem path to folder with multiple files, string', function (t) { - t.plan(2) - - var opts = { - pieceLength: 32768, // force piece length to 32KB so info-hash will - // match what transmission generated, since we use - // a different algo for picking piece length - - private: false, // also force `private: false` to match transmission - announce: [ - 'udp://tracker.webtorrent.io:80' - ] - } + t.plan(6) 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(common.numbers.contentPath, opts, function (torrent) { - t.equal(torrent.infoHash, '80562f38656b385ea78959010e51a2cc9db41ea0') - t.equal(torrent.magnetURI, 'magnet:?xt=urn:btih:80562f38656b385ea78959010e51a2cc9db41ea0&dn=numbers&tr=udp%3A%2F%2Ftracker.webtorrent.io%3A80') - client.destroy() + client.seed(common.numbers.contentPath, function (torrent) { + t.equal(client.torrents.length, 1) + t.equal(torrent.infoHash, common.numbers.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.numbers.magnetURI) + + client.remove(torrent, function (err) { t.error(err, 'torrent destroyed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) diff --git a/test/basic.js b/test/basic.js index 3a19e4c7..64472b47 100644 --- a/test/basic.js +++ b/test/basic.js @@ -1,27 +1,35 @@ +/* global Blob */ + var common = require('./common') var extend = require('xtend') var test = require('tape') var WebTorrent = require('../') -test('client.add/remove: magnet uri, utf-8 string', function (t) { +test('client.add: magnet uri, utf-8 string', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var torrent = client.add('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) + var torrent = client.add(common.leaves.magnetURI) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) === 0) - client.remove('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) + + client.remove(common.leaves.magnetURI, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: torrent file, buffer', function (t) { +test('client.add: torrent file, buffer', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -29,17 +37,21 @@ test('client.add/remove: torrent file, buffer', function (t) { var torrent = client.add(common.leaves.torrent) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.remove(common.leaves.torrent) + + client.remove(common.leaves.torrent, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: info hash, hex string', function (t) { +test('client.add: info hash, hex string', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -47,17 +59,21 @@ test('client.add/remove: info hash, hex string', function (t) { var torrent = client.add(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) === 0) - client.remove(common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, 'magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) + + client.remove(common.leaves.parsedTorrent.infoHash, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: info hash, buffer', function (t) { +test('client.add: info hash, buffer', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -65,17 +81,21 @@ test('client.add/remove: info hash, buffer', function (t) { var torrent = client.add(common.leaves.parsedTorrent.infoHashBuffer) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.ok(torrent.magnetURI.indexOf('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) === 0) - client.remove(new Buffer(common.leaves.parsedTorrent.infoHash, 'hex')) + + client.remove(new Buffer(common.leaves.parsedTorrent.infoHash, 'hex'), function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: parsed torrent, from `parse-torrent`', function (t) { +test('client.add: parsed torrent, from `parse-torrent`', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -83,37 +103,46 @@ test('client.add/remove: parsed torrent, from `parse-torrent`', function (t) { var torrent = client.add(common.leaves.parsedTorrent) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.remove(common.leaves.parsedTorrent) + + client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) -test('client.add/remove: parsed torrent, with string type announce property', function (t) { +test('client.add: parsed torrent, with string type announce property', function (t) { + t.plan(6) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - var modifiedParsedTorrent = extend(common.leaves.parsedTorrent, { - announce: common.leaves.parsedTorrent.announce[0] - }) - var torrent = client.add(modifiedParsedTorrent) + var parsedTorrent = extend(common.leaves.parsedTorrent) + parsedTorrent.announce = 'http://tracker.local:80' + + var torrent = client.add(parsedTorrent) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - client.remove(common.leaves.parsedTorrent) + t.equal(torrent.magnetURI, common.leaves.magnetURI + '&tr=' + encodeURIComponent('http://tracker.local:80')) + + client.remove(common.leaves.parsedTorrent, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('client.remove: remove by Torrent object', function (t) { + t.plan(5) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -121,16 +150,20 @@ test('client.remove: remove by Torrent object', function (t) { var torrent = client.add(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - client.remove(torrent) + + client.remove(torrent, function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('torrent.destroy: destroy and remove torrent', function (t) { + t.plan(5) + var client = new WebTorrent({ dht: false, tracker: false }) client.on('error', function (err) { t.fail(err) }) @@ -138,77 +171,84 @@ test('torrent.destroy: destroy and remove torrent', function (t) { var torrent = client.add(common.leaves.parsedTorrent.infoHash) t.equal(client.torrents.length, 1) + torrent.on('infoHash', function () { t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - torrent.destroy() + + torrent.destroy(function (err) { t.error(err, 'torrent destroyed') }) t.equal(client.torrents.length, 0) - client.destroy() - t.end() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('client.seed: torrent file (Buffer)', function (t) { - t.plan(4) - - var opts = { - name: 'Leaves of Grass by Walt Whitman.epub', - announce: [ - 'http://tracker.thepiratebay.org/announce', - 'udp://tracker.openbittorrent.com:80', - 'udp://tracker.ccc.de:80', - 'udp://tracker.publicbt.com:80', - 'udp://fr33domtracker.h33t.com:3310/announce', - 'http://tracker.bittorrent.am/announce' - ] - } + t.plan(6) 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(common.leaves.content, opts, function (torrent) { + client.seed(common.leaves.content, { + name: 'Leaves of Grass by Walt Whitman.epub' + }, function (torrent) { t.equal(client.torrents.length, 1) t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.remove(torrent) + + client.remove(torrent, function (err) { t.error(err, 'torrent removed') }) t.equal(client.torrents.length, 0) - client.destroy() + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) +}) + +test('client.seed: torrent file (Buffer), set name on buffer', function (t) { + t.plan(6) + + var client = new WebTorrent({ dht: false, tracker: false }) + + client.on('error', function (err) { t.fail(err) }) + client.on('warning', function (err) { t.fail(err) }) + + var buf = new Buffer(common.leaves.content) + buf.name = 'Leaves of Grass by Walt Whitman.epub' + + client.seed(common.leaves.content, function (torrent) { + t.equal(client.torrents.length, 1) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) + + client.remove(torrent, function (err) { t.error(err, 'torrent removed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) }) test('client.seed: torrent file (Blob)', function (t) { - var opts = { - name: 'Leaves of Grass by Walt Whitman.epub', - announce: [ - 'http://tracker.thepiratebay.org/announce', - 'udp://tracker.openbittorrent.com:80', - 'udp://tracker.ccc.de:80', - 'udp://tracker.publicbt.com:80', - 'udp://fr33domtracker.h33t.com:3310/announce', - 'http://tracker.bittorrent.am/announce' - ] - } - - if (global.Blob !== undefined) { - t.plan(4) - 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(new global.Blob([ common.leaves.content ]), opts, function (torrent) { - t.equal(client.torrents.length, 1) - t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) - t.equal(torrent.magnetURI, common.leaves.magnetURI) - client.remove(torrent) - t.equal(client.torrents.length, 0) - client.destroy() - }) - } else { - t.pass('Skipping Blob test because missing `Blob` constructor') - t.end() - } + if (typeof Blob === 'undefined') return t.end() + + t.plan(6) + + 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(new global.Blob([ common.leaves.content ]), { + name: 'Leaves of Grass by Walt Whitman.epub' + }, function (torrent) { + t.equal(client.torrents.length, 1) + t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash) + t.equal(torrent.magnetURI, common.leaves.magnetURI) + + client.remove(torrent, function (err) { t.error(err, 'torrent removed') }) + t.equal(client.torrents.length, 0) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) + }) }) test('after client.destroy(), throw on client.add() or client.seed()', function (t) { @@ -219,9 +259,8 @@ test('after client.destroy(), throw on client.add() or client.seed()', function client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) - client.destroy(function () { - t.pass('client destroyed') - }) + client.destroy(function (err) { t.error(err, 'client destroyed') }) + t.throws(function () { client.add('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash) }) @@ -247,7 +286,6 @@ test('after client.destroy(), no "torrent" or "ready" events emitted', function client.on('ready', function () { t.fail('unexpected "ready" event') }) - client.destroy(function () { - t.pass('client destroyed') - }) + + client.destroy(function (err) { t.error(err, 'client destroyed') }) }) diff --git a/test/blocklist-dht.js b/test/blocklist-dht.js index 60ec4fd7..e437f27e 100644 --- a/test/blocklist-dht.js +++ b/test/blocklist-dht.js @@ -1,9 +1,9 @@ var auto = require('run-auto') var DHT = require('bittorrent-dht/server') -var path = require('path') var fs = require('fs') var networkAddress = require('network-address') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var WebTorrent = require('../') diff --git a/test/blocklist-tracker.js b/test/blocklist-tracker.js index 1db8415f..39269a15 100644 --- a/test/blocklist-tracker.js +++ b/test/blocklist-tracker.js @@ -1,7 +1,7 @@ var auto = require('run-auto') -var path = require('path') var fs = require('fs') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var TrackerServer = require('bittorrent-tracker/server') var WebTorrent = require('../') diff --git a/test/blocklist.js b/test/blocklist.js index 73ec8156..2f4e165c 100644 --- a/test/blocklist.js +++ b/test/blocklist.js @@ -1,7 +1,7 @@ -var path = require('path') var fs = require('fs') var http = require('http') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var WebTorrent = require('../') var zlib = require('zlib') diff --git a/test/cmd.js b/test/cmd.js index 5699a0e6..461b54df 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -1,8 +1,8 @@ var cp = require('child_process') -var spawn = require('cross-spawn-async') -var path = require('path') var fs = require('fs') var parseTorrent = require('parse-torrent') +var path = require('path') +var spawn = require('cross-spawn-async') var test = require('tape') var CMD_PATH = path.resolve(__dirname, '..', 'bin', 'cmd.js') diff --git a/test/common.js b/test/common.js index 691d1785..b68b0f4b 100644 --- a/test/common.js +++ b/test/common.js @@ -17,18 +17,34 @@ module.exports = { parsedTorrent: parseTorrent( fs.readFileSync(path.join(torrents, 'leaves.torrent')) ), - magnetURI: parseTorrent.toMagnetURI( - parseTorrent(fs.readFileSync(path.join(torrents, 'leaves.torrent'))) - ) + magnetURI: parseTorrent.toMagnetURI(parseTorrent( + fs.readFileSync(path.join(torrents, 'leaves.torrent')) + )) }, // Folder which contains single file folder: { - contentPath: path.join(content, 'folder') + contentPath: path.join(content, 'folder'), + torrentPath: path.join(torrents, 'folder.torrent'), + torrent: fs.readFileSync(path.join(torrents, 'folder.torrent')), + parsedTorrent: parseTorrent( + fs.readFileSync(path.join(torrents, 'folder.torrent')) + ), + magnetURI: parseTorrent.toMagnetURI(parseTorrent( + fs.readFileSync(path.join(torrents, 'folder.torrent')) + )) }, // Folder which contains multiple files numbers: { - contentPath: path.join(content, 'numbers') + contentPath: path.join(content, 'numbers'), + torrentPath: path.join(torrents, 'numbers.torrent'), + torrent: fs.readFileSync(path.join(torrents, 'numbers.torrent')), + parsedTorrent: parseTorrent( + fs.readFileSync(path.join(torrents, 'numbers.torrent')) + ), + magnetURI: parseTorrent.toMagnetURI(parseTorrent( + fs.readFileSync(path.join(torrents, 'numbers.torrent')) + )) } } diff --git a/test/download-dht-magnet.js b/test/download-dht-magnet.js index cdd99acb..b9c08690 100644 --- a/test/download-dht-magnet.js +++ b/test/download-dht-magnet.js @@ -1,8 +1,8 @@ var auto = require('run-auto') var DHT = require('bittorrent-dht/server') -var path = require('path') var fs = require('fs') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var WebTorrent = require('../') diff --git a/test/download-dht-torrent.js b/test/download-dht-torrent.js index 19e8b563..99856298 100644 --- a/test/download-dht-torrent.js +++ b/test/download-dht-torrent.js @@ -1,8 +1,8 @@ var auto = require('run-auto') var DHT = require('bittorrent-dht/server') -var path = require('path') var fs = require('fs') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var WebTorrent = require('../') diff --git a/test/download-private-dht.js b/test/download-private-dht.js index 56537556..63ad74c1 100644 --- a/test/download-private-dht.js +++ b/test/download-private-dht.js @@ -1,8 +1,8 @@ var auto = require('run-auto') var DHT = require('bittorrent-dht/server') -var path = require('path') var fs = require('fs') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var WebTorrent = require('../') diff --git a/test/download-tracker-magnet.js b/test/download-tracker-magnet.js index 445c23c8..b4c89a23 100644 --- a/test/download-tracker-magnet.js +++ b/test/download-tracker-magnet.js @@ -1,7 +1,7 @@ var auto = require('run-auto') -var path = require('path') var fs = require('fs') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var TrackerServer = require('bittorrent-tracker/server') var WebTorrent = require('../') diff --git a/test/download-tracker-torrent.js b/test/download-tracker-torrent.js index 438450ac..fdbf559e 100644 --- a/test/download-tracker-torrent.js +++ b/test/download-tracker-torrent.js @@ -1,7 +1,7 @@ var auto = require('run-auto') -var path = require('path') var fs = require('fs') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var TrackerServer = require('bittorrent-tracker/server') var WebTorrent = require('../') diff --git a/test/download-webseed-torrent.js b/test/download-webseed-torrent.js index 3a5c1972..7cbc8895 100644 --- a/test/download-webseed-torrent.js +++ b/test/download-webseed-torrent.js @@ -1,14 +1,13 @@ var auto = require('run-auto') +var finalhandler = require('finalhandler') var fs = require('fs') +var http = require('http') var parseTorrent = require('parse-torrent') +var path = require('path') +var serveStatic = require('serve-static') var test = require('tape') var WebTorrent = require('../') -var http = require('http') -var serveStatic = require('serve-static') -var finalhandler = require('finalhandler') -var path = require('path') - var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub') var leavesFilename = 'Leaves of Grass by Walt Whitman.epub' var leavesFile = fs.readFileSync(leavesPath) diff --git a/test/duplicates.js b/test/duplicates.js index 6c1cbc95..a89f649a 100644 --- a/test/duplicates.js +++ b/test/duplicates.js @@ -1,5 +1,5 @@ -var path = require('path') var fs = require('fs') +var path = require('path') var test = require('tape') var WebTorrent = require('../') diff --git a/test/extensions.js b/test/extensions.js index 14ea63b1..82943d75 100644 --- a/test/extensions.js +++ b/test/extensions.js @@ -1,6 +1,6 @@ -var path = require('path') var fs = require('fs') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var WebTorrent = require('../') diff --git a/test/metadata.js b/test/metadata.js index eb205f3e..efcfbab2 100644 --- a/test/metadata.js +++ b/test/metadata.js @@ -1,6 +1,6 @@ -var path = require('path') var fs = require('fs') var parseTorrent = require('parse-torrent') +var path = require('path') var test = require('tape') var WebTorrent = require('../') diff --git a/test/server.js b/test/server.js index 8aae2681..8571a9ea 100644 --- a/test/server.js +++ b/test/server.js @@ -4,10 +4,11 @@ var get = require('simple-get') var test = require('tape') var WebTorrent = require('../') -test('torrent.createServer(): programmatic http server', function (t) { +test('torrent.createServer: programmatic http server', function (t) { t.plan(9) var client = new WebTorrent({ tracker: false, dht: false }) + client.on('error', function (err) { t.fail(err) }) client.on('warning', function (err) { t.fail(err) }) diff --git a/test/torrents/folder.torrent b/test/torrents/folder.torrent new file mode 100644 index 00000000..cbc8c69f --- /dev/null +++ b/test/torrents/folder.torrent @@ -0,0 +1 @@ +d13:creation datei1449730049429e8:encoding5:UTF-84:infod5:filesld6:lengthi15e4:pathl8:file.txteee4:name6:folder12:piece lengthi16384e6:pieces20:yHӟ+TP./7ee \ No newline at end of file diff --git a/test/torrents/leaves.torrent b/test/torrents/leaves.torrent index f18e7c11399d05b91973b5786c2d42c2073eaac0..f5541afe54dafc63146f4224dc512dc9997ea61d 100644 GIT binary patch delta 11 ScmaFF{-1?0WpfH+9TNZ>jRWcc delta 367 zcmb7=%MHRX5JUw+q751#A2tZ^nG$Hgu{R{b_F85m#D&yEOEdxzBmw2ddEe8_>~r>* zH+n0ib5uKpSt3eiFXEVkR3{7(m*}e?8FP7!C>-M+O<}N>raH4Rf5>QHEo%J^YPuxy z=<`T}zpgsK1l diff --git a/test/torrents/numbers.torrent b/test/torrents/numbers.torrent new file mode 100644 index 00000000..12e2db67 --- /dev/null +++ b/test/torrents/numbers.torrent @@ -0,0 +1 @@ +d13:creation datei1449730287842e8:encoding5:UTF-84:infod5:filesld6:lengthi1e4:pathl5:1.txteed6:lengthi2e4:pathl5:2.txteed6:lengthi3e4:pathl5:3.txteee4:name7:numbers12:piece lengthi16384e6:pieces20:tdPpJ'cSk|ee \ No newline at end of file diff --git a/test/torrents/pride.torrent b/test/torrents/pride.torrent index a9bf635d43be960564a7d6ab837d0fdb6fe24b2b..b74e40eb7810e18d5358f7c0bd956b4d46c7da63 100644 GIT binary patch delta 11 Scmca4^Gtv-W%FCsR1N?gG6Y-z delta 1544 zcmb7EOR}3V5Zu0H4v>W}VA&w-2b?48R0(NpR8L4mh{9x}Gvz8dM9vZMk-*rlm#j6@ z)6?CWKVN=-wJgnvl&X~!l4D*PQ|E_+(S&}0My725AsgRh}L}#Ml?kb(B)XV0z0oZ*^M;PK%`*#}5l#xByf^ zSj55;<`vOR*()iy1SG+PYDXyBLc$HT6@9=i!+o zjHq?93;j?Zbel0F)RBf1oR4Hp_s|d`rGp0AYj-~w`=b2v&{Bz(HJxLUSd4` z9E|Tg!~yyte9r(6E)~$u|F{|GhN?kBm;T5*JW#dMp(%g7V!A{q1j2(?qjl~Uq<5JM zzWqysg^AU&W!H5twOF`&uX}#!_ny$*wrDUkpG diff --git a/test/torrents/sintel-5gb.torrent b/test/torrents/sintel-5gb.torrent index 65f43c4d3d323ba36cbde099b477c1a1d0658656..49c35defb39de93a08a9fdf2ce3020f71d8ef807 100644 GIT binary patch delta 12 TcmaEUf$`NjM#hwl|1#47D|`l4 delta 236 zcmZ{eK?=e!5Jl-#+?%vbp=B=MDO{xKP=nK?jFYv8@FZSDyoKN?jA%s!x1aa_`}a$6 zyB5cx0~?LiC?SG8ODZo-zy6}wLXM`qa@vfmbK%Dah16AE From 914ddd546fff3b6ced22e685a4db8ea407aa4d23 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 17 Dec 2015 23:24:13 +1300 Subject: [PATCH 7/9] torrent.createServer: throw error in browser --- lib/torrent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/torrent.js b/lib/torrent.js index a54bd1dc..00415f39 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -1185,7 +1185,7 @@ Torrent.prototype.load = function (streams, cb) { Torrent.prototype.createServer = function (opts) { var self = this - if (typeof Server !== 'function') return // browser exclude + if (typeof Server !== 'function') throw new Error('node.js-only method') var server = new Server(self, opts) self._servers.push(server) return server From 38e8383bf146f965c3712b095cbbc203f8d787cb Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 17 Dec 2015 23:47:46 +1300 Subject: [PATCH 8/9] test: make fixtures work with brfs --- test/common.js | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/test/common.js b/test/common.js index b68b0f4b..c65ccb14 100644 --- a/test/common.js +++ b/test/common.js @@ -2,49 +2,44 @@ var fs = require('fs') var path = require('path') var parseTorrent = require('parse-torrent') -var content = path.join(__dirname, 'content') -var torrents = path.join(__dirname, 'torrents') - module.exports = { // Leaves of Grass by Walt Whitman.epub leaves: { - contentPath: path.join(content, 'Leaves of Grass by Walt Whitman.epub'), - torrentPath: path.join(torrents, 'leaves.torrent'), - content: fs.readFileSync( - path.join(content, 'Leaves of Grass by Walt Whitman.epub') - ), - torrent: fs.readFileSync(path.join(torrents, 'leaves.torrent')), + contentPath: path.join(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub'), + torrentPath: path.join(__dirname, 'torrents', 'leaves.torrent'), + content: fs.readFileSync(path.join(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')), + torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent')), parsedTorrent: parseTorrent( - fs.readFileSync(path.join(torrents, 'leaves.torrent')) + fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent')) ), magnetURI: parseTorrent.toMagnetURI(parseTorrent( - fs.readFileSync(path.join(torrents, 'leaves.torrent')) + fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent')) )) }, // Folder which contains single file folder: { - contentPath: path.join(content, 'folder'), - torrentPath: path.join(torrents, 'folder.torrent'), - torrent: fs.readFileSync(path.join(torrents, 'folder.torrent')), + contentPath: path.join(__dirname, 'content', 'folder'), + torrentPath: path.join(__dirname, 'torrents', 'folder.torrent'), + torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent')), parsedTorrent: parseTorrent( - fs.readFileSync(path.join(torrents, 'folder.torrent')) + fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent')) ), magnetURI: parseTorrent.toMagnetURI(parseTorrent( - fs.readFileSync(path.join(torrents, 'folder.torrent')) + fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent')) )) }, // Folder which contains multiple files numbers: { - contentPath: path.join(content, 'numbers'), - torrentPath: path.join(torrents, 'numbers.torrent'), - torrent: fs.readFileSync(path.join(torrents, 'numbers.torrent')), + contentPath: path.join(__dirname, 'content', 'numbers'), + torrentPath: path.join(__dirname, 'torrents', 'numbers.torrent'), + torrent: fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent')), parsedTorrent: parseTorrent( - fs.readFileSync(path.join(torrents, 'numbers.torrent')) + fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent')) ), magnetURI: parseTorrent.toMagnetURI(parseTorrent( - fs.readFileSync(path.join(torrents, 'numbers.torrent')) + fs.readFileSync(path.join(__dirname, 'torrents', 'numbers.torrent')) )) } } From 5d67f54f18dd5c581c48c5995cc0e8a37abe9e05 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Thu, 17 Dec 2015 23:48:07 +1300 Subject: [PATCH 9/9] test: port blocklist-dht to new style --- test/blocklist-dht.js | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/test/blocklist-dht.js b/test/blocklist-dht.js index e437f27e..bded83cb 100644 --- a/test/blocklist-dht.js +++ b/test/blocklist-dht.js @@ -1,18 +1,10 @@ var auto = require('run-auto') +var common = require('./common') var DHT = require('bittorrent-dht/server') -var fs = require('fs') var networkAddress = require('network-address') -var parseTorrent = require('parse-torrent') -var path = require('path') var test = require('tape') var WebTorrent = require('../') -var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent')) -var leavesParsed = parseTorrent(leavesTorrent) - -// remove trackers from .torrent file -leavesParsed.announce = [] - test('blocklist blocks peers discovered via DHT', function (t) { t.plan(8) @@ -37,7 +29,7 @@ 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(leavesParsed) + var torrent1 = client1.add(common.leaves.parsedTorrent) torrent1.on('peer', function () { t.fail('client1 should not find any peers') @@ -75,7 +67,7 @@ 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(leavesParsed) + var torrent2 = client2.add(common.leaves.parsedTorrent) torrent2.on('blockedPeer', function () { t.pass('client2 blocked connection to client1') @@ -94,14 +86,14 @@ test('blocklist blocks peers discovered via DHT', function (t) { }, function (err, r) { if (err) throw err - dhtServer.destroy(function () { - t.pass('dht server destroyed') + dhtServer.destroy(function (err) { + t.error(err, 'dht server destroyed') }) - r.client1.destroy(function () { - t.pass('client1 destroyed') + r.client1.destroy(function (err) { + t.error(err, 'client1 destroyed') }) - r.client2.destroy(function () { - t.pass('client2 destroyed') + r.client2.destroy(function (err) { + t.error(err, 'client2 destroyed') }) }) })