-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a `streamPair` convenience function to the interface mocks that returns two `Stream` objects where the duplex streams read/write to/from the other.
- Loading branch information
1 parent
972b10a
commit a1d2c22
Showing
32 changed files
with
1,185 additions
and
446 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { type ContentRouting } from '@libp2p/interface/content-routing' | ||
import { CodeError } from '@libp2p/interface/errors' | ||
import { type PeerRouting } from '@libp2p/interface/peer-routing' | ||
import drain from 'it-drain' | ||
import type { KadDHT, QueryOptions } from './index.js' | ||
import type { PeerId } from '@libp2p/interface/peer-id' | ||
import type { PeerInfo } from '@libp2p/interface/peer-info' | ||
import type { CID } from 'multiformats/cid' | ||
|
||
/** | ||
* Wrapper class to convert events into returned values | ||
*/ | ||
export class DHTContentRouting implements ContentRouting { | ||
private readonly dht: KadDHT | ||
|
||
constructor (dht: KadDHT) { | ||
this.dht = dht | ||
} | ||
|
||
async provide (cid: CID, options: QueryOptions = {}): Promise<void> { | ||
await drain(this.dht.provide(cid, options)) | ||
} | ||
|
||
async * findProviders (cid: CID, options: QueryOptions = {}): AsyncGenerator<PeerInfo, void, undefined> { | ||
for await (const event of this.dht.findProviders(cid, options)) { | ||
if (event.name === 'PROVIDER') { | ||
yield * event.providers | ||
} | ||
} | ||
} | ||
|
||
async put (key: Uint8Array, value: Uint8Array, options?: QueryOptions): Promise<void> { | ||
await drain(this.dht.put(key, value, options)) | ||
} | ||
|
||
async get (key: Uint8Array, options?: QueryOptions): Promise<Uint8Array> { | ||
for await (const event of this.dht.get(key, options)) { | ||
if (event.name === 'VALUE') { | ||
return event.value | ||
} | ||
} | ||
|
||
throw new CodeError('Not found', 'ERR_NOT_FOUND') | ||
} | ||
} | ||
|
||
/** | ||
* Wrapper class to convert events into returned values | ||
*/ | ||
export class DHTPeerRouting implements PeerRouting { | ||
private readonly dht: KadDHT | ||
|
||
constructor (dht: KadDHT) { | ||
this.dht = dht | ||
} | ||
|
||
async findPeer (peerId: PeerId, options: QueryOptions = {}): Promise<PeerInfo> { | ||
for await (const event of this.dht.findPeer(peerId, options)) { | ||
if (event.name === 'FINAL_PEER') { | ||
return event.peer | ||
} | ||
} | ||
|
||
throw new CodeError('Not found', 'ERR_NOT_FOUND') | ||
} | ||
|
||
async * getClosestPeers (key: Uint8Array, options: QueryOptions = {}): AsyncIterable<PeerInfo> { | ||
for await (const event of this.dht.getClosestPeers(key, options)) { | ||
if (event.name === 'FINAL_PEER') { | ||
yield event.peer | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.