Skip to content

Commit d5f1937

Browse files
committed
Merge remote-tracking branch 'origin/master' into feat/merge-stat-properties
2 parents 149c456 + 7fb23cd commit d5f1937

File tree

144 files changed

+1875
-875
lines changed

Some content is hidden

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

144 files changed

+1875
-875
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -167,42 +167,16 @@ jobs:
167167
- uses: ipfs/aegir/actions/cache-node-modules@master
168168
- run: npm run test:interop -- --bail
169169

170-
test-example:
170+
test-examples:
171171
needs: build
172172
runs-on: ubuntu-latest
173-
strategy:
174-
matrix:
175-
example: [
176-
chat,
177-
connection-encryption,
178-
discovery-mechanisms,
179-
echo,
180-
libp2p-in-the-browser,
181-
peer-and-content-routing,
182-
pnet,
183-
protocol-and-stream-muxing,
184-
pubsub,
185-
transports
186-
]
187-
fail-fast: true
188173
steps:
189174
- uses: actions/checkout@v3
190175
- uses: actions/setup-node@v3
191176
with:
192177
node-version: lts/*
193178
- uses: ipfs/aegir/actions/cache-node-modules@master
194-
with:
195-
directories: |
196-
./examples/node_modules
197-
~/.cache
198-
build: |
199-
cd examples
200-
npm i
201-
npx playwright install
202-
cache_name: cache-examples
203-
- run: |
204-
cd examples
205-
npm run test -- ${{ matrix.example }}
179+
- run: npm run --if-present test:example
206180

207181
multidim-interop:
208182
needs: build
@@ -238,7 +212,7 @@ jobs:
238212
test-electron-main,
239213
test-electron-renderer,
240214
test-interop,
241-
test-example,
215+
test-examples,
242216
multidim-interop
243217
]
244218
if: github.event_name == 'push' && github.ref == 'refs/heads/master'

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@
3131

3232
## Structure
3333

34+
- [`/examples/auto-relay`](./examples/auto-relay) Shows how to configure relayed connections
35+
- [`/examples/chat`](./examples/chat) An example chat app using libp2p
36+
- [`/examples/connection-encryption`](./examples/connection-encryption) An example of how to configure connection encrypters
37+
- [`/examples/delegated-routing`](./examples/delegated-routing) How to configure libp2p delegated routers
38+
- [`/examples/discovery-mechanisms`](./examples/discovery-mechanisms) How to configure peer discovery mechanisms
39+
- [`/examples/echo`](./examples/echo) An example echo app
40+
- [`/examples/libp2p-in-the-browser`](./examples/libp2p-in-the-browser) A libp2p node running in the browser
41+
- [`/examples/peer-and-content-routing`](./examples/peer-and-content-routing) How to use peer and content routing
42+
- [`/examples/pnet`](./examples/pnet) How to configure a libp2p private network
43+
- [`/examples/protocol-and-stream-muxing`](./examples/protocol-and-stream-muxing) How to use multiplex protocols streams
44+
- [`/examples/pubsub`](./examples/pubsub) An example using libp2p pubsub
45+
- [`/examples/transports`](./examples/transports) An example using different types of libp2p transport
46+
- [`/interop`](./interop) Multidimension Interop Test
3447
- [`/packages/crypto`](./packages/crypto) Crypto primitives for libp2p
3548
- [`/packages/interface`](./packages/interface) The interface implemented by a libp2p node
3649
- [`/packages/interface-compliance-tests`](./packages/interface-compliance-tests) Compliance tests for JS libp2p interfaces

examples/auto-relay/LICENSE

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This project is dual licensed under MIT and Apache-2.0.
2+
3+
MIT: https://www.opensource.org/licenses/mit
4+
Apache-2.0: https://www.apache.org/licenses/license-2.0

examples/auto-relay/LICENSE-APACHE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
2+
3+
http://www.apache.org/licenses/LICENSE-2.0
4+
5+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

examples/auto-relay/LICENSE-MIT

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
The MIT License (MIT)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

examples/auto-relay/README.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1-
# Auto relay
1+
# @libp2p/example-auto-relay <!-- omit in toc -->
22

3-
Auto Relay enables libp2p nodes to dynamically find and bind to relays on the network. Once binding (listening) is done, the node can and should advertise its addresses on the network, allowing any other node to dial it over its bound relay(s).
4-
While direct connections to nodes are preferable, it's not always possible to do so due to NATs or browser limitations.
3+
[![libp2p.io](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
4+
[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
5+
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
6+
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster)
7+
8+
> Shows how to configure relayed connections
9+
10+
## Table of contents <!-- omit in toc -->
11+
12+
- [0. Setup the example](#0-setup-the-example)
13+
- [1. Set up a relay node](#1-set-up-a-relay-node)
14+
- [2. Set up a listener node with Auto Relay Enabled](#2-set-up-a-listener-node-with-auto-relay-enabled)
15+
- [3. Set up a dialer node for testing connectivity](#3-set-up-a-dialer-node-for-testing-connectivity)
16+
- [4. What is next?](#4-what-is-next)
17+
- [License](#license)
18+
- [Contribution](#contribution)
519

620
## 0. Setup the example
721

@@ -173,3 +187,14 @@ As you can see from the output, the remote address of the established connection
173187
Before moving into production, there are a few things that you should take into account.
174188

175189
A relay node should not advertise its private address in a real world scenario, as the node would not be reachable by others. You should provide an array of public addresses in the libp2p `addresses.announce` option. If you are using websockets, bear in mind that due to browser’s security policies you cannot establish unencrypted connection from secure context. The simplest solution is to setup SSL with nginx and proxy to the node and setup a domain name for the certificate.
190+
191+
## License
192+
193+
Licensed under either of
194+
195+
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
196+
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)
197+
198+
## Contribution
199+
200+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

examples/auto-relay/dialer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { createLibp2p } from 'libp2p'
2-
import { webSockets } from '@libp2p/websockets'
1+
/* eslint-disable no-console */
2+
33
import { noise } from '@chainsafe/libp2p-noise'
4+
import { yamux } from '@chainsafe/libp2p-yamux'
45
import { mplex } from '@libp2p/mplex'
6+
import { webSockets } from '@libp2p/websockets'
57
import { multiaddr } from '@multiformats/multiaddr'
6-
import { yamux } from '@chainsafe/libp2p-yamux'
8+
import { createLibp2p } from 'libp2p'
79
import { circuitRelayTransport } from 'libp2p/circuit-relay'
810
import { identifyService } from 'libp2p/identify'
911

examples/auto-relay/listener.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import { createLibp2p } from 'libp2p'
2-
import { webSockets } from '@libp2p/websockets'
1+
/* eslint-disable no-console */
2+
33
import { noise } from '@chainsafe/libp2p-noise'
4+
import { yamux } from '@chainsafe/libp2p-yamux'
45
import { mplex } from '@libp2p/mplex'
6+
import { webSockets } from '@libp2p/websockets'
57
import { multiaddr } from '@multiformats/multiaddr'
6-
import { yamux } from '@chainsafe/libp2p-yamux'
8+
import { createLibp2p } from 'libp2p'
79
import { circuitRelayTransport } from 'libp2p/circuit-relay'
810
import { identifyService } from 'libp2p/identify'
911

examples/auto-relay/package.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "@libp2p/example-auto-relay",
3+
"version": "0.0.0",
4+
"description": "Shows how to configure relayed connections",
5+
"license": "Apache-2.0 OR MIT",
6+
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/examples/auto-relay#readme",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://github.com/libp2p/js-libp2p.git"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/libp2p/js-libp2p/issues"
13+
},
14+
"type": "module",
15+
"files": [
16+
"src",
17+
"dist",
18+
"!dist/test",
19+
"!**/*.tsbuildinfo"
20+
],
21+
"eslintConfig": {
22+
"extends": "ipfs",
23+
"parserOptions": {
24+
"sourceType": "module"
25+
}
26+
},
27+
"scripts": {
28+
"lint": "aegir lint",
29+
"test:example": "node test.js"
30+
},
31+
"dependencies": {
32+
"@chainsafe/libp2p-noise": "^12.0.1",
33+
"@chainsafe/libp2p-yamux": "^4.0.2",
34+
"@libp2p/mplex": "^8.0.0",
35+
"@libp2p/websockets": "^6.0.0",
36+
"@multiformats/multiaddr": "^12.1.3",
37+
"libp2p": "^0.45.0"
38+
},
39+
"devDependencies": {
40+
"aegir": "^39.0.10",
41+
"execa": "^7.1.1",
42+
"p-defer": "^4.0.0",
43+
"uint8arrays": "^4.0.4"
44+
},
45+
"private": true
46+
}

examples/auto-relay/relay.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { createLibp2p } from 'libp2p'
2-
import { webSockets } from '@libp2p/websockets'
1+
/* eslint-disable no-console */
2+
33
import { noise } from '@chainsafe/libp2p-noise'
4-
import { mplex } from '@libp2p/mplex'
54
import { yamux } from '@chainsafe/libp2p-yamux'
5+
import { mplex } from '@libp2p/mplex'
6+
import { webSockets } from '@libp2p/websockets'
7+
import { createLibp2p } from 'libp2p'
68
import { circuitRelayServer } from 'libp2p/circuit-relay'
79
import { identifyService } from 'libp2p/identify'
810

@@ -20,7 +22,7 @@ async function main () {
2022
noise()
2123
],
2224
streamMuxers: [
23-
yamux(),mplex()
25+
yamux(), mplex()
2426
],
2527
services: {
2628
identify: identifyService(),

examples/auto-relay/test.js

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import path from 'path'
2+
import { fileURLToPath } from 'url'
23
import { execa } from 'execa'
34
import pDefer from 'p-defer'
45
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
5-
import { fileURLToPath } from 'url'
66

77
const __dirname = path.dirname(fileURLToPath(import.meta.url))
88

@@ -13,81 +13,79 @@ function startProcess (name, args = []) {
1313
})
1414
}
1515

16-
export async function test () {
17-
let output1 = ''
18-
let output2 = ''
19-
let output3 = ''
20-
let relayAddr
21-
let autoRelayAddr
16+
let output1 = ''
17+
let output2 = ''
18+
let output3 = ''
19+
let relayAddr
20+
let autoRelayAddr
2221

23-
const proc1Ready = pDefer()
24-
const proc2Ready = pDefer()
22+
const proc1Ready = pDefer()
23+
const proc2Ready = pDefer()
2524

26-
// Step 1 process
27-
process.stdout.write('relay.js\n')
25+
// Step 1 process
26+
process.stdout.write('relay.js\n')
2827

29-
const proc1 = startProcess('relay.js')
30-
proc1.all.on('data', async (data) => {
31-
process.stdout.write(data)
28+
const proc1 = startProcess('relay.js')
29+
proc1.all.on('data', async (data) => {
30+
process.stdout.write(data)
3231

33-
output1 += uint8ArrayToString(data)
32+
output1 += uint8ArrayToString(data)
3433

35-
if (output1.includes('Listening on:') && output1.includes('/p2p/')) {
36-
relayAddr = output1.trim().split('Listening on:\n')[1].split('\n')[0]
37-
proc1Ready.resolve()
38-
}
39-
})
34+
if (output1.includes('Listening on:') && output1.includes('/p2p/')) {
35+
relayAddr = output1.trim().split('Listening on:\n')[1].split('\n')[0]
36+
proc1Ready.resolve()
37+
}
38+
})
4039

41-
await proc1Ready.promise
42-
process.stdout.write('==================================================================\n')
40+
await proc1Ready.promise
41+
process.stdout.write('==================================================================\n')
4342

44-
// Step 2 process
45-
process.stdout.write('listener.js\n')
43+
// Step 2 process
44+
process.stdout.write('listener.js\n')
4645

47-
const proc2 = startProcess('listener.js', [relayAddr])
48-
proc2.all.on('data', async (data) => {
49-
process.stdout.write(data)
46+
const proc2 = startProcess('listener.js', [relayAddr])
47+
proc2.all.on('data', async (data) => {
48+
process.stdout.write(data)
5049

51-
output2 += uint8ArrayToString(data)
50+
output2 += uint8ArrayToString(data)
5251

53-
if (output2.includes('Advertising with a relay address of') && output2.includes('/p2p/')) {
54-
autoRelayAddr = output2.trim().split('Advertising with a relay address of ')[1].trim()
55-
proc2Ready.resolve()
56-
}
57-
})
52+
if (output2.includes('Advertising with a relay address of') && output2.includes('/p2p/')) {
53+
autoRelayAddr = output2.trim().split('Advertising with a relay address of ')[1].trim()
54+
proc2Ready.resolve()
55+
}
56+
})
5857

59-
await proc2Ready.promise
60-
process.stdout.write('==================================================================\n')
58+
await proc2Ready.promise
59+
process.stdout.write('==================================================================\n')
6160

62-
// Step 3 process
63-
process.stdout.write('dialer.js\n')
61+
// Step 3 process
62+
process.stdout.write('dialer.js\n')
6463

65-
const proc3 = startProcess('dialer.js', [autoRelayAddr])
66-
proc3.all.on('data', async (data) => {
67-
process.stdout.write(data)
64+
const proc3 = startProcess('dialer.js', [autoRelayAddr])
65+
proc3.all.on('data', async (data) => {
66+
process.stdout.write(data)
6867

69-
output3 += uint8ArrayToString(data)
68+
output3 += uint8ArrayToString(data)
7069

71-
if (output3.includes('Connected to the auto relay node via')) {
72-
const remoteAddr = output3.trim().split('Connected to the auto relay node via ')[1]
70+
if (output3.includes('Connected to the auto relay node via')) {
71+
const remoteAddr = output3.trim().split('Connected to the auto relay node via ')[1]
7372

74-
if (remoteAddr === autoRelayAddr) {
75-
proc3.kill()
76-
proc2.kill()
77-
proc1.kill()
78-
} else {
79-
throw new Error('dialer did not dial through the relay')
80-
}
73+
if (remoteAddr === autoRelayAddr) {
74+
proc3.kill()
75+
proc2.kill()
76+
proc1.kill()
77+
} else {
78+
throw new Error('dialer did not dial through the relay')
8179
}
82-
})
83-
84-
await Promise.all([
85-
proc1,
86-
proc2,
87-
proc3
88-
]).catch((err) => {
89-
if (err.signal !== 'SIGTERM') {
90-
throw err
91-
}
92-
})
93-
}
80+
}
81+
})
82+
83+
await Promise.all([
84+
proc1,
85+
proc2,
86+
proc3
87+
]).catch((err) => {
88+
if (err.signal !== 'SIGTERM') {
89+
throw err
90+
}
91+
})

0 commit comments

Comments
 (0)