Skip to content

Commit 66572bc

Browse files
committed
fix!: remove @libp2p/components
`@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 66572bc

File tree

102 files changed

+1911
-1263
lines changed

Some content is hidden

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

102 files changed

+1911
-1263
lines changed

.aegir.js

+7-7
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

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
9+
## Modules
10+
11+
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.
12+
13+
These modules now export factory functions that take the same arguments as the older constructors so migration should be relatively straightforward.
14+
15+
The most affected place will be where you configure your libp2p node:
16+
17+
**Before**
18+
19+
```js
20+
import { createLibp2p } from 'libp2p'
21+
import { TCP } from '@libp2p/tcp'
22+
import { Noise } from '@chainsafe/libp2p-noise'
23+
24+
const createNode = async () => {
25+
const node = await createLibp2p({
26+
addresses: {
27+
listen: [
28+
'/ip4/0.0.0.0/tcp/0'
29+
]
30+
},
31+
transports: [
32+
new TCP()
33+
],
34+
connectionEncryption: [
35+
new Noise()
36+
]
37+
})
38+
39+
await node.start()
40+
return node
41+
}
42+
43+
const node = await createNode()
44+
await node.start()
45+
// ...
46+
```
47+
48+
**After**
49+
50+
```js
51+
import { createLibp2p } from 'libp2p'
52+
import { tcp } from '@libp2p/tcp'
53+
import { noise } from '@chainsafe/libp2p-noise'
54+
55+
const createNode = async () => {
56+
const node = await createLibp2p({
57+
addresses: {
58+
listen: [
59+
'/ip4/0.0.0.0/tcp/0'
60+
]
61+
},
62+
transports: [
63+
tcp()
64+
],
65+
connectionEncryption: [
66+
noise()
67+
]
68+
})
69+
70+
await node.start()
71+
return node
72+
}
73+
74+
const node = await createNode()
75+
await node.start()
76+
```

examples/auto-relay/dialer.js

+5-5
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

+5-5
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

+5-5
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

+7-7
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

+5-5
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

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"libp2p": "../../",
99
"@libp2p/delegated-content-routing": "^2.0.2",
1010
"@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",
11+
"@libp2p/kad-dht": "^5.0.0",
12+
"@libp2p/mplex": "^7.0.0",
13+
"@libp2p/webrtc-star": "^5.0.1",
14+
"@libp2p/websockets": "^5.0.0",
1515
"react": "^17.0.2",
1616
"react-dom": "^17.0.2",
1717
"react-scripts": "5.0.0"

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
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'
99
import { DelegatedPeerRouting } from '@libp2p/delegated-peer-routing'
1010
import { DelegatedContentRouting } from '@libp2p/delegated-content-routing'
1111

1212
export default function Libp2pBundle ({peerInfo, peerBook}) {
13-
const wrtcstar = new WebRTCStar()
13+
const wrtcstar = new webRTCStar()
1414
const delegatedApiOptions = {
1515
host: '0.0.0.0',
1616
protocol: 'http',
@@ -33,13 +33,13 @@ export default function Libp2pBundle ({peerInfo, peerBook}) {
3333
],
3434
transports: [
3535
wrtcstar,
36-
new WebSockets()
36+
webSockets()
3737
],
3838
streamMuxers: [
39-
new Mplex()
39+
mplex()
4040
],
4141
connectionEncryption: [
42-
new Noise()
42+
() => new Noise()
4343
],
4444
connectionManager: {
4545
autoDial: false

examples/discovery-mechanisms/1.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/* eslint-disable no-console */
22

33
import { createLibp2p } from 'libp2p'
4-
import { TCP } from '@libp2p/tcp'
5-
import { Mplex } from '@libp2p/mplex'
4+
import { tcp } from '@libp2p/tcp'
5+
import { mplex } from '@libp2p/mplex'
66
import { Noise } from '@chainsafe/libp2p-noise'
7-
import { Bootstrap } from '@libp2p/bootstrap'
7+
import { bootstrap } from '@libp2p/bootstrap'
88
import bootstrapers from './bootstrappers.js'
99

1010
;(async () => {
1111
const node = await createLibp2p({
1212
addresses: {
1313
listen: ['/ip4/0.0.0.0/tcp/0']
1414
},
15-
transports: [new TCP()],
16-
streamMuxers: [new Mplex()],
17-
connectionEncryption: [new Noise()],
15+
transports: [tcp()],
16+
streamMuxers: [mplex()],
17+
connectionEncryption: [() => new Noise()],
1818
peerDiscovery: [
19-
new Bootstrap({
19+
bootstrap({
2020
interval: 60e3,
2121
list: bootstrapers
2222
})

examples/discovery-mechanisms/2.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
/* eslint-disable no-console */
22

33
import { createLibp2p } from 'libp2p'
4-
import { TCP } from '@libp2p/tcp'
5-
import { Mplex } from '@libp2p/mplex'
4+
import { tcp } from '@libp2p/tcp'
5+
import { mplex } from '@libp2p/mplex'
66
import { Noise } from '@chainsafe/libp2p-noise'
7-
import { MulticastDNS } from '@libp2p/mdns'
7+
import { mdns } from '@libp2p/mdns'
88

99
const createNode = async () => {
1010
const node = await createLibp2p({
1111
addresses: {
1212
listen: ['/ip4/0.0.0.0/tcp/0']
1313
},
1414
transports: [
15-
new TCP()
15+
tcp()
1616
],
1717
streamMuxers: [
18-
new Mplex()
18+
mplex()
1919
],
2020
connectionEncryption: [
21-
new Noise()
21+
() => new Noise()
2222
],
2323
peerDiscovery: [
24-
new MulticastDNS({
24+
mdns({
2525
interval: 20e3
2626
})
2727
]

0 commit comments

Comments
 (0)