-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from JoinColony/feature/webrtc-transport
Add webrtc transport option
- Loading branch information
Showing
8 changed files
with
117 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Libp2p = require('libp2p'); | ||
import TCP = require('libp2p-tcp'); | ||
import MulticastDNS = require('libp2p-mdns'); | ||
import WebSocketStarMulti = require('libp2p-websocket-star-multi'); | ||
import KadDHT = require('libp2p-kad-dht'); | ||
import MPLEX = require('pull-mplex'); | ||
import SECIO = require('libp2p-secio'); | ||
import multiaddr = require('multiaddr'); | ||
import wrtc = require('wrtc'); | ||
import WStar = require('libp2p-webrtc-star'); | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
const createConfig = ({ peerInfo, peerBook }) => { | ||
// Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node | ||
const wsstarServers = peerInfo.multiaddrs | ||
.toArray() | ||
.map(String) | ||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
.filter(addr => addr.includes('p2p-websocket-star')); | ||
// the ws-star-multi module will replace this with the chosen ws-star servers | ||
peerInfo.multiaddrs.replace( | ||
wsstarServers.map(multiaddr), | ||
'/p2p-websocket-star', | ||
); | ||
const wsstar = new WebSocketStarMulti({ | ||
servers: wsstarServers, | ||
id: peerInfo.id, | ||
// eslint-disable-next-line @typescript-eslint/camelcase | ||
ignore_no_online: true, | ||
}); | ||
|
||
const wStar = new WStar({ wrtc }); | ||
|
||
// Build and return our libp2p node | ||
return new Libp2p({ | ||
peerInfo, | ||
peerBook, | ||
// Lets limit the connection managers peers and have it check peer health less frequently | ||
connectionManager: { | ||
minPeers: 25, | ||
maxPeers: 100, | ||
pollInterval: 5000, | ||
}, | ||
modules: { | ||
transport: [TCP, wsstar, wStar], | ||
streamMuxer: [MPLEX], | ||
connEncryption: [SECIO], | ||
peerDiscovery: [MulticastDNS, wsstar.discovery, wStar.discovery], | ||
dht: KadDHT, | ||
}, | ||
config: { | ||
peerDiscovery: { | ||
autoDial: true, // auto dial to peers we find when we have less peers than `connectionManager.minPeers` | ||
mdns: { | ||
interval: 10000, | ||
enabled: true, | ||
}, | ||
bootstrap: { | ||
// We're purposefully disabling bootstrap | ||
interval: 30e3, | ||
enabled: false, | ||
list: [], | ||
}, | ||
}, | ||
// Turn on relay with hop active so we can connect to more peers | ||
relay: { | ||
enabled: true, | ||
hop: { | ||
enabled: true, | ||
active: true, | ||
}, | ||
}, | ||
dht: { | ||
enabled: false, | ||
kBucketSize: 20, | ||
randomWalk: { | ||
enabled: false, | ||
interval: 10e3, // This is set low intentionally, so more peers are discovered quickly. Higher intervals are recommended | ||
timeout: 2e3, // End the query quickly since we're running so frequently | ||
}, | ||
}, | ||
EXPERIMENTAL: { | ||
pubsub: true, | ||
}, | ||
}, | ||
}); | ||
}; | ||
|
||
export default createConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
interface Options { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
wrtc: any; | ||
} | ||
|
||
declare module 'libp2p-webrtc-star' { | ||
class WStar { | ||
constructor(options: Options); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
discovery: any; | ||
} | ||
|
||
export = WStar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module 'wrtc' { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const wrtc: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters