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

Commit c71f506

Browse files
committed
fix: dht browser disabled
1 parent c1a5c2f commit c71f506

File tree

2 files changed

+79
-27
lines changed

2 files changed

+79
-27
lines changed

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: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,98 @@ 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+
if (!isNode) { return }
18+
19+
let ipfsd, ipfs
20+
21+
before(function (done) {
22+
this.timeout(30 * 1000)
1523

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

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

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

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

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')
59+
describe('disabled in browser', () => {
60+
if (isNode) { return }
61+
62+
let ipfsd, ipfs
63+
64+
before(function (done) {
65+
this.timeout(30 * 1000)
66+
67+
const factory = IPFSFactory.create({ type: 'proc' })
68+
69+
factory.spawn({
70+
exec: IPFS,
71+
initOptions: { bits: 512 },
72+
config: {
73+
Bootstrap: []
74+
}
75+
}, (err, _ipfsd) => {
76+
expect(err).to.not.exist()
77+
ipfsd = _ipfsd
78+
ipfs = _ipfsd.api
4879
done()
4980
})
5081
})
82+
83+
after((done) => {
84+
if (ipfsd) {
85+
ipfsd.stop(done)
86+
} else {
87+
done()
88+
}
89+
})
90+
91+
describe('put', () => {
92+
it('should callback with error for DHT not available', async () => {
93+
let res
94+
try {
95+
res = await ipfs.dht.put(Buffer.from('a'), Buffer.from('b'))
96+
} catch (err) {
97+
expect(err).to.exist()
98+
}
99+
100+
expect(res).to.not.exist()
101+
})
102+
})
51103
})
52104
})

0 commit comments

Comments
 (0)