diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2015-03-20 05:48:15 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2015-03-20 05:48:15 +0300 |
commit | c132ce47c9146738a0e7a5700aa93409b680b524 (patch) | |
tree | 2a674a9dad5ff0b9f50f600daf664ee3b14997b3 | |
parent | c254fda44c7d498f08b73e134b7994f0394a9e29 (diff) |
internal: simplify how storage opts are passed
-rw-r--r-- | index.js | 6 | ||||
-rw-r--r-- | lib/fs-storage.js | 22 | ||||
-rw-r--r-- | lib/storage.js | 2 | ||||
-rw-r--r-- | lib/torrent.js | 8 |
4 files changed, 16 insertions, 22 deletions
@@ -154,9 +154,6 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) { opts.client = self opts.storage = opts.storage || self.storage - if (!opts.storageOpts) opts.storageOpts = {} - if (opts.tmp) opts.storageOpts.tmp = opts.tmp - var torrent = new Torrent(torrentId, opts) self.torrents.push(torrent) @@ -200,8 +197,7 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { opts = {} } if (!opts) opts = {} - if (!opts.storageOpts) opts.storageOpts = {} - opts.storageOpts.noVerify = true + opts.noVerify = true createTorrent.parseInput(input, opts, function (err, files) { if (err) return self.emit('error', err) diff --git a/lib/fs-storage.js b/lib/fs-storage.js index 7923791..51e5ea5 100644 --- a/lib/fs-storage.js +++ b/lib/fs-storage.js @@ -1,6 +1,5 @@ module.exports = FSStorage -var extend = require('xtend') var fs = require('fs') var inherits = require('inherits') var mkdirp = require('mkdirp') @@ -11,8 +10,7 @@ var rimraf = require('rimraf') var Storage = require('./storage') var thunky = require('thunky') -var TMP = fs.existsSync('/tmp') ? '/tmp' : os.tmpDir() -function noop () {} +var TMP = path.join(fs.existsSync('/tmp') ? '/tmp' : os.tmpDir(), 'webtorrent') inherits(FSStorage, Storage) @@ -24,18 +22,16 @@ inherits(FSStorage, Storage) */ function FSStorage (parsedTorrent, opts) { var self = this - opts = extend({ - nobuffer: true, - tmp: TMP, - name: 'webtorrent' - }, opts) - Storage.call(self, parsedTorrent, opts) - self.path = opts.path || path.join(opts.tmp, opts.name, parsedTorrent.infoHash) - self.piecesMap = [] + self.tmp = opts.tmp || TMP + self.path = opts.path || path.join(self.tmp, parsedTorrent.infoHash) + self.piecesMap = [] self.nonExistentError = new Error('Cannot read from non-existent file') + opts.nobuffer = true + Storage.call(self, parsedTorrent, opts) + self.files.forEach(function (file) { var fileStart = file.offset var fileEnd = fileStart + file.length @@ -179,7 +175,7 @@ FSStorage.prototype._onPieceDone = function (piece) { */ FSStorage.prototype.remove = function (cb) { var self = this - if (!cb) cb = noop + if (!cb) cb = function () {} self.close(function (err) { if (err) return cb(err) @@ -193,7 +189,7 @@ FSStorage.prototype.remove = function (cb) { */ FSStorage.prototype.close = function (cb) { var self = this - if (!cb) cb = noop + if (!cb) cb = function () {} if (self.closed) return cb() Storage.prototype.close.call(self, function (err) { diff --git a/lib/storage.js b/lib/storage.js index 48d88a1..0aefc64 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -329,7 +329,7 @@ function Storage (parsedTorrent, opts) { var self = this EventEmitter.call(self) if (!debug.enabled) self.setMaxListeners(0) - opts = opts || {} + if (!opts) opts = {} self.bitfield = new BitField(parsedTorrent.pieces.length) diff --git a/lib/torrent.js b/lib/torrent.js index b37cc51..010f19f 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -45,17 +45,19 @@ function Torrent (torrentId, opts) { if (!debug.enabled) self.setMaxListeners(0) debug('new torrent') + self.opts = opts self.client = opts.client self.hotswapEnabled = ('hotswap' in opts ? opts.hotswap : true) self.verify = opts.verify - self.storageOpts = opts.storageOpts self.chokeTimeout = opts.chokeTimeout || CHOKE_TIMEOUT self.pieceTimeout = opts.pieceTimeout || PIECE_TIMEOUT self.strategy = opts.strategy || 'sequential' - self._rechokeNumSlots = (opts.uploads === false || opts.uploads === 0) ? 0 : (+opts.uploads || 10) + self._rechokeNumSlots = (opts.uploads === false || opts.uploads === 0) + ? 0 + : (+opts.uploads || 10) self._rechokeOptimisticWire = null self._rechokeOptimisticTime = 0 self._rechokeIntervalId = null @@ -246,7 +248,7 @@ Torrent.prototype._onMetadata = function (metadata) { self.rarityMap = new RarityMap(self.swarm, self.parsedTorrent.pieces.length) - self.storage = new self._storageImpl(self.parsedTorrent, self.storageOpts) + self.storage = new self._storageImpl(self.parsedTorrent, self.opts) self.storage.on('piece', self._onStoragePiece.bind(self)) self.storage.on('file', function (file) { self.emit('file', file) |