Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 9916896

Browse files
authored
fix!: remove @libp2p/components (#192)
`@libp2p/components` is a choke-point for our dependency graph as it depends on every interface, meaning when one interface revs a major `@libp2p/components` major has to change too which means every module depending on it also needs a major. Switch instead to constructor injection of simple objects that let modules declare their dependencies on interfaces directly instead of indirectly via `@libp2p/components` Refs libp2p/js-libp2p-components#6 BREAKING CHANGE: modules no longer implement `Initializable` instead switching to constructor injection
1 parent 3631ad4 commit 9916896

File tree

6 files changed

+129
-123
lines changed

6 files changed

+129
-123
lines changed

README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# @libp2p/floodsub <!-- omit in toc -->
22

33
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
4-
[![IRC](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
54
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
65
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-floodsub.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-floodsub)
7-
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-interfaces/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-floodsub/actions/workflows/js-test-and-release.yml)
6+
[![CI](https://img.shields.io/github/workflow/status/libp2p/js-libp2p-floodsub/test%20&%20maybe%20release/master?style=flat-square)](https://github.com/libp2p/js-libp2p-floodsub/actions/workflows/js-test-and-release.yml)
87

98
> libp2p-floodsub, also known as pubsub-flood or just dumbsub, this implementation of pubsub focused on delivering an API for Publish/Subscribe, but with no CastTree Forming (it just floods the network).
109
@@ -33,18 +32,21 @@ Instead please use [gossipsub](https://www.npmjs.com/package/@chainsafe/libp2p-g
3332
## Usage
3433

3534
```JavaScript
36-
import { FloodSub } from '@libp2p/floodsub'
35+
import { createLibp2pNode } from 'libp2p'
36+
import { floodsub } from '@libp2p/floodsub'
3737

38-
const fsub = new FloodSub()
39-
40-
await fsub.start()
38+
const node = await createLibp2pNode({
39+
pubsub: floodsub()
40+
//... other options
41+
})
42+
await node.start()
4143

42-
fsub.addEventListener('message', (data) => {
43-
console.log(data)
44+
node.pubsub.subscribe('fruit')
45+
node.pubsub.addEventListener('message', (evt) => {
46+
console.log(evt)
4447
})
45-
fsub.subscribe('fruit')
4648

47-
fsub.publish('fruit', new TextEncoder().encode('banana'))
49+
node.pubsub.publish('fruit', new TextEncoder().encode('banana'))
4850
```
4951

5052
## License

package.json

+7-8
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,22 @@
150150
"@libp2p/interface-peer-id": "^1.0.2",
151151
"@libp2p/interface-pubsub": "^3.0.0",
152152
"@libp2p/logger": "^2.0.0",
153-
"@libp2p/pubsub": "^4.0.0",
154-
"protons-runtime": "^3.1.0",
153+
"@libp2p/pubsub": "^5.0.0",
154+
"protons-runtime": "^4.0.1",
155155
"uint8arraylist": "^2.1.1",
156-
"uint8arrays": "^3.0.0"
156+
"uint8arrays": "^4.0.2"
157157
},
158158
"devDependencies": {
159-
"@libp2p/components": "^3.0.1",
160-
"@libp2p/interface-mocks": "^6.0.1",
161-
"@libp2p/interface-pubsub-compliance-tests": "^2.0.1",
159+
"@libp2p/interface-mocks": "^7.0.1",
160+
"@libp2p/interface-pubsub-compliance-tests": "^4.0.0",
162161
"@libp2p/peer-collections": "^2.0.0",
163162
"@libp2p/peer-id": "^1.1.10",
164163
"@libp2p/peer-id-factory": "^1.0.9",
165164
"@multiformats/multiaddr": "^11.0.3",
166165
"aegir": "^37.2.0",
167-
"multiformats": "^9.4.5",
166+
"multiformats": "^10.0.0",
168167
"p-wait-for": "^5.0.0",
169-
"protons": "^5.1.0",
168+
"protons": "^6.0.0",
170169
"sinon": "^14.0.0",
171170
"wherearewe": "^2.0.1"
172171
}

src/index.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { toString } from 'uint8arrays/to-string'
2-
import { PubSubBaseProtocol } from '@libp2p/pubsub'
2+
import { PubSubBaseProtocol, PubSubComponents } from '@libp2p/pubsub'
33
import { multicodec } from './config.js'
44
import { SimpleTimeCache } from './cache.js'
5-
import type { PubSubInit, Message, PubSubRPC, PubSubRPCMessage, PublishResult } from '@libp2p/interface-pubsub'
5+
import type { PubSubInit, Message, PubSubRPC, PubSubRPCMessage, PublishResult, PubSub } from '@libp2p/interface-pubsub'
66
import type { PeerId } from '@libp2p/interface-peer-id'
77
import { logger } from '@libp2p/logger'
88
import { RPC } from './message/rpc.js'
@@ -16,6 +16,10 @@ export interface FloodSubInit extends PubSubInit {
1616
seenTTL?: number
1717
}
1818

19+
export interface FloodSubComponents extends PubSubComponents {
20+
21+
}
22+
1923
/**
2024
* FloodSub (aka dumbsub is an implementation of pubsub focused on
2125
* delivering an API for Publish/Subscribe, but with no CastTree Forming
@@ -24,8 +28,8 @@ export interface FloodSubInit extends PubSubInit {
2428
export class FloodSub extends PubSubBaseProtocol {
2529
public seenCache: SimpleTimeCache<boolean>
2630

27-
constructor (init?: FloodSubInit) {
28-
super({
31+
constructor (components: FloodSubComponents, init?: FloodSubInit) {
32+
super(components, {
2933
...init,
3034
canRelayMessage: true,
3135
multicodecs: [multicodec]
@@ -94,7 +98,7 @@ export class FloodSub extends PubSubBaseProtocol {
9498
}
9599

96100
peers.forEach(id => {
97-
if (this.components.getPeerId().equals(id)) {
101+
if (this.components.peerId.equals(id)) {
98102
log('not sending message on topic %s to myself', message.topic)
99103
return
100104
}
@@ -113,3 +117,7 @@ export class FloodSub extends PubSubBaseProtocol {
113117
return { recipients }
114118
}
115119
}
120+
121+
export function floodsub (init: FloodSubInit = {}): (components: FloodSubComponents) => PubSub {
122+
return (components: FloodSubComponents) => new FloodSub(components, init)
123+
}

0 commit comments

Comments
 (0)