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

Commit

Permalink
fix: dht browser disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Feb 18, 2019
1 parent c1a5c2f commit c71f506
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/core/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
},
dht: {
kBucketSize: get(options, 'dht.kBucketSize', 20),
enabled: get(options, 'dht.enabled', true) && !(get(options, 'offline', false)),
enabled: get(options, 'offline', false) ? false : undefined, // disable if offline
randomWalk: {
enabled: get(options, 'dht.randomWalk.enabled', true)
},
Expand Down
104 changes: 78 additions & 26 deletions test/core/dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,98 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const isNode = require('detect-node')

const IPFSFactory = require('ipfsd-ctl')
const IPFS = require('../../src/core')

describe('dht', () => {
let ipfsd, ipfs
describe('enabled', () => {
if (!isNode) { return }

let ipfsd, ipfs

before(function (done) {
this.timeout(30 * 1000)

before(function (done) {
this.timeout(30 * 1000)
const factory = IPFSFactory.create({ type: 'proc' })

const factory = IPFSFactory.create({ type: 'proc' })
factory.spawn({
exec: IPFS,
initOptions: { bits: 512 },
config: {
Bootstrap: []
}
}, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = _ipfsd.api
done()
})
})

factory.spawn({
exec: IPFS,
initOptions: { bits: 512 },
config: {
Bootstrap: []
after((done) => {
if (ipfsd) {
ipfsd.stop(done)
} else {
done()
}
}, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = _ipfsd.api
done()
})
})

after((done) => {
if (ipfsd) {
ipfsd.stop(done)
} else {
done()
}
describe('findprovs', () => {
it('should callback with error for invalid CID input', (done) => {
ipfs.dht.findProvs('INVALID CID', (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_INVALID_CID')
done()
})
})
})
})

describe('findprovs', () => {
it('should callback with error for invalid CID input', (done) => {
ipfs.dht.findProvs('INVALID CID', (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_INVALID_CID')
describe('disabled in browser', () => {
if (isNode) { return }

let ipfsd, ipfs

before(function (done) {
this.timeout(30 * 1000)

const factory = IPFSFactory.create({ type: 'proc' })

factory.spawn({
exec: IPFS,
initOptions: { bits: 512 },
config: {
Bootstrap: []
}
}, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = _ipfsd.api
done()
})
})

after((done) => {
if (ipfsd) {
ipfsd.stop(done)
} else {
done()
}
})

describe('put', () => {
it('should callback with error for DHT not available', async () => {
let res
try {
res = await ipfs.dht.put(Buffer.from('a'), Buffer.from('b'))
} catch (err) {
expect(err).to.exist()
}

expect(res).to.not.exist()
})
})
})
})

0 comments on commit c71f506

Please sign in to comment.