Skip to content

Commit a3847f2

Browse files
authored
fix!: remove @libp2p/components (#1427)
`@libp2p/components` is a choke-point for our dependency graph as it depends on every interface, meaning when one interface revs a major `@libp2p/components` major has to change too which means every module depending on it also needs a major. Switch instead to constructor injection of simple objects that let modules declare their dependencies on interfaces directly instead of indirectly via `@libp2p/components` Fixes libp2p/js-libp2p-components#6 BREAKING CHANGE: modules no longer implement `Initializable` instead switching to constructor injection
1 parent b717beb commit a3847f2

File tree

103 files changed

+1998
-1312
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+1998
-1312
lines changed

.aegir.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { WebSockets } from '@libp2p/websockets'
2-
import { Mplex } from '@libp2p/mplex'
1+
import { webSockets } from '@libp2p/websockets'
2+
import { mplex } from '@libp2p/mplex'
33
import { Noise } from '@chainsafe/libp2p-noise'
44
import { pipe } from 'it-pipe'
55
import { createFromJSON } from '@libp2p/peer-id-factory'
@@ -14,7 +14,7 @@ export default {
1414
// use dynamic import because we only want to reference these files during the test run, e.g. after building
1515
const { createLibp2p } = await import('./dist/src/index.js')
1616
const { MULTIADDRS_WEBSOCKETS } = await import('./dist/test/fixtures/browser.js')
17-
const { Plaintext } = await import('./dist/src/insecure/index.js')
17+
const { plaintext } = await import('./dist/src/insecure/index.js')
1818
const { default: Peers } = await import('./dist/test/fixtures/peers.js')
1919

2020
// Use the last peer
@@ -28,14 +28,14 @@ export default {
2828
},
2929
peerId,
3030
transports: [
31-
new WebSockets()
31+
webSockets()
3232
],
3333
streamMuxers: [
34-
new Mplex()
34+
mplex()
3535
],
3636
connectionEncryption: [
37-
new Noise(),
38-
new Plaintext()
37+
() => () => new Noise(),
38+
plaintext()
3939
],
4040
relay: {
4141
enabled: true,

doc/migrations/v0.37-v0.40.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Migrating to libp2p@40 <!-- omit in toc -->
2+
3+
A migration guide for refactoring your application code from libp2p v0.37.x to v0.40.0.
4+
5+
## Table of Contents <!-- omit in toc -->
6+
7+
- [Modules](#modules)
8+
- [Autodial](#autodial)
9+
10+
## Modules
11+
12+
Modules in the libp2p ecosystem no longer export constructors. This is because internally libp2p has switched to using constructor dependency injection so needs to control the lifecycle of the various components.
13+
14+
These modules now export factory functions that take the same arguments as the older constructors so migration should be relatively straightforward.
15+
16+
The most affected place will be where you configure your libp2p node:
17+
18+
**Before**
19+
20+
```js
21+
import { createLibp2p } from 'libp2p'
22+
import { TCP } from '@libp2p/tcp'
23+
import { WebSockets } from '@libp2p/websockets'
24+
import { WebRTCStar } from '@libp2p/webrtc-star'
25+
import { WebRTCDirect } from '@libp2p/webrtc-direct'
26+
import { KadDHT } from '@libp2p/kad-dht'
27+
import Gossipsub from '@chainsafe/libp2p-gossipsub'
28+
import { Bootstrap } from '@libp2p/bootstrap'
29+
import { MulticastDNS } from '@libp2p/mdns'
30+
import { Noise } from '@chainsafe/libp2p-noise'
31+
import { Yamux } from '@chainsafe/libp2p-yamux'
32+
import { Mplex } from '@libp2p/mplex'
33+
import { DelegatedContentRouting } from '@libp2p/delegated-content-routing'
34+
import { DelegatedPeerRouting } from '@libp2p/delegated-peer-routing'
35+
import { create as createIpfsHttpClient } from 'ipfs-http-client'
36+
37+
const star = new WebRTCStar()
38+
const ipfsHttpClinet = createIpfsHttpClient(new URL('https://node0.delegate.ipfs.io'))
39+
40+
const node = await createLibp2p({
41+
addresses: {
42+
listen: [
43+
'/ip4/0.0.0.0/tcp/0'
44+
]
45+
},
46+
transports: [
47+
new TCP(),
48+
new WebSockets(),
49+
new WebRTCDirect(),
50+
star
51+
],
52+
peerDiscovery: [
53+
new Bootstrap({ list: [ ... ] }),
54+
new MulticastDNS(),
55+
star.discovery
56+
],
57+
connectionEncryption: [
58+
new Noise()
59+
],
60+
streamMuxers: [
61+
new Yamux(),
62+
new Mplex()
63+
],
64+
contentRouting: [
65+
new DelegatedContentRouting(ipfsHttpClinet)
66+
],
67+
peerRouting: [
68+
new DelegatedPeerRouting(ipfsHttpClinet)
69+
],
70+
dht: new KadDHT(),
71+
pubsub: new Gossipsub()
72+
})
73+
74+
await node.start()
75+
// ...
76+
```
77+
78+
**After**
79+
80+
```js
81+
import { createLibp2p } from 'libp2p'
82+
import { tcp } from '@libp2p/tcp'
83+
import { webSockets } from '@libp2p/websockets'
84+
import { webRTCStar } from '@libp2p/webrtc-star'
85+
import { webRTCDirect } from '@libp2p/webrtc-direct'
86+
import { kadDHT } from '@libp2p/kad-dht'
87+
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
88+
import { bootstrap } from '@libp2p/bootstrap'
89+
import { mdns } from '@libp2p/mdns'
90+
import { noise } from '@chainsafe/libp2p-noise'
91+
import { yamux } from '@chainsafe/libp2p-yamux'
92+
import { mplex } from '@libp2p/mplex'
93+
import { delegatedContentRouting } from '@libp2p/delegated-content-routing'
94+
import { delegatedPeerRouting } from '@libp2p/delegated-peer-routing'
95+
import { create as createIpfsHttpClient } from 'ipfs-http-client'
96+
97+
const star = webRTCStar()
98+
const ipfsHttpClinet = createIpfsHttpClient(new URL('https://node0.delegate.ipfs.io'))
99+
100+
const node = await createLibp2p({
101+
addresses: {
102+
listen: [
103+
'/ip4/0.0.0.0/tcp/0'
104+
]
105+
},
106+
transports: [
107+
tcp(),
108+
webSockets(),
109+
webRTCDirect(),
110+
star
111+
],
112+
peerDiscovery: [
113+
bootstrap({ list: [ ... ] }),
114+
mdns(),
115+
star.discovery
116+
],
117+
connectionEncryption: [
118+
noise()
119+
],
120+
streamMuxers: [
121+
yamux(),
122+
mplex()
123+
],
124+
contentRouting: [
125+
delegatedContentRouting(ipfsHttpClinet)
126+
],
127+
peerRouting: [
128+
delegatedPeerRouting(ipfsHttpClinet)
129+
],
130+
dht: kadDHT(),
131+
pubsub: gossipsub()
132+
})
133+
134+
await node.start()
135+
```
136+
137+
## Autodial
138+
139+
In versions of libp2p prior to 0.40.x, when the peer discovery subsystem announced a new peer, that peer would automatically be dialed and a connection held open, subject to [the normal rules on closing connections](https://github.com/libp2p/js-libp2p/blob/master/doc/LIMITS.md#closing-connections). Prior to having a working DHT this was necessary to increase the chances that you would be connected to a peer that could service any network request you might make. The downside to this is that connections are expensive so nodes displayed high CPU usage even when idle. Now that the DHT is in place this is no longer necessary as we can discover nearby peers using that mechanism.
140+
141+
When dialing a remote peer the [identify protocol](https://docs.libp2p.io/concepts/protocols/#identify) is run, during which we find out which protocols the remote peer supports. This in turn affects the nodes added to [topologies](https://github.com/libp2p/js-libp2p-topology#readme) for a given protocol. Topologies may see fewer nodes added to them as we are now dialing and running identify on fewer peers, though the peers that do get added are likely to be higher value - their PeerIds will be KAD-closer to ours, they would have been dialed directly rather than being random peers on the network, etc.

examples/auto-relay/dialer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLibp2p } from 'libp2p'
2-
import { WebSockets } from '@libp2p/websockets'
2+
import { webSockets } from '@libp2p/websockets'
33
import { Noise } from '@chainsafe/libp2p-noise'
4-
import { Mplex } from '@libp2p/mplex'
4+
import { mplex } from '@libp2p/mplex'
55

66
async function main () {
77
const autoRelayNodeAddr = process.argv[2]
@@ -11,13 +11,13 @@ async function main () {
1111

1212
const node = await createLibp2p({
1313
transports: [
14-
new WebSockets()
14+
webSockets()
1515
],
1616
connectionEncryption: [
17-
new Noise()
17+
() => new Noise()
1818
],
1919
streamMuxers: [
20-
new Mplex()
20+
mplex()
2121
]
2222
})
2323

examples/auto-relay/listener.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLibp2p } from 'libp2p'
2-
import { WebSockets } from '@libp2p/websockets'
2+
import { webSockets } from '@libp2p/websockets'
33
import { Noise } from '@chainsafe/libp2p-noise'
4-
import { Mplex } from '@libp2p/mplex'
4+
import { mplex } from '@libp2p/mplex'
55

66
async function main () {
77
const relayAddr = process.argv[2]
@@ -11,13 +11,13 @@ async function main () {
1111

1212
const node = await createLibp2p({
1313
transports: [
14-
new WebSockets()
14+
webSockets()
1515
],
1616
connectionEncryption: [
17-
new Noise()
17+
() => new Noise()
1818
],
1919
streamMuxers: [
20-
new Mplex()
20+
mplex()
2121
],
2222
relay: {
2323
enabled: true,

examples/auto-relay/relay.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createLibp2p } from 'libp2p'
2-
import { WebSockets } from '@libp2p/websockets'
2+
import { webSockets } from '@libp2p/websockets'
33
import { Noise } from '@chainsafe/libp2p-noise'
4-
import { Mplex } from '@libp2p/mplex'
4+
import { mplex } from '@libp2p/mplex'
55

66
async function main () {
77
const node = await createLibp2p({
@@ -11,13 +11,13 @@ async function main () {
1111
// announce: ['/dns4/auto-relay.libp2p.io/tcp/443/wss/p2p/QmWDn2LY8nannvSWJzruUYoLZ4vV83vfCBwd8DipvdgQc3']
1212
},
1313
transports: [
14-
new WebSockets()
14+
webSockets()
1515
],
1616
connectionEncryption: [
17-
new Noise()
17+
() => new Noise()
1818
],
1919
streamMuxers: [
20-
new Mplex()
20+
mplex()
2121
],
2222
relay: {
2323
enabled: true,

examples/chat/src/libp2p.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { TCP } from '@libp2p/tcp'
2-
import { WebSockets } from '@libp2p/websockets'
3-
import { Mplex } from '@libp2p/mplex'
1+
import { tcp } from '@libp2p/tcp'
2+
import { webSockets } from '@libp2p/websockets'
3+
import { mplex } from '@libp2p/mplex'
44
import { Noise } from '@chainsafe/libp2p-noise'
55
import defaultsDeep from '@nodeutils/defaults-deep'
66
import { createLibp2p as create } from 'libp2p'
77

88
export async function createLibp2p(_options) {
99
const defaults = {
1010
transports: [
11-
new TCP(),
12-
new WebSockets()
11+
tcp(),
12+
webSockets()
1313
],
1414
streamMuxers: [
15-
new Mplex()
15+
mplex()
1616
],
1717
connectionEncryption: [
18-
new Noise()
18+
() => new Noise()
1919
]
2020
}
2121

examples/connection-encryption/1.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createLibp2p } from '../../dist/src/index.js'
2-
import { TCP } from '@libp2p/tcp'
3-
import { Mplex } from '@libp2p/mplex'
2+
import { tcp } from '@libp2p/tcp'
3+
import { mplex } from '@libp2p/mplex'
44
import { Noise } from '@chainsafe/libp2p-noise'
55
import { pipe } from 'it-pipe'
66
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
@@ -11,9 +11,9 @@ const createNode = async () => {
1111
addresses: {
1212
listen: ['/ip4/0.0.0.0/tcp/0']
1313
},
14-
transports: [new TCP()],
15-
streamMuxers: [new Mplex()],
16-
connectionEncryption: [new Noise()]
14+
transports: [tcp()],
15+
streamMuxers: [mplex()],
16+
connectionEncryption: [() => new Noise()]
1717
})
1818

1919
await node.start()

examples/delegated-routing/package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
"dependencies": {
66
"@chainsafe/libp2p-noise": "^9.0.0",
77
"ipfs-core": "^0.15.4",
8+
"ipfs-http-client": "^58.0.1",
89
"libp2p": "../../",
9-
"@libp2p/delegated-content-routing": "^2.0.2",
10-
"@libp2p/delegated-peer-routing": "^2.0.2",
11-
"@libp2p/kad-dht": "^4.0.0",
12-
"@libp2p/mplex": "^6.0.2",
13-
"@libp2p/webrtc-star": "^4.0.1",
14-
"@libp2p/websockets": "^4.0.0",
10+
"@libp2p/delegated-content-routing": "^3.0.0",
11+
"@libp2p/delegated-peer-routing": "^3.0.0",
12+
"@libp2p/kad-dht": "^5.0.1",
13+
"@libp2p/mplex": "^7.0.0",
14+
"@libp2p/webrtc-star": "^5.0.2",
15+
"@libp2p/websockets": "^5.0.0",
1516
"react": "^17.0.2",
1617
"react-dom": "^17.0.2",
1718
"react-scripts": "5.0.0"

examples/delegated-routing/src/libp2p-bundle.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@
22
'use strict'
33

44
import { createLibp2p } from 'libp2p'
5-
import { WebSockets } from '@libp2p/websockets'
6-
import { WebRTCStar } from '@libp2p/webrtc-star'
7-
import { Mplex } from '@libp2p/mplex'
5+
import { webSockets } from '@libp2p/websockets'
6+
import { webRTCStar } from '@libp2p/webrtc-star'
7+
import { mplex } from '@libp2p/mplex'
88
import { Noise } from '@chainsafe/libp2p-noise'
9-
import { DelegatedPeerRouting } from '@libp2p/delegated-peer-routing'
10-
import { DelegatedContentRouting } from '@libp2p/delegated-content-routing'
9+
import { delegatedPeerRouting } from '@libp2p/delegated-peer-routing'
10+
import { delegatedContentRouting } from '@libp2p/delegated-content-routing'
11+
import { create as createIpfsHttpClient } from 'ipfs-http-client'
1112

1213
export default function Libp2pBundle ({peerInfo, peerBook}) {
13-
const wrtcstar = new WebRTCStar()
14-
const delegatedApiOptions = {
14+
const wrtcstar = new webRTCStar()
15+
const client = createIpfsHttpClient({
1516
host: '0.0.0.0',
1617
protocol: 'http',
1718
port: '8080'
18-
}
19+
})
1920

2021
return createLibp2p({
2122
peerInfo,
@@ -26,20 +27,23 @@ export default function Libp2pBundle ({peerInfo, peerBook}) {
2627
pollInterval: 5000
2728
},
2829
contentRouting: [
29-
new DelegatedPeerRouting(peerInfo.id, delegatedApiOptions)
30+
delegatedPeerRouting(client)
3031
],
3132
peerRouting: [
32-
new DelegatedContentRouting(delegatedApiOptions)
33+
delegatedContentRouting(client)
3334
],
3435
transports: [
35-
wrtcstar,
36-
new WebSockets()
36+
wrtcstar.transport,
37+
webSockets()
3738
],
3839
streamMuxers: [
39-
new Mplex()
40+
mplex()
41+
],
42+
peerDiscovery: [
43+
wrtcstar.discovery
4044
],
4145
connectionEncryption: [
42-
new Noise()
46+
() => new Noise()
4347
],
4448
connectionManager: {
4549
autoDial: false

0 commit comments

Comments
 (0)