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

Commit 5f66538

Browse files
achingbrainAlan Shaw
authored and
Alan Shaw
committed
fix: allow disabling mfs preload from config (#1733)
1 parent ac5fa8e commit 5f66538

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

src/core/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const schema = Joi.object().keys({
1010
repoOwner: Joi.boolean().default(true),
1111
preload: Joi.object().keys({
1212
enabled: Joi.boolean().default(true),
13-
addresses: Joi.array().items(Joi.multiaddr().options({ convert: false }))
13+
addresses: Joi.array().items(Joi.multiaddr().options({ convert: false })),
14+
interval: Joi.number().integer().default(30 * 1000)
1415
}).allow(null),
1516
init: Joi.alternatives().try(
1617
Joi.boolean(),

src/core/mfs-preload.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ const debug = require('debug')
55
const log = debug('jsipfs:mfs-preload')
66
log.error = debug('jsipfs:mfs-preload:error')
77

8-
module.exports = (self, options) => {
9-
options = options || {}
8+
module.exports = (self) => {
9+
const options = self._options.preload || {}
1010
options.interval = options.interval || 30 * 1000
1111

12+
if (!options.enabled) {
13+
log('MFS preload disabled')
14+
return {
15+
start: (cb) => setImmediate(cb),
16+
stop: (cb) => setImmediate(cb)
17+
}
18+
}
19+
1220
let rootCid
1321
let timeoutId
1422

test/core/mfs-preload.spec.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,36 @@ const createMockPreload = () => {
2525
}
2626

2727
describe('MFS preload', () => {
28+
// CIDs returned from our mock files.stat function
29+
const statCids = ['QmInitial', 'QmSame', 'QmSame', 'QmUpdated']
30+
let mockPreload
31+
let mockFilesStat
32+
let mockIpfs
33+
34+
beforeEach(() => {
35+
mockPreload = createMockPreload()
36+
mockFilesStat = createMockFilesStat(statCids)
37+
mockIpfs = {
38+
files: {
39+
stat: mockFilesStat
40+
},
41+
_preload: mockPreload,
42+
_options: {
43+
preload: {
44+
interval: 10
45+
}
46+
}
47+
}
48+
})
49+
2850
it('should preload MFS root periodically', function (done) {
2951
this.timeout(80 * 1000)
3052

31-
// CIDs returned from our mock files.stat function
32-
const statCids = ['QmInitial', 'QmSame', 'QmSame', 'QmUpdated']
53+
mockIpfs._options.preload.enabled = true
54+
3355
// The CIDs we expect to have been preloaded
3456
const expectedPreloadCids = ['QmSame', 'QmUpdated']
35-
36-
const mockPreload = createMockPreload()
37-
const mockFilesStat = createMockFilesStat(statCids)
38-
const mockIpfs = { files: { stat: mockFilesStat }, _preload: mockPreload }
39-
40-
const interval = 10
41-
const preloader = mfsPreload(mockIpfs, { interval })
57+
const preloader = mfsPreload(mockIpfs)
4258

4359
preloader.start((err) => {
4460
expect(err).to.not.exist()
@@ -56,4 +72,20 @@ describe('MFS preload', () => {
5672
})
5773
})
5874
})
75+
76+
it('should disable preloading MFS', function (done) {
77+
mockIpfs._options.preload.enabled = false
78+
79+
const preloader = mfsPreload(mockIpfs)
80+
81+
preloader.start((err) => {
82+
expect(err).to.not.exist()
83+
84+
setTimeout(() => {
85+
expect(mockPreload.cids).to.be.empty()
86+
87+
done()
88+
}, 500)
89+
})
90+
})
5991
})

0 commit comments

Comments
 (0)