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

Commit 7c5a843

Browse files
vasco-santosAlan Shaw
authored and
Alan Shaw
committed
fix: dht browser disabled (#1879)
1 parent 935f943 commit 7c5a843

File tree

3 files changed

+79
-28
lines changed

3 files changed

+79
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
"joi": "^14.3.0",
127127
"joi-browser": "^13.4.0",
128128
"joi-multiaddr": "^4.0.0",
129-
"libp2p": "~0.25.0-rc.0",
129+
"libp2p": "~0.25.0-rc.3",
130130
"libp2p-bootstrap": "~0.9.3",
131131
"libp2p-crypto": "~0.16.0",
132132
"libp2p-kad-dht": "~0.14.4",

src/core/components/libp2p.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
7777
},
7878
dht: {
7979
kBucketSize: get(options, 'dht.kBucketSize', 20),
80-
enabled: get(options, 'dht.enabled', true) && !(get(options, 'offline', false)),
80+
enabled: get(options, 'offline', false) ? false : undefined, // disable if offline
8181
randomWalk: {
8282
enabled: get(options, 'dht.randomWalk.enabled', true)
8383
},

test/core/dht.spec.js

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,96 @@ const dirtyChai = require('dirty-chai')
77
const expect = chai.expect
88
chai.use(dirtyChai)
99

10+
const isNode = require('detect-node')
11+
1012
const IPFSFactory = require('ipfsd-ctl')
1113
const IPFS = require('../../src/core')
1214

1315
describe('dht', () => {
14-
let ipfsd, ipfs
16+
describe('enabled', () => {
17+
let ipfsd, ipfs
18+
19+
before(function (done) {
20+
this.timeout(30 * 1000)
1521

16-
before(function (done) {
17-
this.timeout(30 * 1000)
22+
const factory = IPFSFactory.create({ type: 'proc' })
1823

19-
const factory = IPFSFactory.create({ type: 'proc' })
24+
factory.spawn({
25+
exec: IPFS,
26+
initOptions: { bits: 512 },
27+
config: {
28+
Bootstrap: []
29+
}
30+
}, (err, _ipfsd) => {
31+
expect(err).to.not.exist()
32+
ipfsd = _ipfsd
33+
ipfs = _ipfsd.api
34+
done()
35+
})
36+
})
2037

21-
factory.spawn({
22-
exec: IPFS,
23-
initOptions: { bits: 512 },
24-
config: {
25-
Bootstrap: []
38+
after((done) => {
39+
if (ipfsd) {
40+
ipfsd.stop(done)
41+
} else {
42+
done()
2643
}
27-
}, (err, _ipfsd) => {
28-
expect(err).to.not.exist()
29-
ipfsd = _ipfsd
30-
ipfs = _ipfsd.api
31-
done()
3244
})
33-
})
3445

35-
after((done) => {
36-
if (ipfsd) {
37-
ipfsd.stop(done)
38-
} else {
39-
done()
40-
}
46+
describe('findprovs', () => {
47+
it('should callback with error for invalid CID input', (done) => {
48+
ipfs.dht.findProvs('INVALID CID', (err) => {
49+
expect(err).to.exist()
50+
expect(err.code).to.equal('ERR_INVALID_CID')
51+
done()
52+
})
53+
})
54+
})
4155
})
4256

43-
describe('findprovs', () => {
44-
it('should callback with error for invalid CID input', (done) => {
45-
ipfs.dht.findProvs('INVALID CID', (err) => {
46-
expect(err).to.exist()
47-
expect(err.code).to.equal('ERR_INVALID_CID')
57+
describe('disabled in browser', () => {
58+
if (isNode) { return }
59+
60+
let ipfsd, ipfs
61+
62+
before(function (done) {
63+
this.timeout(30 * 1000)
64+
65+
const factory = IPFSFactory.create({ type: 'proc' })
66+
67+
factory.spawn({
68+
exec: IPFS,
69+
initOptions: { bits: 512 },
70+
config: {
71+
Bootstrap: []
72+
}
73+
}, (err, _ipfsd) => {
74+
expect(err).to.not.exist()
75+
ipfsd = _ipfsd
76+
ipfs = _ipfsd.api
77+
done()
78+
})
79+
})
80+
81+
after((done) => {
82+
if (ipfsd) {
83+
ipfsd.stop(done)
84+
} else {
4885
done()
86+
}
87+
})
88+
89+
describe('put', () => {
90+
it('should callback with error for DHT not available', async () => {
91+
let res
92+
try {
93+
res = await ipfs.dht.put(Buffer.from('a'), Buffer.from('b'))
94+
} catch (err) {
95+
expect(err).to.exist()
96+
expect(err.code).to.equal('ERR_DHT_DISABLED')
97+
}
98+
99+
expect(res).to.not.exist()
49100
})
50101
})
51102
})

0 commit comments

Comments
 (0)