Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
chore: apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-Authored-By: dirkmc <dirkmdev@gmail.com>
  • Loading branch information
vasco-santos and dirkmc committed Jun 28, 2019
1 parent e378ea9 commit 98723a7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
18 changes: 13 additions & 5 deletions src/core/components/start.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const get = require('dlv')
const callbackify = require('callbackify')
const series = require('async/series')
const Bitswap = require('ipfs-bitswap')
const setImmediate = require('async/setImmediate')
Expand Down Expand Up @@ -48,11 +49,23 @@ module.exports = (self) => {
if (err) return cb(err)
self.libp2p = libp2p

// create provider
self._provider = new Provider(libp2p, self._repo.blocks, get(config, 'Reprovider'))
cb()
})
})
},
(cb) => {
// start provider if libp2p routing enabled
if (!get(self._options, 'offline') &&
(get(self._options, 'libp2p.config.dht.enabled', false) || get(self._options, 'libp2p.modules.contentRouting', false))) {
const providerStart = callbackify(() => self._provider.start())

providerStart(cb)
} else {
cb()
}
},
(cb) => {
const ipnsRouting = routingConfig(self)
self._ipns = new IPNS(ipnsRouting, self._repo.datastore, self._peerInfo, self._keychain, self._options)
Expand All @@ -64,11 +77,6 @@ module.exports = (self) => {
{ statsEnabled: true }
)

if (!get(self._options, 'offline') &&
(get(self._options, 'libp2p.config.dht.enabled', false) || get(self._options, 'libp2p.modules.contentRouting', false))) {
self._provider.start()
}

self._bitswap.start()
self._blockService.setExchange(self._bitswap)

Expand Down
18 changes: 17 additions & 1 deletion src/core/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const errCode = require('err-code')
const human = require('human-to-milliseconds')
const promisify = require('promisify-es6')
const assert = require('assert')

const CID = require('cids')

Expand All @@ -26,6 +27,8 @@ class Provider {
this._blockstore = blockstore
this._options = options
this.reprovider = undefined

this._validateOptions()
}

/**
Expand Down Expand Up @@ -63,7 +66,7 @@ class Provider {
* @returns {void}
*/
stop () {
this._running = true
this._running = false

// stop the reprovider
this.reprovider.stop()
Expand Down Expand Up @@ -101,6 +104,19 @@ class Provider {
this._contentRouting.findProviders(cid, options, callback)
})()
}

// Validate Provider options
_validateOptions () {
const delay = (this._options.Delay || this._options.delay)
assert(delay && parseInt(delay) !== 0, '0 delay is not a valid value for reprovider')

const interval = (this._options.Interval || this._options.interval)
assert(interval && parseInt(interval) !== 0, '0 interval is not a valid value for reprovider')

const strategy = (this._options.Strategy || this._options.strategy)
assert(strategy && (strategy === 'all' || strategy === 'pinned' || strategy === 'roots'),
'Reprovider must have one of the following strategies: `all`, `pinned` or `roots`')
}
}

exports = module.exports = Provider
6 changes: 3 additions & 3 deletions src/core/provider/reprovider.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Reprovider {
* @param {object} contentRouting
* @param {Blockstore} blockstore
* @param {object} options
* @param {string} options.delay reprovider initial delay in human friendly time
* @param {string} options.interval reprovider interval in human friendly time
* @param {string} options.delay reprovider initial delay in milliseconds
* @param {string} options.interval reprovider interval in milliseconds
* @param {string} options.strategy reprovider strategy
*/
constructor (contentRouting, blockstore, options) {
Expand Down Expand Up @@ -60,7 +60,7 @@ class Reprovider {

if (this._options.strategy === 'pinned') {

} else if (this._options.strategy === 'pinned') {
} else if (this._options.strategy === 'roots') {

}

Expand Down

0 comments on commit 98723a7

Please sign in to comment.