forked from ipfs/js-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libp2p-browser.js
77 lines (71 loc) · 2.26 KB
/
libp2p-browser.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
'use strict'
const WS = require('libp2p-websockets')
const WebRTCStar = require('libp2p-webrtc-star')
const WebSocketStarMulti = require('libp2p-websocket-star-multi')
const Multiplex = require('pull-mplex')
const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap')
const KadDHT = require('libp2p-kad-dht')
const libp2p = require('libp2p')
const mergeOptions = require('merge-options')
const multiaddr = require('multiaddr')
class Node extends libp2p {
constructor (_options) {
const wrtcstar = new WebRTCStar({ id: _options.peerInfo.id })
// this can be replaced once optional listening is supported with the below code. ref: https://github.com/libp2p/interface-transport/issues/41
// const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
const wsstarServers = _options.peerInfo.multiaddrs.toArray().map(String).filter(addr => addr.includes('p2p-websocket-star'))
_options.peerInfo.multiaddrs.replace(wsstarServers.map(multiaddr), '/p2p-websocket-star') // the ws-star-multi module will replace this with the chosen ws-star servers
const wsstar = new WebSocketStarMulti({ servers: wsstarServers, id: _options.peerInfo.id, ignore_no_online: !wsstarServers.length || _options.wsStarIgnoreErrors })
const defaults = {
switch: {
blacklistTTL: 2 * 60 * 1e3, // 2 minute base
blackListAttempts: 5, // back off 5 times
maxParallelDials: 100,
maxColdCalls: 25,
dialTimeout: 20e3
},
modules: {
transport: [
WS,
wrtcstar,
wsstar
],
streamMuxer: [
Multiplex
],
connEncryption: [
SECIO
],
peerDiscovery: [
wrtcstar.discovery,
wsstar.discovery,
Bootstrap
],
dht: KadDHT
},
config: {
peerDiscovery: {
autoDial: true,
bootstrap: {
enabled: true
},
webRTCStar: {
enabled: true
},
websocketStar: {
enabled: true
}
},
dht: {
enabled: false
},
EXPERIMENTAL: {
pubsub: false
}
}
}
super(mergeOptions(defaults, _options))
}
}
module.exports = Node