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

Commit

Permalink
feat: add routing symbols
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
achingbrain committed Apr 26, 2023
1 parent 174fffc commit 9beb4de
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/interface-content-routing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ import type { CID } from 'multiformats/cid'
import type { AbortOptions } from '@libp2p/interfaces'
import type { PeerInfo } from '@libp2p/interface-peer-info'

/**
* Any object that implements this Symbol as a property should return a
* ContentRouting instance as the property value, similar to how
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
*
* @example
*
* ```js
* import { symbol, ContentRouting } from '@libp2p/content-routing'
*
* class MyContentRouter implements ContentRouting {
* get [symbol] () {
* return this
* }
*
* // ...other methods
* }
* ```
*/
export const symbol = Symbol.for('@libp2p/content-routing')

export interface ContentRouting {
/**
* The implementation of this method should ensure that network peers know the
Expand Down
21 changes: 21 additions & 0 deletions packages/interface-peer-routing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ import type { PeerId } from '@libp2p/interface-peer-id'
import type { PeerInfo } from '@libp2p/interface-peer-info'
import type { AbortOptions } from '@libp2p/interfaces'

/**
* Any object that implements this Symbol as a property should return a
* PeerRouting instance as the property value, similar to how
* `Symbol.Iterable` can be used to return an `Iterable` from an `Iterator`.
*
* @example
*
* ```js
* import { symbol, PeerRouting } from '@libp2p/peer-routing'
*
* class MyPeerRouter implements PeerRouting {
* get [symbol] () {
* return this
* }
*
* // ...other methods
* }
* ```
*/
export const symbol = Symbol.for('@libp2p/peer-routing')

export interface PeerRouting {
/**
* Searches the network for peer info corresponding to the passed peer id.
Expand Down

0 comments on commit 9beb4de

Please sign in to comment.