From 7c5a8439bc9b05562397dbaf0ab61a2aa33e1816 Mon Sep 17 00:00:00 2001 From: Vasco Santos Date: Wed, 27 Feb 2019 16:54:05 +0000 Subject: [PATCH] fix: dht browser disabled (#1879) --- package.json | 2 +- src/core/components/libp2p.js | 2 +- test/core/dht.spec.js | 103 +++++++++++++++++++++++++--------- 3 files changed, 79 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 6ca11e0338..58a9b27f8b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/core/components/libp2p.js b/src/core/components/libp2p.js index c45b65f625..24754b02bf 100644 --- a/src/core/components/libp2p.js +++ b/src/core/components/libp2p.js @@ -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) }, diff --git a/test/core/dht.spec.js b/test/core/dht.spec.js index 01812cc8bb..b1a3207429 100644 --- a/test/core/dht.spec.js +++ b/test/core/dht.spec.js @@ -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() + expect(err.code).to.equal('ERR_DHT_DISABLED') + } + + expect(res).to.not.exist() }) }) })