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

fix: regression that dht could not be enabled through conf #2976

Merged
merged 1 commit into from
Apr 9, 2020
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
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