forked from libp2p/js-libp2p-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.aegir.js
88 lines (70 loc) · 1.96 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
'use strict'
const PeerInfo = require('peer-info')
const PeerId = require('peer-id')
const parallel = require('async/parallel')
const WebSockets = require('libp2p-websockets')
const pull = require('pull-stream')
const PeerBook = require('peer-book')
const Switch = require('./src')
const spdy = require('libp2p-spdy')
const fs = require('fs')
const path = require('path')
const sigServer = require('libp2p-webrtc-star/src/sig-server')
let switchA
let switchB
let sigS
const sigOptions = { port: 15555, host: '127.0.0.1' }
function echo (protocol, conn) { pull(conn, conn) }
function idJSON (id) {
const p = path.join(__dirname, `./test/test-data/id-${id}.json`)
return JSON.parse(fs.readFileSync(p))
}
function pre (done) {
function createA (cb) {
PeerId.createFromJSON(idJSON(1), (err, id) => {
if (err) { return cb(err) }
const peerA = new PeerInfo(id)
const maA = '/ip4/127.0.0.1/tcp/9100/ws'
peerA.multiaddrs.add(maA)
switchA = new Switch(peerA, new PeerBook())
switchA.transport.add('ws', new WebSockets())
switchA.transport.listen('ws', {}, echo, cb)
})
}
function createB (cb) {
PeerId.createFromJSON(idJSON(2), (err, id) => {
if (err) { return cb(err) }
const peerB = new PeerInfo(id)
const maB = '/ip4/127.0.0.1/tcp/9200/ws'
peerB.multiaddrs.add(maB)
switchB = new Switch(peerB, new PeerBook())
switchB.transport.add('ws', new WebSockets())
switchB.connection.addStreamMuxer(spdy)
switchB.connection.reuse()
switchB.handle('/echo/1.0.0', echo)
switchB.start(cb)
})
}
parallel([
(cb) => {
sigS = sigServer.start(sigOptions, cb)
},
(cb) => createA(cb),
(cb) => createB(cb)
], done)
}
function post (done) {
parallel([
(cb) => switchA.transport.close('ws', cb),
(cb) => switchB.stop(cb),
(cb) => sigS.stop(cb)
], done)
}
module.exports = {
hooks: {
browser: {
pre: pre,
post: post
}
}
}