Skip to content

Commit fd738f9

Browse files
jacobheunAlan ShawalanshawArnaudValensidaviddias
authored
refactor: add js-libp2p-switch to the libp2p codebase (#388)
Co-authored-by: Alan Shaw <[email protected]> Co-authored-by: Alan Shaw <[email protected]> Co-authored-by: Arnaud <[email protected]> Co-authored-by: David Dias <[email protected]> Co-authored-by: David Dias <[email protected]> Co-authored-by: Dmitriy Ryajov <[email protected]> Co-authored-by: Francisco Baio Dias <[email protected]> Co-authored-by: Friedel Ziegelmayer <[email protected]> Co-authored-by: Haad <[email protected]> Co-authored-by: Hugo Dias <[email protected]> Co-authored-by: Hugo Dias <[email protected]> Co-authored-by: Jacob Heun <[email protected]> Co-authored-by: Kevin Kwok <[email protected]> Co-authored-by: Kobi Gurkan <[email protected]> Co-authored-by: Maciej Krüger <[email protected]> Co-authored-by: Matteo Collina <[email protected]> Co-authored-by: Michael Fakhry <[email protected]> Co-authored-by: Oli Evans <[email protected]> Co-authored-by: Pau Ramon Revilla <[email protected]> Co-authored-by: Pedro Teixeira <[email protected]> Co-authored-by: Pius Nyakoojo <[email protected]> Co-authored-by: Richard Littauer <[email protected]> Co-authored-by: Sid Harder <[email protected]> Co-authored-by: Vasco Santos <[email protected]> Co-authored-by: harrshasri <[email protected]> Co-authored-by: kumavis <[email protected]> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <[email protected]>
1 parent d788433 commit fd738f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+8668
-5
lines changed

.aegir.js

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
const pull = require('pull-stream')
44
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')
55
const sigServer = require('libp2p-webrtc-star/src/sig-server')
6+
const promisify = require('promisify-es6')
7+
const mplex = require('pull-mplex')
8+
const spdy = require('libp2p-spdy')
9+
const PeerBook = require('peer-book')
10+
const PeerId = require('peer-id')
11+
const PeerInfo = require('peer-info')
12+
const path = require('path')
13+
const Switch = require('libp2p-switch')
14+
const WebSockets = require('libp2p-websockets')
615

716
const Node = require('./test/utils/bundle-nodejs.js')
817
const {
@@ -15,9 +24,67 @@ let wrtcRendezvous
1524
let wsRendezvous
1625
let node
1726
let peerInfo
27+
let switchA
28+
let switchB
29+
30+
function echo (protocol, conn) { pull(conn, conn) }
31+
function idJSON (id) {
32+
const p = path.join(__dirname, `./test/switch/test-data/id-${id}.json`)
33+
return require(p)
34+
}
35+
36+
function createSwitchA () {
37+
return new Promise((resolve, reject) => {
38+
PeerId.createFromJSON(idJSON(1), (err, id) => {
39+
if (err) { return reject(err) }
40+
41+
const peerA = new PeerInfo(id)
42+
const maA = '/ip4/127.0.0.1/tcp/15337/ws'
43+
44+
peerA.multiaddrs.add(maA)
45+
const sw = new Switch(peerA, new PeerBook())
46+
47+
sw.transport.add('ws', new WebSockets())
48+
sw.start((err) => {
49+
if (err) { return reject(err) }
50+
resolve(sw)
51+
})
52+
})
53+
})
54+
}
55+
56+
function createSwitchB () {
57+
return new Promise((resolve, reject) => {
58+
PeerId.createFromJSON(idJSON(2), (err, id) => {
59+
if (err) { return reject(err) }
60+
61+
const peerB = new PeerInfo(id)
62+
const maB = '/ip4/127.0.0.1/tcp/15347/ws'
63+
64+
peerB.multiaddrs.add(maB)
65+
const sw = new Switch(peerB, new PeerBook())
66+
67+
sw.transport.add('ws', new WebSockets())
68+
sw.connection.addStreamMuxer(mplex)
69+
sw.connection.addStreamMuxer(spdy)
70+
sw.connection.reuse()
71+
sw.handle('/echo/1.0.0', echo)
72+
sw.start((err) => {
73+
if (err) { return reject(err) }
74+
resolve(sw)
75+
})
76+
})
77+
})
78+
}
1879

1980
const before = async () => {
20-
[wrtcRendezvous, wsRendezvous, peerInfo] = await Promise.all([
81+
[
82+
wrtcRendezvous,
83+
wsRendezvous,
84+
peerInfo,
85+
switchA,
86+
switchB
87+
] = await Promise.all([
2188
sigServer.start({
2289
port: WRTC_RENDEZVOUS_MULTIADDR.nodeAddress().port
2390
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
@@ -28,7 +95,9 @@ const before = async () => {
2895
strictMultiaddr: false,
2996
cryptoChallenge: true
3097
}),
31-
getPeerRelay()
98+
getPeerRelay(),
99+
createSwitchA(),
100+
createSwitchB()
32101
])
33102

34103
node = new Node({
@@ -52,7 +121,9 @@ const after = () => {
52121
return Promise.all([
53122
wrtcRendezvous.stop(),
54123
wsRendezvous.stop(),
55-
node.stop()
124+
node.stop(),
125+
promisify(switchA.stop, { context: switchA })(),
126+
promisify(switchB.stop, { context: switchB })()
56127
])
57128
}
58129

package.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,30 @@
4444
},
4545
"dependencies": {
4646
"async": "^2.6.2",
47+
"bignumber.js": "^8.1.1",
48+
"class-is": "^1.1.0",
4749
"debug": "^4.1.1",
4850
"err-code": "^1.1.2",
4951
"fsm-event": "^2.1.0",
52+
"hashlru": "^2.3.0",
53+
"interface-connection": "~0.3.3",
54+
"libp2p-circuit": "~0.3.6",
5055
"libp2p-connection-manager": "^0.1.0",
56+
"libp2p-identify": "~0.7.6",
5157
"libp2p-ping": "^0.8.5",
52-
"libp2p-switch": "^0.43.0",
58+
"libp2p-switch": "./src/switch",
5359
"libp2p-websockets": "^0.12.2",
5460
"mafmt": "^6.0.7",
61+
"moving-average": "^1.0.0",
5562
"multiaddr": "^6.1.0",
63+
"multistream-select": "~0.14.6",
5664
"once": "^1.4.0",
5765
"peer-book": "^0.9.1",
5866
"peer-id": "^0.12.2",
59-
"peer-info": "^0.15.1",
67+
"peer-info": "~0.15.1",
68+
"pull-stream": "^3.6.13",
6069
"promisify-es6": "^1.0.3",
70+
"retimer": "^2.0.0",
6171
"superstruct": "^0.6.0"
6272
},
6373
"devDependencies": {
@@ -78,6 +88,7 @@
7888
"libp2p-kad-dht": "^0.15.3",
7989
"libp2p-mdns": "^0.12.3",
8090
"libp2p-mplex": "^0.8.4",
91+
"libp2p-pnet": "~0.1.0",
8192
"libp2p-secio": "^0.11.1",
8293
"libp2p-spdy": "^0.13.2",
8394
"libp2p-tcp": "^0.13.0",
@@ -87,11 +98,15 @@
8798
"lodash.times": "^4.3.2",
8899
"merge-options": "^1.0.1",
89100
"nock": "^10.0.6",
101+
"portfinder": "^1.0.20",
90102
"pull-goodbye": "0.0.2",
103+
"pull-length-prefixed": "^1.3.3",
91104
"pull-mplex": "^0.1.2",
105+
"pull-pair": "^1.1.0",
92106
"pull-serializer": "^0.3.2",
93107
"pull-stream": "^3.6.12",
94108
"sinon": "^7.2.7",
109+
"webrtcsupport": "^2.2.0",
95110
"wrtc": "^0.4.1"
96111
},
97112
"contributors": [

0 commit comments

Comments
 (0)