mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 10:32:14 +00:00
fix: update pubsub interfaces (#1194)
Update to latest version of pubsub interface
This commit is contained in:
parent
5397137c65
commit
fab4f1385c
@ -10,7 +10,7 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@libp2p/pubsub-peer-discovery": "^5.0.1",
|
"@libp2p/pubsub-peer-discovery": "^5.0.1",
|
||||||
"@libp2p/floodsub": "^1.0.5",
|
"@libp2p/floodsub": "^1.0.6",
|
||||||
"execa": "^2.1.0",
|
"execa": "^2.1.0",
|
||||||
"fs-extra": "^8.1.0",
|
"fs-extra": "^8.1.0",
|
||||||
"libp2p": "../",
|
"libp2p": "../",
|
||||||
|
@ -48,6 +48,8 @@ const createNode = async () => {
|
|||||||
|
|
||||||
// node2 publishes "news" every second
|
// node2 publishes "news" every second
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!'))
|
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!')).catch(err => {
|
||||||
|
console.error(err)
|
||||||
|
})
|
||||||
}, 1000)
|
}, 1000)
|
||||||
})()
|
})()
|
||||||
|
@ -69,7 +69,9 @@ await node2.pubsub.subscribe(topic)
|
|||||||
|
|
||||||
// node2 publishes "news" every second
|
// node2 publishes "news" every second
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
node2.pubsub.publish(topic, fromString('Bird bird bird, bird is the word!'))
|
node2.pubsub.publish(topic, fromString('Bird bird bird, bird is the word!')).catch(err => {
|
||||||
|
console.error(err)
|
||||||
|
})
|
||||||
}, 1000)
|
}, 1000)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -74,7 +74,9 @@ const createNode = async () => {
|
|||||||
// car is not a fruit !
|
// car is not a fruit !
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
console.log('############## fruit ' + myFruits[count] + ' ##############')
|
console.log('############## fruit ' + myFruits[count] + ' ##############')
|
||||||
node1.pubsub.publish(topic, uint8ArrayFromString(myFruits[count]))
|
node1.pubsub.publish(topic, uint8ArrayFromString(myFruits[count])).catch(err => {
|
||||||
|
console.info(err)
|
||||||
|
})
|
||||||
count++
|
count++
|
||||||
if (count == myFruits.length) {
|
if (count == myFruits.length) {
|
||||||
count = 0
|
count = 0
|
||||||
|
@ -88,7 +88,9 @@ const myFruits = ['banana', 'apple', 'car', 'orange'];
|
|||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
console.log('############## fruit ' + myFruits[count] + ' ##############')
|
console.log('############## fruit ' + myFruits[count] + ' ##############')
|
||||||
node1.pubsub.publish(topic, new TextEncoder().encode(myFruits[count]))
|
node1.pubsub.publish(topic, new TextEncoder().encode(myFruits[count])).catch(err => {
|
||||||
|
console.error(err)
|
||||||
|
})
|
||||||
count++
|
count++
|
||||||
if (count == myFruits.length) {
|
if (count == myFruits.length) {
|
||||||
count = 0
|
count = 0
|
||||||
|
@ -98,7 +98,7 @@
|
|||||||
"@achingbrain/nat-port-mapper": "^1.0.0",
|
"@achingbrain/nat-port-mapper": "^1.0.0",
|
||||||
"@libp2p/connection": "^1.1.5",
|
"@libp2p/connection": "^1.1.5",
|
||||||
"@libp2p/crypto": "^0.22.11",
|
"@libp2p/crypto": "^0.22.11",
|
||||||
"@libp2p/interfaces": "^1.3.22",
|
"@libp2p/interfaces": "^1.3.24",
|
||||||
"@libp2p/logger": "^1.1.4",
|
"@libp2p/logger": "^1.1.4",
|
||||||
"@libp2p/multistream-select": "^1.0.4",
|
"@libp2p/multistream-select": "^1.0.4",
|
||||||
"@libp2p/peer-id": "^1.1.10",
|
"@libp2p/peer-id": "^1.1.10",
|
||||||
@ -162,8 +162,8 @@
|
|||||||
"@libp2p/daemon-server": "^1.0.2",
|
"@libp2p/daemon-server": "^1.0.2",
|
||||||
"@libp2p/delegated-content-routing": "^1.0.2",
|
"@libp2p/delegated-content-routing": "^1.0.2",
|
||||||
"@libp2p/delegated-peer-routing": "^1.0.2",
|
"@libp2p/delegated-peer-routing": "^1.0.2",
|
||||||
"@libp2p/floodsub": "^1.0.5",
|
"@libp2p/floodsub": "^1.0.6",
|
||||||
"@libp2p/interface-compliance-tests": "^1.1.23",
|
"@libp2p/interface-compliance-tests": "^1.1.25",
|
||||||
"@libp2p/interop": "^1.0.3",
|
"@libp2p/interop": "^1.0.3",
|
||||||
"@libp2p/kad-dht": "^1.0.7",
|
"@libp2p/kad-dht": "^1.0.7",
|
||||||
"@libp2p/mdns": "^1.0.4",
|
"@libp2p/mdns": "^1.0.4",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { EventEmitter } from '@libp2p/interfaces'
|
import { EventEmitter } from '@libp2p/interfaces'
|
||||||
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
||||||
import type { PubSub, PubSubEvents, StrictNoSign, StrictSign } from '@libp2p/interfaces/pubsub'
|
import type { PublishResult, PubSub, PubSubEvents, StrictNoSign, StrictSign } from '@libp2p/interfaces/pubsub'
|
||||||
import errCode from 'err-code'
|
import errCode from 'err-code'
|
||||||
import { messages, codes } from '../errors.js'
|
import { messages, codes } from '../errors.js'
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ export class DummyPubSub extends EventEmitter<PubSubEvents> implements PubSub {
|
|||||||
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
|
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
|
||||||
}
|
}
|
||||||
|
|
||||||
publish (): void {
|
async publish (): Promise<PublishResult> {
|
||||||
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
|
throw errCode(new Error(messages.PUBSUB_DISABLED), codes.ERR_PUBSUB_DISABLED)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -90,12 +90,12 @@ describe('Pubsub subscription handlers adapter', () => {
|
|||||||
|
|
||||||
pubsub.subscribe(topic)
|
pubsub.subscribe(topic)
|
||||||
pubsub.addEventListener('message', handler)
|
pubsub.addEventListener('message', handler)
|
||||||
pubsub.publish(topic, uint8ArrayFromString('useless-data'))
|
await pubsub.publish(topic, uint8ArrayFromString('useless-data'))
|
||||||
await defer.promise
|
await defer.promise
|
||||||
|
|
||||||
pubsub.unsubscribe(topic)
|
pubsub.unsubscribe(topic)
|
||||||
pubsub.removeEventListener('message', handler)
|
pubsub.removeEventListener('message', handler)
|
||||||
pubsub.publish(topic, uint8ArrayFromString('useless-data'))
|
await pubsub.publish(topic, uint8ArrayFromString('useless-data'))
|
||||||
|
|
||||||
// wait to guarantee that the handler is not called twice
|
// wait to guarantee that the handler is not called twice
|
||||||
await delay(100)
|
await delay(100)
|
||||||
|
@ -5,7 +5,7 @@ import { WebSockets } from '@libp2p/websockets'
|
|||||||
import * as filters from '@libp2p/websockets/filters'
|
import * as filters from '@libp2p/websockets/filters'
|
||||||
import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js'
|
import { MULTIADDRS_WEBSOCKETS } from '../fixtures/browser.js'
|
||||||
import mergeOptions from 'merge-options'
|
import mergeOptions from 'merge-options'
|
||||||
import type { Message, PubSubInit, PubSubRPC, PubSubRPCMessage } from '@libp2p/interfaces/pubsub'
|
import type { Message, PublishResult, PubSubInit, PubSubRPC, PubSubRPCMessage } from '@libp2p/interfaces/pubsub'
|
||||||
import type { Libp2pInit, Libp2pOptions } from '../../src/index.js'
|
import type { Libp2pInit, Libp2pOptions } from '../../src/index.js'
|
||||||
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
||||||
import * as cborg from 'cborg'
|
import * as cborg from 'cborg'
|
||||||
@ -44,11 +44,12 @@ class MockPubSub extends PubSubBaseProtocol {
|
|||||||
return cborg.encode(rpc)
|
return cborg.encode(rpc)
|
||||||
}
|
}
|
||||||
|
|
||||||
async publishMessage (from: PeerId, message: Message): Promise<void> {
|
async publishMessage (from: PeerId, message: Message): Promise<PublishResult> {
|
||||||
const peers = this.getSubscribers(message.topic)
|
const peers = this.getSubscribers(message.topic)
|
||||||
|
const recipients: PeerId[] = []
|
||||||
|
|
||||||
if (peers == null || peers.length === 0) {
|
if (peers == null || peers.length === 0) {
|
||||||
return
|
return { recipients }
|
||||||
}
|
}
|
||||||
|
|
||||||
peers.forEach(id => {
|
peers.forEach(id => {
|
||||||
@ -60,8 +61,11 @@ class MockPubSub extends PubSubBaseProtocol {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
recipients.push(id)
|
||||||
this.send(id, { messages: [message] })
|
this.send(id, { messages: [message] })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return { recipients }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,13 +114,7 @@ describe('Identify', () => {
|
|||||||
await localIdentify.start()
|
await localIdentify.start()
|
||||||
await remoteIdentify.start()
|
await remoteIdentify.start()
|
||||||
|
|
||||||
const [localToRemote] = connectionPair({
|
const [localToRemote] = connectionPair(localComponents, remoteComponents)
|
||||||
peerId: localComponents.getPeerId(),
|
|
||||||
registrar: localComponents.getRegistrar()
|
|
||||||
}, {
|
|
||||||
peerId: remoteComponents.getPeerId(),
|
|
||||||
registrar: remoteComponents.getRegistrar()
|
|
||||||
})
|
|
||||||
|
|
||||||
const localAddressBookConsumePeerRecordSpy = sinon.spy(localComponents.getPeerStore().addressBook, 'consumePeerRecord')
|
const localAddressBookConsumePeerRecordSpy = sinon.spy(localComponents.getPeerStore().addressBook, 'consumePeerRecord')
|
||||||
const localProtoBookSetSpy = sinon.spy(localComponents.getPeerStore().protoBook, 'set')
|
const localProtoBookSetSpy = sinon.spy(localComponents.getPeerStore().protoBook, 'set')
|
||||||
@ -161,13 +155,7 @@ describe('Identify', () => {
|
|||||||
})
|
})
|
||||||
await remoteIdentify.start()
|
await remoteIdentify.start()
|
||||||
|
|
||||||
const [localToRemote] = connectionPair({
|
const [localToRemote] = connectionPair(localComponents, remoteComponents)
|
||||||
peerId: localComponents.getPeerId(),
|
|
||||||
registrar: localComponents.getRegistrar()
|
|
||||||
}, {
|
|
||||||
peerId: remoteComponents.getPeerId(),
|
|
||||||
registrar: remoteComponents.getRegistrar()
|
|
||||||
})
|
|
||||||
|
|
||||||
sinon.stub(localComponents.getPeerStore().addressBook, 'consumePeerRecord').throws()
|
sinon.stub(localComponents.getPeerStore().addressBook, 'consumePeerRecord').throws()
|
||||||
|
|
||||||
@ -194,13 +182,7 @@ describe('Identify', () => {
|
|||||||
await localIdentify.start()
|
await localIdentify.start()
|
||||||
await remoteIdentify.start()
|
await remoteIdentify.start()
|
||||||
|
|
||||||
const [localToRemote] = connectionPair({
|
const [localToRemote] = connectionPair(localComponents, remoteComponents)
|
||||||
peerId: localComponents.getPeerId(),
|
|
||||||
registrar: localComponents.getRegistrar()
|
|
||||||
}, {
|
|
||||||
peerId: remoteComponents.getPeerId(),
|
|
||||||
registrar: remoteComponents.getRegistrar()
|
|
||||||
})
|
|
||||||
|
|
||||||
// send an invalid message
|
// send an invalid message
|
||||||
await remoteComponents.getRegistrar().unhandle(MULTICODEC_IDENTIFY)
|
await remoteComponents.getRegistrar().unhandle(MULTICODEC_IDENTIFY)
|
||||||
@ -267,13 +249,7 @@ describe('Identify', () => {
|
|||||||
await localIdentify.start()
|
await localIdentify.start()
|
||||||
await remoteIdentify.start()
|
await remoteIdentify.start()
|
||||||
|
|
||||||
const [localToRemote, remoteToLocal] = connectionPair({
|
const [localToRemote, remoteToLocal] = connectionPair(localComponents, remoteComponents)
|
||||||
peerId: localComponents.getPeerId(),
|
|
||||||
registrar: localComponents.getRegistrar()
|
|
||||||
}, {
|
|
||||||
peerId: remoteComponents.getPeerId(),
|
|
||||||
registrar: remoteComponents.getRegistrar()
|
|
||||||
})
|
|
||||||
|
|
||||||
// ensure connections are registered by connection manager
|
// ensure connections are registered by connection manager
|
||||||
localComponents.getUpgrader().dispatchEvent(new CustomEvent('connection', {
|
localComponents.getUpgrader().dispatchEvent(new CustomEvent('connection', {
|
||||||
@ -353,13 +329,7 @@ describe('Identify', () => {
|
|||||||
await localIdentify.start()
|
await localIdentify.start()
|
||||||
await remoteIdentify.start()
|
await remoteIdentify.start()
|
||||||
|
|
||||||
const [localToRemote, remoteToLocal] = connectionPair({
|
const [localToRemote, remoteToLocal] = connectionPair(localComponents, remoteComponents)
|
||||||
peerId: localComponents.getPeerId(),
|
|
||||||
registrar: localComponents.getRegistrar()
|
|
||||||
}, {
|
|
||||||
peerId: remoteComponents.getPeerId(),
|
|
||||||
registrar: remoteComponents.getRegistrar()
|
|
||||||
})
|
|
||||||
|
|
||||||
// ensure connections are registered by connection manager
|
// ensure connections are registered by connection manager
|
||||||
localComponents.getUpgrader().dispatchEvent(new CustomEvent('connection', {
|
localComponents.getUpgrader().dispatchEvent(new CustomEvent('connection', {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user