diff --git a/package.json b/package.json index db770bc8..d08bfea2 100644 --- a/package.json +++ b/package.json @@ -24,8 +24,8 @@ "addr-to-ip-port": "^1.0.1", "bitfield": "^1.0.2", "bittorrent-dht": "^3.0.0", - "bittorrent-swarm": "^5.0.0", - "chunk-store-stream": "^2.0.0", + "bittorrent-swarm": "https://github.com/solderzzc/bittorrent-swarm.git", + "block-stream2": "^1.0.0", "clivas": "^0.2.0", "create-torrent": "^3.4.0", "cross-spawn-async": "^2.0.0", diff --git a/test/content/multiple/Leaves of Grass by Walt Whitman.epub b/test/content/multiple/Leaves of Grass by Walt Whitman.epub new file mode 100644 index 00000000..66791ed3 Binary files /dev/null and b/test/content/multiple/Leaves of Grass by Walt Whitman.epub differ diff --git a/test/content/multiple/blocklist.txt b/test/content/multiple/blocklist.txt new file mode 100644 index 00000000..167627df --- /dev/null +++ b/test/content/multiple/blocklist.txt @@ -0,0 +1,2 @@ +Blah blah description:1.2.3.0-1.2.3.255 +Blah blah different description:5.6.7.0-5.6.7.255 diff --git a/test/content/multiple/blocklist.txt.gz b/test/content/multiple/blocklist.txt.gz new file mode 100644 index 00000000..5f401edc Binary files /dev/null and b/test/content/multiple/blocklist.txt.gz differ diff --git a/test/content/multiple/folder/file.txt b/test/content/multiple/folder/file.txt new file mode 100644 index 00000000..0637880d --- /dev/null +++ b/test/content/multiple/folder/file.txt @@ -0,0 +1 @@ +This is a file diff --git a/test/content/multiple/numbers/1.txt b/test/content/multiple/numbers/1.txt new file mode 100644 index 00000000..56a6051c --- /dev/null +++ b/test/content/multiple/numbers/1.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/test/content/multiple/numbers/2.txt b/test/content/multiple/numbers/2.txt new file mode 100644 index 00000000..8fdd954d --- /dev/null +++ b/test/content/multiple/numbers/2.txt @@ -0,0 +1 @@ +22 \ No newline at end of file diff --git a/test/content/multiple/numbers/3.txt b/test/content/multiple/numbers/3.txt new file mode 100644 index 00000000..4f376707 --- /dev/null +++ b/test/content/multiple/numbers/3.txt @@ -0,0 +1 @@ +333 \ No newline at end of file diff --git a/test/download-webseed-multiple-files-magnet.js b/test/download-webseed-multiple-files-magnet.js new file mode 100644 index 00000000..ef37c2d7 --- /dev/null +++ b/test/download-webseed-multiple-files-magnet.js @@ -0,0 +1,117 @@ +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 multipleFileTorrent = fs.readFileSync(__dirname + '/torrents/multiple.torrent') +var multipleFileTorrentParsed = parseTorrent(multipleFileTorrent) + +// remove trackers from .torrent file +multipleFileTorrentParsed.announce = [] +test('Download using multiple files webseed (via magnet uri)', function (t) { + t.plan(22) + + var serve = serveStatic(path.join(__dirname, 'content')) + var httpServer = http.createServer(function (req, res) { + var done = finalhandler(req, res) + serve(req, res, done) + }) + var magnetUri + + httpServer.on('error', function (err) { t.fail(err) }) + + auto({ + httpPort: function (cb) { + httpServer.listen(cb) + }, + client1: ['httpPort', function (cb) { + var client1 = new WebTorrent({ tracker: false, dht: false }) + client1.on('error', function (err) { t.fail(err) }) + client1.on('warning', function (err) { t.fail(err) }) + + client1.add(multipleFileTorrentParsed) + + var gotTorrent = false + var gotListening = false + function maybeDone () { + if (gotTorrent && gotListening) cb(null, client1) + } + + client1.on('torrent', function (torrent) { + // torrent metadata has been fetched -- sanity check it + t.equal(torrent.name, 'multiple') + + var names = [ + 'Leaves of Grass by Walt Whitman.epub', + 'blocklist.txt', + 'blocklist.txt.gz', + 'file.txt', + '1.txt', + '2.txt', + '3.txt' + ] + + t.deepEqual(torrent.files.map(function (file) { return file.name }), names) + + // NOTE: client1 is *NOT* a seeder. Just has the metadata. + gotTorrent = true + maybeDone() + }) + + client1.on('listening', function () { + gotListening = true + maybeDone() + }) + }], + client2: ['client1', 'httpPort', function (cb, r) { + var webSeedUrl = 'http://localhost:' + httpServer.address().port + '/' + magnetUri = 'magnet:?xt=urn:btih:' + multipleFileTorrentParsed.infoHash + + '&ws=' + encodeURIComponent(webSeedUrl) + + var client2 = new WebTorrent({ tracker: false, dht: false }) + + client2.on('error', function (err) { t.fail(err) }) + client2.on('warning', function (err) { t.fail(err) }) + + client2.on('torrent', function (torrent) { + var count = 0 + torrent.files.forEach(function (file) { + file.getBuffer(function (err, buf) { + t.error(err) + t.deepEqual(buf, fs.readFileSync(__dirname + '/content/' + file.path), 'downloaded correct content') + if (++count === 7) { + t.pass('7 files downloaded from webseed') + cb(null, client2) + } + }) + }) + + torrent.once('done', function () { + t.pass('client2 downloaded torrent from client1') + }) + }) + + client2.add(magnetUri) + + client2.on('listening', function (port, torrent) { + torrent.addPeer('127.0.0.1:' + r.client1.torrentPort) + }) + }] + }, function (err, r) { + t.error(err) + r.client1.destroy(function () { + t.pass('client destroyed') + }) + r.client2.destroy(function () { + t.pass('client destroyed') + }) + httpServer.close(function () { + t.pass('http server closed') + }) + }) +}) diff --git a/test/download-webseed-multiple-files-torrent.js b/test/download-webseed-multiple-files-torrent.js new file mode 100644 index 00000000..9e945789 --- /dev/null +++ b/test/download-webseed-multiple-files-torrent.js @@ -0,0 +1,71 @@ +var auto = require('run-auto') +var fs = require('fs') +var parseTorrent = require('parse-torrent') +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 multipleFileTorrent = fs.readFileSync(__dirname + '/torrents/multiple.torrent') +var multipleFileTorrentParsed = parseTorrent(multipleFileTorrent) + +// remove trackers from .torrent file +multipleFileTorrentParsed.announce = [] + +test('Download multiple files using webseed (via .torrent file)', function (t) { + t.plan(19) + + var serve = serveStatic(path.join(__dirname, 'content')) + var httpServer = http.createServer(function (req, res) { + var done = finalhandler(req, res) + serve(req, res, done) + }) + + httpServer.on('error', function (err) { t.fail(err) }) + + auto({ + httpPort: function (cb) { + httpServer.listen(cb) + }, + client: ['httpPort', function (cb) { + multipleFileTorrentParsed.urlList.push( + 'http://localhost:' + httpServer.address().port + '/' + ) + + 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 count = 0 + client.on('torrent', function (torrent) { + torrent.files.forEach(function (file) { + file.getBuffer(function (err, buf) { + t.error(err) + t.deepEqual(buf, fs.readFileSync(__dirname + '/content/' + file.path), 'downloaded correct content') + if (++count === 7) { + t.pass('7 files downloaded from webseed') + cb(null, client) + } + }) + }) + + torrent.once('done', function () { + t.pass('client downloaded torrent from webseed') + }) + }) + + client.add(multipleFileTorrentParsed) + }] + }, function (err, r) { + t.error(err) + r.client.destroy(function () { + t.pass('client destroyed') + }) + httpServer.close(function () { + t.pass('http server closed') + }) + }) +}) diff --git a/test/torrents/multiple.torrent b/test/torrents/multiple.torrent new file mode 100644 index 00000000..e73e40d3 Binary files /dev/null and b/test/torrents/multiple.torrent differ