mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-12 08:41:22 +00:00
refactor: examples transports (#503)
* refactor: examples-transports * chore: apply suggestions from code review Co-Authored-By: Jacob Heun <jacobheun@gmail.com> * chore: address review * chore: address review Co-authored-by: Jacob Heun <jacobheun@gmail.com>
This commit is contained in:
@ -1,39 +1,31 @@
|
||||
/* eslint-disable no-console */
|
||||
'use strict'
|
||||
|
||||
const libp2p = require('../../')
|
||||
const Libp2p = require('../..')
|
||||
const TCP = require('libp2p-tcp')
|
||||
const SECIO = require('libp2p-secio')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const PeerInfo = require('peer-info')
|
||||
const waterfall = require('async/waterfall')
|
||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
||||
const parallel = require('async/parallel')
|
||||
const pull = require('pull-stream')
|
||||
|
||||
class MyBundle extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
modules: {
|
||||
transport: [
|
||||
TCP
|
||||
]
|
||||
}
|
||||
const pipe = require('it-pipe')
|
||||
const concat = require('it-concat')
|
||||
|
||||
const createNode = async (peerInfo) => {
|
||||
// To signall the addresses we want to be available, we use
|
||||
// the multiaddr format, a self describable address
|
||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
|
||||
const node = await Libp2p.create({
|
||||
peerInfo,
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
connEncryption: [SECIO],
|
||||
streamMuxer: [MPLEX]
|
||||
}
|
||||
})
|
||||
|
||||
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: peerInfo })
|
||||
node.start(cb)
|
||||
}
|
||||
], (err) => callback(err, node))
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
|
||||
function printAddrs (node, number) {
|
||||
@ -41,29 +33,31 @@ function printAddrs (node, number) {
|
||||
node.peerInfo.multiaddrs.forEach((ma) => console.log(ma.toString()))
|
||||
}
|
||||
|
||||
parallel([
|
||||
(cb) => createNode(cb),
|
||||
(cb) => createNode(cb)
|
||||
], (err, nodes) => {
|
||||
if (err) { throw err }
|
||||
|
||||
const node1 = nodes[0]
|
||||
const node2 = nodes[1]
|
||||
;(async () => {
|
||||
const [peerInfo1, peerInfo2] = await Promise.all([
|
||||
PeerInfo.create(),
|
||||
PeerInfo.create()
|
||||
])
|
||||
const [node1, node2] = await Promise.all([
|
||||
createNode(peerInfo1),
|
||||
createNode(peerInfo2)
|
||||
])
|
||||
|
||||
printAddrs(node1, '1')
|
||||
printAddrs(node2, '2')
|
||||
|
||||
node2.handle('/print', (protocol, conn) => {
|
||||
pull(
|
||||
conn,
|
||||
pull.map((v) => v.toString()),
|
||||
pull.log()
|
||||
node2.handle('/print', async ({ stream }) => {
|
||||
const result = await pipe(
|
||||
stream,
|
||||
concat
|
||||
)
|
||||
console.log(result.toString())
|
||||
})
|
||||
|
||||
node1.dialProtocol(node2.peerInfo, '/print', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
const { stream } = await node1.dialProtocol(node2.peerInfo, '/print')
|
||||
|
||||
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
|
||||
})
|
||||
})
|
||||
await pipe(
|
||||
['Hello', ' ', 'p2p', ' ', 'world', '!'],
|
||||
stream
|
||||
)
|
||||
})();
|
||||
|
Reference in New Issue
Block a user