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

fix: fails to start when preload disabled #1516

Merged
merged 3 commits into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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