From 5194e8bfc03db491e53df51b993bfd06200a94bc Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 28 Dec 2015 21:00:26 +0100 Subject: torrent: throw on use after destroy --- lib/torrent.js | 20 +++++++++++++++----- 1 file 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 } -- cgit v1.2.3