diff --git a/examples/package.json b/examples/package.json index 4bc40043fc..297e5d3789 100644 --- a/examples/package.json +++ b/examples/package.json @@ -9,11 +9,12 @@ }, "license": "MIT", "dependencies": { - "@libp2p/pubsub-peer-discovery": "^8.0.0", "@libp2p/floodsub": "^6.0.0", + "@libp2p/pubsub-peer-discovery": "^8.0.0", "@nodeutils/defaults-deep": "^1.1.0", "execa": "^6.1.0", "fs-extra": "^10.1.0", + "it-to-buffer": "^3.0.1", "libp2p": "../", "p-defer": "^4.0.0", "uint8arrays": "^4.0.0", diff --git a/package.json b/package.json index 11141056d9..b7a3645b60 100644 --- a/package.json +++ b/package.json @@ -116,6 +116,7 @@ "@libp2p/interface-peer-routing": "^1.0.1", "@libp2p/interface-peer-store": "^1.2.2", "@libp2p/interface-pubsub": "^3.0.0", + "@libp2p/interface-record": "^2.0.6", "@libp2p/interface-registrar": "^2.0.3", "@libp2p/interface-stream-muxer": "^3.0.0", "@libp2p/interface-transport": "^2.1.0", @@ -137,8 +138,6 @@ "any-signal": "^3.0.0", "datastore-core": "^8.0.1", "err-code": "^3.0.1", - "events": "^3.3.0", - "hashlru": "^2.3.0", "interface-datastore": "^7.0.0", "it-all": "^2.0.0", "it-drain": "^2.0.0", @@ -151,7 +150,6 @@ "it-pair": "^2.0.2", "it-pb-stream": "^3.2.0", "it-pipe": "^2.0.3", - "it-sort": "^2.0.0", "it-stream-types": "^1.0.4", "merge-options": "^3.0.4", "multiformats": "^11.0.0", @@ -188,23 +186,21 @@ "@libp2p/mplex": "^7.0.0", "@libp2p/pubsub": "^6.0.0", "@libp2p/tcp": "^6.0.0", - "@libp2p/webrtc-star": "^6.0.0", "@libp2p/websockets": "^5.0.0", "@types/p-fifo": "^1.0.0", "@types/varint": "^6.0.0", "@types/xsalsa20": "^1.1.0", - "aegir": "^37.5.6", + "aegir": "^38.1.7", "cborg": "^1.8.1", "delay": "^5.0.0", "execa": "^7.0.0", "go-libp2p": "^1.0.1", "it-pushable": "^3.0.0", - "it-to-buffer": "^3.0.0", + "it-to-buffer": "^3.0.1", "npm-run-all": "^4.1.5", "p-event": "^5.0.1", "p-times": "^4.0.0", "p-wait-for": "^5.0.0", - "protons": "^7.0.2", "sinon": "^15.0.1", "sinon-ts": "^1.0.0" }, diff --git a/src/circuit/server/advert-service.ts b/src/circuit/server/advert-service.ts index 3e6589f27c..e9a942870a 100644 --- a/src/circuit/server/advert-service.ts +++ b/src/circuit/server/advert-service.ts @@ -96,7 +96,7 @@ export class AdvertService extends EventEmitter implements if (err.code === codes.ERR_NO_ROUTERS_AVAILABLE) { log.error('a content router, such as a DHT, must be provided in order to advertise the relay service', err) - await this.stop() + this.stop() return } diff --git a/src/circuit/server/index.ts b/src/circuit/server/index.ts index f4b20592d0..f9074d674a 100644 --- a/src/circuit/server/index.ts +++ b/src/circuit/server/index.ts @@ -26,6 +26,7 @@ import type { ConnectionManager } from '@libp2p/interface-connection-manager' import type { CircuitRelayService, RelayReservation } from '../index.js' import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events' import { setMaxListeners } from 'events' +import type { PeerMap } from '@libp2p/peer-collections' const log = logger('libp2p:circuit-relay:server') @@ -247,7 +248,7 @@ class CircuitRelayServer extends EventEmitter implements Star return } - const result = await this.reservationStore.reserve(connection.remotePeer, connection.remoteAddr) + const result = this.reservationStore.reserve(connection.remotePeer, connection.remoteAddr) if (result.status !== Status.OK) { hopstr.write({ type: HopMessage.Type.STATUS, status: result.status }) @@ -266,12 +267,12 @@ class CircuitRelayServer extends EventEmitter implements Star type: HopMessage.Type.STATUS, status: Status.OK, reservation: await this.makeReservation(connection.remotePeer, BigInt(result.expire ?? 0)), - limit: (await this.reservationStore.get(connection.remotePeer))?.limit + limit: this.reservationStore.get(connection.remotePeer)?.limit }) log('sent confirmation response to %s', connection.remotePeer) } catch (err) { log.error('failed to send confirmation response to %p', connection.remotePeer, err) - await this.reservationStore.removeReservation(connection.remotePeer) + this.reservationStore.removeReservation(connection.remotePeer) } } @@ -325,7 +326,7 @@ class CircuitRelayServer extends EventEmitter implements Star return } - if (!await this.reservationStore.hasReservation(dstPeer)) { + if (!this.reservationStore.hasReservation(dstPeer)) { log.error('hop connect denied for destination peer %p not having a reservation for %p with status %s', dstPeer, connection.remotePeer, Status.NO_RESERVATION) hopstr.write({ type: HopMessage.Type.STATUS, status: Status.NO_RESERVATION }) return @@ -368,7 +369,7 @@ class CircuitRelayServer extends EventEmitter implements Star const sourceStream = stream.unwrap() log('connection from %p to %p established - merging streans', connection.remotePeer, dstPeer) - const limit = (await this.reservationStore.get(dstPeer))?.limit + const limit = this.reservationStore.get(dstPeer)?.limit // Short circuit the two streams to create the relayed connection createLimitedRelay(sourceStream, destinationStream, this.shutdownController.signal, limit) } @@ -408,7 +409,7 @@ class CircuitRelayServer extends EventEmitter implements Star stream.close() } - get reservations () { + get reservations (): PeerMap { return this.reservationStore.reservations } } diff --git a/src/circuit/transport/index.ts b/src/circuit/transport/index.ts index de108f56f2..a76e320fc8 100644 --- a/src/circuit/transport/index.ts +++ b/src/circuit/transport/index.ts @@ -143,7 +143,7 @@ class CircuitRelayTransport implements Transport { return true } - get [Symbol.toStringTag] () { + get [Symbol.toStringTag] (): 'libp2p/circuit-relay-v2' { return 'libp2p/circuit-relay-v2' } @@ -208,7 +208,7 @@ class CircuitRelayTransport implements Transport { destinationAddr, relayAddr, ma, disconnectOnFailure }: ConnectOptions - ) { + ): Promise { try { const pbstr = pbStream(stream) const hopstr = pbstr.pb(HopMessage) diff --git a/src/circuit/utils.ts b/src/circuit/utils.ts index 10e4d2ba89..68a920d8a1 100644 --- a/src/circuit/utils.ts +++ b/src/circuit/utils.ts @@ -37,7 +37,7 @@ async function * countStreamBytes (source: Source, } const doRelay = (src: Stream, dst: Stream, abortSignal: AbortSignal, limit: Required): void => { - function abortStreams (err: Error) { + function abortStreams (err: Error): void { src.abort(err) dst.abort(err) clearTimeout(timeout) diff --git a/src/components.ts b/src/components.ts index e0f732cd8c..ee60acbc92 100644 --- a/src/components.ts +++ b/src/components.ts @@ -91,11 +91,11 @@ export class DefaultComponents implements Components, Startable { this._dialer = init.dialer } - isStarted () { + isStarted (): boolean { return this._started } - async beforeStart () { + async beforeStart (): Promise { await Promise.all( Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { if (startable.beforeStart != null) { @@ -105,7 +105,7 @@ export class DefaultComponents implements Components, Startable { ) } - async start () { + async start (): Promise { await Promise.all( Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { await startable.start() @@ -115,7 +115,7 @@ export class DefaultComponents implements Components, Startable { this._started = true } - async afterStart () { + async afterStart (): Promise { await Promise.all( Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { if (startable.afterStart != null) { @@ -125,7 +125,7 @@ export class DefaultComponents implements Components, Startable { ) } - async beforeStop () { + async beforeStop (): Promise { await Promise.all( Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { if (startable.beforeStop != null) { @@ -135,7 +135,7 @@ export class DefaultComponents implements Components, Startable { ) } - async stop () { + async stop (): Promise { await Promise.all( Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { await startable.stop() @@ -145,7 +145,7 @@ export class DefaultComponents implements Components, Startable { this._started = false } - async afterStop () { + async afterStop (): Promise { await Promise.all( Object.values(this).filter(obj => isStartable(obj)).map(async (startable: Startable) => { if (startable.afterStop != null) { diff --git a/src/connection-manager/auto-dialler.ts b/src/connection-manager/auto-dialler.ts index 46fda1faf3..f35337a65e 100644 --- a/src/connection-manager/auto-dialler.ts +++ b/src/connection-manager/auto-dialler.ts @@ -58,14 +58,14 @@ export class AutoDialler implements Startable { log('options: %j', this.options) } - isStarted () { + isStarted (): boolean { return this.running } /** * Starts the auto dialer */ - async start () { + async start (): Promise { if (!this.options.enabled) { log('not enabled') return @@ -83,7 +83,7 @@ export class AutoDialler implements Startable { /** * Stops the auto dialler */ - async stop () { + async stop (): Promise { if (!this.options.enabled) { log('not enabled') return @@ -98,7 +98,7 @@ export class AutoDialler implements Startable { log('stopped') } - async _autoDial () { + async _autoDial (): Promise { if (this.autoDialTimeout != null) { this.autoDialTimeout.clear() } diff --git a/src/connection-manager/dialer/dial-request.ts b/src/connection-manager/dialer/dial-request.ts index 77d1a306a9..d0034518b3 100644 --- a/src/connection-manager/dialer/dial-request.ts +++ b/src/connection-manager/dialer/dial-request.ts @@ -138,7 +138,7 @@ export class DialRequest { c.abort() } }) - tokens.forEach(token => this.dialer.releaseToken(token)) // release tokens back to the dialer + tokens.forEach(token => { this.dialer.releaseToken(token) }) // release tokens back to the dialer } } } diff --git a/src/connection-manager/dialer/index.ts b/src/connection-manager/dialer/index.ts index fc1365b303..a7ec43ca62 100644 --- a/src/connection-manager/dialer/index.ts +++ b/src/connection-manager/dialer/index.ts @@ -117,18 +117,18 @@ export class DefaultDialer implements Startable, Dialer { } } - isStarted () { + isStarted (): boolean { return this.started } - async start () { + async start (): Promise { this.started = true } /** * Clears any pending dials */ - async stop () { + async stop (): Promise { this.started = false for (const dial of this.pendingDials.values()) { @@ -367,14 +367,14 @@ export class DefaultDialer implements Startable, Dialer { return pendingDial } - getTokens (num: number) { + getTokens (num: number): number[] { const total = Math.min(num, this.maxDialsPerPeer, this.tokens.length) const tokens = this.tokens.splice(0, total) log('%d tokens request, returning %d, %d remaining', num, total, this.tokens.length) return tokens } - releaseToken (token: number) { + releaseToken (token: number): void { // Guard against duplicate releases if (this.tokens.includes(token)) { return diff --git a/src/connection-manager/index.ts b/src/connection-manager/index.ts index c22e59ec5b..c09929ecb4 100644 --- a/src/connection-manager/index.ts +++ b/src/connection-manager/index.ts @@ -211,7 +211,7 @@ export class DefaultConnectionManager extends EventEmitter { // track inbound/outbound connections this.components.metrics?.registerMetricGroup('libp2p_connection_manager_connections', { calculate: () => { @@ -307,7 +307,7 @@ export class DefaultConnectionManager extends EventEmitter { this.components.upgrader.addEventListener('connection', this.onConnect) this.components.upgrader.addEventListener('connectionEnd', this.onDisconnect) @@ -352,7 +352,7 @@ export class DefaultConnectionManager extends EventEmitter { // if we are still dialing KEEP_ALIVE peers, abort those dials this.connectOnStartupController?.abort() this.components.upgrader.removeEventListener('connection', this.onConnect) @@ -362,7 +362,7 @@ export class DefaultConnectionManager extends EventEmitter { this.latencyMonitor?.removeEventListener('data', this._onLatencyMeasure) this.latencyMonitor?.stop() @@ -374,7 +374,7 @@ export class DefaultConnectionManager extends EventEmitter { // Close all connections we're tracking const tasks: Array> = [] for (const connectionList of this.connections.values()) { @@ -394,7 +394,7 @@ export class DefaultConnectionManager extends EventEmitter) { + onConnect (evt: CustomEvent): void { void this._onConnect(evt).catch(err => { log.error(err) }) @@ -403,7 +403,7 @@ export class DefaultConnectionManager extends EventEmitter) { + async _onConnect (evt: CustomEvent): Promise { const { detail: connection } = evt if (!this.started) { @@ -436,7 +436,7 @@ export class DefaultConnectionManager extends EventEmitter) { + onDisconnect (evt: CustomEvent): void { const { detail: connection } = evt if (!this.started) { @@ -542,7 +542,7 @@ export class DefaultConnectionManager extends EventEmitter { - return await connection.close() + await connection.close() }) ) } @@ -569,7 +569,7 @@ export class DefaultConnectionManager extends EventEmitter) { + _onLatencyMeasure (evt: CustomEvent): void { const { detail: summary } = evt this._checkMaxLimit('maxEventLoopDelay', summary.avgMs, 1) @@ -581,7 +581,7 @@ export class DefaultConnectionManager extends EventEmitter { const limit = this.opts[name] if (limit == null) { @@ -600,7 +600,7 @@ export class DefaultConnectionManager extends EventEmitter { const connections = this.getConnections() const peerValues = new PeerMap() @@ -726,7 +726,7 @@ export class DefaultConnectionManager extends EventEmitter { this.latencyData = this.initLatencyData() } - start () { + start (): void { // We check for isBrowser because of browsers set max rates of timeouts when a page is hidden, // so we fall back to another library // See: http://stackoverflow.com/questions/6032429/chrome-timeouts-interval-suspended-in-background-tabs @@ -172,7 +172,7 @@ export class LatencyMonitor extends EventEmitter { } } - stop () { + stop (): void { this._stopTimers() } @@ -181,7 +181,7 @@ export class LatencyMonitor extends EventEmitter { * * @private */ - _startTimers () { + _startTimers (): void { // Timer already started, ignore this if (this.checkLatencyID != null) { return @@ -190,7 +190,7 @@ export class LatencyMonitor extends EventEmitter { this.checkLatency() if (this.dataEmitIntervalMs != null) { - this.emitIntervalID = setInterval(() => this._emitSummary(), this.dataEmitIntervalMs) + this.emitIntervalID = setInterval(() => { this._emitSummary() }, this.dataEmitIntervalMs) if (typeof this.emitIntervalID.unref === 'function') { this.emitIntervalID.unref() // Doesn't block exit } @@ -202,7 +202,7 @@ export class LatencyMonitor extends EventEmitter { * * @private */ - _stopTimers () { + _stopTimers (): void { if (this.checkLatencyID != null) { clearTimeout(this.checkLatencyID) this.checkLatencyID = undefined @@ -218,7 +218,7 @@ export class LatencyMonitor extends EventEmitter { * * @private */ - _emitSummary () { + _emitSummary (): void { const summary = this.getSummary() if (summary.events > 0) { this.dispatchEvent(new CustomEvent('data', { @@ -254,7 +254,7 @@ export class LatencyMonitor extends EventEmitter { * Randomly calls an async fn every roughly latencyCheckIntervalMs (plus some randomness). If no async fn is found, * it will simply report on event loop latency. */ - checkLatency () { + checkLatency (): void { // Randomness is needed to avoid alignment by accident to regular things in the event loop const randomness = (Math.random() * this.latencyCheckMultiply) - this.latencyCheckSubtract @@ -264,7 +264,7 @@ export class LatencyMonitor extends EventEmitter { startTime: this.now() } - const cb = () => { + const cb = (): void => { // We are already stopped, ignore this datapoint if (this.checkLatencyID == null) { return @@ -314,6 +314,6 @@ export class LatencyMonitor extends EventEmitter { } } -function isBrowser () { +function isBrowser (): boolean { return typeof globalThis.window !== 'undefined' } diff --git a/src/connection-manager/visibility-change-emitter.ts b/src/connection-manager/visibility-change-emitter.ts index 1e0d2ab61e..3c103e28f2 100644 --- a/src/connection-manager/visibility-change-emitter.ts +++ b/src/connection-manager/visibility-change-emitter.ts @@ -60,7 +60,7 @@ export class VisibilityChangeEmitter extends EventEmitter