Skip to content

Commit

Permalink
feat: add support for arbitrary service modules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
saul-jb authored and achingbrain committed Apr 27, 2023
1 parent ba47c95 commit c071ddf
Show file tree
Hide file tree
Showing 71 changed files with 1,935 additions and 1,913 deletions.
15 changes: 12 additions & 3 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default {
const { plaintext } = await import('./dist/src/insecure/index.js')
const { default: Peers } = await import('./dist/test/fixtures/peers.js')
const { circuitRelayServer, circuitRelayTransport } = await import('./dist/src/circuit-relay/index.js')
const { identifyService } = await import('./dist/src/identify/index.js')
const { pingService } = await import('./dist/src/ping/index.js')
const { fetchService } = await import('./dist/src/fetch/index.js')

// Use the last peer
const peerId = await createFromJSON(Peers[Peers.length - 1])
Expand All @@ -39,9 +42,15 @@ export default {
noise(),
plaintext()
],
relay: circuitRelayServer(),
nat: {
enabled: false
services: {
identify: identifyService(),
ping: pingService(),
fetch: fetchService(),
relay: circuitRelayServer({
reservations: {
maxReservations: Infinity
}
})
}
})
// Add the echo protocol
Expand Down
12 changes: 6 additions & 6 deletions doc/LIMITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ We can also limit the number of connections in a "pending" state. These connecti
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.

```ts
const node = await createLibp2pNode({
const node = await createLibp2p({
connectionManager: {
/**
* The total number of connections allowed to be open at one time
Expand Down Expand Up @@ -69,7 +69,7 @@ To prevent individual peers from opening multiple connections to a node, an `inb
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.

```ts
const node = await createLibp2pNode({
const node = await createLibp2p({
connectionManager: {
/**
* A remote peer may attempt to open up to this many connections per second,
Expand All @@ -93,7 +93,7 @@ These settings are done on a per-muxer basis, please see the README of the relev
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.

```ts
const node = await createLibp2pNode({
const node = await createLibp2p({
muxers: [
mplex({
/**
Expand Down Expand Up @@ -133,7 +133,7 @@ const node = await createLibp2pNode({
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.

```ts
const node = await createLibp2pNode({
const node = await createLibp2p({
muxers: [
yamux({
/**
Expand Down Expand Up @@ -186,7 +186,7 @@ The [@libp2p/tcp](https://github.com/libp2p/js-libp2p-tcp) transport allows addi
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.

```ts
const node = await createLibp2pNode({
const node = await createLibp2p({
transports: [
tcp({
/**
Expand Down Expand Up @@ -215,7 +215,7 @@ const node = await createLibp2pNode({
It is possible to configure some hosts to always accept connections from and some to always reject connections from.

```js
const node = await createLibp2pNode({
const node = await createLibp2p({
connectionManager: {
/**
* A list of multiaddrs, any connection with a `remoteAddress` property
Expand Down
6 changes: 3 additions & 3 deletions doc/METRICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ Although designed to primarily integrate with tools such as [Prometheus](https:/
First enable metrics tracking by supplying a [Metrics](https://www.npmjs.com/package/@libp2p/interface-metrics) implementation:

```js
import { createLibp2pNode } from 'libp2p'
import { createLibp2p } from 'libp2p'
import { prometheusMetrics } from '@libp2p/prometheus-metrics'

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

const libp2p = createLibp2pNode({
const libp2p = createLibp2p({
metrics: prometheusMetrics()
//... other config
})
Expand Down
6 changes: 5 additions & 1 deletion examples/auto-relay/dialer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { noise } from '@chainsafe/libp2p-noise'
import { mplex } from '@libp2p/mplex'
import { multiaddr } from '@multiformats/multiaddr'
import { circuitRelayTransport } from 'libp2p/circuit-relay'
import { identifyService } from 'libp2p/identify'

async function main () {
const autoRelayNodeAddr = process.argv[2]
Expand All @@ -21,7 +22,10 @@ async function main () {
],
streamMuxers: [
mplex()
]
],
services: {
identify: identifyService()
}
})

console.log(`Node started with id ${node.peerId.toString()}`)
Expand Down
6 changes: 5 additions & 1 deletion examples/auto-relay/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { noise } from '@chainsafe/libp2p-noise'
import { mplex } from '@libp2p/mplex'
import { multiaddr } from '@multiformats/multiaddr'
import { circuitRelayTransport } from 'libp2p/circuit-relay'
import { identifyService } from 'libp2p/identify'

async function main () {
const relayAddr = process.argv[2]
Expand All @@ -23,7 +24,10 @@ async function main () {
],
streamMuxers: [
mplex()
]
],
services: {
identify: identifyService()
}
})

console.log(`Node started with id ${node.peerId.toString()}`)
Expand Down
6 changes: 5 additions & 1 deletion examples/auto-relay/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { webSockets } from '@libp2p/websockets'
import { noise } from '@chainsafe/libp2p-noise'
import { mplex } from '@libp2p/mplex'
import { circuitRelayServer } from 'libp2p/circuit-relay'
import { identifyService } from 'libp2p/identify'

async function main () {
const node = await createLibp2p({
Expand All @@ -20,7 +21,10 @@ async function main () {
streamMuxers: [
mplex()
],
relay: circuitRelayServer()
services: {
identify: identifyService(),
relay: circuitRelayServer()
}
})

console.log(`Node started with id ${node.peerId.toString()}`)
Expand Down
4 changes: 2 additions & 2 deletions examples/chat/src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ async function run () {

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

// Handle messages for the protocol
Expand Down
2 changes: 1 addition & 1 deletion examples/delegated-routing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"libp2p": "file:../../",
"@libp2p/delegated-content-routing": "^4.0.0",
"@libp2p/delegated-peer-routing": "^4.0.0",
"@libp2p/kad-dht": "^9.0.0",
"@libp2p/kad-dht": "^9.1.0",
"@libp2p/mplex": "^8.0.1",
"@libp2p/webrtc-star": "^7.0.0",
"@libp2p/websockets": "^6.0.1",
Expand Down
4 changes: 2 additions & 2 deletions examples/discovery-mechanisms/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import bootstrappers from './bootstrappers.js'
})

node.addEventListener('peer:discovery', (evt) => {
const peerId = evt.detail
const peerInfo = evt.detail

console.log('Discovered:', peerId.toString())
console.log('Discovered:', peerInfo.id.toString())
})
})();
15 changes: 11 additions & 4 deletions examples/discovery-mechanisms/3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { floodsub } from '@libp2p/floodsub'
import { bootstrap } from '@libp2p/bootstrap'
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'
import { circuitRelayTransport, circuitRelayServer } from 'libp2p/circuit-relay'
import { identifyService } from 'libp2p/identify'

const createNode = async (bootstrappers) => {
const node = await createLibp2p({
Expand All @@ -17,15 +18,18 @@ const createNode = async (bootstrappers) => {
transports: [tcp()],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
pubsub: floodsub(),
peerDiscovery: [
bootstrap({
list: bootstrappers
}),
pubsubPeerDiscovery({
interval: 1000
})
]
],
services: {
pubsub: floodsub(),
identify: identifyService()
}
})

return node
Expand All @@ -41,13 +45,16 @@ const createNode = async (bootstrappers) => {
transports: [tcp(), circuitRelayTransport()],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
pubsub: floodsub(),
peerDiscovery: [
pubsubPeerDiscovery({
interval: 1000
})
],
relay: circuitRelayServer()
services: {
relay: circuitRelayServer(),
identify: identifyService(),
pubsub: floodsub()
}
})
console.log(`libp2p relay started with id: ${relay.peerId.toString()}`)

Expand Down
4 changes: 2 additions & 2 deletions examples/echo/src/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ async function run() {

// Log a message when we receive a connection
listenerNode.addEventListener('peer:connect', (evt) => {
const connection = evt.detail
console.log('received dial to me from:', connection.remotePeer.toString())
const remotePeer = evt.detail
console.log('received dial to me from:', remotePeer.toString())
})

// Handle incoming connections for the protocol by piping from the stream
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "MIT",
"dependencies": {
"@libp2p/floodsub": "^7.0.1",
"@libp2p/pubsub-peer-discovery": "^8.0.0",
"@libp2p/pubsub-peer-discovery": "^8.0.4",
"@nodeutils/defaults-deep": "^1.1.0",
"execa": "^6.1.0",
"fs-extra": "^10.1.0",
Expand Down
6 changes: 5 additions & 1 deletion examples/peer-and-content-routing/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */

import { createLibp2p } from 'libp2p'
import { identifyService } from 'libp2p/identify'
import { tcp } from '@libp2p/tcp'
import { mplex } from '@libp2p/mplex'
import { noise } from '@chainsafe/libp2p-noise'
Expand All @@ -15,7 +16,10 @@ const createNode = async () => {
transports: [tcp()],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
dht: kadDHT()
services: {
dht: kadDHT(),
identify: identifyService()
}
})

return node
Expand Down
6 changes: 5 additions & 1 deletion examples/peer-and-content-routing/2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */

import { createLibp2p } from 'libp2p'
import { identifyService } from 'libp2p/identify'
import { tcp } from '@libp2p/tcp'
import { mplex } from '@libp2p/mplex'
import { noise } from '@chainsafe/libp2p-noise'
Expand All @@ -17,7 +18,10 @@ const createNode = async () => {
transports: [tcp()],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
dht: kadDHT()
services: {
dht: kadDHT(),
identify: identifyService()
}
})

return node
Expand Down
16 changes: 10 additions & 6 deletions examples/pubsub/1.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */

import { createLibp2p } from 'libp2p'
import { identifyService } from 'libp2p/identify'
import { tcp } from '@libp2p/tcp'
import { mplex } from '@libp2p/mplex'
import { noise } from '@chainsafe/libp2p-noise'
Expand All @@ -16,7 +17,10 @@ const createNode = async () => {
transports: [tcp()],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
pubsub: floodsub()
services: {
pubsub: floodsub(),
identify: identifyService()
}
})

return node
Expand All @@ -36,20 +40,20 @@ const createNode = async () => {
})
await node1.dial(node2.peerId)

node1.pubsub.subscribe(topic)
node1.pubsub.addEventListener('message', (evt) => {
node1.services.pubsub.subscribe(topic)
node1.services.pubsub.addEventListener('message', (evt) => {
console.log(`node1 received: ${uint8ArrayToString(evt.detail.data)} on topic ${evt.detail.topic}`)
})

// Will not receive own published messages by default
node2.pubsub.subscribe(topic)
node2.pubsub.addEventListener('message', (evt) => {
node2.services.pubsub.subscribe(topic)
node2.services.pubsub.addEventListener('message', (evt) => {
console.log(`node2 received: ${uint8ArrayToString(evt.detail.data)} on topic ${evt.detail.topic}`)
})

// node2 publishes "news" every second
setInterval(() => {
node2.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!')).catch(err => {
node2.services.pubsub.publish(topic, uint8ArrayFromString('Bird bird bird, bird is the word!')).catch(err => {
console.error(err)
})
}, 1000)
Expand Down
Loading

0 comments on commit c071ddf

Please sign in to comment.