-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
114 lines (103 loc) · 3.77 KB
/
index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// @ts-check
import { createLibp2p } from 'libp2p'
import { identify } from '@libp2p/identify'
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { multiaddr } from '@multiformats/multiaddr'
import { gossipsub } from '@chainsafe/libp2p-gossipsub'
import { webSockets } from '@libp2p/websockets'
import { webTransport } from '@libp2p/webtransport'
import { webRTC, webRTCDirect } from '@libp2p/webrtc'
import { circuitRelayTransport, circuitRelayServer } from '@libp2p/circuit-relay-v2'
import { enable, disable } from '@libp2p/logger'
import { pubsubPeerDiscovery } from '@libp2p/pubsub-peer-discovery'
import { PUBSUB_PEER_DISCOVERY } from './constants'
import { update, getPeerTypes, getAddresses, getPeerDetails } from './utils'
import { bootstrap } from '@libp2p/bootstrap'
import * as filters from '@libp2p/websockets/filters'
const App = async () => {
const libp2p = await createLibp2p({
// addresses: {
// listen: [
// // 👇 Listen for webRTC connection
// '/webrtc',
// ],
// },
transports: [
webSockets({
// Allow all WebSocket connections inclusing without TLS
filter: filters.all,
}),
webTransport(),
webRTC(),
// // 👇 Required to create circuit relay reservations in order to hole punch browser-to-browser WebRTC connections
// circuitRelayTransport({
// discoverRelays: 1,
// }),
],
connectionEncryption: [noise()],
streamMuxers: [yamux()],
connectionGater: {
// Allow private addresses for local testing
denyDialMultiaddr: async () => false,
},
peerDiscovery: [
// bootstrap({
// list: [''],
// }),
// pubsubPeerDiscovery({
// interval: 10_000,
// topics: [PUBSUB_PEER_DISCOVERY],
// }),
],
services: {
// pubsub: gossipsub(),
identify: identify(),
},
})
globalThis.libp2p = libp2p
const DOM = {
nodePeerId: () => document.getElementById('output-node-peer-id'),
nodeStatus: () => document.getElementById('output-node-status'),
nodePeerCount: () => document.getElementById('output-peer-count'),
nodePeerTypes: () => document.getElementById('output-peer-types'),
nodePeerDetails: () => document.getElementById('output-peer-details'),
nodeAddressCount: () => document.getElementById('output-address-count'),
nodeAddresses: () => document.getElementById('output-addresses'),
inputMultiaddr: () => document.getElementById('input-multiaddr'),
connectButton: () => document.getElementById('button-connect'),
loggingButtonEnable: () => document.getElementById('button-logging-enable'),
loggingButtonDisable: () => document.getElementById('button-logging-disable'),
outputQuery: () => document.getElementById('output'),
}
update(DOM.nodePeerId(), libp2p.peerId.toString())
update(DOM.nodeStatus(), 'Online')
libp2p.addEventListener('peer:connect', (event) => {})
libp2p.addEventListener('peer:disconnect', (event) => {})
setInterval(() => {
update(DOM.nodePeerCount(), libp2p.getConnections().length)
update(DOM.nodePeerTypes(), getPeerTypes(libp2p))
update(DOM.nodeAddressCount(), libp2p.getMultiaddrs().length)
update(DOM.nodeAddresses(), getAddresses(libp2p))
update(DOM.nodePeerDetails(), getPeerDetails(libp2p))
}, 1000)
DOM.loggingButtonEnable().onclick = (e) => {
enable('*,*:debug')
}
DOM.loggingButtonDisable().onclick = (e) => {
disable()
}
DOM.connectButton().onclick = async (e) => {
e.preventDefault()
let maddr = multiaddr(DOM.inputMultiaddr().value)
console.log(maddr)
try {
await libp2p.dial(maddr)
} catch (e) {
console.log(e)
}
}
}
App().catch((err) => {
console.error(err) // eslint-disable-line no-console
})