diff --git a/index.js b/index.js index b90fc87d..d3eb9564 100644 --- a/index.js +++ b/index.js @@ -390,6 +390,10 @@ WebTorrent.prototype._remove = function (torrentId, cb) { var torrent = this.get(torrentId) if (!torrent) return this.torrents.splice(this.torrents.indexOf(torrent), 1) + // clear hash entity in cache to be able use pears search again + if (this.dht) { + this.dht._tables.remove(torrent.infoHash) + } torrent.destroy(cb) } diff --git a/lib/torrent.js b/lib/torrent.js index 6d53ee9c..7ecff340 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -31,6 +31,8 @@ var uniq = require('uniq') var utMetadata = require('ut_metadata') var utPex = require('ut_pex') // browser exclude var parseRange = require('parse-numeric-range') +var thunky = require('thunky') +var raf = require('random-access-file') var File = require('./file') var Peer = require('./peer') @@ -1602,10 +1604,33 @@ Torrent.prototype._checkDone = function () { } if (!done) break } - if (!self.done && done) { - self.done = true - self._debug('torrent done: ' + self.infoHash) - self.emit('done') + if (done) { + const done = () => { + self.done = true + self._debug('torrent done: ' + self.infoHash) + self.emit('done') + } + // flush files to be able to open after download finish + if(self._store === FSChunkStore) + { + let tasks = self.store.store.files.map(function (storeFile) { + return function (cb) { + storeFile.open(function (err, file) { + // an open error is okay because that means the file is not open + if (err) return cb(null) + file.close(() => { + storeFile.open = thunky((cb) => cb(null, raf(storeFile.path))) + cb() + }) + }) + } + }) + parallel(tasks, done) + } + else + { + done() + } } self._gcSelections() diff --git a/package.json b/package.json index e514ac45..f1ce6b94 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "stream-to-blob": "^1.0.0", "stream-to-blob-url": "^2.1.0", "stream-with-known-length-to-buffer": "^1.0.0", - "torrent-discovery": "^8.3.1", + "torrent-discovery": "^9.1.1", "torrent-piece": "^1.1.0", "uniq": "^1.0.1", "unordered-array-remove": "^1.0.2",