Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: lint content routing (libp2p#1566)
Browse files Browse the repository at this point in the history
maschad committed Mar 5, 2023
1 parent cb42399 commit edd04ba
Showing 4 changed files with 22 additions and 20 deletions.
14 changes: 7 additions & 7 deletions src/connection/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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)
}
18 changes: 10 additions & 8 deletions src/content-routing/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
this.started = true
}

async stop () {
async stop (): Promise<void> {
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<PeerInfo> {
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<void> {
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<void> {
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)
}
6 changes: 3 additions & 3 deletions src/content-routing/utils.ts
Original file line number Diff line number Diff line change
@@ -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<PeerInfo>, peerStore: PeerStore) {
export async function * storeAddresses (source: Source<PeerInfo>, peerStore: PeerStore): AsyncIterable<PeerInfo> {
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<PeerInfo>, peerStore: Pee
/**
* Filter peers by unique peer id
*/
export function uniquePeers (source: Source<PeerInfo>) {
export function uniquePeers (source: Source<PeerInfo>): AsyncIterable<PeerInfo> {
/** @type Set<string> */
const seen = new Set()

@@ -39,7 +39,7 @@ export function uniquePeers (source: Source<PeerInfo>) {
/**
* Require at least `min` peers to be yielded from `source`
*/
export async function * requirePeers (source: Source<PeerInfo>, min: number = 1) {
export async function * requirePeers (source: Source<PeerInfo>, min: number = 1): AsyncIterable<PeerInfo> {
let seen = 0

for await (const peer of source) {
4 changes: 2 additions & 2 deletions src/dht/dht-content-routing.ts
Original file line number Diff line number Diff line change
@@ -15,11 +15,11 @@ export class DHTContentRouting implements ContentRouting {
this.dht = dht
}

async provide (cid: CID) {
async provide (cid: CID): Promise<void> {
await drain(this.dht.provide(cid))
}

async * findProviders (cid: CID, options: AbortOptions = {}) {
async * findProviders (cid: CID, options: AbortOptions = {}): AsyncIterable<Uint8Array> {
for await (const event of this.dht.findProviders(cid, options)) {
if (event.name === 'PROVIDER') {
yield * event.providers

0 comments on commit edd04ba

Please sign in to comment.