Skip to content

Commit 83dd9df

Browse files
authored
deps: update libp2p deps to v2 (#170)
Updates examples to use the new libp2p release
1 parent d006979 commit 83dd9df

File tree

28 files changed

+101
-131
lines changed

28 files changed

+101
-131
lines changed

examples/js-libp2p-example-browser-pubsub/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const libp2p = await createLibp2p({
5959
})
6060
],
6161
// a connection encrypter is necessary to dial the relay
62-
connectionEncryption: [noise()],
62+
connectionEncrypters: [noise()],
6363
// a stream muxer is necessary to dial the relay
6464
streamMuxers: [yamux()],
6565
connectionGater: {
@@ -75,9 +75,6 @@ const libp2p = await createLibp2p({
7575
identify: identify(),
7676
pubsub: gossipsub(),
7777
dcutr: dcutr()
78-
},
79-
connectionManager: {
80-
minConnections: 0
8178
}
8279
})
8380

examples/js-libp2p-example-browser-pubsub/package.json

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@
2121
"test": "npm run build && test-browser-example test"
2222
},
2323
"dependencies": {
24-
"@chainsafe/libp2p-gossipsub": "^13.0.0",
25-
"@chainsafe/libp2p-noise": "^15.0.0",
26-
"@chainsafe/libp2p-yamux": "^6.0.1",
27-
"@libp2p/circuit-relay-v2": "^1.0.4",
28-
"@libp2p/dcutr": "^1.0.3",
29-
"@libp2p/identify": "^2.1.0",
30-
"@libp2p/mplex": "^10.1.0",
31-
"@libp2p/webrtc": "^4.0.5",
32-
"@libp2p/websockets": "^8.0.4",
33-
"@multiformats/multiaddr": "^12.1.11",
34-
"libp2p": "^1.0.8",
24+
"@chainsafe/libp2p-gossipsub": "^14.0.0",
25+
"@chainsafe/libp2p-noise": "^16.0.0",
26+
"@chainsafe/libp2p-yamux": "^7.0.0",
27+
"@libp2p/circuit-relay-v2": "^2.0.0",
28+
"@libp2p/dcutr": "^2.0.0",
29+
"@libp2p/identify": "^3.0.0",
30+
"@libp2p/webrtc": "^5.0.0",
31+
"@libp2p/websockets": "^9.0.0",
32+
"@multiformats/multiaddr": "^12.3.1",
33+
"libp2p": "^2.0.0",
3534
"vite": "^5.3.1"
3635
},
3736
"devDependencies": {

examples/js-libp2p-example-browser-pubsub/relay.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { noise } from '@chainsafe/libp2p-noise'
44
import { yamux } from '@chainsafe/libp2p-yamux'
55
import { circuitRelayServer } from '@libp2p/circuit-relay-v2'
66
import { identify } from '@libp2p/identify'
7-
import { mplex } from '@libp2p/mplex'
87
import { webSockets } from '@libp2p/websockets'
98
import * as filters from '@libp2p/websockets/filters'
109
import { createLibp2p } from 'libp2p'
@@ -18,18 +17,15 @@ const server = await createLibp2p({
1817
filter: filters.all
1918
})
2019
],
21-
connectionEncryption: [noise()],
22-
streamMuxers: [yamux(), mplex()],
20+
connectionEncrypters: [noise()],
21+
streamMuxers: [yamux()],
2322
services: {
2423
identify: identify(),
2524
relay: circuitRelayServer({
2625
reservations: {
2726
maxReservations: Infinity
2827
}
2928
})
30-
},
31-
connectionManager: {
32-
minConnections: 0
3329
}
3430
})
3531

examples/js-libp2p-example-browser-pubsub/test/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function spawnRelay () {
3636
filter: filters.all
3737
})
3838
],
39-
connectionEncryption: [noise()],
39+
connectionEncrypters: [noise()],
4040
streamMuxers: [yamux()],
4141
services: {
4242
identify: identify(),

examples/js-libp2p-example-chat/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
1. Run the listener in window 1, `node listener.js`
2828
2. Run the dialer in window 2, `node dialer.js`
29-
3. Type a message in either window and hit *enter*
30-
4. Tell yourself secrets to your hearts content!
29+
3. Wait until the two peers discover each other
30+
4. Type a message in either window and hit *enter*
31+
5. Tell yourself secrets to your hearts content!
3132

3233
## License
3334

examples/js-libp2p-example-chat/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
"test": "test-node-example test/*"
1717
},
1818
"dependencies": {
19-
"@chainsafe/libp2p-noise": "^15.0.0",
20-
"@chainsafe/libp2p-yamux": "^6.0.1",
21-
"@libp2p/peer-id-factory": "^4.0.0",
22-
"@libp2p/tcp": "^9.0.4",
23-
"@libp2p/websockets": "^8.0.4",
24-
"@multiformats/multiaddr": "^12.1.11",
19+
"@chainsafe/libp2p-noise": "^16.0.0",
20+
"@chainsafe/libp2p-yamux": "^7.0.0",
21+
"@libp2p/mdns": "^11.0.1",
22+
"@libp2p/tcp": "^10.0.0",
23+
"@libp2p/websockets": "^9.0.0",
24+
"@multiformats/multiaddr": "^12.3.1",
2525
"@nodeutils/defaults-deep": "^1.1.0",
2626
"it-length-prefixed": "^9.0.1",
2727
"it-map": "^3.0.3",
2828
"it-pipe": "^3.0.1",
29-
"libp2p": "^1.0.8",
29+
"libp2p": "^2.0.0",
3030
"p-defer": "^4.0.0",
3131
"uint8arrays": "^5.1.0"
3232
},
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,37 @@
11
/* eslint-disable no-console */
22

3-
import { createFromJSON } from '@libp2p/peer-id-factory'
4-
import { multiaddr } from '@multiformats/multiaddr'
53
import { createLibp2p } from './libp2p.js'
6-
import peerIdDialerJson from './peer-id-dialer.js'
7-
import peerIdListenerJson from './peer-id-listener.js'
84
import { stdinToStream, streamToConsole } from './stream.js'
95

106
async function run () {
11-
const [idDialer, idListener] = await Promise.all([
12-
createFromJSON(peerIdDialerJson),
13-
createFromJSON(peerIdListenerJson)
14-
])
15-
167
// Create a new libp2p node on localhost with a randomly chosen port
17-
const nodeDialer = await createLibp2p({
18-
peerId: idDialer,
8+
const dialer = await createLibp2p({
199
addresses: {
2010
listen: ['/ip4/0.0.0.0/tcp/0']
2111
}
2212
})
2313

2414
// Output this node's address
2515
console.log('Dialer ready, listening on:')
26-
nodeDialer.getMultiaddrs().forEach((ma) => {
16+
dialer.getMultiaddrs().forEach((ma) => {
2717
console.log(ma.toString())
2818
})
2919

30-
// Dial to the remote peer (the "listener")
31-
const listenerMa = multiaddr(`/ip4/127.0.0.1/tcp/10333/p2p/${idListener.toString()}`)
32-
const stream = await nodeDialer.dialProtocol(listenerMa, '/chat/1.0.0')
20+
dialer.addEventListener('peer:discovery', (evt) => {
21+
console.info('peer:discovery', evt.detail)
3322

34-
console.log('Dialer dialed to listener on protocol: /chat/1.0.0')
35-
console.log('Type a message and see what happens')
23+
// Dial to the remote peer (the "listener")
24+
dialer.dialProtocol(evt.detail.multiaddrs, '/chat/1.0.0')
25+
.then(stream => {
26+
console.log('Dialer dialed to listener on protocol: /chat/1.0.0')
27+
console.log('Type a message and see what happens')
3628

37-
// Send stdin to the stream
38-
stdinToStream(stream)
39-
// Read the stream and output to console
40-
streamToConsole(stream)
29+
// Send stdin to the stream
30+
stdinToStream(stream)
31+
// Read the stream and output to console
32+
streamToConsole(stream)
33+
})
34+
})
4135
}
4236

4337
run()

examples/js-libp2p-example-chat/src/libp2p.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { noise } from '@chainsafe/libp2p-noise'
22
import { yamux } from '@chainsafe/libp2p-yamux'
3+
import { mdns } from '@libp2p/mdns'
34
import { tcp } from '@libp2p/tcp'
45
import { webSockets } from '@libp2p/websockets'
56
import defaultsDeep from '@nodeutils/defaults-deep'
@@ -14,8 +15,11 @@ export async function createLibp2p (_options) {
1415
streamMuxers: [
1516
yamux()
1617
],
17-
connectionEncryption: [
18+
connectionEncrypters: [
1819
noise()
20+
],
21+
peerDiscovery: [
22+
mdns()
1923
]
2024
}
2125

examples/js-libp2p-example-chat/src/listener.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
/* eslint-disable no-console */
22

3-
import { createFromJSON } from '@libp2p/peer-id-factory'
43
import { createLibp2p } from './libp2p.js'
5-
import peerIdListenerJson from './peer-id-listener.js'
64
import { stdinToStream, streamToConsole } from './stream.js'
75

86
async function run () {
97
// Create a new libp2p node with the given multi-address
10-
const idListener = await createFromJSON(peerIdListenerJson)
11-
const nodeListener = await createLibp2p({
12-
peerId: idListener,
8+
const listener = await createLibp2p({
139
addresses: {
1410
listen: ['/ip4/0.0.0.0/tcp/10333']
1511
}
1612
})
1713

1814
// Log a message when a remote peer connects to us
19-
nodeListener.addEventListener('peer:connect', (evt) => {
15+
listener.addEventListener('peer:connect', (evt) => {
2016
const remotePeer = evt.detail
2117
console.log('connected to: ', remotePeer.toString())
2218
})
2319

2420
// Handle messages for the protocol
25-
await nodeListener.handle('/chat/1.0.0', async ({ stream }) => {
21+
await listener.handle('/chat/1.0.0', async ({ stream }) => {
2622
// Send stdin to the stream
2723
stdinToStream(stream)
2824
// Read the stream and output to console
@@ -31,7 +27,7 @@ async function run () {
3127

3228
// Output listen addresses to the console
3329
console.log('Listener ready, listening on:')
34-
nodeListener.getMultiaddrs().forEach((ma) => {
30+
listener.getMultiaddrs().forEach((ma) => {
3531
console.log(ma.toString())
3632
})
3733
}

examples/js-libp2p-example-chat/src/peer-id-dialer.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)