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

Commit 9beb4de

Browse files
committed
feat: add routing symbols
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 9beb4de

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-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 { symbol, ContentRouting } from '@libp2p/content-routing'
14+
*
15+
* class MyContentRouter implements ContentRouting {
16+
* get [symbol] () {
17+
* return this
18+
* }
19+
*
20+
* // ...other methods
21+
* }
22+
* ```
23+
*/
24+
export const symbol = 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-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 { symbol, PeerRouting } from '@libp2p/peer-routing'
14+
*
15+
* class MyPeerRouter implements PeerRouting {
16+
* get [symbol] () {
17+
* return this
18+
* }
19+
*
20+
* // ...other methods
21+
* }
22+
* ```
23+
*/
24+
export const symbol = 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)