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

Commit 8141f03

Browse files
committed
chore: use new human-to-milliseconds
1 parent ca7ab95 commit 8141f03

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/core/provider/index.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,29 @@ class Provider {
2121
* @param {string} options.strategy reprovider strategy
2222
*/
2323
constructor (libp2p, blockstore, options = {}) {
24+
// Assert options
25+
this._validateOptions(options)
26+
2427
this._running = false
2528

2629
this._contentRouting = libp2p.contentRouting
2730
this._blockstore = blockstore
28-
this._options = options
29-
this.reprovider = undefined
3031

31-
this._validateOptions()
32+
// handle options (config uses uppercase)
33+
const humanDelay = options.Delay || options.delay || '15s'
34+
const delay = human(humanDelay)
35+
const humanInterval = options.Interval || options.interval || '12h'
36+
const interval = human(humanInterval)
37+
const strategy = options.Strategy || options.strategy || 'all'
38+
39+
this._options = {
40+
delay,
41+
interval,
42+
strategy
43+
}
44+
45+
this.reprovider = new Reprovider(this._contentRouting, this._blockstore, this._options)
46+
3247
}
3348

3449
/**
@@ -43,20 +58,6 @@ class Provider {
4358

4459
this._running = true
4560

46-
// handle options (config uses uppercase)
47-
const humanDelay = this._options.Delay || this._options.delay || '15s'
48-
const delay = await human(humanDelay)
49-
const humanInterval = this._options.Interval || this._options.interval || '12h'
50-
const interval = await human(humanInterval)
51-
const strategy = this._options.Strategy || this._options.strategy || 'all'
52-
const options = {
53-
delay,
54-
interval,
55-
strategy
56-
}
57-
58-
this.reprovider = new Reprovider(this._contentRouting, this._blockstore, options)
59-
6061
// Start reprovider
6162
this.reprovider.start()
6263
}
@@ -106,14 +107,14 @@ class Provider {
106107
}
107108

108109
// Validate Provider options
109-
_validateOptions () {
110-
const delay = (this._options.Delay || this._options.delay)
110+
_validateOptions (options) {
111+
const delay = (options.Delay || options.delay)
111112
assert(delay && parseInt(delay) !== 0, '0 delay is not a valid value for reprovider')
112113

113-
const interval = (this._options.Interval || this._options.interval)
114+
const interval = (options.Interval || options.interval)
114115
assert(interval && parseInt(interval) !== 0, '0 interval is not a valid value for reprovider')
115116

116-
const strategy = (this._options.Strategy || this._options.strategy)
117+
const strategy = (options.Strategy || options.strategy)
117118
assert(strategy && (strategy === 'all' || strategy === 'pinned' || strategy === 'roots'),
118119
'Reprovider must have one of the following strategies: `all`, `pinned` or `roots`')
119120
}

test/core/provider.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ chai.use(dirtyChai)
88
const sinon = require('sinon')
99

1010
const CID = require('cids')
11+
const human = require('human-to-milliseconds')
1112

1213
const IPFS = require('../../src')
1314
const DaemonFactory = require('ipfsd-ctl')
@@ -52,7 +53,7 @@ describe('record provider', () => {
5253

5354
it('should not be running', () => {
5455
expect(node._provider._running).to.equal(false)
55-
expect(node._provider.reprovider).to.not.exist()
56+
expect(node._provider.reprovider._timeoutId).to.not.exist()
5657
})
5758
})
5859

@@ -92,8 +93,8 @@ describe('record provider', () => {
9293
})
9394

9495
it('should use the defaults', () => {
95-
expect(node._provider._options.Interval).to.equal('12h')
96-
expect(node._provider._options.Strategy).to.equal('all')
96+
expect(node._provider._options.interval).to.equal(human('12h'))
97+
expect(node._provider._options.strategy).to.equal('all')
9798
})
9899

99100
it('should be able to provide a valid CIDs', async () => {
@@ -181,8 +182,8 @@ describe('record provider', () => {
181182
})
182183

183184
it('should use the provided configuration', () => {
184-
expect(node._provider._options.Interval).to.equal(INTERVAL)
185-
expect(node._provider._options.Strategy).to.equal(STRATEGY)
185+
expect(node._provider._options.interval).to.equal(human(INTERVAL))
186+
expect(node._provider._options.strategy).to.equal(STRATEGY)
186187
})
187188

188189
it('should reprovide after tens seconds', function (done) {

0 commit comments

Comments
 (0)