|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const libp2p = require('../../') |
| 3 | +const Libp2p = require('../../') |
4 | 4 | const TCP = require('libp2p-tcp')
|
5 |
| -const SPDY = require('libp2p-spdy') |
| 5 | +const Mplex = require('libp2p-mplex') |
6 | 6 | const SECIO = require('libp2p-secio')
|
7 | 7 | const PeerInfo = require('peer-info')
|
8 |
| -const waterfall = require('async/waterfall') |
9 |
| -const parallel = require('async/parallel') |
10 |
| -const pull = require('pull-stream') |
11 |
| -const defaultsDeep = require('@nodeutils/defaults-deep') |
12 |
| - |
13 |
| -class MyBundle extends libp2p { |
14 |
| - constructor (_options) { |
15 |
| - const defaults = { |
16 |
| - modules: { |
17 |
| - transport: [ TCP ], |
18 |
| - streamMuxer: [ SPDY ], |
19 |
| - connEncryption: [ SECIO ] |
20 |
| - } |
21 |
| - } |
22 | 8 |
|
23 |
| - super(defaultsDeep(_options, defaults)) |
24 |
| - } |
25 |
| -} |
| 9 | +const pipe = require('it-pipe') |
26 | 10 |
|
27 |
| -function createNode (callback) { |
28 |
| - let node |
29 |
| - |
30 |
| - waterfall([ |
31 |
| - (cb) => PeerInfo.create(cb), |
32 |
| - (peerInfo, cb) => { |
33 |
| - peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') |
34 |
| - node = new MyBundle({ |
35 |
| - peerInfo |
36 |
| - }) |
37 |
| - node.start(cb) |
| 11 | +const createNode = async () => { |
| 12 | + const peerInfo = await PeerInfo.create() |
| 13 | + peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0') |
| 14 | + |
| 15 | + const node = await Libp2p.create({ |
| 16 | + peerInfo, |
| 17 | + modules: { |
| 18 | + transport: [TCP], |
| 19 | + streamMuxer: [Mplex], |
| 20 | + connEncryption: [SECIO] |
38 | 21 | }
|
39 |
| - ], (err) => callback(err, node)) |
40 |
| -} |
| 22 | + }) |
41 | 23 |
|
42 |
| -parallel([ |
43 |
| - (cb) => createNode(cb), |
44 |
| - (cb) => createNode(cb) |
45 |
| -], (err, nodes) => { |
46 |
| - if (err) { throw err } |
| 24 | + await node.start() |
47 | 25 |
|
48 |
| - const node1 = nodes[0] |
49 |
| - const node2 = nodes[1] |
| 26 | + return node |
| 27 | +} |
50 | 28 |
|
51 |
| - node2.handle('/a-protocol', (protocol, conn) => { |
52 |
| - pull( |
53 |
| - conn, |
54 |
| - pull.map((v) => v.toString()), |
55 |
| - pull.log() |
| 29 | +;(async () => { |
| 30 | + const [node1, node2] = await Promise.all([ |
| 31 | + createNode(), |
| 32 | + createNode() |
| 33 | + ]) |
| 34 | + |
| 35 | + node2.handle('/a-protocol', ({ stream }) => { |
| 36 | + pipe( |
| 37 | + stream, |
| 38 | + async function (source) { |
| 39 | + for await (const msg of source) { |
| 40 | + console.log(msg.toString()) |
| 41 | + } |
| 42 | + } |
56 | 43 | )
|
57 | 44 | })
|
58 | 45 |
|
59 |
| - node1.dialProtocol(node2.peerInfo, '/a-protocol', (err, conn) => { |
60 |
| - if (err) { throw err } |
61 |
| - pull(pull.values(['This information is sent out encrypted to the other peer']), conn) |
62 |
| - }) |
63 |
| -}) |
| 46 | + const { stream } = await node1.dialProtocol(node2.peerInfo, '/a-protocol') |
| 47 | + |
| 48 | + await pipe( |
| 49 | + ['This information is sent out encrypted to the other peer'], |
| 50 | + stream |
| 51 | + ) |
| 52 | +})(); |
0 commit comments