diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2015-12-28 23:00:26 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2015-12-28 23:00:26 +0300 |
commit | 5194e8bfc03db491e53df51b993bfd06200a94bc (patch) | |
tree | aa3ec30f617199d8c5b36fe04617e2a85fd09b66 | |
parent | bff953e21aa6faa9eaea2fd0cbd4e6086a39a8ba (diff) |
torrent: throw on use after destroy
-rw-r--r-- | lib/torrent.js | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index 71ee9dc..ed571f6 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -448,6 +448,7 @@ Torrent.prototype.destroy = function (cb) { */ Torrent.prototype.addPeer = function (peer) { var self = this + if (self.destroyed) throw new Error('torrent is destroyed') function addPeer () { self.swarm.addPeer(peer) @@ -472,9 +473,9 @@ Torrent.prototype.addPeer = function (peer) { * @param {string} url web seed url */ Torrent.prototype.addWebSeed = function (url) { - var self = this + if (this.destroyed) throw new Error('torrent is destroyed') debug('add web seed %s', url) - self.swarm.addWebSeed(url, self) + this.swarm.addWebSeed(url, this) } /** @@ -487,6 +488,8 @@ Torrent.prototype.addWebSeed = function (url) { */ Torrent.prototype.select = function (start, end, priority, notify) { var self = this + if (self.destroyed) throw new Error('torrent is destroyed') + if (start > end || start < 0 || end >= self.pieces.length) { throw new Error('invalid selection ', start, ':', end) } @@ -518,6 +521,8 @@ Torrent.prototype.select = function (start, end, priority, notify) { */ Torrent.prototype.deselect = function (start, end, priority) { var self = this + if (self.destroyed) throw new Error('torrent is destroyed') + priority = Number(priority) || 0 debug('deselect %s-%s (priority %s)', start, end, priority) @@ -540,6 +545,8 @@ Torrent.prototype.deselect = function (start, end, priority) { */ Torrent.prototype.critical = function (start, end) { var self = this + if (self.destroyed) throw new Error('torrent is destroyed') + debug('critical %s-%s', start, end) for (var i = start; i <= end; ++i) { @@ -1179,6 +1186,8 @@ Torrent.prototype._checkDone = function () { Torrent.prototype.load = function (streams, cb) { var self = this + if (self.destroyed) throw new Error('torrent is destroyed') + if (!Array.isArray(streams)) streams = [ streams ] if (!cb) cb = noop @@ -1198,10 +1207,11 @@ Torrent.prototype.load = function (streams, cb) { } Torrent.prototype.createServer = function (opts) { - var self = this + if (this.destroyed) throw new Error('torrent is destroyed') + if (typeof Server !== 'function') throw new Error('node.js-only method') - var server = new Server(self, opts) - self._servers.push(server) + var server = new Server(this, opts) + this._servers.push(server) return server } |