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

fix: dht browser disabled #1879

Merged
merged 1 commit into from
Feb 27, 2019
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"joi": "^14.3.0",
"joi-browser": "^13.4.0",
"joi-multiaddr": "^4.0.0",
"libp2p": "~0.25.0-rc.0",
"libp2p": "~0.25.0-rc.3",
"libp2p-bootstrap": "~0.9.3",
"libp2p-crypto": "~0.16.0",
"libp2p-kad-dht": "~0.14.4",
Expand Down
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
103 changes: 77 additions & 26 deletions test/core/dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,96 @@ 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', () => {
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an error code or message we can assert on?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No! But, I can add it to js-libp2p. However, we will have to bump the rc. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to ensure the error is because the DHT is disabled and not because of anything else. Whatever you can do to make that happen is good 👍

expect(err.code).to.equal('ERR_DHT_DISABLED')
}

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