diff options
Diffstat (limited to 'lib/torrent.js')
-rw-r--r-- | lib/torrent.js | 77 |
1 files changed, 22 insertions, 55 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index cea4ac2..ab6cb5a 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -12,29 +12,30 @@ var WIRE_TIMEOUT = 10000 inherits(Torrent, EventEmitter) function Torrent (uri, opts) { - if (!(this instanceof Torrent)) return new Torrent(uri, opts) - EventEmitter.call(this) + var self = this + if (!(self instanceof Torrent)) return new Torrent(uri, opts) + EventEmitter.call(self) - var info = this._parseMagnetUri(uri) + var info = parseMagnetUri(uri) if (!info.infoHash) throw new Error('invalid torrent uri') - this.infoHash = info.infoHash - this.title = info.title - this.metadata = null + self.infoHash = info.infoHash + self.title = info.title + self.metadata = null - this.swarm = new Swarm(this.infoHash, opts.peerId, { dht: true }) + self.swarm = new Swarm(self.infoHash, opts.peerId, { dht: true }) if (opts.port) { - this.swarm.listen(opts.port, function (port) { - this.emit('listening', port) - }.bind(this)) + self.swarm.listen(opts.port, function (port) { + self.emit('listening', port) + }) } - this.swarm.on('error', function (err) { + self.swarm.on('error', function (err) { console.error(err.message) }) - this.swarm.on('wire', function (wire) { + self.swarm.on('wire', function (wire) { // Send KEEP-ALIVE (every 60s) so peers will not disconnect the wire wire.setKeepAlive(true) @@ -130,57 +131,22 @@ function Torrent (uri, opts) { console.log('METADATA') console.log(wire.metadata.toString()) - this.metadata = { + self.metadata = { 'announce-list': [], info: bncode.decode(wire.metadata), // info_hash: } - console.log(this.metadata) - this.emit('metadata', this.metadata) + console.log(self.metadata) + self.emit('metadata', this.metadata) } } - }.bind(this)) - - }.bind(this)) -} - -Torrent.prototype.downloadMetadata = function () { - if (!this.metadata) - return - - var errorHandler = function (err) { - console.error('error' + err.toString()) - } - - chrome.fileSystem.chooseEntry({ - type: 'saveFile', - suggestedName: this.title + '.torrent' - }, function (fileEntry) { - if (!fileEntry) - return - - fileEntry.createWriter(function (writer) { - writer.onerror = errorHandler - writer.onwriteend = function (e) { - console.log('write complete') - } - - var metadata = new Buffer(bncode.encode(this.metadata)) - - writer.write(new Blob([metadata]), { type: 'application/x-bittorrent' }) - }.bind(this), errorHandler) - }.bind(this)) + }) + }) } -Object.defineProperty(Torrent.prototype, 'numPeers', { - get: function () { - return this.swarm.wires.length - } -}) - Object.defineProperty(Torrent.prototype, 'progress', { get: function () { - return 0.7 // TODO + return 0 // TODO } }) @@ -189,7 +155,8 @@ Object.defineProperty(Torrent.prototype, 'progress', { * @param {string} addr */ Torrent.prototype.addPeer = function (addr) { - this.swarm.add(addr) + var self = this + self.swarm.add(addr) } // @@ -201,7 +168,7 @@ Torrent.prototype.addPeer = function (addr) { * @param {string} uri * @return {Object} */ -Torrent.prototype._parseMagnetUri = function (uri) { +function parseMagnetUri (uri) { var parsed = magnet(uri) return { title: parsed.dn, // displayName |