mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-25 15:01:34 +00:00
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:
@ -41,7 +41,8 @@ const DefaultConfig = {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
}
|
||||
},
|
||||
transport: {}
|
||||
}
|
||||
}
|
||||
|
||||
|
10
src/index.js
10
src/index.js
@ -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) {
|
||||
|
@ -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)
|
||||
@ -38,6 +39,7 @@ class TransportManager {
|
||||
}
|
||||
|
||||
const transport = new Transport({
|
||||
...transportOptions,
|
||||
libp2p: this.libp2p,
|
||||
upgrader: this.upgrader
|
||||
})
|
||||
|
Reference in New Issue
Block a user