Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow transport options to be passed on creation #524

Merged
merged 3 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions doc/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [Configuring Dialing](#configuring-dialing)
- [Configuring Connection Manager](#configuring-connection-manager)
- [Configuring Metrics](#configuring-metrics)
- [Customizing Transports](#customizing-transports)
- [Configuration examples](#configuration-examples)

## Overview
Expand Down Expand Up @@ -499,6 +500,34 @@ const node = await Libp2p.create({
})
```

#### Customizing Transports

Some Transports can be passed additional options when they are created. For example, `libp2p-webrtc-star` accepts an optional, custom `wrtc` implementation. In addition to libp2p passing itself and an `Upgrader` to handle connection upgrading, libp2p will also pass the options, if they are provided, from `config.transport`.

```js
const Libp2p = require('libp2p')
const WebRTCStar = require('libp2p-webrtc-star')
const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const wrtc = require('wrtc')

const transportKey = WebRTCStar.prototype[Symbol.toStringTag]
const node = await Libp2p.create({
modules: {
transport: [WebRTCStar],
streamMuxer: [MPLEX],
connEncryption: [SECIO]
},
config: {
transport: {
[transportKey]: {
wrtc // You can use `wrtc` when running in Node.js
}
}
}
})
```

## Configuration examples

As libp2p is designed to be a modular networking library, its usage will vary based on individual project needs. We've included links to some existing project configurations for your reference, in case you wish to replicate their configuration:
Expand Down
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const DefaultConfig = {
enabled: false,
active: false
}
}
},
transport: {}
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,14 @@ class Libp2p extends EventEmitter {
})

this._modules.transport.forEach((Transport) => {
this.transportManager.add(Transport.prototype[Symbol.toStringTag], Transport)
const key = Transport.prototype[Symbol.toStringTag]
const transportOptions = this._config.transport[key]
this.transportManager.add(key, Transport, transportOptions)
})
// TODO: enable relay if enabled
this.transportManager.add(Circuit.prototype[Symbol.toStringTag], Circuit)

if (this._config.relay.enabled) {
this.transportManager.add(Circuit.prototype[Symbol.toStringTag], Circuit)
}

// Attach stream multiplexers
if (this._modules.streamMuxer) {
Expand Down
4 changes: 3 additions & 1 deletion src/transport-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class TransportManager {
*
* @param {String} key
* @param {Transport} Transport
* @param {*} transportOptions Additional options to pass to the transport
* @returns {void}
*/
add (key, Transport) {
add (key, Transport, transportOptions = {}) {
log('adding %s', key)
if (!key) {
throw errCode(new Error(`Transport must have a valid key, was given '${key}'`), codes.ERR_INVALID_KEY)
Expand All @@ -38,6 +39,7 @@ class TransportManager {
}

const transport = new Transport({
...transportOptions,
libp2p: this.libp2p,
upgrader: this.upgrader
})
Expand Down
29 changes: 29 additions & 0 deletions test/transports/transport-manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@ describe('libp2p.transportManager', () => {
expect(libp2p.transportManager._transports.size).to.equal(2)
})

it('should be able to customize a transport', () => {
const spy = sinon.spy()
const key = spy.prototype[Symbol.toStringTag] = 'TransportSpy'
const customOptions = {
another: 'value'
}
libp2p = new Libp2p({
peerInfo,
modules: {
transport: [spy]
},
config: {
transport: {
[key]: customOptions
}
}
})

expect(libp2p.transportManager).to.exist()
// Our transport and circuit relay
expect(libp2p.transportManager._transports.size).to.equal(2)
expect(spy).to.have.property('callCount', 1)
expect(spy.getCall(0)).to.have.deep.property('args', [{
...customOptions,
libp2p,
upgrader: libp2p.upgrader
}])
})

it('starting and stopping libp2p should start and stop TransportManager', async () => {
libp2p = new Libp2p({
peerInfo,
Expand Down