-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathindex.js
180 lines (162 loc) · 5.18 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint-disable linebreak-style */
/* eslint-disable no-console */
import { noise } from '@chainsafe/libp2p-noise'
import { yamux } from '@chainsafe/libp2p-yamux'
import { webRTCDirect } from '@libp2p/webrtc'
import { multiaddr } from '@multiformats/multiaddr'
import { pipe } from 'it-pipe'
import { createLibp2p } from 'libp2p'
import { fromString, toString } from 'uint8arrays'
// const WEBRTC_CODE = protocols('webrtc').code
const isBrowser = typeof window !== 'undefined'
let node, conn, ma
const elements = isBrowser
? {
sendSection: document.getElementById('send-section'),
messageBox: document.getElementById('messages'),
sendButton: document.getElementById('send'),
inputField: document.getElementById('messageInput'),
connections: document.getElementById('connections'),
multiaddrs: document.getElementById('multiaddrs'),
status: document.getElementById('status'),
output: document.getElementById('output'),
peer: document.getElementById('peer'),
connect: document.getElementById('connect'),
appendOutput: (line) => {
const div = document.createElement('div')
div.appendChild(document.createTextNode(line))
elements.output?.appendChild(div)
}
}
: {}
async function createNode () {
try {
if (isBrowser) {
try {
return await createLibp2p({
transports: [webRTCDirect()],
connectionEncryption: [noise()],
streamMuxers: [yamux()]
})
} catch (error) {
console.log(`Failed to create private node: ${error.message}`)
return null
}
} else {
try {
return await createLibp2p({
addresses: {
listen: ['/ip4/127.0.0.1/tcp/4001/webrtc-direct', '/ip4/192.168.0.5/tcp/4001/webrtc-direct']
},
transports: [webRTCDirect()],
connectionEncryption: [noise()],
streamMuxers: [yamux()]
})
} catch (error) {
console.log(`Failed to create public node: ${error.message}`)
return null
}
}
} catch (error) {
console.log(`Failed to create node: ${error.message}`)
return null
}
}
async function start () {
node = await createNode()
if (!node) return
try {
await node.start()
console.log('Node started! Peer ID:', node.peerId.toString())
node.getMultiaddrs().forEach(addr => console.log('Listening on:', addr.toString()))
updateStatus(`Node started! Peer ID: ${node.peerId.toString()}`, 'green')
if (isBrowser) {
elements.status.textContent = `Node started! Peer ID: ${node.peerId.toString()}`
}
} catch (error) {
console.log(`Failed to start node: ${error.message}`)
return
}
if (isBrowser) {
connectToPublicPeer()
} else {
console.log('Running as public peer (Node.js)')
node.getMultiaddrs().forEach(addr => console.log('Listening on:', addr.toString()))
}
}
function connectToPublicPeer () {
// eslint-disable-next-line no-alert
const publicPeerAddr = document.getElementById('peer').value
if (!publicPeerAddr) return
ma = multiaddr(publicPeerAddr)
node.dial(ma)
.then(() => {
elements.status.textContent = `Connected to ${publicPeerAddr}`
updateStatus(`Connected to ${publicPeerAddr}`, 'green')
setupMessaging()
})
.catch(error => updateStatus(`Failed to dial peer: ${error.message}`, 'red'))
}
if (elements.connect) {
elements.connect.addEventListener('click', async () => {
console.log('connect clicked')
ma = multiaddr(elements.peer.value)
elements.appendOutput(`Dialing '${ma}'`)
const signal = AbortSignal.timeout(5000)
try {
await node.dial(ma, {
signal
})
elements.appendOutput(`Connected to '${ma}'`)
} catch (err) {
if (signal.aborted) {
elements.appendOutput(`Timed out connecting to '${ma}'`)
} else {
elements.appendOutput(`Connecting to '${ma}' failed - ${err.message}`)
}
}
})
}
function setupMessaging () {
elements.sendButton.addEventListener('click', async () => {
const message = elements.inputField.value
if (!message || !conn) return
try {
const { stream } = await conn.newStream(['/chat/1.0.0'])
await pipe([fromString(message)], stream.sink)
appendMessage(`You: ${message}`)
elements.inputField.value = ''
} catch (error) {
console.error(`Error sending message: ${error.message}`)
}
})
node.handle('/chat/1.0.0', async ({ stream }) => {
try {
await pipe(stream.source, async function (source) {
for await (const msg of source) {
appendMessage(`Peer: ${toString(msg)}`)
}
})
} catch (error) {
console.error(`Error receiving message: ${error.message}`)
}
})
}
function appendMessage (text) {
const messageElement = document.createElement('p')
messageElement.textContent = text
elements.messageBox.appendChild(messageElement)
}
function updateStatus (text, color = 'black') {
if (elements.status) {
elements.status.textContent = text
elements.status.style.color = color
}
}
if (isBrowser) {
document.addEventListener('DOMContentLoaded', () => {
start().catch(console.error)
})
} else {
start().catch(console.error)
}