@@ -40,7 +40,7 @@ import {
40
40
verifySignature
41
41
} from './sign.js'
42
42
import { toMessage , ensureArray , noSignMsgId , msgId , toRpcMessage , randomSeqno } from './utils.js'
43
- import type { PubSub , Message , StrictNoSign , StrictSign , PubSubInit , PubSubEvents , PeerStreams , PubSubRPCMessage , PubSubRPC , PubSubRPCSubscription , SubscriptionChangeData , PublishResult , TopicValidatorFn , ComponentLogger , Logger , Connection , PeerId , PrivateKey , IncomingStreamData } from '@libp2p/interface'
43
+ import type { PubSub , Message , StrictNoSign , StrictSign , PubSubInit , PubSubEvents , PeerStreams , PubSubRPCMessage , PubSubRPC , PubSubRPCSubscription , SubscriptionChangeData , PublishResult , TopicValidatorFn , ComponentLogger , Logger , Connection , PeerId , PrivateKey , IncomingStreamData , PeerStreamEvents } from '@libp2p/interface'
44
44
import type { Registrar } from '@libp2p/interface-internal'
45
45
import type { Uint8ArrayList } from 'uint8arraylist'
46
46
@@ -55,7 +55,7 @@ export interface PubSubComponents {
55
55
* PubSubBaseProtocol handles the peers and connections logic for pubsub routers
56
56
* and specifies the API that pubsub routers should have.
57
57
*/
58
- export abstract class PubSubBaseProtocol < Events extends Record < string , any > = PubSubEvents > extends TypedEventEmitter < Events > implements PubSub < Events > {
58
+ export abstract class PubSubBaseProtocol < Events extends Record < string , any > = PubSubEvents , PeerEvents extends PeerStreamEvents = PeerStreamEvents > extends TypedEventEmitter < Events > implements PubSub < Events > {
59
59
protected log : Logger
60
60
61
61
public started : boolean
@@ -70,7 +70,7 @@ export abstract class PubSubBaseProtocol<Events extends Record<string, any> = Pu
70
70
/**
71
71
* Map of peer streams
72
72
*/
73
- public peers : PeerMap < PeerStreams >
73
+ public peers : PeerMap < PeerStreams < PeerEvents > >
74
74
/**
75
75
* The signature policy to follow by default
76
76
*/
@@ -119,7 +119,7 @@ export abstract class PubSubBaseProtocol<Events extends Record<string, any> = Pu
119
119
this . started = false
120
120
this . topics = new Map ( )
121
121
this . subscriptions = new Set ( )
122
- this . peers = new PeerMap < PeerStreams > ( )
122
+ this . peers = new PeerMap < PeerStreams < PeerEvents > > ( )
123
123
this . globalSignaturePolicy = globalSignaturePolicy === 'StrictNoSign' ? 'StrictNoSign' : 'StrictSign'
124
124
this . canRelayMessage = canRelayMessage
125
125
this . emitSelf = emitSelf
@@ -268,7 +268,7 @@ export abstract class PubSubBaseProtocol<Events extends Record<string, any> = Pu
268
268
/**
269
269
* Notifies the router that a peer has been connected
270
270
*/
271
- addPeer ( peerId : PeerId , protocol : string ) : PeerStreams {
271
+ addPeer ( peerId : PeerId , protocol : string ) : PeerStreams < PeerEvents > {
272
272
const existing = this . peers . get ( peerId )
273
273
274
274
// If peer streams already exists, do nothing
@@ -279,7 +279,7 @@ export abstract class PubSubBaseProtocol<Events extends Record<string, any> = Pu
279
279
// else create a new peer streams
280
280
this . log ( 'new peer %p' , peerId )
281
281
282
- const peerStreams : PeerStreams = new PeerStreamsImpl ( this . components , {
282
+ const peerStreams : PeerStreams < PeerEvents > = new PeerStreamsImpl < PeerEvents > ( this . components , {
283
283
id : peerId ,
284
284
protocol
285
285
} )
@@ -295,7 +295,7 @@ export abstract class PubSubBaseProtocol<Events extends Record<string, any> = Pu
295
295
/**
296
296
* Notifies the router that a peer has been disconnected
297
297
*/
298
- protected _removePeer ( peerId : PeerId ) : PeerStreams | undefined {
298
+ protected _removePeer ( peerId : PeerId ) : PeerStreams < PeerEvents > | undefined {
299
299
const peerStreams = this . peers . get ( peerId )
300
300
if ( peerStreams == null ) {
301
301
return
@@ -321,7 +321,7 @@ export abstract class PubSubBaseProtocol<Events extends Record<string, any> = Pu
321
321
/**
322
322
* Responsible for processing each RPC message received by other peers.
323
323
*/
324
- async processMessages ( peerId : PeerId , stream : AsyncIterable < Uint8ArrayList > , peerStreams : PeerStreams ) : Promise < void > {
324
+ async processMessages ( peerId : PeerId , stream : AsyncIterable < Uint8ArrayList > , peerStreams : PeerStreams < PeerEvents > ) : Promise < void > {
325
325
try {
326
326
await pipe (
327
327
stream ,
@@ -369,7 +369,7 @@ export abstract class PubSubBaseProtocol<Events extends Record<string, any> = Pu
369
369
/**
370
370
* Handles an rpc request from a peer
371
371
*/
372
- async processRpc ( from : PeerId , peerStreams : PeerStreams , rpc : PubSubRPC ) : Promise < boolean > {
372
+ async processRpc ( from : PeerId , peerStreams : PeerStreams < PeerEvents > , rpc : PubSubRPC ) : Promise < boolean > {
373
373
if ( ! this . acceptFrom ( from ) ) {
374
374
this . log ( 'received message from unacceptable peer %p' , from )
375
375
return false
0 commit comments