Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit 9ee7691

Browse files
authored
feat: add routing symbols (#388)
When libp2p is [configured with arbitrary services](libp2p/js-libp2p#1563) some of those services may be content routers or peer routers or both. An example of this is the `@libp2p/kad-dht` module. In order to communicate to libp2p that they can provide content/peer routing cabapiliy, add well-known symbols that libp2p can use to get references to the routing implementations.
1 parent 174fffc commit 9ee7691

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

packages/interface-content-routing/src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@ import type { CID } from 'multiformats/cid'
22
import type { AbortOptions } from '@libp2p/interfaces'
33
import type { PeerInfo } from '@libp2p/interface-peer-info'
44

5+
/**
6+
* Any object that implements this Symbol as a property should return a
7+
* ContentRouting instance as the property value, similar to how
8+
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
9+
*
10+
* @example
11+
*
12+
* ```js
13+
* import { contentRouting, ContentRouting } from '@libp2p/content-routing'
14+
*
15+
* class MyContentRouter implements ContentRouting {
16+
* get [contentRouting] () {
17+
* return this
18+
* }
19+
*
20+
* // ...other methods
21+
* }
22+
* ```
23+
*/
24+
export const contentRouting = Symbol.for('@libp2p/content-routing')
25+
526
export interface ContentRouting {
627
/**
728
* The implementation of this method should ensure that network peers know the

packages/interface-peer-discovery/src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
import type { PeerInfo } from '@libp2p/interface-peer-info'
22
import type { EventEmitter } from '@libp2p/interfaces/events'
33

4+
/**
5+
* Any object that implements this Symbol as a property should return a
6+
* PeerDiscovery instance as the property value, similar to how
7+
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
8+
*
9+
* @example
10+
*
11+
* ```js
12+
* import { peerDiscovery, PeerDiscovery } from '@libp2p/peer-discovery'
13+
*
14+
* class MyPeerDiscoverer implements PeerDiscovery {
15+
* get [peerDiscovery] () {
16+
* return this
17+
* }
18+
*
19+
* // ...other methods
20+
* }
21+
* ```
22+
*/
23+
export const peerDiscovery = Symbol.for('@libp2p/peer-discovery')
24+
425
export const symbol = Symbol.for('@libp2p/peer-discovery')
526

627
export interface PeerDiscoveryEvents {

packages/interface-peer-routing/src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@ import type { PeerId } from '@libp2p/interface-peer-id'
22
import type { PeerInfo } from '@libp2p/interface-peer-info'
33
import type { AbortOptions } from '@libp2p/interfaces'
44

5+
/**
6+
* Any object that implements this Symbol as a property should return a
7+
* PeerRouting instance as the property value, similar to how
8+
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
9+
*
10+
* @example
11+
*
12+
* ```js
13+
* import { peerRouting, PeerRouting } from '@libp2p/peer-routing'
14+
*
15+
* class MyPeerRouter implements PeerRouting {
16+
* get [peerRouting] () {
17+
* return this
18+
* }
19+
*
20+
* // ...other methods
21+
* }
22+
* ```
23+
*/
24+
export const peerRouting = Symbol.for('@libp2p/peer-routing')
25+
526
export interface PeerRouting {
627
/**
728
* Searches the network for peer info corresponding to the passed peer id.

0 commit comments

Comments
 (0)