Skip to content

Commit c339be1

Browse files
committed
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
1 parent 7b326cc commit c339be1

File tree

5 files changed

+70
-5
lines changed

5 files changed

+70
-5
lines changed

doc/CONFIGURATION.md

+29
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- [Configuring Dialing](#configuring-dialing)
2424
- [Configuring Connection Manager](#configuring-connection-manager)
2525
- [Configuring Metrics](#configuring-metrics)
26+
- [Customizing Transports](#customizing-transports)
2627
- [Configuration examples](#configuration-examples)
2728

2829
## Overview
@@ -499,6 +500,34 @@ const node = await Libp2p.create({
499500
})
500501
```
501502
503+
#### Customizing Transports
504+
505+
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`.
506+
507+
```js
508+
const Libp2p = require('libp2p')
509+
const WebRTCStar = require('libp2p-webrtc-star')
510+
const MPLEX = require('libp2p-mplex')
511+
const SECIO = require('libp2p-secio')
512+
const wrtc = require('wrtc')
513+
514+
const transportKey = WebRTCStar.prototype[Symbol.toStringTag]
515+
const node = await Libp2p.create({
516+
modules: {
517+
transport: [WebRTCStar],
518+
streamMuxer: [MPLEX],
519+
connEncryption: [SECIO]
520+
},
521+
config: {
522+
transport: {
523+
[transportKey]: {
524+
wrtc // You can use `wrtc` when running in Node.js
525+
}
526+
}
527+
}
528+
})
529+
```
530+
502531
## Configuration examples
503532
504533
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:

src/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const DefaultConfig = {
4141
enabled: false,
4242
active: false
4343
}
44-
}
44+
},
45+
transport: {}
4546
}
4647
}
4748

src/index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,14 @@ class Libp2p extends EventEmitter {
114114
})
115115

116116
this._modules.transport.forEach((Transport) => {
117-
this.transportManager.add(Transport.prototype[Symbol.toStringTag], Transport)
117+
const key = Transport.prototype[Symbol.toStringTag]
118+
const transportOptions = this._config.transport[key]
119+
this.transportManager.add(key, Transport, transportOptions)
118120
})
119-
// TODO: enable relay if enabled
120-
this.transportManager.add(Circuit.prototype[Symbol.toStringTag], Circuit)
121+
122+
if (this._config.relay.enabled) {
123+
this.transportManager.add(Circuit.prototype[Symbol.toStringTag], Circuit)
124+
}
121125

122126
// Attach stream multiplexers
123127
if (this._modules.streamMuxer) {

src/transport-manager.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ class TransportManager {
2626
*
2727
* @param {String} key
2828
* @param {Transport} Transport
29+
* @param {*} transportOptions Additional options to pass to the transport
2930
* @returns {void}
3031
*/
31-
add (key, Transport) {
32+
add (key, Transport, transportOptions = {}) {
3233
log('adding %s', key)
3334
if (!key) {
3435
throw errCode(new Error(`Transport must have a valid key, was given '${key}'`), codes.ERR_INVALID_KEY)
@@ -38,6 +39,7 @@ class TransportManager {
3839
}
3940

4041
const transport = new Transport({
42+
...transportOptions,
4143
libp2p: this.libp2p,
4244
upgrader: this.upgrader
4345
})

test/transports/transport-manager.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,35 @@ describe('libp2p.transportManager', () => {
115115
expect(libp2p.transportManager._transports.size).to.equal(2)
116116
})
117117

118+
it('should be able to customize a transport', () => {
119+
const spy = sinon.spy()
120+
const key = spy.prototype[Symbol.toStringTag] = 'TransportSpy'
121+
const customOptions = {
122+
another: 'value'
123+
}
124+
libp2p = new Libp2p({
125+
peerInfo,
126+
modules: {
127+
transport: [spy]
128+
},
129+
config: {
130+
transport: {
131+
[key]: customOptions
132+
}
133+
}
134+
})
135+
136+
expect(libp2p.transportManager).to.exist()
137+
// Our transport and circuit relay
138+
expect(libp2p.transportManager._transports.size).to.equal(2)
139+
expect(spy).to.have.property('callCount', 1)
140+
expect(spy.getCall(0)).to.have.deep.property('args', [{
141+
...customOptions,
142+
libp2p,
143+
upgrader: libp2p.upgrader
144+
}])
145+
})
146+
118147
it('starting and stopping libp2p should start and stop TransportManager', async () => {
119148
libp2p = new Libp2p({
120149
peerInfo,

0 commit comments

Comments
 (0)