A migration guide for refactoring your application code from libp2p v0.41.x to v0.42.0.
A definition of the libp2p interface is now available from the module @libp2p/interface-libp2p
. This allows modules to signal what fields and methods they expect from a libp2p node without having to depend on the libp2p
module directly making it lighter-weight and easier to consume.
Not all fields from the returned object of the createLibp2p
are defined on the interface, notably the registrar
and connectionManager
fields have been removed. The functionality of these fields is available by calling methods on the root libp2p
object instead.
Before
import { createLibp2p } from 'libp2p'
import { createTopology } from '@libp2p/topology'
const node = await createLibp2p({
// ... other options
})
node.registrar.getProtocols()
// ['/ipfs/ping/1.0.0', '/ipfs/id/1.0.0']
node.connectionManager.getConnections()
// [Connection, Connection]
await node.connectionManager.openConnection(peerId)
// Connection
// Topology operations
const id = await node.registrar.register('/my/protocol/1.0.0', createTopology({}))
node.registrar.unregister(id)
After
import { createLibp2p } from 'libp2p'
const node = await createLibp2p({
// ... other options
})
node.getProtocols()
// ['/ipfs/ping/1.0.0', '/ipfs/id/1.0.0']
node.getConnections()
// [Connection, Connection]
await node.dial(peerId)
// Connection
// Topology operations
const id = await node.register('/my/protocol/1.0.0', createTopology({}))
node.unregister(id)
The connection manager events peer:connect
and peer:disconnect
are now emitted by the libp2p node.
Before
import { createLibp2p } from 'libp2p'
const node = await createLibp2p({
// ... other options
})
node.connectionManager.addEventListener('peer:connect', (event) => {
// ...
})
node.connectionManager.addEventListener('peer:disconnect', (event) => {
// ...
})
After
import { createLibp2p } from 'libp2p'
const node = await createLibp2p({
// ... other options
})
node.addEventListener('peer:connect', (event) => {
// ...
})
node.addEventListener('peer:disconnect', (event) => {
// ...
})
The transport manager has been removed from the exports map. To access the FaultTolerance
enum, import it from the @libp2p/interface-transport
module instead:
import { FaultTolerance } from 'libp2p/transport-manager'
After
import { FaultTolerance } from '@libp2p/interface-transport'