diff options
-rw-r--r-- | index.js | 2 | ||||
-rw-r--r-- | lib/torrent.js | 24 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test/basic-node.js | 27 | ||||
-rw-r--r-- | test/basic.js | 60 |
5 files changed, 80 insertions, 35 deletions
@@ -192,8 +192,6 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { } if (!opts) opts = {} - // TODO: support an array of paths - createTorrent.parseInput(input, opts, function (err, files) { if (err) return self.emit('error', err) var streams = files.map(function (file) { return file.getStream }) diff --git a/lib/torrent.js b/lib/torrent.js index 131c092..db8c33f 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -95,9 +95,9 @@ function Torrent (torrentId, opts) { } else throw new Error('invalid torrent id') function onTorrentId (torrentId) { - parsedTorrent = parseTorrent(torrentId) - self.infoHash = parsedTorrent.infoHash - if (parsedTorrent.name) self.name = parsedTorrent.name // preliminary name + self.parsedTorrent = parseTorrent(torrentId) + self.infoHash = self.parsedTorrent.infoHash + if (self.parsedTorrent.name) self.name = self.parsedTorrent.name // preliminary name // create swarm self.swarm = new Swarm(self.infoHash, self.client.peerId, { @@ -112,10 +112,10 @@ function Torrent (torrentId, opts) { if (process.browser) { // in browser, swarm does not listen - self._onSwarmListening(parsedTorrent) + self._onSwarmListening() } else { // listen for peers - self.swarm.listen(self.client.torrentPort, self._onSwarmListening.bind(self, parsedTorrent)) + self.swarm.listen(self.client.torrentPort, self._onSwarmListening.bind(self)) } process.nextTick(function () { self.emit('infoHash') @@ -166,7 +166,13 @@ Object.defineProperty(Torrent.prototype, 'ratio', { } }) -Torrent.prototype._onSwarmListening = function (parsed, port) { +Object.defineProperty(Torrent.prototype, 'magnetURI', { + get: function () { + return parseTorrent.toMagnetURI(this.parsedTorrent) + } +}) + +Torrent.prototype._onSwarmListening = function (port) { var self = this if (self._destroyed) return @@ -174,7 +180,7 @@ Torrent.prototype._onSwarmListening = function (parsed, port) { // begin discovering peers via the DHT and tracker servers self.discovery = new Discovery({ - announce: parsed.announce, + announce: self.parsedTorrent.announce, dht: self.client.dht, tracker: self.client.tracker, peerId: self.client.peerId, @@ -187,7 +193,7 @@ Torrent.prototype._onSwarmListening = function (parsed, port) { reemit(self.discovery, self, ['dhtAnnounce', 'warning', 'error']) // if full metadata was included in initial torrent id, use it - if (parsed.info) self._onMetadata(parsed) + if (self.parsedTorrent.info) self._onMetadata(self.parsedTorrent) self.emit('listening', port) } @@ -202,7 +208,7 @@ Torrent.prototype._onMetadata = function (metadata) { if (metadata && metadata.infoHash) { // `metadata` is a parsed torrent (from parse-torrent module) - self.metadata = parseTorrent.toBuffer(metadata) + self.metadata = parseTorrent.toTorrentFile(metadata) self.parsedTorrent = metadata } else { self.metadata = metadata diff --git a/package.json b/package.json index cec673b..9b05eb7 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "multistream": "^1.4.2", "network-address": "0.0.5", "once": "^1.3.1", - "parse-torrent": "^2.1.0", + "parse-torrent": "^3.0.0", "pretty-bytes": "^1.0.1", "pump": "^1.0.0", "random-access-file": "^0.3.1", diff --git a/test/basic-node.js b/test/basic-node.js index 6039405..ad269f0 100644 --- a/test/basic-node.js +++ b/test/basic-node.js @@ -10,9 +10,10 @@ var leaves = fs.readFileSync(leavesPath) var leavesTorrent = parseTorrent(leaves) var leavesBookPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub' var numbersPath = __dirname + '/content/numbers' +var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce' test('client.add (http url to a torrent file (string))', function (t) { - t.plan(1) + t.plan(2) var server = http.createServer(function (req, res) { res.end(leaves) @@ -25,6 +26,7 @@ test('client.add (http url to a torrent file (string))', function (t) { var client = new WebTorrent({ dht: false, tracker: false }) client.add(url, function (torrent) { t.equal(torrent.infoHash, leavesTorrent.infoHash) + t.equal(torrent.magnetURI, leavesMagnetURI) client.destroy() server.close() }) @@ -33,27 +35,41 @@ test('client.add (http url to a torrent file (string))', function (t) { }) test('client.add (filesystem path to a torrent file (string))', function (t) { - t.plan(1) + t.plan(2) var client = new WebTorrent({ dht: false, tracker: false }) client.add(leavesPath, function (torrent) { t.equal(torrent.infoHash, leavesTorrent.infoHash) + t.equal(torrent.magnetURI, leavesMagnetURI) client.destroy() }) }) test('client.seed (filesystem path to file (string))', function (t) { - t.plan(1) + 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' + ] + } var client = new WebTorrent({ dht: false, tracker: false }) - client.seed(leavesBookPath, function (torrent) { + client.seed(leavesBookPath, opts, function (torrent) { t.equal(torrent.infoHash, leavesTorrent.infoHash) + t.equal(torrent.magnetURI, leavesMagnetURI) client.destroy() }) }) test('client.seed (filesystem path to folder (string))', function (t) { - t.plan(1) + t.plan(2) var opts = { pieceLength: 32768, // force piece length to 32KB so info-hash will @@ -66,6 +82,7 @@ test('client.seed (filesystem path to folder (string))', function (t) { var client = new WebTorrent({ dht: false, tracker: false }) 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.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.webtorrent.io%3A80&tr=wss%3A%2F%2Ftracker.webtorrent.io') client.destroy() }) }) diff --git a/test/basic.js b/test/basic.js index 5de4f67..d244bdd 100644 --- a/test/basic.js +++ b/test/basic.js @@ -7,55 +7,79 @@ var leaves = fs.readFileSync(__dirname + '/torrents/leaves.torrent') var leavesTorrent = parseTorrent(leaves) var leavesBook = fs.readFileSync(__dirname + '/content/Leaves of Grass by Walt Whitman.epub') -function verify (t, client, torrent) { - t.equal(torrent.infoHash, leavesTorrent.infoHash) - client.destroy() -} +var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce' test('client.add (magnet uri, torrent file, info hash, and parsed torrent)', function (t) { - t.plan(5) - // magnet uri (utf8 string) var client1 = new WebTorrent({ dht: false, tracker: false }) - verify(t, client1, client1.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash)) + var torrent1 = client1.add('magnet:?xt=urn:btih:' + leavesTorrent.infoHash) + t.equal(torrent1.infoHash, leavesTorrent.infoHash) + t.equal(torrent1.magnetURI, 'magnet:?xt=urn:btih:' + leavesTorrent.infoHash) + client1.destroy() // torrent file (buffer) var client2 = new WebTorrent({ dht: false, tracker: false }) - verify(t, client2, client2.add(leaves)) + var torrent2 = client2.add(leaves) + t.equal(torrent2.infoHash, leavesTorrent.infoHash) + t.equal(torrent2.magnetURI, leavesMagnetURI) + client2.destroy() // info hash (hex string) var client3 = new WebTorrent({ dht: false, tracker: false }) - verify(t, client3, client3.add(leavesTorrent.infoHash)) + var torrent3 = client3.add(leavesTorrent.infoHash) + t.equal(torrent3.infoHash, leavesTorrent.infoHash) + t.equal(torrent3.magnetURI, 'magnet:?xt=urn:btih:' + leavesTorrent.infoHash) + client3.destroy() // info hash (buffer) var client4 = new WebTorrent({ dht: false, tracker: false }) - verify(t, client4, client4.add(new Buffer(leavesTorrent.infoHash, 'hex'))) + var torrent4 = client4.add(new Buffer(leavesTorrent.infoHash, 'hex')) + t.equal(torrent4.infoHash, leavesTorrent.infoHash) + t.equal(torrent4.magnetURI, 'magnet:?xt=urn:btih:' + leavesTorrent.infoHash) + client4.destroy() // parsed torrent (from parse-torrent) var client5 = new WebTorrent({ dht: false, tracker: false }) - verify(t, client5, client5.add(leavesTorrent)) + var torrent5 = client5.add(leavesTorrent) + t.equal(torrent5.infoHash, leavesTorrent.infoHash) + t.equal(torrent5.magnetURI, leavesMagnetURI) + client5.destroy() + + t.end() }) test('client.seed (Buffer, Blob)', function (t) { - t.plan(2) + t.plan(typeof Blob !== 'undefined' ? 4 : 2) var opts = { - name: 'Leaves of Grass by Walt Whitman.epub' + 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' + ] } // torrent file (Buffer) var client1 = new WebTorrent({ dht: false, tracker: false }) - client1.seed(leavesBook, opts, function (torrent) { - verify(t, client1, torrent) + client1.seed(leavesBook, opts, function (torrent1) { + t.equal(torrent1.infoHash, leavesTorrent.infoHash) + t.equal(torrent1.magnetURI, leavesMagnetURI) + client1.destroy() }) // Blob if (typeof Blob !== 'undefined') { var client2 = new WebTorrent({ dht: false, tracker: false }) - client2.seed(new Blob([ leavesBook ]), opts, function (torrent) { - verify(t, client2, torrent) + client2.seed(new Blob([ leavesBook ]), opts, function (torrent2) { + t.equal(torrent2.infoHash, leavesTorrent.infoHash) + t.equal(torrent2.magnetURI, leavesMagnetURI) + client2.destroy() }) } else { - t.pass('Skipping Blob test because missing `Blob` constructor') + console.log('Skipping Blob test because missing `Blob` constructor') } }) |