-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathinject.js
422 lines (372 loc) · 11.8 KB
/
inject.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
'use strict'
const Notify = require('pull-notify')
const pull = require('pull-stream')
const isBlobId = require('ssb-ref').isBlob
const pullDefer = require('pull-defer')
const MB = 1024 * 1024
const MAX_SIZE = 5 * MB
function isEmpty (o) {
for (const k in o) if (k) return false
return true
}
function clone (obj) {
const o = {}
for (const k in obj) { o[k] = obj[k] }
return o
}
function onAbort (abortCb) {
return function (read) {
return function (abort, cb) {
if (abort) abortCb(abort, cb)
else read(null, cb)
}
}
}
function toBlobId (id) {
if (Array.isArray(id)) return id// .map(toBlobId)
return isBlobId(id) ? id : isBlobId(id && id.id) ? id.id : null
}
function wrap (fn) {
return function (id, cb) {
if (!toBlobId(id)) {
cb = id
return fn.call(this, cb)
}
return fn.call(this, toBlobId(id), cb)
}
}
module.exports = function inject (blobStore, blobPush, name, opts) {
opts = opts || {}
// sympathy controls whether you'll replicate
const sympathy = opts.sympathy == null ? 3 : opts.sympathy | 0
const stingy = opts.stingy === true
const legacy = opts.legacy !== false
const pushy = opts.pushy || 3
const max = opts.max || MAX_SIZE
const peers = {} // currently connected peers - { PeerId: RPC }
const peerBlobs = {} // tracks blob locations - { PeerId: { BlobId: Size } }
const streams = {} // { PeerId: PullSource }
const want = {} // which blobs we'd like to get ahold of { BlobId: hops }
const push = {} // who we've pushed a Blob to this session - { BlobId: { PeerId } }
const wantCallbacks = {} // queue of callbacks from the want method - { BlobId: [function] }
const getting = {} // blobs and which peers we're currently getting them from - { BlobId: PeerId }
const pushed = Notify() // makes a stream of blob hashes which have reached 'pushy' goal
const notify = Notify() // used with createWants + legacySync
blobPush.updates(update => {
const notification = {}
Object.keys(update).forEach(blobId => {
notification[blobId] = -1
push[blobId] = push[blobId] || {}
})
notify(notification) // update all currently connected peers about new push
})
function registerWant (id, hops = -1) {
if (hops >= 0) throw new Error(`registerWant expects hops >= 0, got ${hops}`)
want[id] = hops
notify({ [id]: hops })
}
function findPeerWithBlob (blobId) {
for (const peerId in peers) {
if (
peers[peerId] &&
peerBlobs[peerId] &&
peerBlobs[peerId][blobId] < max
) return peerId
}
}
function get (peerId, id) {
if (getting[id]) return
if (!peers[peerId]) peerId = findPeerWithBlob(id)
if (!peerId) return
getting[id] = peerId
pull(
peers[peerId].blobs.get({ key: id, max: max }), // source
blobStore.add(id, (err, _id) => { // sink
delete getting[id]
if (err) {
if (peerBlobs[peerId]) delete peerBlobs[peerId][id]
// check if another peer has this.
// if so get it from them.
peerId = findPeerWithBlob(id)
if (peerId) get(peerId, id)
}
})
)
}
pull(
blobStore.ls({ old: false, meta: true }),
pull.drain(function (data) {
registerHas(data.id, data.size)
if (wantCallbacks[data.id]) {
while (wantCallbacks[data.id].length) {
wantCallbacks[data.id].shift()(null, true)
}
}
})
)
function registerHas (id, size) {
delete want[id]
notify({ [id]: size })
}
function registerRemoteHas (peerId, blobId, size) {
if (typeof peerId !== 'string') throw new Error('peer must be string id')
peerBlobs[peerId] = peerBlobs[peerId] || {}
peerBlobs[peerId][blobId] = size
// if we are broadcasting this blob,
// mark this peer has it.id
// if N peers have it, we can stop broadcasting.
if (push[blobId]) {
push[blobId][peerId] = size
if (Object.keys(push[blobId]).length >= pushy) {
pushed({ key: blobId, peers: push[blobId] })
blobPush.remove(blobId)
delete push[blobId]
}
}
if (getting[blobId]) return
if (want[blobId]) {
if (size < max) get(peerId, blobId)
else console.warn(`want blob ${blobId} but size ${size} >= ${max} (currently set blobs.max)`)
}
}
function dead (peerId) {
delete peers[peerId]
delete peerBlobs[peerId]
delete streams[peerId]
}
function createWantStream (id) {
if (!streams[id]) {
streams[id] = notify.listen()
// merge in ids we are pushing.
const w = clone(want)
for (const k in push) w[k] = -1
streams[id].push(w)
}
return pull(
streams[id],
onAbort((err, cb) => {
streams[id] = false
cb(err)
})
)
}
function wantSink (peer) {
createWantStream(peer.id) // set streams[peer.id]
let modern = false
return pull.drain(
function (data) {
modern = true
// respond with list of blobs you already have,
process(data, peer.id, function (err, hasData) {
if (err) console.error(err) // ):
if (isEmpty(hasData)) return
if (streams[peer.id]) streams[peer.id].push(hasData)
})
},
err => {
if (err && !modern) {
streams[peer.id] = false
if (legacy) legacySync(peer)
return
}
// if can handle unpeer another way,
// then can refactor legacy handling out of sight.
// handle error and fallback to legacy mode, if enabled.
if (peers[peer.id] === peer) dead(peer.id)
}
)
}
function process (data, peer, cb) {
const res = {}
let n = 0
for (const id in data) {
(function (id) {
if (isBlobId(id) && Number.isInteger(data[id])) {
if (data[id] < 0 && (opts.stingy !== true || push[id])) { // interpret as "WANT"
n++
// check whether we already *HAVE* this file.
// respond with it's size, if we do.
blobStore.size(id, function (err, size) { // XXX
if (err) return cb(err)
if (size) res[id] = size
else sympatheticGet(id, data[id] - 1) // a sympathetic get
next()
})
} else if (data[id] > 0) { // interpret as "HAS"
if (data[id] >= max) console.warn(`process found blob ${id} has size >= blobs.max`)
registerRemoteHas(peer, id, data[id])
}
}
}(id))
}
function next () {
if (--n) return
cb(null, res)
}
}
function sympatheticGet (id, hops) {
if (Math.abs(hops) > sympathy) return // sorry!
if (want[id] && want[id] <= hops) return // higher priority want already happening
registerWant(id, hops)
const peerId = findPeerWithBlob(id)
if (peerId) get(peerId, id)
}
// LEGACY LEGACY LEGACY
function legacySync (peer) {
if (!legacy) return
let drain // eslint-disable-line
// we need to keep a reference to drain so we can abort it when we get an error.
function hasLegacy (hashes) {
const ary = Object.keys(hashes).filter(function (k) {
return hashes[k] < 0
})
if (ary.length) {
peer.blobs.has(ary, function (err, haves) {
if (err) drain.abort(err) // ERROR: abort this stream.
else {
haves.forEach(function (have, i) {
if (have) registerRemoteHas(peer.id, ary[i], have)
})
}
})
}
}
drain = pull.drain(
hash => registerRemoteHas(peer.id, hash, true),
err => { if (err) dead(peer.id) }
)
pull(peer.blobs.changes(), drain)
hasLegacy(want)
// a stream of hashes
pull(
notify.listen(),
pull.drain(
hasLegacy,
err => { if (err) dead(peer.id) }
)
)
}
// LEGACY LEGACY LEGACY
const self = {
// id: name,
has: function (id, cb) {
id = toBlobId(id)
if (Array.isArray(id)) {
for (let i = 0; i < id.length; i++) {
if (!isBlobId(id[i])) return cb(new Error('invalid id:' + id[i]))
}
} else if (!isBlobId(id)) {
return cb(new Error('invalid id:' + id))
}
if (legacy !== true) {
blobStore.has.call(this, id, cb)
return
}
// LEGACY LEGACY LEGACY
if (this === self || !this || this === global) { // a local call
return blobStore.has.call(this, id, cb)
}
// ELSE, interpret remote calls to has as a WANT request.
// handling this by calling process (which calls size())
// avoids a race condition in the tests.
// (and avoids doubling the number of calls)
const a = Array.isArray(id) ? id : [id]
const o = {}
a.forEach(function (h) { o[h] = -1 })
// since this is always "has" process will never use the second arg.
process(o, null, function (err, res) {
if (err) return cb(err)
const a = []; for (const k in o) a.push(res[k] > 0)
cb(null, Array.isArray(id) ? a : a[0])
})
// LEGACY LEGACY LEGACY
},
size: wrap(blobStore.size),
get: blobStore.get,
getSlice: blobStore.getSlice,
add (id, cb) {
const blobId = toBlobId(id)
cb = typeof cb === 'function'
? cb
: typeof id === 'function'
? id
: (err, blobId) => { if (err) throw err }
const sink = blobId ? blobStore.add(blobId, cb) : blobStore.add(cb)
let size = 0
return pull(
pull.asyncMap((m, cb) => {
size += m.length
if (size >= max) cb(new Error("Cannot add blob, it's bigger than blobs.max"))
else cb(null, m)
}),
sink
)
},
rm: wrap(blobStore.rm),
ls: blobStore.ls,
changes: function () {
// XXX for bandwidth sensitive peers, don't tell them about blobs we arn't trying to push.
return pull(
blobStore.ls({ old: false, meta: false }),
pull.filter(function (id) {
return !stingy || push[id]
})
)
},
want: function (id, cb) {
id = toBlobId(id)
if (!isBlobId(id)) { return cb(new Error('invalid id:' + id)) }
// always broadcast wants immediately, because of race condition
// between has and adding a blob (needed to pass test/async.js)
if (blobStore.isEmptyHash(id)) return cb(null, true)
if (wantCallbacks[id]) wantCallbacks[id].push(cb)
else {
wantCallbacks[id] = [cb]
blobStore.size(id, function (err, size) {
if (err) return cb(err)
if (size != null) {
while (wantCallbacks[id].length) {
wantCallbacks[id].shift()(null, true)
}
delete wantCallbacks[id]
}
})
}
const peerId = findPeerWithBlob(id)
if (peerId) get(peerId, id) // NOTE used to early return here
if (wantCallbacks[id]) registerWant(id)
},
push: function (id, cb) {
id = toBlobId(id)
if (!isBlobId(id)) return cb(new Error('invalid hash:' + id))
registerWant(id) // pretend we want the blob to stimulate sympathetic replication
blobPush.add(id, cb) // leads to update of push ^
},
pushed: function () {
return pushed.listen()
},
createWants: function () {
const source = pullDefer.source()
blobPush.onReady(() => {
source.resolve(createWantStream(this.id))
})
return source
},
// private api. used for testing. not exposed over rpc.
_wantSink: wantSink,
_onConnect: function (other, name) {
peers[other.id] = other
// sending of your wants starts when we know they are not legacy style.
// process is called when wantSync doesn't immediately get an error.
pull(
other.blobs.createWants(),
// pull.through(m => console.log('WANT STREAM', m)),
wantSink(other)
)
},
help: function () {
return require('./help')
}
}
return self
}