| 1 | import { cidCodec } from './codecs/cid.js' |
| 2 | import { customProgressEventCodec } from './codecs/custom-progress-event.js' |
| 3 | import { multiaddrCodec } from './codecs/multiaddr.js' |
| 4 | import { peerIdCodec } from './codecs/peer-id.js' |
| 5 | import type { ContentRouting, PeerId, PeerRouting, AbortOptions, IdentifyResult, ConnectionStatus, ConnectionLimits, MultiaddrConnectionTimeline, MessageStreamDirection } from '@libp2p/interface' |
| 6 | import type { OpenConnectionOptions } from '@libp2p/interface-internal' |
| 7 | import type { Multiaddr } from '@multiformats/multiaddr' |
| 8 | import type { ValueCodec } from 'it-rpc' |
| 9 | |
| 10 | export const valueCodecs: Array<ValueCodec<any>> = [ |
| 11 | cidCodec, |
| 12 | multiaddrCodec, |
| 13 | peerIdCodec, |
| 14 | customProgressEventCodec |
| 15 | ] |
| 16 | |
| 17 | export interface PeerAddress { |
| 18 | multiaddr: Multiaddr |
| 19 | isConnected?: boolean |
| 20 | isCertified?: boolean |
| 21 | } |
| 22 | |
| 23 | export interface Peer { |
| 24 | /** |
| 25 | * The identifier of the remote peer |
| 26 | */ |
| 27 | id: PeerId |
| 28 | |
| 29 | /** |
| 30 | * The list of addresses the peer has that we know about |
| 31 | */ |
| 32 | addresses: PeerAddress[] |
| 33 | |
| 34 | /** |
| 35 | * Any peer store tags the peer has |
| 36 | */ |
| 37 | tags: Record<string, number> |
| 38 | |
| 39 | /** |
| 40 | * Any peer store metadata the peer has |
| 41 | */ |
| 42 | metadata: Record<string, string> |
| 43 | |
| 44 | /** |
| 45 | * The protocols the peer supports, if known |
| 46 | */ |
| 47 | protocols: string[] |
| 48 | } |
| 49 | |
| 50 | export interface Connection { |
| 51 | id: string |
| 52 | remoteAddr: Multiaddr |
| 53 | remotePeer: PeerId |
| 54 | direction: MessageStreamDirection |
| 55 | timeline: MultiaddrConnectionTimeline |
| 56 | multiplexer?: string |
| 57 | encryption?: string |
| 58 | status: ConnectionStatus |
| 59 | limits?: ConnectionLimits |
| 60 | rtt?: number |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * RPC operations exposed by the metrics |
| 65 | */ |
| 66 | export interface MetricsRPC { |
| 67 | /** |
| 68 | * Called by DevTools on initial connect |
| 69 | */ |
| 70 | init(options?: AbortOptions): Promise<{ self: Peer, peers: Peer[], debug: string, capabilities: Record<string, string[]> }> |
| 71 | |
| 72 | /** |
| 73 | * Update the currently active debugging namespaces |
| 74 | */ |
| 75 | setDebug(namespace?: string): Promise<void> |
| 76 | |
| 77 | /** |
| 78 | * Open a connection to the passed peer or multiaddr |
| 79 | */ |
| 80 | openConnection(peerIdOrMultiaddr: string, options?: OpenConnectionOptions): Promise<Connection> |
| 81 | |
| 82 | /** |
| 83 | * Close connections open to the specified peer |
| 84 | */ |
| 85 | closeConnection(peerId: PeerId, options?: AbortOptions): Promise<void> |
| 86 | |
| 87 | /** |
| 88 | * Make content routing queries |
| 89 | */ |
| 90 | contentRouting: ContentRouting |
| 91 | |
| 92 | /** |
| 93 | * Make peer routing queries |
| 94 | */ |
| 95 | peerRouting: PeerRouting |
| 96 | |
| 97 | /** |
| 98 | * PubSub operations |
| 99 | */ |
| 100 | pubsub: { |
| 101 | /** |
| 102 | * Subscribe to a PubSub topic |
| 103 | */ |
| 104 | subscribe(component: string, topic: string): Promise<void> |
| 105 | |
| 106 | /** |
| 107 | * Unsubscribe from a PubSub topic |
| 108 | */ |
| 109 | unsubscribe(component: string, topic: string): Promise<void> |
| 110 | |
| 111 | /** |
| 112 | * Get the list of subscriptions for the current node |
| 113 | */ |
| 114 | getTopics (component: string): Promise<string[]> |
| 115 | |
| 116 | /** |
| 117 | * Get the list of peers we know about who subscribe to the topic |
| 118 | */ |
| 119 | getSubscribers (component: string, topic: string): Promise<PeerId[]> |
| 120 | |
| 121 | /** |
| 122 | * Publish a message to a given topic |
| 123 | */ |
| 124 | publish (component: string, topic: string, message: Uint8Array): Promise<void> |
| 125 | }, |
| 126 | |
| 127 | /** |
| 128 | * Ping a remote node and return the RTT |
| 129 | */ |
| 130 | ping(component: string, peerIdOrMultiaddr: string, options?: AbortOptions): Promise<number> |
| 131 | |
| 132 | identify(component: string, peerIdOrMultiaddr: string, options?: AbortOptions): Promise<IdentifyResult> |
| 133 | } |
| 134 | |
| 135 | export interface InspectorRPCEvents { |
| 136 | /** |
| 137 | * Node metrics have been updated |
| 138 | */ |
| 139 | metrics: CustomEvent<Record<string, any>> |
| 140 | |
| 141 | /** |
| 142 | * The node's status has changed - new addresses and/or protocols, etc |
| 143 | */ |
| 144 | self: CustomEvent<Peer> |
| 145 | |
| 146 | /** |
| 147 | * The node's connected peers have changed |
| 148 | */ |
| 149 | peers: CustomEvent<Peer[]> |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * RPC operations exposed by the DevTools |
| 154 | */ |
| 155 | export interface InspectorRPC { |
| 156 | safeDispatchEvent<Detail>(type: keyof InspectorRPCEvents, detail?: CustomEventInit<Detail>, options?: AbortOptions): Promise<void> |
| 157 | } |