From 63f48e99843242e4de3da27a8b10155376d2c24e Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 20 May 2015 15:32:36 -0700 Subject: cleanup torrent.torrentFileURL blob on destroy For #322 --- lib/torrent.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/torrent.js b/lib/torrent.js index e34041f..3bb44a0 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -75,6 +75,7 @@ function Torrent (torrentId, opts) { self._selections = [] self._critical = [] self._storageImpl = opts.storage || Storage + this._torrentFileURL = null var parsedTorrent try { @@ -213,9 +214,12 @@ Object.defineProperty(Torrent.prototype, 'torrentFile', { Object.defineProperty(Torrent.prototype, 'torrentFileURL', { get: function () { - return window.URL.createObjectURL( + if (typeof window === 'undefined') throw new Error('browser-only property') + if (this._torrentFileURL) return this._torrentFileURL + this._torrentFileURL = window.URL.createObjectURL( new window.Blob([ this.torrentFile ], { type: 'application/x-bittorrent' }) ) + return this._torrentFileURL } }) @@ -361,6 +365,8 @@ Torrent.prototype.destroy = function (cb) { self._rechokeIntervalId = null } + if (self._torrentFileURL) window.URL.revokeObjectURL(self._torrentFileURL) + var tasks = [] if (self.swarm) tasks.push(function (cb) { self.swarm.destroy(cb) }) if (self.discovery) tasks.push(function (cb) { self.discovery.stop(cb) }) -- cgit v1.2.3