From edd04ba1ec93cf2e45231c1c32c879591e71564a Mon Sep 17 00:00:00 2001 From: chad Date: Fri, 3 Mar 2023 16:45:25 -0500 Subject: [PATCH] chore: lint content routing (#1566) --- src/connection/index.ts | 14 +++++++------- src/content-routing/index.ts | 18 ++++++++++-------- src/content-routing/utils.ts | 6 +++--- src/dht/dht-content-routing.ts | 4 ++-- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/connection/index.ts b/src/connection/index.ts index 953ca3bcb1..7d73e6130e 100644 --- a/src/connection/index.ts +++ b/src/connection/index.ts @@ -87,18 +87,18 @@ export class ConnectionImpl implements Connection { this._closing = false } - get [Symbol.toStringTag] () { + get [Symbol.toStringTag] (): 'Connection' { return 'Connection' } - get [symbol] () { + get [symbol] (): true { return true } /** * Get all the streams of the muxer */ - get streams () { + get streams (): Stream[] { return this._getStreams() } @@ -128,21 +128,21 @@ export class ConnectionImpl implements Connection { /** * Add a stream when it is opened to the registry */ - addStream (stream: Stream) { + addStream (stream: Stream): void { stream.stat.direction = 'inbound' } /** * Remove stream registry after it is closed */ - removeStream (id: string) { + removeStream (id: string): void { } /** * Close the connection */ - async close () { + async close (): Promise { if (this.stat.status === CLOSED || this._closing) { return } @@ -151,7 +151,7 @@ export class ConnectionImpl implements Connection { // close all streams - this can throw if we're not multiplexed try { - this.streams.forEach(s => s.close()) + this.streams.forEach(s => { s.close() }) } catch (err) { log.error(err) } diff --git a/src/content-routing/index.ts b/src/content-routing/index.ts index 0834100048..f1cedcef73 100644 --- a/src/content-routing/index.ts +++ b/src/content-routing/index.ts @@ -14,6 +14,8 @@ import type { Startable } from '@libp2p/interfaces/startable' import type { CID } from 'multiformats/cid' import type { PeerStore } from '@libp2p/interface-peer-store' import type { DualDHT } from '@libp2p/interface-dht' +import type { PeerInfo } from '@libp2p/interface-peer-info' +import type { PeerId } from '@libp2p/interface-peer-id' export interface CompoundContentRoutingInit { routers: ContentRouting[] @@ -35,22 +37,22 @@ export class CompoundContentRouting implements ContentRouting, Startable { this.components = components } - isStarted () { + isStarted (): boolean { return this.started } - async start () { + async start (): Promise { this.started = true } - async stop () { + async stop (): Promise { this.started = false } /** * Iterates over all content routers in parallel to find providers of the given key */ - async * findProviders (key: CID, options: AbortOptions = {}) { + async * findProviders (key: CID, options: AbortOptions = {}): AsyncIterable { if (this.routers.length === 0) { throw errCode(new Error('No content this.routers available'), codes.ERR_NO_ROUTERS_AVAILABLE) } @@ -69,18 +71,18 @@ export class CompoundContentRouting implements ContentRouting, Startable { * Iterates over all content routers in parallel to notify it is * a provider of the given key */ - async provide (key: CID, options: AbortOptions = {}) { + async provide (key: CID, options: AbortOptions = {}): Promise { if (this.routers.length === 0) { throw errCode(new Error('No content routers available'), codes.ERR_NO_ROUTERS_AVAILABLE) } - await Promise.all(this.routers.map(async (router) => await router.provide(key, options))) + await Promise.all(this.routers.map(async (router) => { await router.provide(key, options) })) } /** * Store the given key/value pair in the available content routings */ - async put (key: Uint8Array, value: Uint8Array, options?: AbortOptions) { + async put (key: Uint8Array, value: Uint8Array, options?: AbortOptions): Promise { if (!this.isStarted()) { throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) } @@ -117,7 +119,7 @@ export class CompoundContentRouting implements ContentRouting, Startable { /** * Get the `n` values to the given key without sorting */ - async * getMany (key: Uint8Array, nVals: number, options: AbortOptions) { // eslint-disable-line require-await + async * getMany (key: Uint8Array, nVals: number, options: AbortOptions): AsyncIterable<{ from: PeerId, val: Uint8Array }> { if (!this.isStarted()) { throw errCode(new Error(messages.NOT_STARTED_YET), codes.DHT_NOT_STARTED) } diff --git a/src/content-routing/utils.ts b/src/content-routing/utils.ts index bc6fa84b92..70832688e8 100644 --- a/src/content-routing/utils.ts +++ b/src/content-routing/utils.ts @@ -8,7 +8,7 @@ import type { PeerStore } from '@libp2p/interface-peer-store' /** * Store the multiaddrs from every peer in the passed peer store */ -export async function * storeAddresses (source: Source, peerStore: PeerStore) { +export async function * storeAddresses (source: Source, peerStore: PeerStore): AsyncIterable { yield * map(source, async (peer) => { // ensure we have the addresses for a given peer await peerStore.addressBook.add(peer.id, peer.multiaddrs) @@ -20,7 +20,7 @@ export async function * storeAddresses (source: Source, peerStore: Pee /** * Filter peers by unique peer id */ -export function uniquePeers (source: Source) { +export function uniquePeers (source: Source): AsyncIterable { /** @type Set */ const seen = new Set() @@ -39,7 +39,7 @@ export function uniquePeers (source: Source) { /** * Require at least `min` peers to be yielded from `source` */ -export async function * requirePeers (source: Source, min: number = 1) { +export async function * requirePeers (source: Source, min: number = 1): AsyncIterable { let seen = 0 for await (const peer of source) { diff --git a/src/dht/dht-content-routing.ts b/src/dht/dht-content-routing.ts index f520db4546..650c4fefd3 100644 --- a/src/dht/dht-content-routing.ts +++ b/src/dht/dht-content-routing.ts @@ -15,11 +15,11 @@ export class DHTContentRouting implements ContentRouting { this.dht = dht } - async provide (cid: CID) { + async provide (cid: CID): Promise { await drain(this.dht.provide(cid)) } - async * findProviders (cid: CID, options: AbortOptions = {}) { + async * findProviders (cid: CID, options: AbortOptions = {}): AsyncIterable { for await (const event of this.dht.findProviders(cid, options)) { if (event.name === 'PROVIDER') { yield * event.providers