Skip to content

Commit c071ddf

Browse files
saul-jbachingbrain
authored andcommitted
feat: add support for arbitrary service modules
Updates the libp2p init args to accept an object of service factory functions that can use internal libp2p components. The returned libp2p object has a `.services` key that corresponds to the service factory keys.
1 parent ba47c95 commit c071ddf

Some content is hidden

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

71 files changed

+1935
-1913
lines changed

.aegir.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export default {
1717
const { plaintext } = await import('./dist/src/insecure/index.js')
1818
const { default: Peers } = await import('./dist/test/fixtures/peers.js')
1919
const { circuitRelayServer, circuitRelayTransport } = await import('./dist/src/circuit-relay/index.js')
20+
const { identifyService } = await import('./dist/src/identify/index.js')
21+
const { pingService } = await import('./dist/src/ping/index.js')
22+
const { fetchService } = await import('./dist/src/fetch/index.js')
2023

2124
// Use the last peer
2225
const peerId = await createFromJSON(Peers[Peers.length - 1])
@@ -39,9 +42,15 @@ export default {
3942
noise(),
4043
plaintext()
4144
],
42-
relay: circuitRelayServer(),
43-
nat: {
44-
enabled: false
45+
services: {
46+
identify: identifyService(),
47+
ping: pingService(),
48+
fetch: fetchService(),
49+
relay: circuitRelayServer({
50+
reservations: {
51+
maxReservations: Infinity
52+
}
53+
})
4554
}
4655
})
4756
// Add the echo protocol

doc/LIMITS.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ We can also limit the number of connections in a "pending" state. These connecti
2929
All fields are optional. The default values are defined in [src/connection-manager/index.ts](https://github.com/libp2p/js-libp2p/blob/master/src/connection-manager/index.ts) - please see that file for the current values.
3030

3131
```ts
32-
const node = await createLibp2pNode({
32+
const node = await createLibp2p({
3333
connectionManager: {
3434
/**
3535
* The total number of connections allowed to be open at one time
@@ -69,7 +69,7 @@ To prevent individual peers from opening multiple connections to a node, an `inb
6969
All fields are optional. The default values are defined in [src/connection-manager/index.ts](https://github.com/libp2p/js-libp2p/blob/master/src/connection-manager/index.ts) - please see that file for the current values.
7070

7171
```ts
72-
const node = await createLibp2pNode({
72+
const node = await createLibp2p({
7373
connectionManager: {
7474
/**
7575
* A remote peer may attempt to open up to this many connections per second,
@@ -93,7 +93,7 @@ These settings are done on a per-muxer basis, please see the README of the relev
9393
All fields are optional. The default values are defined in [@libp2p/mplex/src/mplex.ts](https://github.com/libp2p/js-libp2p-mplex/blob/master/src/mplex.ts) - please see that file for the current values.
9494

9595
```ts
96-
const node = await createLibp2pNode({
96+
const node = await createLibp2p({
9797
muxers: [
9898
mplex({
9999
/**
@@ -133,7 +133,7 @@ const node = await createLibp2pNode({
133133
All fields are optional. The default values are defined in [@chainsafe/libp2p-yamux/src/config.ts](https://github.com/ChainSafe/js-libp2p-yamux/blob/master/src/config.ts) - please see that file for the current values.
134134

135135
```ts
136-
const node = await createLibp2pNode({
136+
const node = await createLibp2p({
137137
muxers: [
138138
yamux({
139139
/**
@@ -186,7 +186,7 @@ The [@libp2p/tcp](https://github.com/libp2p/js-libp2p-tcp) transport allows addi
186186
All fields are optional. The full list of options is defined in [@libp2p/tcp/src/index.ts](https://github.com/libp2p/js-libp2p-tcp/blob/master/src/index.ts) - please see that file for more details.
187187

188188
```ts
189-
const node = await createLibp2pNode({
189+
const node = await createLibp2p({
190190
transports: [
191191
tcp({
192192
/**
@@ -215,7 +215,7 @@ const node = await createLibp2pNode({
215215
It is possible to configure some hosts to always accept connections from and some to always reject connections from.
216216

217217
```js
218-
const node = await createLibp2pNode({
218+
const node = await createLibp2p({
219219
connectionManager: {
220220
/**
221221
* A list of multiaddrs, any connection with a `remoteAddress` property

doc/METRICS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ Although designed to primarily integrate with tools such as [Prometheus](https:/
3232
First enable metrics tracking by supplying a [Metrics](https://www.npmjs.com/package/@libp2p/interface-metrics) implementation:
3333

3434
```js
35-
import { createLibp2pNode } from 'libp2p'
35+
import { createLibp2p } from 'libp2p'
3636
import { prometheusMetrics } from '@libp2p/prometheus-metrics'
3737

38-
const node = await createLibp2pNode({
38+
const node = await createLibp2p({
3939
metrics: prometheusMetrics()
4040
//... other config
4141
})
@@ -164,7 +164,7 @@ Metrics implementations will allow extracting the values for presentation in an
164164
import { prometheusMetrics } from '@libp2p/prometheus-metrics'
165165
import client from 'prom-client'
166166

167-
const libp2p = createLibp2pNode({
167+
const libp2p = createLibp2p({
168168
metrics: prometheusMetrics()
169169
//... other config
170170
})

examples/auto-relay/dialer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { noise } from '@chainsafe/libp2p-noise'
44
import { mplex } from '@libp2p/mplex'
55
import { multiaddr } from '@multiformats/multiaddr'
66
import { circuitRelayTransport } from 'libp2p/circuit-relay'
7+
import { identifyService } from 'libp2p/identify'
78

89
async function main () {
910
const autoRelayNodeAddr = process.argv[2]
@@ -21,7 +22,10 @@ async function main () {
2122
],
2223
streamMuxers: [
2324
mplex()
24-
]
25+
],
26+
services: {
27+
identify: identifyService()
28+
}
2529
})
2630

2731
console.log(`Node started with id ${node.peerId.toString()}`)

examples/auto-relay/listener.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { noise } from '@chainsafe/libp2p-noise'
44
import { mplex } from '@libp2p/mplex'
55
import { multiaddr } from '@multiformats/multiaddr'
66
import { circuitRelayTransport } from 'libp2p/circuit-relay'
7+
import { identifyService } from 'libp2p/identify'
78

89
async function main () {
910
const relayAddr = process.argv[2]
@@ -23,7 +24,10 @@ async function main () {
2324
],
2425
streamMuxers: [
2526
mplex()
26-
]
27+
],
28+
services: {
29+
identify: identifyService()
30+
}
2731
})
2832

2933
console.log(`Node started with id ${node.peerId.toString()}`)

examples/auto-relay/relay.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { webSockets } from '@libp2p/websockets'
33
import { noise } from '@chainsafe/libp2p-noise'
44
import { mplex } from '@libp2p/mplex'
55
import { circuitRelayServer } from 'libp2p/circuit-relay'
6+
import { identifyService } from 'libp2p/identify'
67

78
async function main () {
89
const node = await createLibp2p({
@@ -20,7 +21,10 @@ async function main () {
2021
streamMuxers: [
2122
mplex()
2223
],
23-
relay: circuitRelayServer()
24+
services: {
25+
identify: identifyService(),
26+
relay: circuitRelayServer()
27+
}
2428
})
2529

2630
console.log(`Node started with id ${node.peerId.toString()}`)

examples/chat/src/listener.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ async function run () {
1717

1818
// Log a message when a remote peer connects to us
1919
nodeListener.addEventListener('peer:connect', (evt) => {
20-
const connection = evt.detail
21-
console.log('connected to: ', connection.remotePeer.toString())
20+
const remotePeer = evt.detail
21+
console.log('connected to: ', remotePeer.toString())
2222
})
2323

2424
// Handle messages for the protocol

examples/delegated-routing/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"libp2p": "file:../../",
1010
"@libp2p/delegated-content-routing": "^4.0.0",
1111
"@libp2p/delegated-peer-routing": "^4.0.0",
12-
"@libp2p/kad-dht": "^9.0.0",
12+
"@libp2p/kad-dht": "^9.1.0",
1313
"@libp2p/mplex": "^8.0.1",
1414
"@libp2p/webrtc-star": "^7.0.0",
1515
"@libp2p/websockets": "^6.0.1",

examples/discovery-mechanisms/1.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import bootstrappers from './bootstrappers.js'
2828
})
2929

3030
node.addEventListener('peer:discovery', (evt) => {
31-
const peerId = evt.detail
31+
const peerInfo = evt.detail
3232

33-
console.log('Discovered:', peerId.toString())
33+
console.log('Discovered:', peerInfo.id.toString())
3434
})
3535
})();

examples/discovery-mechanisms/3.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { floodsub } from '@libp2p/floodsub'
88
import { bootstrap } from '@libp2p/bootstrap'
99
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'
1010
import { circuitRelayTransport, circuitRelayServer } from 'libp2p/circuit-relay'
11+
import { identifyService } from 'libp2p/identify'
1112

1213
const createNode = async (bootstrappers) => {
1314
const node = await createLibp2p({
@@ -17,15 +18,18 @@ const createNode = async (bootstrappers) => {
1718
transports: [tcp()],
1819
streamMuxers: [mplex()],
1920
connectionEncryption: [noise()],
20-
pubsub: floodsub(),
2121
peerDiscovery: [
2222
bootstrap({
2323
list: bootstrappers
2424
}),
2525
pubsubPeerDiscovery({
2626
interval: 1000
2727
})
28-
]
28+
],
29+
services: {
30+
pubsub: floodsub(),
31+
identify: identifyService()
32+
}
2933
})
3034

3135
return node
@@ -41,13 +45,16 @@ const createNode = async (bootstrappers) => {
4145
transports: [tcp(), circuitRelayTransport()],
4246
streamMuxers: [mplex()],
4347
connectionEncryption: [noise()],
44-
pubsub: floodsub(),
4548
peerDiscovery: [
4649
pubsubPeerDiscovery({
4750
interval: 1000
4851
})
4952
],
50-
relay: circuitRelayServer()
53+
services: {
54+
relay: circuitRelayServer(),
55+
identify: identifyService(),
56+
pubsub: floodsub()
57+
}
5158
})
5259
console.log(`libp2p relay started with id: ${relay.peerId.toString()}`)
5360

0 commit comments

Comments
 (0)