-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
345 lines (297 loc) · 8.7 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
const path = require('path')
const pull = require('pull-stream')
const toPull = require('push-stream-to-pull-stream')
const EBT = require('epidemic-broadcast-trees')
const Store = require('key-value-file-store')
const toUrlFriendly = require('base64-url').escape
const getSeverity = require('ssb-network-errors')
const pullDefer = require('pull-defer')
const classicMethods = require('./formats/classic')
function hook(hookable, fn) {
if (typeof hookable === 'function' && hookable.hook) {
hookable.hook(fn)
}
}
exports.name = 'ebt'
exports.version = '1.0.0'
exports.manifest = {
replicate: 'duplex',
replicateFormat: 'duplex',
request: 'sync',
block: 'sync',
forget: 'sync',
peerStatus: 'sync',
clock: 'async',
}
exports.permissions = {
anonymous: {
allow: ['replicate', 'replicateFormat', 'clock'],
},
}
// there was a bug that caused some peers
// to request things that weren't feeds.
// this is fixed, so just ignore anything that isn't a feed.
function cleanClock(clock, isFeed) {
for (const k in clock) {
if (!isFeed(k)) {
delete clock[k]
}
}
}
function isMuxrpcMissingError(err, methodName) {
const jsErrorMessage =
'method:ebt,' + methodName + ' is not in list of allowed methods'
const goErrorMessage = 'muxrpc: no such command: ebt.' + methodName
return err.message === jsErrorMessage || err.message === goErrorMessage
}
function isReconnectedError(err) {
return err.message === 'reconnected to peer'
}
exports.init = function (sbot, config) {
const ebts = []
registerFormat(classicMethods)
function registerFormat(format) {
if (!format.name) throw new Error('format must have a name')
const dirName = 'ebt' + (format.name === 'classic' ? '' : format.name)
const dir = config.path ? path.join(config.path, dirName) : null
const store = Store(dir, null, toUrlFriendly)
// EBT expects a function of only feedId so we bind sbot here
const { isMsg, getMsgAuthor, getMsgSequence, isFeed } = format
const ebt = EBT({
logging: config.ebt && config.ebt.logging,
id: sbot.id,
getClock(id, cb) {
store.ensure(id, function () {
const clock = store.get(id) || {}
cleanClock(clock, isFeed)
cb(null, clock)
})
},
setClock(id, clock) {
cleanClock(clock, isFeed)
store.set(id, clock)
},
getAt(pair, cb) {
format.getAtSequence(sbot, pair, cb)
},
append(msgVal, cb) {
format.appendMsg(sbot, msgVal, cb)
},
isFeed,
isMsg,
getMsgAuthor,
getMsgSequence,
})
// attach a few methods we need in this module
ebt.isFeed = isFeed
ebt.name = format.name
ebt.clearClock = store.delete.bind(store)
const existingId = ebts.findIndex((e) => e.name === format.name)
if (existingId !== -1) ebts[existingId] = ebt
else ebts.push(ebt)
}
function getEBT(formatName) {
const ebt = ebts.find((ebt) => ebt.name === formatName)
if (!ebt) throw new Error('Unknown format: ' + formatName)
return ebt
}
let isReady = false
let waiting = []
function onReady(fn) {
if (isReady) fn()
else waiting.push(fn)
}
sbot.getVectorClock((err, clock) => {
if (err) console.warn('Failed to getVectorClock in ssb-ebt because:', err)
ebts.forEach((ebt) => {
const validClock = {}
for (const k in clock) {
if (ebt.isFeed(k)) {
validClock[k] = clock[k]
}
}
ebt.state.clock = validClock
ebt.update()
})
isReady = true
for (let i = 0; i < waiting.length; ++i) waiting[i]()
waiting = []
})
if (sbot.db) {
sbot.db.onMsgAdded((data) => {
onReady(() => {
ebts.forEach((ebt) => {
if (ebt.name === data.feedFormat) ebt.onAppend(data.nativeMsg)
})
})
})
} else {
sbot.post((msg) => {
onReady(() => {
ebts.forEach((ebt) => {
if (ebt.isFeed(msg.value.author)) ebt.onAppend(msg.value)
})
})
})
}
// TODO: remove this when no one uses ssb-db anymore, because
// sbot.progress is defined in ssb-db but not in ssb-db2
if (sbot.progress) {
hook(sbot.progress, function (fn) {
const _progress = fn()
const ebt = ebts.find((ebt) => ebt.name === 'classic')
const ebtProg = ebt.progress()
if (ebtProg.target) _progress.ebt = ebtProg
return _progress
})
}
sbot.on('rpc:connect', function (rpc, isClient) {
if (rpc.id === sbot.id) return // ssb-client connecting to ssb-server
if (isClient) {
onReady(() => {
ebts.forEach((ebt) => {
const format = ebt.name
const opts = { version: 3, format }
const local = toPull.duplex(
ebt.createStream(rpc.id, opts.version, true)
)
// for backwards compatibility we always replicate classic
// feeds using existing replicate RPC
const methodName =
format === 'classic' ? 'replicate' : 'replicateFormat'
const remote = rpc.ebt[methodName](opts, (networkError) => {
if (networkError && getSeverity(networkError) >= 3) {
if (isMuxrpcMissingError(networkError, methodName)) {
console.warn(
`peer ${rpc.id} does not support RPC ebt.${methodName}`
)
} else if (isReconnectedError(networkError)) {
// Do nothing, this is a harmless error
} else {
console.error('rpc.ebt.replicate exception:', networkError)
}
}
})
pull(local, remote, local)
})
})
}
})
function findEBTForFeed(feedId, formatName) {
let ebt
if (formatName) {
ebt = ebts.find((ebt) => ebt.name === formatName)
} else {
ebt = ebts.find((ebt) => ebt.isFeed(feedId))
}
if (!ebt) {
ebt = ebts.find((ebt) => ebt.name === 'classic')
}
return ebt
}
function request(destFeedId, requesting, formatName) {
onReady(() => {
if (requesting) {
const ebt = findEBTForFeed(destFeedId, formatName)
if (!ebt.isFeed(destFeedId)) return
ebt.request(destFeedId, true)
} else {
// If we don't want a destFeedId, make sure it's not registered anywhere
ebts.forEach((ebt) => {
ebt.request(destFeedId, false)
})
}
})
}
function forget(destFeedId) {
onReady(() => {
for (const ebt of ebts) {
ebt.request(destFeedId, false)
ebt.clearClock(destFeedId)
delete ebt.state.clock[destFeedId]
}
})
}
function block(origFeedId, destFeedId, blocking, formatName) {
onReady(() => {
const ebt = findEBTForFeed(origFeedId, formatName)
if (!ebt.isFeed(origFeedId)) return
if (!ebt.isFeed(destFeedId)) return
if (blocking) {
ebt.block(origFeedId, destFeedId, true)
} else if (
ebt.state.blocks[origFeedId] &&
ebt.state.blocks[origFeedId][destFeedId]
) {
// only update unblock if they were already blocked
ebt.block(origFeedId, destFeedId, false)
}
})
}
function replicateFormat(opts) {
if (opts.version !== 3) {
throw new Error('expected ebt.replicate({version: 3})')
}
const formatName = opts.format || 'classic'
const ebt = getEBT(formatName)
const deferred = pullDefer.duplex()
onReady(() => {
// `this` refers to the remote peer who called this muxrpc API
deferred.resolve(
toPull.duplex(ebt.createStream(this.id, opts.version, false))
)
})
return deferred
}
// get replication status for feeds for this id
function peerStatus(id) {
id = id || sbot.id
const ebt = findEBTForFeed(id)
const data = {
id: id,
seq: ebt.state.clock[id],
peers: {},
}
for (const k in ebt.state.peers) {
const peer = ebt.state.peers[k]
if (
peer.clock[id] != null ||
(peer.replicating && peer.replicating[id] != null)
) {
const rep = peer.replicating && peer.replicating[id]
data.peers[k] = {
seq: peer.clock[id],
replicating: rep,
}
}
}
return data
}
function clock(opts, cb) {
if (!cb) {
cb = opts
opts = { format: 'classic' }
}
onReady(() => {
const ebt = getEBT(opts.format)
cb(null, ebt.state.clock)
})
}
function setClockForSlicedReplication(feedId, sequence, formatName) {
onReady(() => {
const ebt = findEBTForFeed(feedId, formatName)
ebt.state.clock[feedId] = sequence
})
}
return {
request,
block,
forget,
replicate: replicateFormat,
replicateFormat,
peerStatus,
clock,
setClockForSlicedReplication,
registerFormat,
}
}