mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-13 01:01:23 +00:00
refactor: async routing (#489)
* feat: async routing * chore: put dht extra api commands under content routing * chore: add default option to createPeerInfo Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * chore: address review * chore: rm dlv
This commit is contained in:
@ -1,32 +1,71 @@
|
||||
'use strict'
|
||||
|
||||
const pTimes = require('p-times')
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
|
||||
const Libp2p = require('../../../src')
|
||||
const Peers = require('../../fixtures/peers')
|
||||
const defaultOptions = require('../base-options.browser')
|
||||
|
||||
async function createPeerInfo (length) {
|
||||
const peers = await Promise.all(
|
||||
Array.from({ length })
|
||||
.map((_, i) => PeerId.create())
|
||||
)
|
||||
const listenAddr = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||
|
||||
return peers.map((peer) => new PeerInfo(peer))
|
||||
/**
|
||||
* Create libp2p nodes.
|
||||
* @param {Object} [properties]
|
||||
* @param {Object} [properties.config]
|
||||
* @param {number} [properties.number] number of peers (default: 1).
|
||||
* @param {boolean} [properties.fixture] use fixture for peer-id generation (default: true)
|
||||
* @param {boolean} [properties.started] nodes should start (defaul: true)
|
||||
* @return {Promise<Array<Libp2p>>}
|
||||
*/
|
||||
async function createPeer ({ number = 1, fixture = true, started = true, config = defaultOptions } = {}) {
|
||||
const peerInfos = await createPeerInfo({ number, fixture })
|
||||
|
||||
const peers = await pTimes(number, (i) => Libp2p.create({
|
||||
peerInfo: peerInfos[i],
|
||||
...config
|
||||
}))
|
||||
|
||||
if (started) {
|
||||
await Promise.all(peers.map((p) => {
|
||||
p.peerInfo.multiaddrs.add(listenAddr)
|
||||
return p.start()
|
||||
}))
|
||||
}
|
||||
|
||||
return peers
|
||||
}
|
||||
|
||||
function createPeerIdsFromFixture (length) {
|
||||
return Promise.all(
|
||||
Array.from({ length })
|
||||
.map((_, i) => PeerId.createFromJSON(Peers[i]))
|
||||
/**
|
||||
* Create Peer-ids.
|
||||
* @param {Object} [properties]
|
||||
* @param {number} [properties.number] number of peers (default: 1).
|
||||
* @param {boolean} [properties.fixture] use fixture for peer-id generation (default: true)
|
||||
* @return {Promise<Array<PeerInfo>>}
|
||||
*/
|
||||
async function createPeerInfo ({ number = 1, fixture = true } = {}) {
|
||||
const peerIds = await createPeerId({ number, fixture })
|
||||
|
||||
return pTimes(number, (i) => PeerInfo.create(peerIds[i]))
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Peer-ids.
|
||||
* @param {Object} [properties]
|
||||
* @param {number} [properties.number] number of peers (default: 1).
|
||||
* @param {boolean} [properties.fixture] use fixture for peer-id generation (default: true)
|
||||
* @return {Promise<Array<PeerId>>}
|
||||
*/
|
||||
function createPeerId ({ number = 1, fixture = true } = {}) {
|
||||
return pTimes(number, (i) => fixture
|
||||
? PeerId.createFromJSON(Peers[i])
|
||||
: PeerId.create()
|
||||
)
|
||||
}
|
||||
|
||||
async function createPeerInfoFromFixture (length) {
|
||||
const peers = await createPeerIdsFromFixture(length)
|
||||
|
||||
return peers.map((peer) => new PeerInfo(peer))
|
||||
}
|
||||
|
||||
module.exports.createPeer = createPeer
|
||||
module.exports.createPeerInfo = createPeerInfo
|
||||
module.exports.createPeerIdsFromFixture = createPeerIdsFromFixture
|
||||
module.exports.createPeerInfoFromFixture = createPeerInfoFromFixture
|
||||
module.exports.createPeerId = createPeerId
|
||||
|
@ -16,7 +16,7 @@ module.exports = async (properties = {}) => {
|
||||
const localAddr = multiaddr('/ip4/127.0.0.1/tcp/8080')
|
||||
const remoteAddr = multiaddr('/ip4/127.0.0.1/tcp/8081')
|
||||
|
||||
const [localPeer, remotePeer] = await peerUtils.createPeerInfoFromFixture(2)
|
||||
const [localPeer, remotePeer] = await peerUtils.createPeerInfo({ number: 2 })
|
||||
const openStreams = []
|
||||
let streamId = 0
|
||||
|
||||
|
Reference in New Issue
Block a user