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

Commit

Permalink
fix: regression that dht could not be enabled through conf (#2976)
Browse files Browse the repository at this point in the history
When the API Manager code came in, the DHT component seem to have been
missed from the `start` component, this PR re-adds it.
  • Loading branch information
achingbrain authored Apr 9, 2020
1 parent b6a737f commit 9d88a2e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/ipfs/src/core/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ exports.dag = {
resolve: require('./dag/resolve'),
tree: require('./dag/tree')
}
exports.dht = require('./dht')
exports.dns = require('./dns')
exports.files = require('./files')
exports.get = require('./get')
Expand Down
1 change: 0 additions & 1 deletion packages/ipfs/src/core/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = ({
options,
peerInfo,
repo,
print,
config
}) => {
options = options || {}
Expand Down
15 changes: 15 additions & 0 deletions packages/ipfs/src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ function createApi ({
dag.put = Components.dag.put({ ipld, pin, gcLock, preload })
const add = Components.add({ ipld, preload, pin, gcLock, options: constructorOptions })
const isOnline = Components.isOnline({ libp2p })

const dhtNotEnabled = async () => { // eslint-disable-line require-await
throw new NotEnabledError('dht not enabled')
}

const dht = get(libp2p, '_config.dht.enabled', false) ? Components.dht({ libp2p, repo }) : {
get: dhtNotEnabled,
put: dhtNotEnabled,
findProvs: dhtNotEnabled,
findPeer: dhtNotEnabled,
provide: dhtNotEnabled,
query: dhtNotEnabled
}

const dns = Components.dns()
const name = {
pubsub: {
Expand Down Expand Up @@ -209,6 +223,7 @@ function createApi ({
cat: Components.cat({ ipld, preload }),
config: Components.config({ repo }),
dag,
dht,
dns,
files,
get: Components.get({ ipld, preload }),
Expand Down
64 changes: 56 additions & 8 deletions packages/ipfs/test/core/dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,41 @@

const { expect } = require('interface-ipfs-core/src/utils/mocha')
const { isNode } = require('ipfs-utils/src/env')

const factory = require('../utils/factory')

// TODO: unskip when DHT is enabled: https://github.com/ipfs/js-ipfs/pull/1994
describe.skip('dht', () => {
describe('enabled', () => {
describe('dht', () => {
describe('enabled by config', () => {
const df = factory()
let ipfsd, ipfs

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

ipfsd = await df.spawn()
ipfsd = await df.spawn({
ipfsOptions: {
libp2p: {
config: {
dht: {
enabled: true
}
}
}
}
})
ipfs = ipfsd.api
})

after(() => df.clean())

describe('findprovs', () => {
describe('put', () => {
it('should put a value when enabled', async () => {
await expect(ipfs.dht.put(Buffer.from('a'), Buffer.from('b')))
.to.eventually.be.undefined()
})
})

// TODO: unskip when DHT works: https://github.com/ipfs/js-ipfs/pull/1994
describe.skip('findprovs', () => {
it('should callback with error for invalid CID input', (done) => {
ipfs.dht.findProvs('INVALID CID', (err) => {
expect(err).to.exist()
Expand All @@ -33,12 +49,44 @@ describe.skip('dht', () => {
})
})

describe('disabled by config', () => {
const df = factory()
let ipfsd, ipfs

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

ipfsd = await df.spawn({
ipfsOptions: {
libp2p: {
config: {
dht: {
enabled: false
}
}
}
}
})
ipfs = ipfsd.api
})

after(() => df.clean())

describe('put', () => {
it('should error when DHT not available', async () => {
await expect(ipfs.dht.put(Buffer.from('a'), Buffer.from('b')))
.to.eventually.be.rejected()
.and.to.have.property('code', 'ERR_NOT_ENABLED')
})
})
})

describe('disabled in browser', () => {
if (isNode) { return }
const df = factory()
let ipfsd, ipfs

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

ipfsd = await df.spawn()
Expand All @@ -51,7 +99,7 @@ describe.skip('dht', () => {
it('should error when DHT not available', async () => {
await expect(ipfs.dht.put(Buffer.from('a'), Buffer.from('b')))
.to.eventually.be.rejected()
.and.to.have.property('code', 'ERR_DHT_DISABLED')
.and.to.have.property('code', 'ERR_NOT_ENABLED')
})
})
})
Expand Down

0 comments on commit 9d88a2e

Please sign in to comment.