mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-17 16:10:52 +00:00
* docs: update chat example and add info to its readme * docs: update echo example * docs: update libp2p in browser example * docs: update pubsub example * docs: update peer and content routing examples * docs: update discovery mechanisms example * docs: update encrypted comms example * docs: update protocol and stream muxing example * feat: add config validation * test: update CI configs, use only node 8
84 lines
1.8 KiB
JavaScript
84 lines
1.8 KiB
JavaScript
'use strict'
|
|
|
|
const libp2p = require('../../')
|
|
const TCP = require('libp2p-tcp')
|
|
const SPDY = require('libp2p-spdy')
|
|
const PeerInfo = require('peer-info')
|
|
const waterfall = require('async/waterfall')
|
|
const parallel = require('async/parallel')
|
|
const series = require('async/series')
|
|
const pull = require('pull-stream')
|
|
const defaultsDeep = require('@nodeutils/defaults-deep')
|
|
|
|
class MyBundle extends libp2p {
|
|
constructor (_options) {
|
|
const defaults = {
|
|
modules: {
|
|
transport: [ TCP ],
|
|
streamMuxer: [ SPDY ]
|
|
}
|
|
}
|
|
|
|
super(defaultsDeep(_options, defaults))
|
|
}
|
|
}
|
|
|
|
function createNode (callback) {
|
|
let node
|
|
|
|
waterfall([
|
|
(cb) => PeerInfo.create(cb),
|
|
(peerInfo, cb) => {
|
|
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
|
node = new MyBundle({
|
|
peerInfo
|
|
})
|
|
node.start(cb)
|
|
}
|
|
], (err) => callback(err, node))
|
|
}
|
|
|
|
parallel([
|
|
(cb) => createNode(cb),
|
|
(cb) => createNode(cb)
|
|
], (err, nodes) => {
|
|
if (err) { throw err }
|
|
|
|
const node1 = nodes[0]
|
|
const node2 = nodes[1]
|
|
|
|
node2.handle('/a', (protocol, conn) => {
|
|
pull(
|
|
conn,
|
|
pull.map((v) => v.toString()),
|
|
pull.log()
|
|
)
|
|
})
|
|
|
|
node2.handle('/b', (protocol, conn) => {
|
|
pull(
|
|
conn,
|
|
pull.map((v) => v.toString()),
|
|
pull.log()
|
|
)
|
|
})
|
|
|
|
series([
|
|
(cb) => node1.dialProtocol(node2.peerInfo, '/a', (err, conn) => {
|
|
if (err) { throw err }
|
|
pull(pull.values(['protocol (a)']), conn)
|
|
cb()
|
|
}),
|
|
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
|
if (err) { throw err }
|
|
pull(pull.values(['protocol (b)']), conn)
|
|
cb()
|
|
}),
|
|
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
|
if (err) { throw err }
|
|
pull(pull.values(['another conn on protocol (b)']), conn)
|
|
cb()
|
|
})
|
|
])
|
|
})
|