mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-05-04 15:02:13 +00:00
refactor(docs): async await version of examples/echo (#483)
* fix: performance bottleneck in stat.js (#463) Array.shift seems to be very slow, perhaps linear, on some engines, resulting in _update consuming a lot of CPU. * docs(fix): correct docs and example for pnet (#464) * docs(fix): correct docs and example for pnet * docs(fix): correct pnet docs * docs(fix): update README.md language (#468) * docs: reciprocate (#474) * docs(example): fix ipfs cat (#475) `ipfs.files.cat` is incorrect. the correct function is `ipfs.cat` * fix: async await examples/echo * fix: examples readme typos (#481) * fix: simplify libp2p bundle for echo example
This commit is contained in:
parent
c563e06a60
commit
953d185c39
@ -8,51 +8,54 @@
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const pull = require('pull-stream')
|
||||
const async = require('async')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
async.parallel([
|
||||
(cb) => PeerId.createFromJSON(require('./id-d'), cb),
|
||||
(cb) => PeerId.createFromJSON(require('./id-l'), cb)
|
||||
], (err, ids) => {
|
||||
if (err) { throw err }
|
||||
async function run() {
|
||||
const [dialerId, listenerId] = await Promise.all([
|
||||
PeerId.createFromJSON(require('./id-d')),
|
||||
PeerId.createFromJSON(require('./id-l'))
|
||||
])
|
||||
|
||||
// Dialer
|
||||
const dialerId = ids[0]
|
||||
const dialerPeerInfo = new PeerInfo(dialerId)
|
||||
dialerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
||||
const dialerNode = new Node({
|
||||
peerInfo: dialerPeerInfo
|
||||
})
|
||||
|
||||
// Peer to Dial
|
||||
const listenerPeerInfo = new PeerInfo(ids[1])
|
||||
const listenerId = ids[1]
|
||||
// Peer to Dial (the listener)
|
||||
const listenerPeerInfo = new PeerInfo(listenerId)
|
||||
const listenerMultiaddr = '/ip4/127.0.0.1/tcp/10333/p2p/' +
|
||||
listenerId.toB58String()
|
||||
listenerPeerInfo.multiaddrs.add(listenerMultiaddr)
|
||||
|
||||
dialerNode.start((err) => {
|
||||
if (err) { throw err }
|
||||
// Start the dialer libp2p node
|
||||
await dialerNode.start()
|
||||
|
||||
console.log('Dialer ready, listening on:')
|
||||
dialerPeerInfo.multiaddrs.forEach((ma) => console.log(ma.toString() +
|
||||
'/p2p/' + dialerId.toB58String()))
|
||||
|
||||
// Dial the listener node
|
||||
console.log('Dialing to peer:', listenerMultiaddr.toString())
|
||||
dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0', (err, conn) => {
|
||||
if (err) { throw err }
|
||||
const { stream } = await dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0')
|
||||
|
||||
console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')
|
||||
|
||||
pull(
|
||||
pull.values(['hey']),
|
||||
conn,
|
||||
pull.collect((err, data) => {
|
||||
if (err) { throw err }
|
||||
pipe(
|
||||
// Source data
|
||||
['hey'],
|
||||
// Write to the stream, and pass its output to the next function
|
||||
stream,
|
||||
// Sink function
|
||||
async function (source) {
|
||||
// For each chunk of data
|
||||
for await (const data of source) {
|
||||
// Output the data
|
||||
console.log('received echo:', data.toString())
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
run()
|
||||
|
@ -1,41 +1,12 @@
|
||||
'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 mplex = require('libp2p-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
|
||||
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 [mplex, spdy]
|
||||
}
|
||||
}
|
||||
|
||||
class Node extends libp2p {
|
||||
constructor (_options) {
|
||||
const defaults = {
|
||||
@ -44,29 +15,8 @@ class Node extends libp2p {
|
||||
TCP,
|
||||
WS
|
||||
],
|
||||
streamMuxer: getMuxers(_options.muxer),
|
||||
connEncryption: [ secio ],
|
||||
peerDiscovery: [
|
||||
MulticastDNS,
|
||||
Bootstrap
|
||||
],
|
||||
dht: KadDHT
|
||||
},
|
||||
config: {
|
||||
peerDiscovery: {
|
||||
mdns: {
|
||||
interval: 10000,
|
||||
enabled: false
|
||||
},
|
||||
bootstrap: {
|
||||
interval: 10000,
|
||||
enabled: false,
|
||||
list: _options.bootstrapList
|
||||
}
|
||||
},
|
||||
dht: {
|
||||
kBucketSize: 20
|
||||
}
|
||||
streamMuxer: [ mplex ],
|
||||
connEncryption: [ secio ]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,39 +8,34 @@
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const Node = require('./libp2p-bundle')
|
||||
const pull = require('pull-stream')
|
||||
const series = require('async/series')
|
||||
const pipe = require('it-pipe')
|
||||
|
||||
let listenerId
|
||||
let listenerNode
|
||||
async function run() {
|
||||
const listenerId = await PeerId.createFromJSON(require('./id-l'))
|
||||
|
||||
series([
|
||||
(cb) => {
|
||||
PeerId.createFromJSON(require('./id-l'), (err, id) => {
|
||||
if (err) { return cb(err) }
|
||||
listenerId = id
|
||||
cb()
|
||||
})
|
||||
},
|
||||
(cb) => {
|
||||
// Listener libp2p node
|
||||
const listenerPeerInfo = new PeerInfo(listenerId)
|
||||
listenerPeerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/10333')
|
||||
listenerNode = new Node({
|
||||
const listenerNode = new Node({
|
||||
peerInfo: listenerPeerInfo
|
||||
})
|
||||
|
||||
// Log a message when we receive a connection
|
||||
listenerNode.on('peer:connect', (peerInfo) => {
|
||||
console.log('received dial to me from:', peerInfo.id.toB58String())
|
||||
})
|
||||
|
||||
listenerNode.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||
listenerNode.start(cb)
|
||||
}
|
||||
], (err) => {
|
||||
if (err) { throw err }
|
||||
// Handle incoming connections for the protocol by piping from the stream
|
||||
// back to itself (an echo)
|
||||
await listenerNode.handle('/echo/1.0.0', ({ stream }) => pipe(stream.source, stream.sink))
|
||||
|
||||
// Start listening
|
||||
await listenerNode.start()
|
||||
|
||||
console.log('Listener ready, listening on:')
|
||||
listenerNode.peerInfo.multiaddrs.forEach((ma) => {
|
||||
console.log(ma.toString() + '/p2p/' + listenerId.toB58String())
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
run()
|
||||
|
Loading…
x
Reference in New Issue
Block a user