Skip to content

Commit d40e34d

Browse files
authored
feat: upgrade to v3 api (#294)
Upgrades all examples to `[email protected]` API
1 parent 7079fb6 commit d40e34d

File tree

57 files changed

+664
-511
lines changed

Some content is hidden

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

57 files changed

+664
-511
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
- js-libp2p-example-pubsub
3535
- js-libp2p-example-transports
3636
- js-libp2p-example-webrtc-private-to-private
37+
- js-libp2p-example-webrtc-private-to-public
3738
defaults:
3839
run:
3940
working-directory: examples/${{ matrix.project }}
@@ -95,6 +96,7 @@ jobs:
9596
- js-libp2p-example-pubsub
9697
- js-libp2p-example-transports
9798
- js-libp2p-example-webrtc-private-to-private
99+
- js-libp2p-example-webrtc-private-to-public
98100
steps:
99101
- uses: convictional/trigger-workflow-and-wait@f69fa9eedd3c62a599220f4d5745230e237904be
100102
with:

examples/js-libp2p-example-auto-tls/package.json

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@
1616
},
1717
"type": "module",
1818
"dependencies": {
19-
"@chainsafe/libp2p-noise": "^16.0.0",
20-
"@chainsafe/libp2p-yamux": "^7.0.0",
19+
"@chainsafe/libp2p-noise": "^17.0.0",
20+
"@chainsafe/libp2p-yamux": "^8.0.0",
2121
"@ipshipyard/libp2p-auto-tls": "^1.0.0",
22-
"@libp2p/autonat": "^2.0.13",
23-
"@libp2p/bootstrap": "^11.0.14",
22+
"@libp2p/autonat": "^3.0.0",
23+
"@libp2p/bootstrap": "^12.0.0",
2424
"@libp2p/config": "^1.0.0",
25-
"@libp2p/identify": "^3.0.13",
26-
"@libp2p/kad-dht": "^15.0.0",
27-
"@libp2p/keychain": "^5.0.11",
28-
"@libp2p/ping": "^2.0.27",
29-
"@libp2p/upnp-nat": "^3.0.1",
30-
"@libp2p/websockets": "^9.1.0",
31-
"@multiformats/multiaddr-matcher": "^1.6.0",
25+
"@libp2p/identify": "^4.0.0",
26+
"@libp2p/kad-dht": "^16.0.0",
27+
"@libp2p/keychain": "^6.0.0",
28+
"@libp2p/ping": "^3.0.0",
29+
"@libp2p/tcp": "^11.0.1",
30+
"@libp2p/upnp-nat": "^4.0.0",
31+
"@libp2p/websockets": "^10.0.0",
32+
"@multiformats/multiaddr-matcher": "^3.0.1",
3233
"datastore-level": "^11.0.1",
33-
"libp2p": "^2.0.0"
34+
"libp2p": "^3.0.0"
3435
},
3536
"private": true
3637
}

examples/js-libp2p-example-browser-pubsub/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
1010
This example leverages the [vite bundler](https://vitejs.dev/) to compile and serve the libp2p code in the browser. You can use other bundlers such as Webpack, but we will not be covering them here.
1111

12+
> [!TIP]
13+
> This example uses `@libp2p/floodsub` as a pubsub implementation - it is not suitable for production use, instead use `@chainsafe/libp2p-gossipsub`
14+
1215
## Table of contents <!-- omit in toc -->
1316

1417
- [Setup](#setup)

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
21
import { noise } from '@chainsafe/libp2p-noise'
32
import { yamux } from '@chainsafe/libp2p-yamux'
43
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
5-
import { dcutr } from '@libp2p/dcutr'
4+
import { floodsub } from '@libp2p/floodsub'
65
import { identify } from '@libp2p/identify'
76
import { webRTC } from '@libp2p/webrtc'
87
import { webSockets } from '@libp2p/websockets'
9-
import * as filters from '@libp2p/websockets/filters'
108
import { multiaddr } from '@multiformats/multiaddr'
9+
import { WebRTC } from '@multiformats/multiaddr-matcher'
1110
import { createLibp2p } from 'libp2p'
1211
import { fromString, toString } from 'uint8arrays'
1312

@@ -48,10 +47,7 @@ const libp2p = await createLibp2p({
4847
},
4948
transports: [
5049
// the WebSocket transport lets us dial a local relay
51-
webSockets({
52-
// this allows non-secure WebSocket connections for purposes of the demo
53-
filter: filters.all
54-
}),
50+
webSockets(),
5551
// support dialing/listening on WebRTC addresses
5652
webRTC(),
5753
// support dialing/listening on Circuit Relay addresses
@@ -72,8 +68,7 @@ const libp2p = await createLibp2p({
7268
},
7369
services: {
7470
identify: identify(),
75-
pubsub: gossipsub(),
76-
dcutr: dcutr()
71+
pubsub: floodsub()
7772
}
7873
})
7974

@@ -113,6 +108,7 @@ libp2p.addEventListener('connection:close', () => {
113108
// update listening addresses
114109
libp2p.addEventListener('self:peer:update', () => {
115110
const multiaddrs = libp2p.getMultiaddrs()
111+
.filter(ma => WebRTC.matches(ma))
116112
.map((ma) => {
117113
const el = document.createElement('li')
118114
el.textContent = ma.toString()

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@
2121
"test": "npm run build && test-browser-example test"
2222
},
2323
"dependencies": {
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": "^3.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",
24+
"@chainsafe/libp2p-noise": "^17.0.0",
25+
"@chainsafe/libp2p-yamux": "^8.0.0",
26+
"@libp2p/circuit-relay-v2": "^4.0.1",
27+
"@libp2p/floodsub": "^11.0.1",
28+
"@libp2p/identify": "^4.0.1",
29+
"@libp2p/webrtc": "^6.0.3",
30+
"@libp2p/websockets": "^10.0.1",
31+
"@multiformats/multiaddr": "^13.0.1",
32+
"@multiformats/multiaddr-matcher": "^3.0.1",
33+
"libp2p": "^3.0.2",
34+
"uint8arrays": "^5.1.0",
3435
"vite": "^6.0.3"
3536
},
3637
"devDependencies": {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ import { yamux } from '@chainsafe/libp2p-yamux'
55
import { circuitRelayServer } from '@libp2p/circuit-relay-v2'
66
import { identify } from '@libp2p/identify'
77
import { webSockets } from '@libp2p/websockets'
8-
import * as filters from '@libp2p/websockets/filters'
98
import { createLibp2p } from 'libp2p'
109

1110
const server = await createLibp2p({
1211
addresses: {
1312
listen: ['/ip4/127.0.0.1/tcp/0/ws']
1413
},
1514
transports: [
16-
webSockets({
17-
filter: filters.all
18-
})
15+
webSockets()
1916
],
2017
connectionEncrypters: [noise()],
2118
streamMuxers: [yamux()],

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
/* eslint-disable no-console */
21
import { noise } from '@chainsafe/libp2p-noise'
32
import { yamux } from '@chainsafe/libp2p-yamux'
43
import { circuitRelayServer } from '@libp2p/circuit-relay-v2'
54
import { identify } from '@libp2p/identify'
65
import { webSockets } from '@libp2p/websockets'
7-
import * as filters from '@libp2p/websockets/filters'
86
import { createLibp2p } from 'libp2p'
97
import { setup, expect } from 'test-ipfs-example/browser'
108

@@ -32,9 +30,7 @@ async function spawnRelay () {
3230
listen: ['/ip4/127.0.0.1/tcp/0/ws']
3331
},
3432
transports: [
35-
webSockets({
36-
filter: filters.all
37-
})
33+
webSockets()
3834
],
3935
connectionEncrypters: [noise()],
4036
streamMuxers: [yamux()],
@@ -53,7 +49,6 @@ test.describe('pubsub browser example:', () => {
5349
let relayNode
5450
let relayNodeAddr
5551

56-
// eslint-disable-next-line no-empty-pattern
5752
test.beforeAll(async ({ servers }, testInfo) => {
5853
testInfo.setTimeout(5 * 60_000)
5954
const r = await spawnRelay()
@@ -70,7 +65,7 @@ test.describe('pubsub browser example:', () => {
7065
await page.goto(url)
7166
})
7267

73-
test('should connect via a relay node', async ({ page: pageA, context }) => {
68+
test('should send and receive a message', async ({ page: pageA, context }) => {
7469
// load second page
7570
const pageB = await context.newPage()
7671
await pageB.goto(url)

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,18 @@
1616
"test": "test-node-example test/*"
1717
},
1818
"dependencies": {
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",
19+
"@chainsafe/libp2p-noise": "^17.0.0",
20+
"@chainsafe/libp2p-yamux": "^8.0.0",
21+
"@libp2p/mdns": "^12.0.2",
22+
"@libp2p/tcp": "^11.0.1",
23+
"@libp2p/utils": "^7.0.1",
24+
"@libp2p/websockets": "^10.0.1",
2525
"@nodeutils/defaults-deep": "^1.1.0",
26-
"it-length-prefixed": "^10.0.1",
27-
"it-map": "^3.0.3",
28-
"it-pipe": "^3.0.1",
29-
"libp2p": "^2.0.0",
30-
"p-defer": "^4.0.0",
26+
"libp2p": "^3.0.2",
3127
"uint8arrays": "^5.1.0"
3228
},
3329
"devDependencies": {
30+
"p-defer": "^4.0.0",
3431
"test-ipfs-example": "^1.1.0"
3532
},
3633
"private": true

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function run () {
1818
})
1919

2020
// Handle messages for the protocol
21-
await listener.handle('/chat/1.0.0', async ({ stream }) => {
21+
await listener.handle('/chat/1.0.0', async (stream) => {
2222
// Send stdin to the stream
2323
stdinToStream(stream)
2424
// Read the stream and output to console
Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
/* eslint-disable no-console */
22

3-
import * as lp from 'it-length-prefixed'
4-
import map from 'it-map'
5-
import { pipe } from 'it-pipe'
6-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
3+
import { lpStream } from '@libp2p/utils'
74
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
85

96
export function stdinToStream (stream) {
10-
// Read utf-8 from stdin
11-
process.stdin.setEncoding('utf8')
12-
pipe(
13-
// Read from stdin (the source)
14-
process.stdin,
15-
// Turn strings into buffers
16-
(source) => map(source, (string) => uint8ArrayFromString(string)),
17-
// Encode with length prefix (so receiving side knows how much data is coming)
18-
(source) => lp.encode(source),
19-
// Write to the stream (the sink)
20-
stream.sink
21-
)
7+
// Encode with length prefix (so receiving side knows how much data is coming)
8+
const lp = lpStream(stream)
9+
10+
process.stdin.addListener('data', (buf) => {
11+
lp.write(buf)
12+
})
2213
}
2314

2415
export function streamToConsole (stream) {
25-
pipe(
26-
// Read from the stream (the source)
27-
stream.source,
28-
// Decode length-prefixed data
29-
(source) => lp.decode(source),
30-
// Turn buffers into strings
31-
(source) => map(source, (buf) => uint8ArrayToString(buf.subarray())),
32-
// Sink function
33-
async function (source) {
34-
// For each chunk of data
35-
for await (const msg of source) {
36-
// Output the data as a utf8 string
37-
console.log('> ' + msg.toString().replace('\n', ''))
38-
}
16+
const lp = lpStream(stream)
17+
18+
Promise.resolve().then(async () => {
19+
while (true) {
20+
// Read from the stream
21+
const message = await lp.read()
22+
23+
// Output the data as a utf8 string
24+
console.log('> ' + uint8ArrayToString(message.subarray()).replace('\n', ''))
3925
}
40-
)
26+
})
4127
}

0 commit comments

Comments
 (0)