-
Notifications
You must be signed in to change notification settings - Fork 0
/
replicate.js
242 lines (225 loc) · 6.78 KB
/
replicate.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { keys } from './keys.js'
import { logs } from './log.js'
import { render } from './render.js'
import { open } from './sbog.js'
import { make, find } from './blob.js'
import { encode } from './lib/base64.js'
import { getBoth } from './avatar.js'
import { h } from './lib/misc.js'
import { addSocket, rmSocket, gossipMsg } from './gossip.js'
let blastcache = []
export function blast (msg) {
if (!blastcache.includes(msg)) {
gossipMsg(msg, keys.pubkey())
blastcache.push(msg)
}
}
function replicate (ws) {
// rec our latest
gossipMsg(keys.pubkey(), keys.pubkey())
logs.getFeeds().then(feedList => {
//console.log(feedList)
feedList.forEach(feed => {
gossipMsg(feed, keys.pubkey())
logs.getLatest(feed).then(latest => {
gossipMsg(latest.raw, keys.pubkey())
})
})
})
// next check for the route feed
var src = window.location.hash.substring(1)
if (src.length === 44) {
//console.log('checking for route feed' + src)
logs.query(src).then(query => {
if (!query[0]) {
gossipMsg(src, keys.pubkey())
}
})
}
ws.onclose = (e) => {
setTimeout(function () {
console.log('connection to ' + ws.url + ' closed, reconnecting')
connect(ws.url, keys)
}, 10000)
}
}
//let serverId = 0
function processReq (req, ws) {
if (req.length === 44) {
let gotit = false
if (req === keys.pubkey()) {
gotit = true
}
logs.getFeeds().then(feeds => {
if (feeds.includes(req)) {
gotit = true
}
})
//console.log('check to see if '+ req + ' is a feed')
logs.getLatest(req).then(latest => {
if (latest) {
//console.log(req + ' is a feed we have, sending')
logs.get(latest.hash).then(got => {
if (got) {
gotit = true
gossipMsg(got.hash, keys.pubkey())
//gossipMsg(got.data, keys.pubkey())
}
})
} else {
logs.get(req).then(post => {
if (post) {
gotit = true
//console.log(req + ' is a post, sending')
gossipMsg(post.raw, keys.pubkey())
//gossipMsg(post.data, keys.pubkey())
}
})
}
})
setTimeout(function () {
if (!gotit) {
find(req).then(file => {
if (file) {
gotit = true
//console.log(req + ' is a blob, sending')
//console.log(file)
gossipMsg('blob:' + req + file, keys.pubkey())
setTimeout(function () {
if (!gotit) {
//console.log('WE do not have '+ req +', blasting for it ')
blast(req)
}
}, 500)
}
})
}
}, 500)
}
if (req.length > 44) {
if (req.startsWith('connect:')) {
const disgot = document.getElementById('disconnect:' + req.substring(8))
if (disgot) { got.parentNode.removeChild(disgot) }
const got = document.getElementById(req)
if (got) { got.parentNode.removeChild(got) }
const connect = h('div', {classList: 'message', id: req}, [
getBoth(req.substring(8)),
' connected.'
])
scroller.insertBefore(connect, scroller.childNodes[1])
logs.getFeeds().then(feedList => {
feedList.forEach(feed => {
gossipMsg(feed, keys.pubkey())
logs.getLatest(feed).then(latest => {
gossipMsg(latest.raw, keys.pubkey())
})
})
})
} else if (req.startsWith('disconnect:')) {
console.log(req)
const disgot = document.getElementById(req)
if (disgot) { got.parentNode.removeChild(disgot) }
const got = document.getElementById('connect:' + req.substring(11))
if (got) { got.parentNode.removeChild(got) }
const disconnect = h('div', {classList: 'message', id: 'connect:' + req.substring(11)}, [
getBoth(req.substring(11)),
' disconnected.'
])
scroller.insertBefore(disconnect, scroller.childNodes[1])
} else if (req.startsWith('blob:')) {
//console.log('THIS IS A BLOB')
const hash = req.substring(5, 49)
find(hash).then(found => {
if (!found) {
const file = req.substring(49)
blastcache.push(hash)
make(file)
}
})
} else {
open(req).then(opened => {
if (opened) {
logs.get(opened.hash).then(got => {
if (!got) {
logs.add(req)
const src = window.location.hash.substring(1)
const getMsg = document.getElementById(opened.hash)
if (!getMsg && src == '' || src == opened.hash || src == opened.author) {
const scroller = document.getElementById('scroller')
render(opened).then(rendered => {
logs.getNext(opened.hash).then(next => {
if (!next) {
scroller.insertBefore(rendered, scroller.childNodes[1])
} else {
const getNext = document.getElementById(opened.hash)
if (!getNext) {
scroller.appendChild(rendered)
}
if (getNext) {
getNext.appendChild(rendered)
}
}
})
})
}
}
})
}
})
}
}
}
//function sendAvatar (ws) {
// const id = keys.pubkey()
// logs.query(id).then(querylog => {
// if (querylog && querylog[0]) {
// querylog.forEach(msg => {
// if (msg.text && msg.text.startsWith('name:') && msg.text.substring(49) === id) {
// ws.send(msg.hash)
// const query = msg.text.substring(5, 49)
// find(query).then(name => {
// ws.send('blob:' + query + name)
// })
// }
// if (msg.text && msg.text.startsWith('image:') && msg.text.substring(50) === id) {
// ws.send(msg.raw)
// const query = msg.text.substring(6, 50)
// find(query).then(image => {
// ws.send('blob:' + query + image)
// })
// }
// })
// }
// })
//}
export function connect (server) {
console.log('Connecting to ' + server)
const ws = new WebSocket(server)
ws.binaryType = 'arraybuffer'
ws.onopen = () => {
ws.send('connect:' + keys.pubkey())
//sendAvatar(ws)
addSocket(ws)
replicate(ws)
}
ws.onmessage = (msg) => {
processReq(msg.data, ws)
}
addEventListener('beforeunload', event => {
ws.send('disconnect:' + keys.pubkey())
})
ws.onclose = (e) => {
rmSocket(ws)
setTimeout(function () {
connect(server)
}, 1000)
}
let retryCount = 1
ws.onerror = (err) => {
setTimeout(function () {
ws.close()
rmSocket(ws)
retryCount++
}, 10000 * retryCount)
}
}