diff --git a/src/circuit/client.ts b/src/circuit/client.ts index 9ffaa0028e..568ad1f6fb 100644 --- a/src/circuit/client.ts +++ b/src/circuit/client.ts @@ -22,7 +22,7 @@ import { PeerSet, PeerMap, PeerList } from '@libp2p/peer-collections' const log = logger('libp2p:circuit:client') -const noop = () => {} +const noop = (): void => {} /** * CircuitServiceInit initializes the circuit service using values @@ -81,17 +81,17 @@ export class RelayReservationManager extends EventEmitter { log.error('error listening on relays', err) }) this.started = true } - async stop () { - this.reservationMap.forEach((timer) => clearTimeout(timer)) + async stop (): Promise { + this.reservationMap.forEach((timer) => { clearTimeout(timer) }) this.reservationMap.clear() this.relays.clear() } @@ -102,7 +102,7 @@ export class RelayReservationManager extends EventEmitter { if (peerId.equals(this.components.peerId)) { return } @@ -151,11 +151,11 @@ export class RelayReservationManager extends EventEmitter) { + _onPeerConnect ({ detail: connection }: CustomEvent): void { void this.components.peerStore.protoBook.get(connection.remotePeer) .then((protocols) => { void this._onProtocolChange({ peerId: connection.remotePeer, protocols }) - .catch((err) => log.error('handling reconnect failed', err)) + .catch((err) => { log.error('handling reconnect failed', err) }) }, (err) => { // this is not necessarily an error as we will only have the protocols stored @@ -167,7 +167,7 @@ export class RelayReservationManager extends EventEmitter) { + _onPeerDisconnected (evt: CustomEvent): void { const connection = evt.detail const peerId = connection.remotePeer clearTimeout(this.reservationMap.get(peerId)) @@ -238,11 +238,11 @@ export class RelayReservationManager extends EventEmitter { const recheck = this.relays.has(PeerId) this.relays.delete(PeerId) if (recheck) { - // TODO: this should be responsibility of the connMgr + // TODO: this should be responsibility of the connMgr https://github.com/libp2p/js-libp2p/issues/1610 await this._listenOnAvailableHopRelays(new PeerList([PeerId])) } } @@ -254,7 +254,7 @@ export class RelayReservationManager extends EventEmitter { // Check if already listening on enough relays if (this.relays.size >= this.maxReservations) { return @@ -321,7 +321,7 @@ export class RelayReservationManager extends EventEmitter { try { if (peerId.equals(this.components.peerId)) { log.trace('Skipping dialling self %p', peerId.toString()) @@ -335,7 +335,7 @@ export class RelayReservationManager extends EventEmitter { + private readonly createOrRefreshReservation = async (peerId: PeerId): Promise => { try { const connections = this.components.connectionManager.getConnections(peerId) diff --git a/src/circuit/hop.ts b/src/circuit/hop.ts index a93c3128ba..cf6b694ced 100644 --- a/src/circuit/hop.ts +++ b/src/circuit/hop.ts @@ -87,7 +87,7 @@ async function handleReserve ({ connection, stream: pbstr, relayPeer, relayAddrs return } - const result = await reservationStore.reserve(connection.remotePeer, connection.remoteAddr) + const result = reservationStore.reserve(connection.remotePeer, connection.remoteAddr) if (result.status !== Status.OK) { hopstr.write({ type: HopMessage.Type.STATUS, status: result.status }) @@ -105,12 +105,12 @@ async function handleReserve ({ connection, stream: pbstr, relayPeer, relayAddrs type: HopMessage.Type.STATUS, status: Status.OK, reservation: await makeReservation(relayAddrs, relayPeer, connection.remotePeer, BigInt(result.expire ?? 0)), - limit: (await reservationStore.get(relayPeer))?.limit + limit: reservationStore.get(relayPeer)?.limit }) log('sent confirmation response to %s', connection.remotePeer) } catch (err) { log.error('failed to send confirmation response to %s', connection.remotePeer) - await reservationStore.removeReservation(connection.remotePeer) + reservationStore.removeReservation(connection.remotePeer) } } @@ -138,12 +138,12 @@ async function handleConnect (options: HopProtocolOptions): Promise { const status = await acl.allowConnect(connection.remotePeer, connection.remoteAddr, dstPeer) if (status !== Status.OK) { log.error('hop connect denied for %s with status %s', connection.remotePeer, status) - hopstr.write({ type: HopMessage.Type.STATUS, status: status }) + hopstr.write({ type: HopMessage.Type.STATUS, status }) return } } - if (!await reservationStore.hasReservation(dstPeer)) { + if (!reservationStore.hasReservation(dstPeer)) { log.error('hop connect denied for %s with status %s', connection.remotePeer, Status.NO_RESERVATION) hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION }) return @@ -179,9 +179,9 @@ async function handleConnect (options: HopProtocolOptions): Promise { const sourceStream = stream.unwrap() log('connection to destination established, short circuiting streams...') - const limit = (await reservationStore.get(dstPeer))?.limit + const limit = reservationStore.get(dstPeer)?.limit // Short circuit the two streams to create the relayed connection - return createLimitedRelay(sourceStream, destinationStream, limit) + createLimitedRelay(sourceStream, destinationStream, limit) } async function makeReservation ( diff --git a/src/circuit/interfaces.ts b/src/circuit/interfaces.ts index 3c0d428c68..9bb582b551 100644 --- a/src/circuit/interfaces.ts +++ b/src/circuit/interfaces.ts @@ -11,7 +11,7 @@ export interface Reservation { } export interface ReservationStore { - reserve: (peer: PeerId, addr: Multiaddr, limit?: Limit) => {status: ReservationStatus, expire?: number} + reserve: (peer: PeerId, addr: Multiaddr, limit?: Limit) => { status: ReservationStatus, expire?: number } removeReservation: (peer: PeerId) => void hasReservation: (dst: PeerId) => boolean get: (peer: PeerId) => Reservation | undefined diff --git a/src/circuit/listener.ts b/src/circuit/listener.ts index 7a6422729d..09af1a5147 100644 --- a/src/circuit/listener.ts +++ b/src/circuit/listener.ts @@ -54,7 +54,7 @@ export function createListener (options: ListenerOptions): Listener { * * @returns {Multiaddr[]} */ - function getAddrs () { + function getAddrs (): Multiaddr[] { const addrs = [] for (const addr of listeningAddrs.values()) { addrs.push(addr) @@ -63,7 +63,7 @@ export function createListener (options: ListenerOptions): Listener { } const listener: Listener = Object.assign(new EventEmitter(), { - close: async () => await Promise.resolve(), + close: async () => { await Promise.resolve() }, listen, getAddrs }) diff --git a/src/circuit/relay.ts b/src/circuit/relay.ts index 6ec8e3e015..9e163191bb 100644 --- a/src/circuit/relay.ts +++ b/src/circuit/relay.ts @@ -39,14 +39,14 @@ export class Relay implements Startable { this._advertiseService = this._advertiseService.bind(this) } - isStarted () { + isStarted (): boolean { return this.started } /** * Start Relay service */ - async start () { + async start (): Promise { // Advertise service if HOP enabled and advertising enabled if (this.init.hop.enabled === true && this.init.advertise.enabled === true) { this.timeout = setDelayedInterval( @@ -60,7 +60,7 @@ export class Relay implements Startable { /** * Stop Relay service */ - async stop () { + async stop (): Promise { try { clearDelayedInterval(this.timeout) } catch (err) { } @@ -71,7 +71,7 @@ export class Relay implements Startable { /** * Advertise hop relay service in the network. */ - async _advertiseService () { + async _advertiseService (): Promise { try { const cid = await namespaceToCid(RELAY_RENDEZVOUS_NS) await this.components.contentRouting.provide(cid) diff --git a/src/circuit/reservation-store.ts b/src/circuit/reservation-store.ts index b48cbff8b9..d70a4ea8fb 100644 --- a/src/circuit/reservation-store.ts +++ b/src/circuit/reservation-store.ts @@ -38,7 +38,7 @@ export type ReservationStoreOptions = RecursivePartial export class ReservationStore implements IReservationStore, Startable { private readonly reservations = new PeerMap() - private _started = false; + private _started = false private interval: any private readonly init: ReservationStoreInit @@ -53,11 +53,11 @@ export class ReservationStore implements IReservationStore, Startable { } } - isStarted () { + isStarted (): boolean { return this._started } - start () { + start (): void { if (this._started) { return } @@ -75,7 +75,7 @@ export class ReservationStore implements IReservationStore, Startable { ) } - stop () { + stop (): void { clearInterval(this.interval) } diff --git a/src/circuit/reservation-voucher.ts b/src/circuit/reservation-voucher.ts index 9979ae0692..38e6a26450 100644 --- a/src/circuit/reservation-voucher.ts +++ b/src/circuit/reservation-voucher.ts @@ -22,7 +22,7 @@ export class ReservationVoucherRecord implements Record { this.expiration = expiration } - marshal () { + marshal (): Uint8Array { return ReservationVoucher.encode({ relay: this.relay.toBytes(), peer: this.peer.toBytes(), @@ -30,7 +30,7 @@ export class ReservationVoucherRecord implements Record { }) } - equals (other: Record) { + equals (other: Record): boolean { if (!(other instanceof ReservationVoucherRecord)) { return false } diff --git a/src/circuit/stop.ts b/src/circuit/stop.ts index e650fe6683..d791c0627e 100644 --- a/src/circuit/stop.ts +++ b/src/circuit/stop.ts @@ -30,7 +30,7 @@ export async function handleStop ({ connection, request, pbstr -}: HandleStopOptions) { +}: HandleStopOptions): Promise { const stopstr = pbstr.pb(StopMessage) log('new circuit relay v2 stop stream from %s', connection.remotePeer) // Validate the STOP request has the required input @@ -45,7 +45,7 @@ export async function handleStop ({ return } - // TODO: go-libp2p marks connection transient if there is limit field present in request. + // TODO: go-libp2p marks connection transient if there is limit field present in request. Depends on https://github.com/libp2p/js-libp2p/issues/1611 // Cannot find any reference to transient connections in js-libp2p stopstr.write({ type: StopMessage.Type.STATUS, status: Status.OK }) diff --git a/src/circuit/transport.ts b/src/circuit/transport.ts index f1957dd855..0283519f1b 100644 --- a/src/circuit/transport.ts +++ b/src/circuit/transport.ts @@ -69,7 +69,7 @@ export class Circuit implements Transport, Startable { this._started = false } - isStarted () { + isStarted (): boolean { return this._started } @@ -102,11 +102,11 @@ export class Circuit implements Transport, Startable { }) if (this._init.hop.enabled === true) { - void this.reservationStore.start() + this.reservationStore.start() } } - async stop () { + async stop (): Promise { if (this._init.hop.enabled === true) { this.reservationStore.stop() await this.components.registrar.unhandle(RELAY_V2_HOP_CODEC) @@ -118,11 +118,11 @@ export class Circuit implements Transport, Startable { return true } - get [Symbol.toStringTag] () { + get [Symbol.toStringTag] (): 'libp2p/circuit-relay-v2' { return 'libp2p/circuit-relay-v2' } - async onHop ({ connection, stream }: IncomingStreamData) { + async onHop ({ connection, stream }: IncomingStreamData): Promise { log('received circuit v2 hop protocol stream from %s', connection.remotePeer) const hopTimeoutPromise = pDefer() @@ -165,7 +165,7 @@ export class Circuit implements Transport, Startable { } } - async onStop ({ connection, stream }: IncomingStreamData) { + async onStop ({ connection, stream }: IncomingStreamData): Promise { const pbstr = pbStream(stream) const request = await pbstr.readPB(CircuitV2.StopMessage) log('received circuit v2 stop protocol request from %s', connection.remotePeer) @@ -248,7 +248,7 @@ export class Circuit implements Transport, Startable { destinationAddr, relayAddr, ma, disconnectOnFailure }: ConnectOptions - ) { + ): Promise { try { const pbstr = pbStream(stream) const hopstr = pbstr.pb(CircuitV2.HopMessage) diff --git a/src/circuit/utils.ts b/src/circuit/utils.ts index bad7597ba4..479febc4b7 100644 --- a/src/circuit/utils.ts +++ b/src/circuit/utils.ts @@ -8,17 +8,17 @@ import type { Stream } from '@libp2p/interface-connection' const log = logger('libp2p:circuit:v2:util') -const doRelay = (src: Stream, dst: Stream) => { +const doRelay = (src: Stream, dst: Stream): void => { queueMicrotask(() => { - void dst.sink(src.source).catch(err => log.error('error while relating streams:', err)) + void dst.sink(src.source).catch(err => { log.error('error while relating streams:', err) }) }) queueMicrotask(() => { - void src.sink(dst.source).catch(err => log.error('error while relaying streams:', err)) + void src.sink(dst.source).catch(err => { log.error('error while relaying streams:', err) }) }) } -export function createLimitedRelay (source: Stream, destination: Stream, limit?: Limit) { +export function createLimitedRelay (source: Stream, destination: Stream, limit?: Limit): void { // trivial case if (limit == null) { doRelay(source, destination)