Skip to content

Commit 01707d7

Browse files
authored
fix!: remove @libp2p/components (#106)
`@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 cb50ec0 commit 01707d7

9 files changed

+173
-188
lines changed

package.json

+4-5
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@
176176
"release": "aegir release"
177177
},
178178
"dependencies": {
179-
"@libp2p/components": "^3.1.1",
180179
"@libp2p/crypto": "^1.0.0",
181180
"@libp2p/interface-connection": "^3.0.1",
182181
"@libp2p/interface-peer-id": "^1.0.2",
@@ -193,10 +192,10 @@
193192
"it-length-prefixed": "^8.0.2",
194193
"it-pipe": "^2.0.3",
195194
"it-pushable": "^3.0.0",
196-
"multiformats": "^9.6.3",
195+
"multiformats": "^10.0.0",
197196
"p-queue": "^7.2.0",
198197
"uint8arraylist": "^2.0.0",
199-
"uint8arrays": "^3.0.0"
198+
"uint8arrays": "^4.0.2"
200199
},
201200
"devDependencies": {
202201
"@libp2p/peer-id-factory": "^1.0.0",
@@ -205,8 +204,8 @@
205204
"it-pair": "^2.0.2",
206205
"p-defer": "^4.0.0",
207206
"p-wait-for": "^5.0.0",
208-
"protons": "^5.1.0",
209-
"protons-runtime": "^3.1.0",
207+
"protons": "^6.0.0",
208+
"protons-runtime": "^4.0.1",
210209
"sinon": "^14.0.0",
211210
"util": "^0.12.4"
212211
}

src/index.ts

+18-17
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,24 @@ import {
1212
verifySignature
1313
} from './sign.js'
1414
import type { PeerId } from '@libp2p/interface-peer-id'
15-
import type { IncomingStreamData } from '@libp2p/interface-registrar'
15+
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
1616
import type { Connection } from '@libp2p/interface-connection'
1717
import { PubSub, Message, StrictNoSign, StrictSign, PubSubInit, PubSubEvents, PeerStreams, PubSubRPCMessage, PubSubRPC, PubSubRPCSubscription, SubscriptionChangeData, PublishResult, TopicValidatorFn, TopicValidatorResult } from '@libp2p/interface-pubsub'
1818
import { PeerMap, PeerSet } from '@libp2p/peer-collections'
19-
import { Components, Initializable } from '@libp2p/components'
2019
import type { Uint8ArrayList } from 'uint8arraylist'
2120

2221
const log = logger('libp2p:pubsub')
2322

23+
export interface PubSubComponents {
24+
peerId: PeerId
25+
registrar: Registrar
26+
}
27+
2428
/**
2529
* PubSubBaseProtocol handles the peers and connections logic for pubsub routers
2630
* and specifies the API that pubsub routers should have.
2731
*/
28-
export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = PubSubEvents> extends EventEmitter<Events> implements PubSub<Events>, Initializable {
32+
export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = PubSubEvents> extends EventEmitter<Events> implements PubSub<Events> {
2933
public started: boolean
3034
/**
3135
* Map of topics to which peers are subscribed to
@@ -60,14 +64,14 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
6064
public topicValidators: Map<string, TopicValidatorFn>
6165
public queue: Queue
6266
public multicodecs: string[]
63-
public components: Components = new Components()
67+
public components: PubSubComponents
6468

6569
private _registrarTopologyIds: string[] | undefined
6670
protected enabled: boolean
6771
private readonly maxInboundStreams: number
6872
private readonly maxOutboundStreams: number
6973

70-
constructor (props: PubSubInit) {
74+
constructor (components: PubSubComponents, props: PubSubInit) {
7175
super()
7276

7377
const {
@@ -80,6 +84,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
8084
maxOutboundStreams = 1
8185
} = props
8286

87+
this.components = components
8388
this.multicodecs = ensureArray(multicodecs)
8489
this.enabled = props.enabled !== false
8590
this.started = false
@@ -99,10 +104,6 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
99104
this._onPeerDisconnected = this._onPeerDisconnected.bind(this)
100105
}
101106

102-
init (components: Components) {
103-
this.components = components
104-
}
105-
106107
// LIFECYCLE METHODS
107108

108109
/**
@@ -117,7 +118,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
117118

118119
log('starting')
119120

120-
const registrar = this.components.getRegistrar()
121+
const registrar = this.components.registrar
121122
// Incoming streams
122123
// Called after a peer dials us
123124
await Promise.all(this.multicodecs.map(async multicodec => await registrar.handle(multicodec, this._onIncomingStream, {
@@ -145,7 +146,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
145146
return
146147
}
147148

148-
const registrar = this.components.getRegistrar()
149+
const registrar = this.components.registrar
149150

150151
// unregister protocol and handlers
151152
if (this._registrarTopologyIds != null) {
@@ -412,7 +413,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
412413
* Handles a message from a peer
413414
*/
414415
async processMessage (from: PeerId, msg: Message) {
415-
if (this.components.getPeerId().equals(from) && !this.emitSelf) {
416+
if (this.components.peerId.equals(from) && !this.emitSelf) {
416417
return
417418
}
418419

@@ -425,7 +426,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
425426
}
426427

427428
if (this.subscriptions.has(msg.topic)) {
428-
const isFromSelf = this.components.getPeerId().equals(from)
429+
const isFromSelf = this.components.peerId.equals(from)
429430

430431
if (!isFromSelf || this.emitSelf) {
431432
super.dispatchEvent(new CustomEvent<Message>('message', {
@@ -584,7 +585,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
584585
const signaturePolicy = this.globalSignaturePolicy
585586
switch (signaturePolicy) {
586587
case 'StrictSign':
587-
return await signMessage(this.components.getPeerId(), message, this.encodeMessage.bind(this))
588+
return await signMessage(this.components.peerId, message, this.encodeMessage.bind(this))
588589
case 'StrictNoSign':
589590
return await Promise.resolve({
590591
type: 'unsigned',
@@ -627,7 +628,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
627628
}
628629

629630
const message = {
630-
from: this.components.getPeerId(),
631+
from: this.components.peerId,
631632
topic,
632633
data: data ?? new Uint8Array(0),
633634
sequenceNumber: randomSeqno()
@@ -649,10 +650,10 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
649650
}
650651

651652
// send to all the other peers
652-
const result = await this.publishMessage(this.components.getPeerId(), rpcMessage)
653+
const result = await this.publishMessage(this.components.peerId, rpcMessage)
653654

654655
if (emittedToSelf) {
655-
result.recipients = [...result.recipients, this.components.getPeerId()]
656+
result.recipients = [...result.recipients, this.components.peerId]
656657
}
657658

658659
return result

test/emit-self.spec.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
} from './utils/index.js'
77
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
88
import delay from 'delay'
9-
import { Components } from '@libp2p/components'
109

1110
const protocol = '/pubsub/1.0.0'
1211
const topic = 'foo'
@@ -21,13 +20,12 @@ describe('emitSelf', () => {
2120
const peerId = await createPeerId()
2221

2322
pubsub = new PubsubImplementation({
23+
peerId,
24+
registrar: new MockRegistrar()
25+
}, {
2426
multicodecs: [protocol],
2527
emitSelf: true
2628
})
27-
pubsub.init(new Components({
28-
peerId,
29-
registrar: new MockRegistrar()
30-
}))
3129
})
3230

3331
before(async () => {
@@ -77,13 +75,12 @@ describe('emitSelf', () => {
7775
const peerId = await createPeerId()
7876

7977
pubsub = new PubsubImplementation({
78+
peerId,
79+
registrar: new MockRegistrar()
80+
}, {
8081
multicodecs: [protocol],
8182
emitSelf: false
8283
})
83-
pubsub.init(new Components({
84-
peerId,
85-
registrar: new MockRegistrar()
86-
}))
8784
})
8885

8986
before(async () => {

test/instance.spec.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { expect } from 'aegir/chai'
22
import { PubSubBaseProtocol } from '../src/index.js'
33
import type { PublishResult, PubSubRPC, PubSubRPCMessage } from '@libp2p/interface-pubsub'
44
import type { Uint8ArrayList } from 'uint8arraylist'
5+
import { MockRegistrar } from './utils/index.js'
6+
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
57

68
class PubsubProtocol extends PubSubBaseProtocol {
79
decodeRpc (bytes: Uint8Array): PubSubRPC {
@@ -33,9 +35,14 @@ describe('pubsub instance', () => {
3335
}).to.throw()
3436
})
3537

36-
it('should accept valid parameters', () => {
38+
it('should accept valid parameters', async () => {
39+
const peerId = await createEd25519PeerId()
40+
3741
expect(() => {
38-
new PubsubProtocol({ // eslint-disable-line no-new
42+
return new PubsubProtocol({
43+
peerId,
44+
registrar: new MockRegistrar()
45+
}, { // eslint-disable-line no-new
3946
multicodecs: ['/pubsub/1.0.0']
4047
})
4148
}).not.to.throw()

test/lifecycle.spec.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import type { PeerId } from '@libp2p/interface-peer-id'
1212
import type { Registrar } from '@libp2p/interface-registrar'
1313
import type { PublishResult, PubSubRPC, PubSubRPCMessage } from '@libp2p/interface-pubsub'
14-
import { Components } from '@libp2p/components'
1514
import type { Uint8ArrayList } from 'uint8arraylist'
1615

1716
class PubsubProtocol extends PubSubBaseProtocol {
@@ -52,12 +51,11 @@ describe('pubsub base lifecycle', () => {
5251
}
5352

5453
pubsub = new PubsubProtocol({
55-
multicodecs: ['/pubsub/1.0.0']
56-
})
57-
pubsub.init(new Components({
5854
peerId: peerId,
5955
registrar: sinonMockRegistrar
60-
}))
56+
}, {
57+
multicodecs: ['/pubsub/1.0.0']
58+
})
6159

6260
expect(pubsub.peers.size).to.be.eql(0)
6361
})
@@ -112,19 +110,17 @@ describe('pubsub base lifecycle', () => {
112110
registrarB = new MockRegistrar()
113111

114112
pubsubA = new PubsubImplementation({
115-
multicodecs: [protocol]
116-
})
117-
pubsubA.init(new Components({
118113
peerId: peerIdA,
119114
registrar: registrarA
120-
}))
121-
pubsubB = new PubsubImplementation({
115+
}, {
122116
multicodecs: [protocol]
123117
})
124-
pubsubB.init(new Components({
118+
pubsubB = new PubsubImplementation({
125119
peerId: peerIdB,
126120
registrar: registrarB
127-
}))
121+
}, {
122+
multicodecs: [protocol]
123+
})
128124
})
129125

130126
// start pubsub

test/message.spec.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
} from './utils/index.js'
1010
import type { PeerId } from '@libp2p/interface-peer-id'
1111
import type { Message } from '@libp2p/interface-pubsub'
12-
import { Components } from '@libp2p/components'
1312
import { randomSeqno } from '../src/utils.js'
1413

1514
describe('pubsub base messages', () => {
@@ -19,12 +18,11 @@ describe('pubsub base messages', () => {
1918
before(async () => {
2019
peerId = await createPeerId()
2120
pubsub = new PubsubImplementation({
22-
multicodecs: ['/pubsub/1.0.0']
23-
})
24-
pubsub.init(new Components({
2521
peerId: peerId,
2622
registrar: new MockRegistrar()
27-
}))
23+
}, {
24+
multicodecs: ['/pubsub/1.0.0']
25+
})
2826
})
2927

3028
afterEach(() => {

0 commit comments

Comments
 (0)