diff --git a/index.js b/index.js
index 72d6f7ab..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')
@@ -205,7 +205,8 @@ 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
+ if (!self.tracker) opts.announce = []
var streams
var torrent = self.add(undefined, opts, function (torrent) {
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))
diff --git a/lib/torrent.js b/lib/torrent.js
index ccf77820..00415f39 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]
@@ -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
diff --git a/test/basic-node.js b/test/basic-node.js
index cff40ebd..a33cde14 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)
+ t.plan(8)
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,104 +20,97 @@ 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)
- client.destroy()
- server.close()
+ 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 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 })
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.destroy()
+ 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.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(leavesBookPath, opts, function (torrent) {
- t.equal(torrent.infoHash, leavesTorrent.infoHash)
- t.equal(torrent.magnetURI, leavesMagnetURI)
- client.destroy()
+ 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.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(folderPath, 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(numbersPath, 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 4fedcb88..64472b47 100644
--- a/test/basic.js
+++ b/test/basic.js
@@ -1,222 +1,254 @@
-var path = require('path')
-var fs = require('fs')
+/* global Blob */
+
+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: magnet uri, utf-8 string', function (t) {
+ t.plan(6)
-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(common.leaves.magnetURI)
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.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) })
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, 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) })
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.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) })
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'), 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) })
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, 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(leavesTorrent, {
- announce: leavesTorrent.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, leavesTorrent.infoHash)
- client.remove(leavesTorrent)
+ t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
+ 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) })
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)
- client.remove(torrent)
+ t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
+
+ 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) })
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)
- torrent.destroy()
+ t.equal(torrent.infoHash, common.leaves.parsedTorrent.infoHash)
+
+ 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(leavesBook, 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, leavesTorrent.infoHash)
- t.equal(torrent.magnetURI, leavesMagnetURI)
- client.remove(torrent)
+ 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()
+
+ 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([ leavesBook ]), opts, function (torrent) {
- t.equal(client.torrents.length, 1)
- t.equal(torrent.infoHash, leavesTorrent.infoHash)
- t.equal(torrent.magnetURI, leavesMagnetURI)
- 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) {
@@ -227,11 +259,10 @@ 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:' + leavesTorrent.infoHash)
+ client.add('magnet:?xt=urn:btih:' + common.leaves.parsedTorrent.infoHash)
})
t.throws(function () {
client.seed(new Buffer('sup'))
@@ -246,16 +277,15 @@ 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 () {
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..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 path = require('path')
-var fs = require('fs')
var networkAddress = require('network-address')
-var parseTorrent = require('parse-torrent')
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')
})
})
})
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
new file mode 100644
index 00000000..c65ccb14
--- /dev/null
+++ b/test/common.js
@@ -0,0 +1,45 @@
+var fs = require('fs')
+var path = require('path')
+var parseTorrent = require('parse-torrent')
+
+module.exports = {
+ // Leaves of Grass by Walt Whitman.epub
+ leaves: {
+ 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(__dirname, 'torrents', 'leaves.torrent'))
+ ),
+ magnetURI: parseTorrent.toMagnetURI(parseTorrent(
+ fs.readFileSync(path.join(__dirname, 'torrents', 'leaves.torrent'))
+ ))
+ },
+
+ // Folder which contains single file
+ folder: {
+ 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(__dirname, 'torrents', 'folder.torrent'))
+ ),
+ magnetURI: parseTorrent.toMagnetURI(parseTorrent(
+ fs.readFileSync(path.join(__dirname, 'torrents', 'folder.torrent'))
+ ))
+ },
+
+ // Folder which contains multiple files
+ numbers: {
+ 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(__dirname, 'torrents', 'numbers.torrent'))
+ ),
+ magnetURI: parseTorrent.toMagnetURI(parseTorrent(
+ fs.readFileSync(path.join(__dirname, '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 dfa1730c..8571a9ea 100644
--- a/test/server.js
+++ b/test/server.js
@@ -1,41 +1,47 @@
-var path = require('path')
+var common = require('./common')
var fs = require('fs')
var get = require('simple-get')
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(common.leaves.torrent, 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(common.leaves.contentPath), 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, common.leaves.content)
+
+ 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')
})
})
})
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 f18e7c11..f5541afe 100644
Binary files a/test/torrents/leaves.torrent and b/test/torrents/leaves.torrent differ
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 a9bf635d..b74e40eb 100644
Binary files a/test/torrents/pride.torrent and b/test/torrents/pride.torrent differ
diff --git a/test/torrents/sintel-5gb.torrent b/test/torrents/sintel-5gb.torrent
index 65f43c4d..49c35def 100644
Binary files a/test/torrents/sintel-5gb.torrent and b/test/torrents/sintel-5gb.torrent differ