mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-03 08:11:04 +00:00
* fix: use new libp2p-crypto
* fix: remove node globals and reduce size
- adds buffer require
- adds globalThis where needed
- streaming-iterables was remove and new utils created, this will be consolidated in `ipfs-utils` and backported here to normalize all these iterator helper functions
- latency-monitor was copied inside the repo
- iso-random-stream is now used instead of node crypto
- the test `should ignore self on discovery` was moved to node only
Size: 172.97KB
47.03KB below the 220KB limit.
`aegir build --node false` and `aegir test -t browser --node false` now work 🎉
* fix: fix require path
* fix: feedback
* fix: update deps and bundle size
* chore: bump interfaces
* chore: update size
128 lines
3.2 KiB
JavaScript
128 lines
3.2 KiB
JavaScript
'use strict'
|
|
/* eslint-env mocha */
|
|
|
|
const chai = require('chai')
|
|
chai.use(require('dirty-chai'))
|
|
const { expect } = chai
|
|
const sinon = require('sinon')
|
|
|
|
const defer = require('p-defer')
|
|
const mergeOptions = require('merge-options')
|
|
|
|
const WebRTCStar = require('libp2p-webrtc-star')
|
|
|
|
const Libp2p = require('../../src')
|
|
const baseOptions = require('../utils/base-options.browser')
|
|
const { createPeerInfo } = require('../utils/creators/peer')
|
|
|
|
describe('peer discovery', () => {
|
|
describe('basic functions', () => {
|
|
let peerInfo
|
|
let remotePeerInfo
|
|
let libp2p
|
|
|
|
before(async () => {
|
|
[peerInfo, remotePeerInfo] = await createPeerInfo({ number: 2 })
|
|
})
|
|
|
|
afterEach(async () => {
|
|
libp2p && await libp2p.stop()
|
|
sinon.reset()
|
|
})
|
|
|
|
it('should dial know peers on startup', async () => {
|
|
libp2p = new Libp2p({
|
|
...baseOptions,
|
|
peerInfo
|
|
})
|
|
libp2p.peerStore.add(remotePeerInfo)
|
|
const deferred = defer()
|
|
sinon.stub(libp2p.dialer, 'connectToPeer').callsFake((remotePeerInfo) => {
|
|
expect(remotePeerInfo).to.equal(remotePeerInfo)
|
|
deferred.resolve()
|
|
})
|
|
const spy = sinon.spy()
|
|
libp2p.on('peer:discovery', spy)
|
|
|
|
libp2p.start()
|
|
await deferred.promise
|
|
expect(spy.getCall(0).args).to.eql([remotePeerInfo])
|
|
})
|
|
|
|
it('should stop discovery on libp2p start/stop', async () => {
|
|
const mockDiscovery = {
|
|
tag: 'mock',
|
|
start: () => {},
|
|
stop: () => {},
|
|
on: () => {},
|
|
removeListener: () => {}
|
|
}
|
|
const startSpy = sinon.spy(mockDiscovery, 'start')
|
|
const stopSpy = sinon.spy(mockDiscovery, 'stop')
|
|
|
|
libp2p = new Libp2p(mergeOptions(baseOptions, {
|
|
peerInfo,
|
|
modules: {
|
|
peerDiscovery: [mockDiscovery]
|
|
}
|
|
}))
|
|
|
|
await libp2p.start()
|
|
expect(startSpy).to.have.property('callCount', 1)
|
|
expect(stopSpy).to.have.property('callCount', 0)
|
|
await libp2p.stop()
|
|
expect(startSpy).to.have.property('callCount', 1)
|
|
expect(stopSpy).to.have.property('callCount', 1)
|
|
})
|
|
})
|
|
|
|
describe('discovery modules from transports', () => {
|
|
let peerInfo, libp2p
|
|
|
|
before(async () => {
|
|
[peerInfo] = await createPeerInfo()
|
|
})
|
|
|
|
it('should add discovery module if present in transports and enabled', async () => {
|
|
libp2p = new Libp2p(mergeOptions(baseOptions, {
|
|
peerInfo,
|
|
modules: {
|
|
transport: [WebRTCStar]
|
|
},
|
|
config: {
|
|
peerDiscovery: {
|
|
webRTCStar: {
|
|
enabled: true
|
|
}
|
|
}
|
|
}
|
|
}))
|
|
|
|
await libp2p.start()
|
|
|
|
expect(libp2p._discovery.size).to.eql(1)
|
|
expect(libp2p._discovery.has('webRTCStar')).to.eql(true)
|
|
})
|
|
|
|
it('should not add discovery module if present in transports but disabled', async () => {
|
|
libp2p = new Libp2p(mergeOptions(baseOptions, {
|
|
peerInfo,
|
|
modules: {
|
|
transport: [WebRTCStar]
|
|
},
|
|
config: {
|
|
peerDiscovery: {
|
|
webRTCStar: {
|
|
enabled: false
|
|
}
|
|
}
|
|
}
|
|
}))
|
|
|
|
await libp2p.start()
|
|
|
|
expect(libp2p._discovery.size).to.eql(0)
|
|
})
|
|
})
|
|
})
|