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

Commit 7609545

Browse files
deps(dev): bump aegir from 37.12.1 to 38.1.6 (#128)
* deps(dev): bump aegir from 37.12.1 to 38.1.6 Bumps [aegir](https://github.com/ipfs/aegir) from 37.12.1 to 38.1.6. - [Release notes](https://github.com/ipfs/aegir/releases) - [Changelog](https://github.com/ipfs/aegir/blob/master/CHANGELOG.md) - [Commits](ipfs/aegir@v37.12.1...v38.1.6) --- updated-dependencies: - dependency-name: aegir dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * chore: fix linting --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: achingbrain <alex@achingbrain.net>
1 parent 9c2c7cb commit 7609545

11 files changed

+93
-88
lines changed

package.json

+3-5
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@
163163
"scripts": {
164164
"clean": "aegir clean",
165165
"lint": "aegir lint",
166-
"dep-check": "aegir dep-check",
166+
"dep-check": "aegir dep-check -i protons",
167167
"build": "aegir build",
168168
"generate": "protons test/message/rpc.proto",
169169
"test": "aegir test",
@@ -187,7 +187,6 @@
187187
"@libp2p/peer-collections": "^3.0.0",
188188
"@libp2p/peer-id": "^2.0.0",
189189
"@libp2p/topology": "^4.0.0",
190-
"@multiformats/multiaddr": "^11.0.0",
191190
"abortable-iterator": "^4.0.2",
192191
"it-length-prefixed": "^8.0.2",
193192
"it-pipe": "^2.0.3",
@@ -199,14 +198,13 @@
199198
},
200199
"devDependencies": {
201200
"@libp2p/peer-id-factory": "^2.0.0",
202-
"aegir": "^37.9.1",
201+
"aegir": "^38.1.6",
203202
"delay": "^5.0.0",
204203
"it-pair": "^2.0.2",
205204
"p-defer": "^4.0.0",
206205
"p-wait-for": "^5.0.0",
207206
"protons": "^7.0.2",
208207
"protons-runtime": "^5.0.0",
209-
"sinon": "^15.0.1",
210-
"util": "^0.12.4"
208+
"sinon": "^15.0.1"
211209
}
212210
}

src/index.ts

+35-31
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface PubSubComponents {
2929
* PubSubBaseProtocol handles the peers and connections logic for pubsub routers
3030
* and specifies the API that pubsub routers should have.
3131
*/
32-
export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = PubSubEvents> extends EventEmitter<Events> implements PubSub<Events> {
32+
export abstract class PubSubBaseProtocol<Events extends Record<string, any> = PubSubEvents> extends EventEmitter<Events> implements PubSub<Events> {
3333
public started: boolean
3434
/**
3535
* Map of topics to which peers are subscribed to
@@ -108,10 +108,8 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
108108

109109
/**
110110
* Register the pubsub protocol onto the libp2p node.
111-
*
112-
* @returns {void}
113111
*/
114-
async start () {
112+
async start (): Promise<void> {
115113
if (this.started || !this.enabled) {
116114
return
117115
}
@@ -121,10 +119,12 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
121119
const registrar = this.components.registrar
122120
// Incoming streams
123121
// Called after a peer dials us
124-
await Promise.all(this.multicodecs.map(async multicodec => await registrar.handle(multicodec, this._onIncomingStream, {
125-
maxInboundStreams: this.maxInboundStreams,
126-
maxOutboundStreams: this.maxOutboundStreams
127-
})))
122+
await Promise.all(this.multicodecs.map(async multicodec => {
123+
await registrar.handle(multicodec, this._onIncomingStream, {
124+
maxInboundStreams: this.maxInboundStreams,
125+
maxOutboundStreams: this.maxOutboundStreams
126+
})
127+
}))
128128

129129
// register protocol with topology
130130
// Topology callbacks called on connection manager changes
@@ -141,7 +141,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
141141
/**
142142
* Unregister the pubsub protocol and the streams with other peers will be closed.
143143
*/
144-
async stop () {
144+
async stop (): Promise<void> {
145145
if (!this.started || !this.enabled) {
146146
return
147147
}
@@ -150,10 +150,14 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
150150

151151
// unregister protocol and handlers
152152
if (this._registrarTopologyIds != null) {
153-
this._registrarTopologyIds?.map(id => registrar.unregister(id))
153+
this._registrarTopologyIds?.forEach(id => {
154+
registrar.unregister(id)
155+
})
154156
}
155157

156-
await Promise.all(this.multicodecs.map(async multicodec => await registrar.unhandle(multicodec)))
158+
await Promise.all(this.multicodecs.map(async multicodec => {
159+
await registrar.unhandle(multicodec)
160+
}))
157161

158162
log('stopping')
159163
for (const peerStreams of this.peers.values()) {
@@ -166,14 +170,14 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
166170
log('stopped')
167171
}
168172

169-
isStarted () {
173+
isStarted (): boolean {
170174
return this.started
171175
}
172176

173177
/**
174178
* On an inbound stream opened
175179
*/
176-
protected _onIncomingStream (data: IncomingStreamData) {
180+
protected _onIncomingStream (data: IncomingStreamData): void {
177181
const { stream, connection } = data
178182
const peerId = connection.remotePeer
179183

@@ -186,13 +190,13 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
186190
const inboundStream = peer.attachInboundStream(stream)
187191

188192
this.processMessages(peerId, inboundStream, peer)
189-
.catch(err => log(err))
193+
.catch(err => { log(err) })
190194
}
191195

192196
/**
193197
* Registrar notifies an established connection with pubsub protocol
194198
*/
195-
protected _onPeerConnected (peerId: PeerId, conn: Connection) {
199+
protected _onPeerConnected (peerId: PeerId, conn: Connection): void {
196200
log('connected %p', peerId)
197201

198202
void Promise.resolve().then(async () => {
@@ -221,7 +225,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
221225
/**
222226
* Registrar notifies a closing connection with pubsub protocol
223227
*/
224-
protected _onPeerDisconnected (peerId: PeerId, conn?: Connection) {
228+
protected _onPeerDisconnected (peerId: PeerId, conn?: Connection): void {
225229
const idB58Str = peerId.toString()
226230

227231
log('connection ended', idB58Str)
@@ -258,7 +262,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
258262
/**
259263
* Notifies the router that a peer has been disconnected
260264
*/
261-
protected _removePeer (peerId: PeerId) {
265+
protected _removePeer (peerId: PeerId): PeerStreams | undefined {
262266
const peerStreams = this.peers.get(peerId)
263267
if (peerStreams == null) {
264268
return
@@ -284,7 +288,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
284288
/**
285289
* Responsible for processing each RPC message received by other peers.
286290
*/
287-
async processMessages (peerId: PeerId, stream: AsyncIterable<Uint8ArrayList>, peerStreams: PeerStreams) {
291+
async processMessages (peerId: PeerId, stream: AsyncIterable<Uint8ArrayList>, peerStreams: PeerStreams): Promise<void> {
288292
try {
289293
await pipe(
290294
stream,
@@ -320,7 +324,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
320324
})),
321325
messages
322326
})
323-
.catch(err => log(err))
327+
.catch(err => { log(err) })
324328
}
325329
}
326330
)
@@ -378,7 +382,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
378382
log.error(err)
379383
}
380384
}))
381-
.catch(err => log(err))
385+
.catch(err => { log(err) })
382386
}
383387

384388
return true
@@ -387,7 +391,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
387391
/**
388392
* Handles a subscription change from a peer
389393
*/
390-
processRpcSubOpt (id: PeerId, subOpt: PubSubRPCSubscription) {
394+
processRpcSubOpt (id: PeerId, subOpt: PubSubRPCSubscription): void {
391395
const t = subOpt.topic
392396

393397
if (t == null) {
@@ -412,7 +416,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
412416
/**
413417
* Handles a message from a peer
414418
*/
415-
async processMessage (from: PeerId, msg: Message) {
419+
async processMessage (from: PeerId, msg: Message): Promise<void> {
416420
if (this.components.peerId.equals(from) && !this.emitSelf) {
417421
return
418422
}
@@ -442,7 +446,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
442446
* The default msgID implementation
443447
* Child class can override this.
444448
*/
445-
getMsgId (msg: Message) {
449+
getMsgId (msg: Message): Promise<Uint8Array> | Uint8Array {
446450
const signaturePolicy = this.globalSignaturePolicy
447451
switch (signaturePolicy) {
448452
case 'StrictSign':
@@ -470,7 +474,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
470474
* Whether to accept a message from a peer
471475
* Override to create a graylist
472476
*/
473-
acceptFrom (id: PeerId) {
477+
acceptFrom (id: PeerId): boolean {
474478
return true
475479
}
476480

@@ -495,10 +499,10 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
495499
/**
496500
* Send an rpc object to a peer
497501
*/
498-
send (peer: PeerId, data: { messages?: Message[], subscriptions?: string[], subscribe?: boolean }) {
502+
send (peer: PeerId, data: { messages?: Message[], subscriptions?: string[], subscribe?: boolean }): void {
499503
const { messages, subscriptions, subscribe } = data
500504

501-
return this.sendRpc(peer, {
505+
this.sendRpc(peer, {
502506
subscriptions: (subscriptions ?? []).map(str => ({ topic: str, subscribe: Boolean(subscribe) })),
503507
messages: (messages ?? []).map(toRpcMessage)
504508
})
@@ -507,7 +511,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
507511
/**
508512
* Send an rpc object to a peer
509513
*/
510-
sendRpc (peer: PeerId, rpc: PubSubRPC) {
514+
sendRpc (peer: PeerId, rpc: PubSubRPC): void {
511515
const peerStreams = this.peers.get(peer)
512516

513517
if (peerStreams == null || !peerStreams.isWritable) {
@@ -523,7 +527,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
523527
* Validates the given message. The signature will be checked for authenticity.
524528
* Throws an error on invalid messages
525529
*/
526-
async validate (from: PeerId, message: Message) { // eslint-disable-line require-await
530+
async validate (from: PeerId, message: Message): Promise<void> { // eslint-disable-line require-await
527531
const signaturePolicy = this.globalSignaturePolicy
528532
switch (signaturePolicy) {
529533
case 'StrictNoSign':
@@ -671,7 +675,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
671675
/**
672676
* Subscribes to a given topic.
673677
*/
674-
subscribe (topic: string) {
678+
subscribe (topic: string): void {
675679
if (!this.started) {
676680
throw new Error('Pubsub has not started')
677681
}
@@ -690,7 +694,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
690694
/**
691695
* Unsubscribe from the given topic
692696
*/
693-
unsubscribe (topic: string) {
697+
unsubscribe (topic: string): void {
694698
if (!this.started) {
695699
throw new Error('Pubsub is not started')
696700
}
@@ -713,7 +717,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
713717
/**
714718
* Get the list of topics which the peer is subscribed to.
715719
*/
716-
getTopics () {
720+
getTopics (): string[] {
717721
if (!this.started) {
718722
throw new Error('Pubsub is not started')
719723
}

src/peer-streams.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,22 @@ export class PeerStreams extends EventEmitter<PeerStreamEvents> {
5858
/**
5959
* Do we have a connection to read from?
6060
*/
61-
get isReadable () {
61+
get isReadable (): boolean {
6262
return Boolean(this.inboundStream)
6363
}
6464

6565
/**
6666
* Do we have a connection to write on?
6767
*/
68-
get isWritable () {
68+
get isWritable (): boolean {
6969
return Boolean(this.outboundStream)
7070
}
7171

7272
/**
7373
* Send a message to this peer.
7474
* Throws if there is no `stream` to write to available.
7575
*/
76-
write (data: Uint8Array | Uint8ArrayList) {
76+
write (data: Uint8Array | Uint8ArrayList): void {
7777
if (this.outboundStream == null) {
7878
const id = this.id.toString()
7979
throw new Error('No writable connection to ' + id)
@@ -85,7 +85,7 @@ export class PeerStreams extends EventEmitter<PeerStreamEvents> {
8585
/**
8686
* Attach a raw inbound stream and setup a read stream
8787
*/
88-
attachInboundStream (stream: Stream) {
88+
attachInboundStream (stream: Stream): AsyncIterable<Uint8ArrayList> {
8989
// Create and attach a new inbound stream
9090
// The inbound stream is:
9191
// - abortable, set to only return on abort, rather than throw
@@ -107,12 +107,12 @@ export class PeerStreams extends EventEmitter<PeerStreamEvents> {
107107
/**
108108
* Attach a raw outbound stream and setup a write stream
109109
*/
110-
async attachOutboundStream (stream: Stream) {
110+
async attachOutboundStream (stream: Stream): Promise<Pushable<Uint8ArrayList>> {
111111
// If an outbound stream already exists, gently close it
112112
const _prevStream = this.outboundStream
113113
if (this.outboundStream != null) {
114114
// End the stream without emitting a close event
115-
await this.outboundStream.end()
115+
this.outboundStream.end()
116116
}
117117

118118
this._rawOutboundStream = stream
@@ -151,7 +151,7 @@ export class PeerStreams extends EventEmitter<PeerStreamEvents> {
151151
/**
152152
* Closes the open connection to peer
153153
*/
154-
close () {
154+
close (): void {
155155
if (this.closed) {
156156
return
157157
}

src/sign.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function signMessage (peerId: PeerId, message: { from: PeerId, topi
4545
/**
4646
* Verifies the signature of the given message
4747
*/
48-
export async function verifySignature (message: SignedMessage, encode: (rpc: PubSubRPCMessage) => Uint8Array) {
48+
export async function verifySignature (message: SignedMessage, encode: (rpc: PubSubRPCMessage) => Uint8Array): Promise<boolean> {
4949
if (message.type !== 'signed') {
5050
throw new Error('Message type must be "signed" to be verified')
5151
}
@@ -80,7 +80,7 @@ export async function verifySignature (message: SignedMessage, encode: (rpc: Pub
8080
* Returns the PublicKey associated with the given message.
8181
* If no valid PublicKey can be retrieved an error will be returned.
8282
*/
83-
export async function messagePublicKey (message: SignedMessage) {
83+
export async function messagePublicKey (message: SignedMessage): Promise<Uint8Array> {
8484
if (message.type !== 'signed') {
8585
throw new Error('Message type must be "signed" to have a public key')
8686
}

src/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function randomSeqno (): bigint {
1717
/**
1818
* Generate a message id, based on the `key` and `seqno`
1919
*/
20-
export const msgId = (key: Uint8Array, seqno: bigint) => {
20+
export const msgId = (key: Uint8Array, seqno: bigint): Uint8Array => {
2121
const seqnoBytes = uint8ArrayFromString(seqno.toString(16).padStart(16, '0'), 'base16')
2222

2323
const msgId = new Uint8Array(key.length + seqnoBytes.length)
@@ -30,15 +30,15 @@ export const msgId = (key: Uint8Array, seqno: bigint) => {
3030
/**
3131
* Generate a message id, based on message `data`
3232
*/
33-
export const noSignMsgId = (data: Uint8Array) => {
33+
export const noSignMsgId = (data: Uint8Array): Uint8Array | Promise<Uint8Array> => {
3434
return sha256.encode(data)
3535
}
3636

3737
/**
3838
* Check if any member of the first set is also a member
3939
* of the second set
4040
*/
41-
export const anyMatch = (a: Set<number> | number[], b: Set<number> | number[]) => {
41+
export const anyMatch = (a: Set<number> | number[], b: Set<number> | number[]): boolean => {
4242
let bHas
4343
if (Array.isArray(b)) {
4444
bHas = (val: number) => b.includes(val)
@@ -58,7 +58,7 @@ export const anyMatch = (a: Set<number> | number[], b: Set<number> | number[]) =
5858
/**
5959
* Make everything an array
6060
*/
61-
export const ensureArray = function <T> (maybeArray: T | T[]) {
61+
export const ensureArray = function <T> (maybeArray: T | T[]): T[] {
6262
if (!Array.isArray(maybeArray)) {
6363
return [maybeArray]
6464
}

0 commit comments

Comments
 (0)