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

Commit

Permalink
fix: fails to start when preload disabled
Browse files Browse the repository at this point in the history
fixes #1514

License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
alanshaw committed Aug 18, 2018
1 parent 9368f37 commit 1b33adb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/core/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ module.exports = self => {
options.addresses = options.addresses || []

if (!options.enabled || !options.addresses.length) {
return (_, callback) => {
const api = (_, callback) => {
if (callback) {
setImmediate(() => callback())
}
}
api.start = () => {}
api.stop = () => {}
return api
}

let stopped = true
Expand Down
30 changes: 30 additions & 0 deletions test/core/preload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* eslint-env mocha */
'use strict'

const path = require('path')
const os = require('os')
const hat = require('hat')
const CID = require('cids')
const parallel = require('async/parallel')
Expand All @@ -19,6 +21,7 @@ describe('preload', () => {

before((done) => {
ipfs = new IPFS({
repo: path.join(os.tmpdir(), hat()),
config: {
Addresses: {
Swarm: []
Expand Down Expand Up @@ -286,4 +289,31 @@ describe('preload', () => {
})
})
})

it('should not preload if disabled', (done) => {
const ipfs = new IPFS({
repo: path.join(os.tmpdir(), hat()),
config: {
Addresses: {
Swarm: []
}
},
preload: {
enabled: false,
addresses: [MockPreloadNode.defaultAddr]
}
})

ipfs.on('ready', () => {
ipfs.files.add(Buffer.from(hat()), (err, res) => {
expect(err).to.not.exist()

MockPreloadNode.waitForCids(res[0].hash, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_TIMEOUT')
done()
})
})
})
})
})
5 changes: 3 additions & 2 deletions test/utils/mock-preload-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const http = require('http')
const toUri = require('multiaddr-to-uri')
const URL = require('url').URL || self.URL
const errCode = require('err-code')

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

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

setTimeout(checkForCid, 10)
setTimeout(checkForCid, 5)
})
}

Expand Down

0 comments on commit 1b33adb

Please sign in to comment.