mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-19 04:01:24 +00:00
chore: address review
This commit is contained in:
@ -46,6 +46,12 @@ module.exports = (node, Pubsub, config) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Publish messages to the given topics.
|
||||||
|
* @param {Array<string>|string} topic
|
||||||
|
* @param {Buffer} data
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
*/
|
||||||
publish: (topic, data) => {
|
publish: (topic, data) => {
|
||||||
if (!node.isStarted() && !pubsub.started) {
|
if (!node.isStarted() && !pubsub.started) {
|
||||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
||||||
@ -60,6 +66,10 @@ module.exports = (node, Pubsub, config) => {
|
|||||||
return pubsub.publish(topic, data)
|
return pubsub.publish(topic, data)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of topics the node is subscribed to.
|
||||||
|
* @returns {Array<String>} topics
|
||||||
|
*/
|
||||||
getTopics: () => {
|
getTopics: () => {
|
||||||
if (!node.isStarted() && !pubsub.started) {
|
if (!node.isStarted() && !pubsub.started) {
|
||||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
||||||
@ -68,6 +78,11 @@ module.exports = (node, Pubsub, config) => {
|
|||||||
return pubsub.getTopics()
|
return pubsub.getTopics()
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of the peer-ids that are subscribed to one topic.
|
||||||
|
* @param {string} topic
|
||||||
|
* @returns {Array<string>}
|
||||||
|
*/
|
||||||
getPeersSubscribed: (topic) => {
|
getPeersSubscribed: (topic) => {
|
||||||
if (!node.isStarted() && !pubsub.started) {
|
if (!node.isStarted() && !pubsub.started) {
|
||||||
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
throw errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED)
|
||||||
|
@ -6,12 +6,11 @@ chai.use(require('dirty-chai'))
|
|||||||
const { expect } = chai
|
const { expect } = chai
|
||||||
|
|
||||||
const mergeOptions = require('merge-options')
|
const mergeOptions = require('merge-options')
|
||||||
|
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const PeerInfo = require('peer-info')
|
|
||||||
|
|
||||||
const { create } = require('../../src')
|
const { create } = require('../../src')
|
||||||
const { baseOptions, subsystemOptions } = require('./utils')
|
const { baseOptions, subsystemOptions } = require('./utils')
|
||||||
|
const peerUtils = require('../utils/creators/peer')
|
||||||
|
|
||||||
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
|
|
||||||
@ -33,7 +32,7 @@ describe('Pubsub subsystem is configurable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should start and stop by default once libp2p starts', async () => {
|
it('should start and stop by default once libp2p starts', async () => {
|
||||||
const peerInfo = await PeerInfo.create()
|
const [peerInfo] = await peerUtils.createPeerInfoFromFixture(1)
|
||||||
peerInfo.multiaddrs.add(listenAddr)
|
peerInfo.multiaddrs.add(listenAddr)
|
||||||
|
|
||||||
const customOptions = mergeOptions(subsystemOptions, {
|
const customOptions = mergeOptions(subsystemOptions, {
|
||||||
@ -51,7 +50,7 @@ describe('Pubsub subsystem is configurable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should not start if disabled once libp2p starts', async () => {
|
it('should not start if disabled once libp2p starts', async () => {
|
||||||
const peerInfo = await PeerInfo.create()
|
const [peerInfo] = await peerUtils.createPeerInfoFromFixture(1)
|
||||||
peerInfo.multiaddrs.add(listenAddr)
|
peerInfo.multiaddrs.add(listenAddr)
|
||||||
|
|
||||||
const customOptions = mergeOptions(subsystemOptions, {
|
const customOptions = mergeOptions(subsystemOptions, {
|
||||||
@ -71,7 +70,7 @@ describe('Pubsub subsystem is configurable', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('should allow a manual start', async () => {
|
it('should allow a manual start', async () => {
|
||||||
const peerInfo = await PeerInfo.create()
|
const [peerInfo] = await peerUtils.createPeerInfoFromFixture(1)
|
||||||
peerInfo.multiaddrs.add(listenAddr)
|
peerInfo.multiaddrs.add(listenAddr)
|
||||||
|
|
||||||
const customOptions = mergeOptions(subsystemOptions, {
|
const customOptions = mergeOptions(subsystemOptions, {
|
||||||
|
@ -15,10 +15,10 @@ const { multicodec: floodsubMulticodec } = require('libp2p-floodsub')
|
|||||||
const { multicodec: gossipsubMulticodec } = require('libp2p-gossipsub')
|
const { multicodec: gossipsubMulticodec } = require('libp2p-gossipsub')
|
||||||
|
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const PeerInfo = require('peer-info')
|
|
||||||
|
|
||||||
const { create } = require('../../src')
|
const { create } = require('../../src')
|
||||||
const { baseOptions } = require('./utils')
|
const { baseOptions } = require('./utils')
|
||||||
|
const peerUtils = require('../utils/creators/peer')
|
||||||
|
|
||||||
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
const remoteListenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
const remoteListenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
@ -29,10 +29,7 @@ describe('Pubsub subsystem is able to use different implementations', () => {
|
|||||||
let remAddr
|
let remAddr
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
[peerInfo, remotePeerInfo] = await Promise.all([
|
[peerInfo, remotePeerInfo] = await peerUtils.createPeerInfoFromFixture(2)
|
||||||
PeerInfo.create(),
|
|
||||||
PeerInfo.create()
|
|
||||||
])
|
|
||||||
|
|
||||||
peerInfo.multiaddrs.add(listenAddr)
|
peerInfo.multiaddrs.add(listenAddr)
|
||||||
remotePeerInfo.multiaddrs.add(remoteListenAddr)
|
remotePeerInfo.multiaddrs.add(remoteListenAddr)
|
||||||
@ -79,8 +76,6 @@ describe('Pubsub subsystem is able to use different implementations', () => {
|
|||||||
remAddr = remoteLibp2p.transportManager.getAddrs()[0]
|
remAddr = remoteLibp2p.transportManager.getAddrs()[0]
|
||||||
|
|
||||||
const connection = await libp2p.dialProtocol(remAddr, multicodec)
|
const connection = await libp2p.dialProtocol(remAddr, multicodec)
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
||||||
expect(connection).to.exist()
|
expect(connection).to.exist()
|
||||||
|
|
||||||
libp2p.pubsub.subscribe(topic, (msg) => {
|
libp2p.pubsub.subscribe(topic, (msg) => {
|
||||||
@ -93,9 +88,8 @@ describe('Pubsub subsystem is able to use different implementations', () => {
|
|||||||
const subscribedPeers = remoteLibp2p.pubsub.getPeersSubscribed(topic)
|
const subscribedPeers = remoteLibp2p.pubsub.getPeersSubscribed(topic)
|
||||||
return subscribedPeers.includes(libp2pId)
|
return subscribedPeers.includes(libp2pId)
|
||||||
})
|
})
|
||||||
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
||||||
remoteLibp2p.pubsub.publish(topic, data)
|
|
||||||
|
|
||||||
|
remoteLibp2p.pubsub.publish(topic, data)
|
||||||
await defer.promise
|
await defer.promise
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -9,12 +9,11 @@ const sinon = require('sinon')
|
|||||||
const pWaitFor = require('p-wait-for')
|
const pWaitFor = require('p-wait-for')
|
||||||
const pDefer = require('p-defer')
|
const pDefer = require('p-defer')
|
||||||
const mergeOptions = require('merge-options')
|
const mergeOptions = require('merge-options')
|
||||||
|
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const PeerInfo = require('peer-info')
|
|
||||||
|
|
||||||
const { create } = require('../../src')
|
const { create } = require('../../src')
|
||||||
const { subsystemOptions, subsystemMulticodecs } = require('./utils')
|
const { subsystemOptions, subsystemMulticodecs } = require('./utils')
|
||||||
|
const peerUtils = require('../utils/creators/peer')
|
||||||
|
|
||||||
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
const remoteListenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
const remoteListenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
@ -25,10 +24,7 @@ describe('Pubsub subsystem operates correctly', () => {
|
|||||||
let remAddr
|
let remAddr
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
[peerInfo, remotePeerInfo] = await Promise.all([
|
[peerInfo, remotePeerInfo] = await peerUtils.createPeerInfoFromFixture(2)
|
||||||
PeerInfo.create(),
|
|
||||||
PeerInfo.create()
|
|
||||||
])
|
|
||||||
|
|
||||||
peerInfo.multiaddrs.add(listenAddr)
|
peerInfo.multiaddrs.add(listenAddr)
|
||||||
remotePeerInfo.multiaddrs.add(remoteListenAddr)
|
remotePeerInfo.multiaddrs.add(remoteListenAddr)
|
||||||
@ -44,8 +40,10 @@ describe('Pubsub subsystem operates correctly', () => {
|
|||||||
peerInfo: remotePeerInfo
|
peerInfo: remotePeerInfo
|
||||||
}))
|
}))
|
||||||
|
|
||||||
await libp2p.start()
|
await Promise.all([
|
||||||
await remoteLibp2p.start()
|
libp2p.start(),
|
||||||
|
remoteLibp2p.start()
|
||||||
|
])
|
||||||
|
|
||||||
remAddr = remoteLibp2p.transportManager.getAddrs()[0]
|
remAddr = remoteLibp2p.transportManager.getAddrs()[0]
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user