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

* feat: allow transport options to be passed on creation

* fix: only add circuit transport if enabled

* chore: fix lint
This commit is contained in:
Jacob Heun 2019-12-20 08:32:46 -08:00
parent 7b326cc525
commit c339be10ee
5 changed files with 70 additions and 5 deletions

View File

@ -23,6 +23,7 @@
- [Configuring Dialing](#configuring-dialing) - [Configuring Dialing](#configuring-dialing)
- [Configuring Connection Manager](#configuring-connection-manager) - [Configuring Connection Manager](#configuring-connection-manager)
- [Configuring Metrics](#configuring-metrics) - [Configuring Metrics](#configuring-metrics)
- [Customizing Transports](#customizing-transports)
- [Configuration examples](#configuration-examples) - [Configuration examples](#configuration-examples)
## Overview ## Overview
@ -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 ## 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: 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:

View File

@ -41,7 +41,8 @@ const DefaultConfig = {
enabled: false, enabled: false,
active: false active: false
} }
} },
transport: {}
} }
} }

View File

@ -114,10 +114,14 @@ class Libp2p extends EventEmitter {
}) })
this._modules.transport.forEach((Transport) => { 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 // Attach stream multiplexers
if (this._modules.streamMuxer) { if (this._modules.streamMuxer) {

View File

@ -26,9 +26,10 @@ class TransportManager {
* *
* @param {String} key * @param {String} key
* @param {Transport} Transport * @param {Transport} Transport
* @param {*} transportOptions Additional options to pass to the transport
* @returns {void} * @returns {void}
*/ */
add (key, Transport) { add (key, Transport, transportOptions = {}) {
log('adding %s', key) log('adding %s', key)
if (!key) { if (!key) {
throw errCode(new Error(`Transport must have a valid key, was given '${key}'`), codes.ERR_INVALID_KEY) throw errCode(new Error(`Transport must have a valid key, was given '${key}'`), codes.ERR_INVALID_KEY)
@ -38,6 +39,7 @@ class TransportManager {
} }
const transport = new Transport({ const transport = new Transport({
...transportOptions,
libp2p: this.libp2p, libp2p: this.libp2p,
upgrader: this.upgrader upgrader: this.upgrader
}) })

View File

@ -115,6 +115,35 @@ describe('libp2p.transportManager', () => {
expect(libp2p.transportManager._transports.size).to.equal(2) 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 () => { it('starting and stopping libp2p should start and stop TransportManager', async () => {
libp2p = new Libp2p({ libp2p = new Libp2p({
peerInfo, peerInfo,