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 (#1516)
Browse files Browse the repository at this point in the history
fixes #1514

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
alanshaw authored Aug 20, 2018
1 parent 9368f37 commit 511ab47
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 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
36 changes: 35 additions & 1 deletion 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 @@ -17,8 +19,11 @@ const IPFS = require('../../src')
describe('preload', () => {
let ipfs

before((done) => {
before(function (done) {
this.timeout(10 * 1000)

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

it('should not preload if disabled', function (done) {
this.timeout(10 * 1000)

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 511ab47

Please sign in to comment.