@@ -18,13 +18,11 @@ import type { AbortOptions } from '@libp2p/interfaces'
18
18
import type { EventEmitter } from '@libp2p/interfaces/events'
19
19
import type { Startable } from '@libp2p/interfaces/startable'
20
20
import type { Multiaddr } from '@multiformats/multiaddr'
21
- import type { DualDHT } from '@libp2p/interface-dht'
22
21
import type { PeerStore } from '@libp2p/interface-peer-store'
23
22
import type { PeerId } from '@libp2p/interface-peer-id'
24
23
import type { Connection , Stream } from '@libp2p/interface-connection'
25
24
import type { PeerRouting } from '@libp2p/interface-peer-routing'
26
25
import type { ContentRouting } from '@libp2p/interface-content-routing'
27
- import type { PubSub } from '@libp2p/interface-pubsub'
28
26
import type { StreamHandler , StreamHandlerOptions , Topology } from '@libp2p/interface-registrar'
29
27
import type { Metrics } from '@libp2p/interface-metrics'
30
28
import type { PeerInfo } from '@libp2p/interface-peer-info'
@@ -78,16 +76,31 @@ export interface Libp2pEvents {
78
76
}
79
77
80
78
/**
81
- * Fetch service lookup function
79
+ * A map of user defined services available on the libp2p node via the
80
+ * `services` key
81
+ *
82
+ * @example
83
+ *
84
+ * ```js
85
+ * const node = await createLibp2p({
86
+ * // ...other options
87
+ * services: {
88
+ * myService: myService({
89
+ * // ...service options
90
+ * })
91
+ * }
92
+ * })
93
+ *
94
+ * // invoke methods on the service
95
+ * node.services.myService.anOperation()
96
+ * ```
82
97
*/
83
- export interface LookupFunction {
84
- ( key : string ) : Promise < Uint8Array | null >
85
- }
98
+ export type ServiceMap = Record < string , unknown >
86
99
87
100
/**
88
101
* Libp2p nodes implement this interface.
89
102
*/
90
- export interface Libp2p extends Startable , EventEmitter < Libp2pEvents > {
103
+ export interface Libp2p < T extends ServiceMap = { } > extends Startable , EventEmitter < Libp2pEvents > {
91
104
/**
92
105
* The PeerId is a unique identifier for a node on the network.
93
106
*
@@ -177,7 +190,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
177
190
* @example
178
191
*
179
192
* ```js
180
- * const metric = libp2p.registerMetric({
193
+ * const metric = libp2p.metrics. registerMetric({
181
194
* 'my-metric'
182
195
* })
183
196
*
@@ -187,98 +200,6 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
187
200
*/
188
201
metrics ?: Metrics
189
202
190
- /**
191
- * The pubsub component implements a distributed [Publish-subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern)
192
- * network made up of libp2p nodes listening on various topics.
193
- *
194
- * @example
195
- *
196
- * ```js
197
- * libp2p.pubsub.addEventListener('message', (event) => {
198
- * // ...
199
- * })
200
- * libp2p.pubsub.subscribe('my-topic')
201
- * ```
202
- */
203
- pubsub : PubSub
204
-
205
- /**
206
- * The [DHT](https://en.wikipedia.org/wiki/Distributed_hash_table) is used by
207
- * libp2p to store and find values such as provider records and also to discover
208
- * information about peers.
209
- *
210
- * @example
211
- *
212
- * ```js
213
- * for await (const event of libp2p.dht.findPeer(peerId)) {
214
- * // ...
215
- * }
216
- * ```
217
- */
218
- dht : DualDHT
219
-
220
- /**
221
- * The fetch service allows registering and unregistering functions that supply
222
- * values for fetch queries - see the [fetch spec](https://github.com/libp2p/specs/tree/master/fetch).
223
- */
224
- fetchService : {
225
- /**
226
- * Registers a new lookup callback that can map keys to values, for a given set of keys that
227
- * share the same prefix
228
- *
229
- * @example
230
- *
231
- * ```js
232
- * libp2p.fetchService.registerLookupFunction('/prefix', (key) => { ... })
233
- * ```
234
- */
235
- registerLookupFunction : ( prefix : string , lookup : LookupFunction ) => void
236
-
237
- /**
238
- * Registers a new lookup callback that can map keys to values, for a given set of keys that
239
- * share the same prefix.
240
- *
241
- * @example
242
- *
243
- * ```js
244
- * libp2p.fetchService.unregisterLookupFunction('/prefix')
245
- * ```
246
- */
247
- unregisterLookupFunction : ( prefix : string , lookup ?: LookupFunction ) => void
248
- }
249
-
250
- /**
251
- * The identify service supplies information about this node on request by network peers - see
252
- * this [identify spec](https://github.com/libp2p/specs/blob/master/identify/README.md)
253
- */
254
- identifyService : {
255
- host : {
256
- /**
257
- * Specifies the supported protocol version
258
- *
259
- * @example
260
- *
261
- * ```js
262
- * libp2p.identifyService.host.protocolVersion
263
- * // ipfs/0.1.0
264
- * ```
265
- */
266
- protocolVersion : string
267
-
268
- /**
269
- * Specifies the supported protocol version
270
- *
271
- * @example
272
- *
273
- * ```js
274
- * libp2p.identifyService.host.agentVersion
275
- * // helia/1.0.0
276
- * ```
277
- */
278
- agentVersion : string
279
- }
280
- }
281
-
282
203
/**
283
204
* Get a deduplicated list of peer advertising multiaddrs by concatenating
284
205
* the listen addresses used by transports with any configured
@@ -449,30 +370,7 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
449
370
unregister : ( id : string ) => void
450
371
451
372
/**
452
- * Pings the given peer in order to obtain the operation latency
453
- *
454
- * @example
455
- *
456
- * ```js
457
- * const latency = await libp2p.ping(otherPeerId)
458
- * ```
459
- */
460
- ping : ( peer : PeerId | Multiaddr , options ?: AbortOptions ) => Promise < number >
461
-
462
- /**
463
- * Sends a request to fetch the value associated with the given key from the given peer.
464
- *
465
- * @example
466
- *
467
- * ```js
468
- * const value = await libp2p.fetch(otherPeerId, '/some/key')
469
- * ```
470
- */
471
- fetch : ( peer : PeerId | Multiaddr , key : string , options ?: AbortOptions ) => Promise < Uint8Array | null >
472
-
473
- /**
474
- * Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type
475
- * this may mean searching the DHT if the key is not present in the KeyStore.
373
+ * A set of user defined services
476
374
*/
477
- getPublicKey : ( peer : PeerId , options ?: AbortOptions ) => Promise < Uint8Array >
375
+ services : T
478
376
}
0 commit comments