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

Commit 511ab47

Browse files
authored
fix: fails to start when preload disabled (#1516)
fixes #1514 License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent 9368f37 commit 511ab47

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/core/preload.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ module.exports = self => {
1818
options.addresses = options.addresses || []
1919

2020
if (!options.enabled || !options.addresses.length) {
21-
return (_, callback) => {
21+
const api = (_, callback) => {
2222
if (callback) {
2323
setImmediate(() => callback())
2424
}
2525
}
26+
api.start = () => {}
27+
api.stop = () => {}
28+
return api
2629
}
2730

2831
let stopped = true

test/core/preload.spec.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/* eslint-env mocha */
33
'use strict'
44

5+
const path = require('path')
6+
const os = require('os')
57
const hat = require('hat')
68
const CID = require('cids')
79
const parallel = require('async/parallel')
@@ -17,8 +19,11 @@ const IPFS = require('../../src')
1719
describe('preload', () => {
1820
let ipfs
1921

20-
before((done) => {
22+
before(function (done) {
23+
this.timeout(10 * 1000)
24+
2125
ipfs = new IPFS({
26+
repo: path.join(os.tmpdir(), hat()),
2227
config: {
2328
Addresses: {
2429
Swarm: []
@@ -286,4 +291,33 @@ describe('preload', () => {
286291
})
287292
})
288293
})
294+
295+
it('should not preload if disabled', function (done) {
296+
this.timeout(10 * 1000)
297+
298+
const ipfs = new IPFS({
299+
repo: path.join(os.tmpdir(), hat()),
300+
config: {
301+
Addresses: {
302+
Swarm: []
303+
}
304+
},
305+
preload: {
306+
enabled: false,
307+
addresses: [MockPreloadNode.defaultAddr]
308+
}
309+
})
310+
311+
ipfs.on('ready', () => {
312+
ipfs.files.add(Buffer.from(hat()), (err, res) => {
313+
expect(err).to.not.exist()
314+
315+
MockPreloadNode.waitForCids(res[0].hash, (err) => {
316+
expect(err).to.exist()
317+
expect(err.code).to.equal('ERR_TIMEOUT')
318+
done()
319+
})
320+
})
321+
})
322+
})
289323
})

test/utils/mock-preload-node.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const http = require('http')
55
const toUri = require('multiaddr-to-uri')
66
const URL = require('url').URL || self.URL
7+
const errCode = require('err-code')
78

89
const defaultPort = 1138
910
const defaultAddr = `/dnsaddr/localhost/tcp/${defaultPort}`
@@ -146,10 +147,10 @@ module.exports.waitForCids = (cids, opts, cb) => {
146147
}
147148

148149
if (Date.now() > start + opts.timeout) {
149-
return cb(new Error('Timed out waiting for CIDs to be preloaded'))
150+
return cb(errCode(new Error('Timed out waiting for CIDs to be preloaded'), 'ERR_TIMEOUT'))
150151
}
151152

152-
setTimeout(checkForCid, 10)
153+
setTimeout(checkForCid, 5)
153154
})
154155
}
155156

0 commit comments

Comments
 (0)