Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BREAKING: Many fixes; all leaks fixed #762

Merged
merged 12 commits into from Apr 22, 2016

torrent: remove _onError, add _destroy(err, cb)

  • Loading branch information
feross committed Apr 21, 2016
commit 6085dc79c577ceab5f9f5f305748af5a7d5b33e7
@@ -1,4 +1,3 @@
// TODO: remove _onError, add _destroy(err, cb)
// TODO: cleanup event listeners
// TODO: Remove all inline docs, and move to docs/api.md

@@ -238,7 +237,7 @@ Torrent.prototype._onTorrentId = function (torrentId) {
// operation, i.e. http/https link, filesystem path, or Blob.
parseTorrent.remote(torrentId, function (err, parsedTorrent) {
if (self.destroyed) return
if (err) return self._onError(err)
if (err) return self._destroy(err)
self._onParsedTorrent(parsedTorrent)
})
}
@@ -247,12 +246,11 @@ Torrent.prototype._onTorrentId = function (torrentId) {
Torrent.prototype._onParsedTorrent = function (parsedTorrent) {
var self = this
if (self.destroyed) return
console.log('on parsed torrent')

self._processParsedTorrent(parsedTorrent)

if (!self.infoHash) {
return self._onError(new Error('Malformed torrent data: No info hash'))
return self._destroy(new Error('Malformed torrent data: No info hash'))
}

if (!self.path) self.path = path.join(TMP, self.infoHash)
@@ -296,13 +294,11 @@ Torrent.prototype._processParsedTorrent = function (parsedTorrent) {

this.magnetURI = parseTorrent.toMagnetURI(parsedTorrent)
this.torrentFile = parseTorrent.toTorrentFile(parsedTorrent)
console.log('process done')
}

Torrent.prototype._onListening = function () {
var self = this
if (self.discovery || self.destroyed) return
console.log('on listening')
var trackerOpts = {
rtcConfig: self.client._rtcConfig,
wrtc: self.client._wrtc,
@@ -327,7 +323,7 @@ Torrent.prototype._onListening = function () {
port: self.client.torrentPort
})
self.discovery.on('error', function (err) {
self._onError(err)
self._destroy(err)
})
self.discovery.on('peer', function (peer) {
// Don't create new outgoing TCP connections when torrent is done
@@ -367,7 +363,7 @@ Torrent.prototype._onMetadata = function (metadata) {
try {
parsedTorrent = parseTorrent(metadata)
} catch (err) {
return self._onError(err)
return self._destroy(err)
}
}

@@ -426,7 +422,7 @@ Torrent.prototype._onMetadata = function (metadata) {
if (self._fileModtimes && self._store === FSChunkStore) {
// don't verify if the files haven't been modified since we last checked
self.getFileModtimes(function (err, fileModtimes) {
if (err) return self._onError(err)
if (err) return self._destroy(err)

var unchanged = self.files.map(function (_, index) {
return fileModtimes[index] === self._fileModtimes[index]
@@ -490,7 +486,7 @@ Torrent.prototype._verifyPieces = function () {
})
}
}), FILESYSTEM_CONCURRENCY, function (err) {
if (err) return self._onError(err)
if (err) return self._destroy(err)
self._debug('done verifying')
self._onStore()
})
@@ -527,6 +523,11 @@ Torrent.prototype._onStore = function () {
* Destroy and cleanup this torrent.
*/
Torrent.prototype.destroy = function (cb) {
var self = this
self._destroy(null, cb)
}

Torrent.prototype._destroy = function (err, cb) {
var self = this
if (self.destroyed) return
self.destroyed = true
@@ -562,6 +563,16 @@ Torrent.prototype.destroy = function (cb) {
}

parallel(tasks, cb)

if (err) {
// When there is no `torrent.on('error')` listener, emit `client.on('error')` instead.
// The more-specific, torrent error handler is preferred.
if (self.listenerCount('error') === 0) {
self.client.emit('error', err)
} else {
self.emit('error', err)
}
}
}

/**
@@ -574,8 +585,6 @@ Torrent.prototype.addPeer = function (peer) {
var self = this
if (self.destroyed) throw new Error('torrent is destroyed')
if (!self.infoHash) throw new Error('addPeer() must not be called before the `infoHash` event')
console.log('addPeer:', peer)
console.log(self.infoHash)

if (self.client.blocked) {
var host
@@ -1541,17 +1550,6 @@ Torrent.prototype.resume = function () {
this._drain()
}

Torrent.prototype._onError = function (err) {
var self = this
self._debug('torrent error: %s', err.message || err)
self.destroy()
if (self.listenerCount('error') === 0) {
self.client.emit('error', err)
} else {
self.emit('error', err)
}
}

Torrent.prototype._debug = function () {
var args = [].slice.call(arguments)
args[0] = '[' + this._debugId + '] ' + args[0]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.