Skip to content

Commit

Permalink
refactor: add js-libp2p-switch to the libp2p codebase (#388)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
1 parent d788433 commit fd738f9
Show file tree
Hide file tree
Showing 57 changed files with 8,668 additions and 5 deletions.
77 changes: 74 additions & 3 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
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 {
Expand All @@ -15,9 +24,67 @@ 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] = await Promise.all([
[
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
Expand All @@ -28,7 +95,9 @@ const before = async () => {
strictMultiaddr: false,
cryptoChallenge: true
}),
getPeerRelay()
getPeerRelay(),
createSwitchA(),
createSwitchB()
])

node = new Node({
Expand All @@ -52,7 +121,9 @@ const after = () => {
return Promise.all([
wrtcRendezvous.stop(),
wsRendezvous.stop(),
node.stop()
node.stop(),
promisify(switchA.stop, { context: switchA })(),
promisify(switchB.stop, { context: switchB })()
])
}

Expand Down
19 changes: 17 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,30 @@
},
"dependencies": {
"async": "^2.6.2",
"bignumber.js": "^8.1.1",
"class-is": "^1.1.0",
"debug": "^4.1.1",
"err-code": "^1.1.2",
"fsm-event": "^2.1.0",
"hashlru": "^2.3.0",
"interface-connection": "~0.3.3",
"libp2p-circuit": "~0.3.6",
"libp2p-connection-manager": "^0.1.0",
"libp2p-identify": "~0.7.6",
"libp2p-ping": "^0.8.5",
"libp2p-switch": "^0.43.0",
"libp2p-switch": "./src/switch",
"libp2p-websockets": "^0.12.2",
"mafmt": "^6.0.7",
"moving-average": "^1.0.0",
"multiaddr": "^6.1.0",
"multistream-select": "~0.14.6",
"once": "^1.4.0",
"peer-book": "^0.9.1",
"peer-id": "^0.12.2",
"peer-info": "^0.15.1",
"peer-info": "~0.15.1",
"pull-stream": "^3.6.13",
"promisify-es6": "^1.0.3",
"retimer": "^2.0.0",
"superstruct": "^0.6.0"
},
"devDependencies": {
Expand All @@ -78,6 +88,7 @@
"libp2p-kad-dht": "^0.15.3",
"libp2p-mdns": "^0.12.3",
"libp2p-mplex": "^0.8.4",
"libp2p-pnet": "~0.1.0",
"libp2p-secio": "^0.11.1",
"libp2p-spdy": "^0.13.2",
"libp2p-tcp": "^0.13.0",
Expand All @@ -87,11 +98,15 @@
"lodash.times": "^4.3.2",
"merge-options": "^1.0.1",
"nock": "^10.0.6",
"portfinder": "^1.0.20",
"pull-goodbye": "0.0.2",
"pull-length-prefixed": "^1.3.3",
"pull-mplex": "^0.1.2",
"pull-pair": "^1.1.0",
"pull-serializer": "^0.3.2",
"pull-stream": "^3.6.12",
"sinon": "^7.2.7",
"webrtcsupport": "^2.2.0",
"wrtc": "^0.4.1"
},
"contributors": [
Expand Down
Loading

0 comments on commit fd738f9

Please sign in to comment.