@@ -18,13 +18,11 @@ import type { AbortOptions } from '@libp2p/interfaces'
1818import type { EventEmitter } from '@libp2p/interfaces/events'
1919import type { Startable } from '@libp2p/interfaces/startable'
2020import type { Multiaddr } from '@multiformats/multiaddr'
21- import type { DualDHT } from '@libp2p/interface-dht'
2221import type { Address , Peer , PeerStore } from '@libp2p/interface-peer-store'
2322import type { PeerId } from '@libp2p/interface-peer-id'
2423import type { Connection , Stream } from '@libp2p/interface-connection'
2524import type { PeerRouting } from '@libp2p/interface-peer-routing'
2625import type { ContentRouting } from '@libp2p/interface-content-routing'
27- import type { PubSub } from '@libp2p/interface-pubsub'
2826import type { StreamHandler , StreamHandlerOptions , Topology } from '@libp2p/interface-registrar'
2927import type { Metrics } from '@libp2p/interface-metrics'
3028import type { PeerInfo } from '@libp2p/interface-peer-info'
@@ -91,7 +89,7 @@ export interface Libp2pEvents {
9189 *
9290 * ```js
9391 * libp2p.connectionManager.addEventListener('peer:disconnect', (event) => {
94- * const connection = event.detail
92+ * const peerId = event.detail
9593 * // ...
9694 * })
9795 * ```
@@ -157,11 +155,26 @@ export interface Libp2pEvents {
157155}
158156
159157/**
160- * Fetch service lookup function
158+ * A map of user defined services available on the libp2p node via the
159+ * `services` key
160+ *
161+ * @example
162+ *
163+ * ```js
164+ * const node = await createLibp2p({
165+ * // ...other options
166+ * services: {
167+ * myService: myService({
168+ * // ...service options
169+ * })
170+ * }
171+ * })
172+ *
173+ * // invoke methods on the service
174+ * node.services.myService.anOperation()
175+ * ```
161176 */
162- export interface LookupFunction {
163- ( key : string ) : Promise < Uint8Array | null >
164- }
177+ export type ServiceMap = Record < string , unknown >
165178
166179export type PendingDialStatus = 'queued' | 'active' | 'error' | 'success'
167180
@@ -195,7 +208,7 @@ export interface PendingDial {
195208/**
196209 * Libp2p nodes implement this interface.
197210 */
198- export interface Libp2p extends Startable , EventEmitter < Libp2pEvents > {
211+ export interface Libp2p < T extends ServiceMap = { } > extends Startable , EventEmitter < Libp2pEvents > {
199212 /**
200213 * The PeerId is a unique identifier for a node on the network.
201214 *
@@ -285,7 +298,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
285298 * @example
286299 *
287300 * ```js
288- * const metric = libp2p.registerMetric({
301+ * const metric = libp2p.metrics. registerMetric({
289302 * 'my-metric'
290303 * })
291304 *
@@ -295,98 +308,6 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
295308 */
296309 metrics ?: Metrics
297310
298- /**
299- * The pubsub component implements a distributed [Publish-subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)
300- * network made up of libp2p nodes listening on various topics.
301- *
302- * @example
303- *
304- * ```js
305- * libp2p.pubsub.addEventListener('message', (event) => {
306- * // ...
307- * })
308- * libp2p.pubsub.subscribe('my-topic')
309- * ```
310- */
311- pubsub : PubSub
312-
313- /**
314- * The [DHT](https://en.wikipedia.org/wiki/Distributed_hash_table) is used by
315- * libp2p to store and find values such as provider records and also to discover
316- * information about peers.
317- *
318- * @example
319- *
320- * ```js
321- * for await (const event of libp2p.dht.findPeer(peerId)) {
322- * // ...
323- * }
324- * ```
325- */
326- dht : DualDHT
327-
328- /**
329- * The fetch service allows registering and unregistering functions that supply
330- * values for fetch queries - see the [fetch spec](https://github.com/libp2p/specs/tree/master/fetch).
331- */
332- fetchService : {
333- /**
334- * Registers a new lookup callback that can map keys to values, for a given set of keys that
335- * share the same prefix
336- *
337- * @example
338- *
339- * ```js
340- * libp2p.fetchService.registerLookupFunction('/prefix', (key) => { ... })
341- * ```
342- */
343- registerLookupFunction : ( prefix : string , lookup : LookupFunction ) => void
344-
345- /**
346- * Registers a new lookup callback that can map keys to values, for a given set of keys that
347- * share the same prefix.
348- *
349- * @example
350- *
351- * ```js
352- * libp2p.fetchService.unregisterLookupFunction('/prefix')
353- * ```
354- */
355- unregisterLookupFunction : ( prefix : string , lookup ?: LookupFunction ) => void
356- }
357-
358- /**
359- * The identify service supplies information about this node on request by network peers - see
360- * this [identify spec](https://github.com/libp2p/specs/blob/master/identify/README.md)
361- */
362- identifyService : {
363- host : {
364- /**
365- * Specifies the supported protocol version
366- *
367- * @example
368- *
369- * ```js
370- * libp2p.identifyService.host.protocolVersion
371- * // ipfs/0.1.0
372- * ```
373- */
374- protocolVersion : string
375-
376- /**
377- * Specifies the supported protocol version
378- *
379- * @example
380- *
381- * ```js
382- * libp2p.identifyService.host.agentVersion
383- * // helia/1.0.0
384- * ```
385- */
386- agentVersion : string
387- }
388- }
389-
390311 /**
391312 * Get a deduplicated list of peer advertising multiaddrs by concatenating
392313 * the listen addresses used by transports with any configured
@@ -569,31 +490,15 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
569490 */
570491 unregister : ( id : string ) => void
571492
572- /**
573- * Pings the given peer in order to obtain the operation latency
574- *
575- * @example
576- *
577- * ```js
578- * const latency = await libp2p.ping(otherPeerId)
579- * ```
580- */
581- ping : ( peer : PeerId | Multiaddr , options ?: AbortOptions ) => Promise < number >
582-
583- /**
584- * Sends a request to fetch the value associated with the given key from the given peer.
585- *
586- * @example
587- *
588- * ```js
589- * const value = await libp2p.fetch(otherPeerId, '/some/key')
590- * ```
591- */
592- fetch : ( peer : PeerId | Multiaddr , key : string , options ?: AbortOptions ) => Promise < Uint8Array | null >
593-
594493 /**
595494 * Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type
596495 * this may mean searching the DHT if the key is not present in the KeyStore.
496+ * A set of user defined services
597497 */
598498 getPublicKey : ( peer : PeerId , options ?: AbortOptions ) => Promise < Uint8Array >
499+
500+ /**
501+ * A set of user defined services
502+ */
503+ services : T
599504}
0 commit comments