forked from libp2p/js-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.aegir.js
136 lines (119 loc) · 3.2 KB
/
.aegir.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'use strict'
const pull = require('pull-stream')
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')
const sigServer = require('libp2p-webrtc-star/src/sig-server')
const promisify = require('promisify-es6')
const mplex = require('pull-mplex')
const spdy = require('libp2p-spdy')
const PeerBook = require('peer-book')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const path = require('path')
const Switch = require('libp2p-switch')
const WebSockets = require('libp2p-websockets')
const Node = require('./test/utils/bundle-nodejs.js')
const {
getPeerRelay,
WRTC_RENDEZVOUS_MULTIADDR,
WS_RENDEZVOUS_MULTIADDR
} = require('./test/utils/constants')
let wrtcRendezvous
let wsRendezvous
let node
let peerInfo
let switchA
let switchB
function echo (protocol, conn) { pull(conn, conn) }
function idJSON (id) {
const p = path.join(__dirname, `./test/switch/test-data/id-${id}.json`)
return require(p)
}
function createSwitchA () {
return new Promise((resolve, reject) => {
PeerId.createFromJSON(idJSON(1), (err, id) => {
if (err) { return reject(err) }
const peerA = new PeerInfo(id)
const maA = '/ip4/127.0.0.1/tcp/15337/ws'
peerA.multiaddrs.add(maA)
const sw = new Switch(peerA, new PeerBook())
sw.transport.add('ws', new WebSockets())
sw.start((err) => {
if (err) { return reject(err) }
resolve(sw)
})
})
})
}
function createSwitchB () {
return new Promise((resolve, reject) => {
PeerId.createFromJSON(idJSON(2), (err, id) => {
if (err) { return reject(err) }
const peerB = new PeerInfo(id)
const maB = '/ip4/127.0.0.1/tcp/15347/ws'
peerB.multiaddrs.add(maB)
const sw = new Switch(peerB, new PeerBook())
sw.transport.add('ws', new WebSockets())
sw.connection.addStreamMuxer(mplex)
sw.connection.addStreamMuxer(spdy)
sw.connection.reuse()
sw.handle('/echo/1.0.0', echo)
sw.start((err) => {
if (err) { return reject(err) }
resolve(sw)
})
})
})
}
const before = async () => {
[
wrtcRendezvous,
wsRendezvous,
peerInfo,
switchA,
switchB
] = await Promise.all([
sigServer.start({
port: WRTC_RENDEZVOUS_MULTIADDR.nodeAddress().port
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
}),
WebSocketStarRendezvous.start({
port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port,
refreshPeerListIntervalMS: 1000,
strictMultiaddr: false,
cryptoChallenge: true
}),
getPeerRelay(),
createSwitchA(),
createSwitchB()
])
node = new Node({
peerInfo,
config: {
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
}
})
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
await node.start()
}
const after = () => {
return Promise.all([
wrtcRendezvous.stop(),
wsRendezvous.stop(),
node.stop(),
promisify(switchA.stop, { context: switchA })(),
promisify(switchB.stop, { context: switchB })()
])
}
module.exports = {
bundlesize: { maxSize: '220kB' },
hooks: {
pre: before,
post: after
}
}