This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
09-swarm-with-muxing.node.js
256 lines (217 loc) · 6.57 KB
/
09-swarm-with-muxing.node.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* eslint-env mocha */
'use strict'
const expect = require('chai').expect
const parallel = require('run-parallel')
const multiaddr = require('multiaddr')
const Peer = require('peer-info')
const TCP = require('libp2p-tcp')
const WebSockets = require('libp2p-websockets')
const spdy = require('libp2p-spdy')
const pull = require('pull-stream')
const Swarm = require('../src')
describe('high level API - with everything mixed all together!', () => {
let swarmA // tcp
let peerA
let swarmB // tcp+ws
let peerB
let swarmC // tcp+ws
let peerC
let swarmD // ws
let peerD
let swarmE // ws
let peerE
before((done) => {
peerA = new Peer()
peerB = new Peer()
peerC = new Peer()
peerD = new Peer()
peerE = new Peer()
// console.log('peer A', peerA.id.toB58String())
// console.log('peer B', peerB.id.toB58String())
// console.log('peer C', peerC.id.toB58String())
swarmA = new Swarm(peerA)
swarmB = new Swarm(peerB)
swarmC = new Swarm(peerC)
swarmD = new Swarm(peerD)
swarmE = new Swarm(peerE)
done()
})
after((done) => {
parallel([
(cb) => swarmA.close(cb),
(cb) => swarmB.close(cb),
// (cb) => swarmC.close(cb),
(cb) => swarmD.close(cb),
(cb) => swarmE.close(cb)
], done)
})
it('add tcp', (done) => {
peerA.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/0'))
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/0'))
peerC.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/0'))
swarmA.transport.add('tcp', new TCP())
swarmB.transport.add('tcp', new TCP())
swarmC.transport.add('tcp', new TCP())
parallel([
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
(cb) => swarmB.transport.listen('tcp', {}, null, cb)
// (cb) => swarmC.transport.listen('tcp', {}, null, cb)
], done)
})
it.skip('add utp', (done) => {})
it('add websockets', (done) => {
peerB.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9012/ws'))
peerC.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9022/ws'))
peerD.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9032/ws'))
peerE.multiaddr.add(multiaddr('/ip4/127.0.0.1/tcp/9042/ws'))
swarmB.transport.add('ws', new WebSockets())
swarmC.transport.add('ws', new WebSockets())
swarmD.transport.add('ws', new WebSockets())
swarmE.transport.add('ws', new WebSockets())
parallel([
(cb) => swarmB.transport.listen('ws', {}, null, cb),
// (cb) => swarmC.transport.listen('ws', {}, null, cb),
(cb) => swarmD.transport.listen('ws', {}, null, cb),
(cb) => swarmE.transport.listen('ws', {}, null, cb)
], done)
})
it('listen automatically', (done) => {
swarmC.listen(done)
})
it('add spdy', () => {
swarmA.connection.addStreamMuxer(spdy)
swarmB.connection.addStreamMuxer(spdy)
swarmC.connection.addStreamMuxer(spdy)
swarmD.connection.addStreamMuxer(spdy)
swarmE.connection.addStreamMuxer(spdy)
swarmA.connection.reuse()
swarmB.connection.reuse()
swarmC.connection.reuse()
swarmD.connection.reuse()
swarmE.connection.reuse()
})
it.skip('add multiplex', () => {})
it('warm up from A to B on tcp to tcp+ws', (done) => {
parallel([
(cb) => swarmB.once('peer-mux-established', (peerInfo) => {
expect(peerInfo.id.toB58String()).to.equal(peerA.id.toB58String())
cb()
}),
(cb) => swarmA.once('peer-mux-established', (peerInfo) => {
expect(peerInfo.id.toB58String()).to.equal(peerB.id.toB58String())
cb()
}),
(cb) => swarmA.dial(peerB, (err) => {
expect(err).to.not.exist
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
cb()
})
], done)
})
it('warm up a warmed up, from B to A', (done) => {
swarmB.dial(peerA, (err) => {
expect(err).to.not.exist
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
done()
})
})
it('dial from tcp to tcp+ws, on protocol', (done) => {
swarmB.handle('/anona/1.0.0', (conn) => {
pull(conn, conn)
})
swarmA.dial(peerB, '/anona/1.0.0', (err, conn) => {
expect(err).to.not.exist
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
pull(
pull.empty(),
conn,
pull.onEnd(done)
)
})
})
it('dial from ws to ws no proto', (done) => {
swarmD.dial(peerE, (err) => {
expect(err).to.not.exist
expect(Object.keys(swarmD.muxedConns).length).to.equal(1)
done()
})
})
it('dial from ws to ws', (done) => {
swarmE.handle('/abacaxi/1.0.0', (conn) => {
pull(conn, conn)
})
swarmD.dial(peerE, '/abacaxi/1.0.0', (err, conn) => {
expect(err).to.not.exist
expect(Object.keys(swarmD.muxedConns).length).to.equal(1)
pull(
pull.empty(),
conn,
pull.onEnd((err) => {
expect(err).to.not.exist
setTimeout(() => {
expect(Object.keys(swarmE.muxedConns).length).to.equal(1)
done()
}, 1000)
})
)
})
})
it('dial from tcp to tcp+ws (returned conn)', (done) => {
swarmB.handle('/grapes/1.0.0', (conn) => {
pull(conn, conn)
})
const conn = swarmA.dial(peerB, '/grapes/1.0.0', (err, conn) => {
expect(err).to.not.exist
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
})
pull(
pull.empty(),
conn,
pull.onEnd(done)
)
})
it('dial from tcp+ws to tcp+ws', (done) => {
swarmC.handle('/mamao/1.0.0', (conn) => {
conn.getPeerInfo((err, peerInfo) => {
expect(err).to.not.exist
expect(peerInfo).to.exist
})
pull(conn, conn)
})
swarmA.dial(peerC, '/mamao/1.0.0', (err, conn) => {
expect(err).to.not.exist
conn.getPeerInfo((err, peerInfo) => {
expect(err).to.not.exist
expect(peerInfo).to.exist
})
expect(Object.keys(swarmA.muxedConns).length).to.equal(2)
pull(
pull.empty(),
conn,
pull.onEnd(done)
)
})
})
it('hangUp', (done) => {
let count = 0
const ready = () => ++count === 3 ? done() : null
swarmB.once('peer-mux-closed', (peerInfo) => {
expect(Object.keys(swarmB.muxedConns).length).to.equal(0)
ready()
})
swarmA.once('peer-mux-closed', (peerInfo) => {
expect(Object.keys(swarmA.muxedConns).length).to.equal(1)
ready()
})
swarmA.hangUp(peerB, (err) => {
expect(err).to.not.exist
ready()
})
})
it('close a muxer emits event', (done) => {
parallel([
(cb) => swarmC.close(cb),
(cb) => swarmA.once('peer-mux-closed', (peerInfo) => cb())
], done)
})
})