mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-17 16:10:52 +00:00
* feat: allow for configuring content and peer routing * feat: support multiple peer and content routing modules * docs: add delegated routing example
42 lines
950 B
JavaScript
42 lines
950 B
JavaScript
/* 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
|