mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-13 01:01:23 +00:00
refactor(async): update transports subsystem (#461)
* test: remove all tests for a clean slate The refactor will require a large number of updates to the tests. In order to ensure we have done a decent deduplication, and have a cleaner suite of tests we've removed all tests. This will also allow us to more easily see tests for the refactored systems. We have a record of the latest test suites in master, so we are not losing any history. * chore: update tcp and websockets * chore: remove other transports until they are converted * chore: use mafmt and multiaddr async versions * chore: add and fix dependencies * chore: clean up travis file * feat: add new transport manager * docs: add constructor jsdocs * refactor(config): check that transports exist This also removes the other logic, it can be added when those subsystems are refactored * chore(deps): use async peer-id and peer-info * feat: wire up the transport manager with libp2p * chore: remove superstruct dep
This commit is contained in:
@ -1,103 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const WS = require('libp2p-websockets')
|
||||
const WebRTCStar = require('libp2p-webrtc-star')
|
||||
const WebSocketStar = require('libp2p-websocket-star')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const PULLMPLEX = require('pull-mplex')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const GossipSub = require('libp2p-gossipsub')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../..')
|
||||
|
||||
function mapMuxers (list) {
|
||||
return list.map((pref) => {
|
||||
if (typeof pref !== 'string') { return pref }
|
||||
switch (pref.trim().toLowerCase()) {
|
||||
case 'spdy': return SPDY
|
||||
case 'mplex': return MPLEX
|
||||
case 'pullmplex': return PULLMPLEX
|
||||
default:
|
||||
throw new Error(pref + ' muxer not available')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function getMuxers (options) {
|
||||
if (options) {
|
||||
return mapMuxers(options)
|
||||
} else {
|
||||
return [PULLMPLEX, MPLEX, SPDY]
|
||||
}
|
||||
}
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
_options = _options || {}
|
||||
|
||||
const starOpts = { id: _options.peerInfo.id }
|
||||
const wrtcStar = new WebRTCStar(starOpts)
|
||||
const wsStar = new WebSocketStar(starOpts)
|
||||
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
wrtcStar,
|
||||
wsStar,
|
||||
new WS()
|
||||
],
|
||||
streamMuxer: getMuxers(_options.muxer),
|
||||
connEncryption: [
|
||||
SECIO
|
||||
],
|
||||
peerDiscovery: [
|
||||
wrtcStar.discovery,
|
||||
wsStar.discovery,
|
||||
Bootstrap
|
||||
],
|
||||
dht: KadDHT,
|
||||
pubsub: GossipSub
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true,
|
||||
webRTCStar: {
|
||||
enabled: true
|
||||
},
|
||||
websocketStar: {
|
||||
enabled: true
|
||||
},
|
||||
bootstrap: {
|
||||
interval: 10000,
|
||||
enabled: false,
|
||||
list: _options.boostrapList
|
||||
}
|
||||
},
|
||||
relay: {
|
||||
enabled: false,
|
||||
hop: {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
randomWalk: {
|
||||
enabled: true
|
||||
},
|
||||
enabled: false
|
||||
},
|
||||
pubsub: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
@ -1,96 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const TCP = require('libp2p-tcp')
|
||||
const MulticastDNS = require('libp2p-mdns')
|
||||
const WS = require('libp2p-websockets')
|
||||
const Bootstrap = require('libp2p-bootstrap')
|
||||
const SPDY = require('libp2p-spdy')
|
||||
const KadDHT = require('libp2p-kad-dht')
|
||||
const GossipSub = require('libp2p-gossipsub')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const PULLMPLEX = require('pull-mplex')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const libp2p = require('../..')
|
||||
|
||||
function mapMuxers (list) {
|
||||
return list.map((pref) => {
|
||||
if (typeof pref !== 'string') { return pref }
|
||||
switch (pref.trim().toLowerCase()) {
|
||||
case 'spdy': return SPDY
|
||||
case 'mplex': return MPLEX
|
||||
case 'pullmplex': return PULLMPLEX
|
||||
default:
|
||||
throw new Error(pref + ' muxer not available')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function getMuxers (muxers) {
|
||||
const muxerPrefs = process.env.LIBP2P_MUXER
|
||||
if (muxerPrefs && !muxers) {
|
||||
return mapMuxers(muxerPrefs.split(','))
|
||||
} else if (muxers) {
|
||||
return mapMuxers(muxers)
|
||||
} else {
|
||||
return [PULLMPLEX, MPLEX, SPDY]
|
||||
}
|
||||
}
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
TCP,
|
||||
WS
|
||||
],
|
||||
streamMuxer: getMuxers(_options.muxer),
|
||||
connEncryption: [
|
||||
SECIO
|
||||
],
|
||||
peerDiscovery: [
|
||||
MulticastDNS,
|
||||
Bootstrap
|
||||
],
|
||||
dht: KadDHT,
|
||||
pubsub: GossipSub
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
autoDial: true,
|
||||
mdns: {
|
||||
interval: 10000,
|
||||
enabled: false
|
||||
},
|
||||
bootstrap: {
|
||||
interval: 10000,
|
||||
enabled: false,
|
||||
list: _options.bootstrapList
|
||||
}
|
||||
},
|
||||
relay: {
|
||||
enabled: false,
|
||||
hop: {
|
||||
enabled: false,
|
||||
active: false
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20,
|
||||
randomWalk: {
|
||||
enabled: true
|
||||
},
|
||||
enabled: true
|
||||
},
|
||||
pubsub: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super(defaultsDeep(_options, defaults))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Node
|
@ -1,41 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const nextTick = require('async/nextTick')
|
||||
const peerJSON = require('../fixtures/test-peer')
|
||||
const multiaddr = require('multiaddr')
|
||||
const promisify = require('promisify-es6')
|
||||
|
||||
let peerRelay = null
|
||||
|
||||
/**
|
||||
* Creates a `PeerInfo` that can be used across testing. Once the
|
||||
* relay `PeerInfo` has been requested, it will be reused for each
|
||||
* additional request.
|
||||
*
|
||||
* This is currently being used to create a relay on test bootstrapping
|
||||
* so that it can be used by browser nodes during their test suite. This
|
||||
* is necessary for running a TCP node during browser tests.
|
||||
* @private
|
||||
* @param {function(error, PeerInfo)} callback
|
||||
* @returns {void}
|
||||
*/
|
||||
module.exports.getPeerRelay = promisify((callback) => {
|
||||
if (peerRelay) return nextTick(callback, null, peerRelay)
|
||||
|
||||
PeerId.createFromJSON(peerJSON, (err, peerId) => {
|
||||
if (err) {
|
||||
return callback(err)
|
||||
}
|
||||
peerRelay = new PeerInfo(peerId)
|
||||
|
||||
peerRelay.multiaddrs.add('/ip4/127.0.0.1/tcp/9200/ws')
|
||||
peerRelay.multiaddrs.add('/ip4/127.0.0.1/tcp/9245')
|
||||
|
||||
callback(null, peerRelay)
|
||||
})
|
||||
})
|
||||
|
||||
module.exports.WS_RENDEZVOUS_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/14444/ws')
|
||||
module.exports.WRTC_RENDEZVOUS_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/15555/ws')
|
@ -1,41 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const PeerInfo = require('peer-info')
|
||||
const PeerId = require('peer-id')
|
||||
const waterfall = require('async/waterfall')
|
||||
const Node = require('./bundle-nodejs')
|
||||
|
||||
function createNode (multiaddrs, options, callback) {
|
||||
if (typeof options === 'function') {
|
||||
callback = options
|
||||
options = {}
|
||||
}
|
||||
|
||||
options = options || {}
|
||||
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
|
||||
waterfall([
|
||||
(cb) => createPeerInfo(cb),
|
||||
(peerInfo, cb) => {
|
||||
multiaddrs.map((ma) => peerInfo.multiaddrs.add(ma))
|
||||
options.peerInfo = peerInfo
|
||||
cb(null, new Node(options))
|
||||
}
|
||||
], callback)
|
||||
}
|
||||
|
||||
function createPeerInfo (callback) {
|
||||
waterfall([
|
||||
(cb) => PeerId.create({ bits: 512 }, cb),
|
||||
(peerId, cb) => PeerInfo.create(peerId, cb)
|
||||
], callback)
|
||||
}
|
||||
|
||||
module.exports = createNode
|
||||
module.exports.createPeerInfo = createPeerInfo
|
@ -1,11 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream')
|
||||
|
||||
function echo (protocol, conn) {
|
||||
pull(conn, conn)
|
||||
}
|
||||
|
||||
module.exports = echo
|
||||
module.exports.multicodec = '/echo/1.0.0'
|
6
test/utils/mockUpgrader.js
Normal file
6
test/utils/mockUpgrader.js
Normal file
@ -0,0 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
upgradeInbound: (maConn) => maConn,
|
||||
upgradeOutbound: (maConn) => maConn
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
'use strict'
|
||||
|
||||
const pull = require('pull-stream')
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
|
||||
module.exports = (conn, callback) => {
|
||||
const values = [Buffer.from('echo')]
|
||||
|
||||
pull(
|
||||
pull.values(values),
|
||||
conn,
|
||||
pull.collect((err, _values) => {
|
||||
expect(err).to.not.exist()
|
||||
expect(_values).to.eql(values)
|
||||
callback()
|
||||
})
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user