Skip to content

Commit bdf53ae

Browse files
authored
fix: add register and unregister methods to root node object (#1536)
Expose the register and unregister methods as part of the root libp2p node object.
1 parent ea8f279 commit bdf53ae

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

doc/migrations/v0.41-v0.42.md

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Not all fields from the returned object of the `createLibp2p` are defined on the
1818

1919
```js
2020
import { createLibp2p } from 'libp2p'
21+
import { createTopology } from '@libp2p/topology'
2122

2223
const node = await createLibp2p({
2324
// ... other options
@@ -31,6 +32,10 @@ node.connectionManager.getConnections()
3132

3233
await node.connectionManager.openConnection(peerId)
3334
// Connection
35+
36+
// Topology operations
37+
const id = await node.registrar.register('/my/protocol/1.0.0', createTopology({}))
38+
node.registrar.unregister(id)
3439
```
3540

3641
**After**
@@ -50,6 +55,10 @@ node.getConnections()
5055

5156
await node.dial(peerId)
5257
// Connection
58+
59+
// Topology operations
60+
const id = await node.register('/my/protocol/1.0.0', createTopology({}))
61+
node.unregister(id)
5362
```
5463

5564
## Connection manager events

src/libp2p.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import type { Connection } from '@libp2p/interface-connection'
3232
import type { PeerRouting } from '@libp2p/interface-peer-routing'
3333
import type { ContentRouting } from '@libp2p/interface-content-routing'
3434
import type { PubSub } from '@libp2p/interface-pubsub'
35-
import type { Registrar, StreamHandler, StreamHandlerOptions } from '@libp2p/interface-registrar'
35+
import type { Registrar, StreamHandler, StreamHandlerOptions, Topology } from '@libp2p/interface-registrar'
3636
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
3737
import type { PeerInfo } from '@libp2p/interface-peer-info'
3838
import type { Libp2p, Libp2pEvents, Libp2pInit, Libp2pOptions } from './index.js'
@@ -474,6 +474,14 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
474474
)
475475
}
476476

477+
async register (protocol: string, topology: Topology): Promise<string> {
478+
return await this.registrar.register(protocol, topology)
479+
}
480+
481+
unregister (id: string) {
482+
this.registrar.unregister(id)
483+
}
484+
477485
/**
478486
* Called whenever peer discovery services emit `peer` events.
479487
* Known peers may be emitted.

0 commit comments

Comments
 (0)