Skip to content

Commit

Permalink
refactor!: dht advertiser takes kad-dht not libp2p
Browse files Browse the repository at this point in the history
  • Loading branch information
tabcat committed Sep 4, 2023
1 parent 450fa59 commit d0ee166
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions src/advertisers/dht.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
import type { Advertiser } from '../index.js'
import type { Ed25519PeerId } from '@libp2p/interface-peer-id'
import type { QueryEvent, DualKadDHT, SingleKadDHT } from '@libp2p/kad-dht'
import type { Libp2p } from 'libp2p'
import type { Ed25519PeerId } from '@libp2p/interface/peer-id'
import type { QueryEvent, KadDHT } from '@libp2p/kad-dht'
import type { CID } from 'multiformats/cid'

export type Libp2pWithDHT = Libp2p<{ dht: DualKadDHT }>

export interface CreateEphemeralLibp2p {
(peerId: Ed25519PeerId): Promise<{ stop?: (libp2p: Libp2pWithDHT) => Promise<void>, libp2p: Libp2pWithDHT }>
export interface CreateEphemeralKadDHT {
(provider: Ed25519PeerId): Promise<{ dht: KadDHT, stop?: () => Promise<void> }>
}

const scopedDht = ({ services: { dht } }: Libp2pWithDHT, scope?: Scope): DualKadDHT | SingleKadDHT =>
scope != null && scope in dht ? dht[scope] : dht

const collaborate = (createEphemeralLibp2p: CreateEphemeralLibp2p, options: Options): Advertiser['collaborate'] =>
const collaborate = (createEphemeralKadDHT: CreateEphemeralKadDHT): Advertiser['collaborate'] =>
async function * (dcid: CID, provider: Ed25519PeerId): AsyncIterable<QueryEvent> {
const { stop, libp2p: ephemeral } = await createEphemeralLibp2p(provider)
yield * scopedDht(ephemeral, options.scope).provide(dcid)
if (stop != null) {
await stop(ephemeral)
const { dht, stop } = await createEphemeralKadDHT(provider)

try {
yield * dht.provide(dcid)
} finally {
if (stop != null) {
await stop()
}
}
}

const findCollaborators = (libp2p: Libp2pWithDHT, options: Options): Advertiser['findCollaborators'] =>
const findCollaborators = (dht: KadDHT): Advertiser['findCollaborators'] =>
function (dcid: CID): AsyncIterable<QueryEvent> {
return scopedDht(libp2p, options.scope).findProviders(dcid)
return dht.findProviders(dcid)
}

type Scope = 'wan' | 'lan'

interface Options {
scope?: Scope
}

export function dht (libp2p: Libp2pWithDHT, createEphemeralLibp2p: CreateEphemeralLibp2p, options?: Options): Advertiser {
options = options ?? {}
export function dht (dht: KadDHT, createEphemeralKadDHT: CreateEphemeralKadDHT): Advertiser {
return {
collaborate: collaborate(createEphemeralLibp2p, options),
findCollaborators: findCollaborators(libp2p, options)
collaborate: collaborate(createEphemeralKadDHT),
findCollaborators: findCollaborators(dht)
}
}

0 comments on commit d0ee166

Please # to comment.