-
Notifications
You must be signed in to change notification settings - Fork 66
/
index.js
310 lines (248 loc) · 7.71 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
var EventEmitter = require('events').EventEmitter
var i = require('iterate')
var duplex = require('duplex')
var inherits = require('util').inherits
var serializer = require('stream-serializer')
var u = require('./util')
var timestamp = require('monotonic-timestamp')
exports =
module.exports = Scuttlebutt
exports.createID = u.createID
exports.updateIsRecent = u.filter
exports.filter = u.filter
exports.timestamp = timestamp
function dutyOfSubclass() {
throw new Error('method must be implemented by subclass')
}
function validate (data) {
if(!(Array.isArray(data)
&& 'string' === typeof data[2]
&& '__proto__' !== data[2] //THIS WOULD BREAK STUFF
&& 'number' === typeof data[1]
)) return false
return true
}
inherits (Scuttlebutt, EventEmitter)
function Scuttlebutt (opts) {
if(!(this instanceof Scuttlebutt)) return new Scuttlebutt(opts)
var id = 'string' === typeof opts ? opts : opts && opts.id
this.sources = {}
this.setMaxListeners(Number.MAX_VALUE)
//count how many other instances we are replicating to.
this._streams = 0
if(opts && opts.sign && opts.verify) {
this.setId(opts.id || opts.createId())
this._sign = opts.sign
this._verify = opts.verify
} else {
this.setId(id || u.createId())
}
}
var sb = Scuttlebutt.prototype
var emit = EventEmitter.prototype.emit
sb.applyUpdate = dutyOfSubclass
sb.history = dutyOfSubclass
sb.localUpdate = function (trx) {
this._update([trx, timestamp(), this.id])
return this
}
sb._update = function (update) {
//validated when it comes into the stream
var ts = update[1]
var source = update[2]
//if this message is old for it's source, ignore it. it's out of
//order. each node must emit it's changes in order!
//emit an 'old_data' event because i'll want to track how many
//unnecessary messages are sent.
var latest = this.sources[source]
if(latest && latest >= ts)
return emit.call(this, 'old_data', update), false
this.sources[source] = ts
var self = this
function didVerification (err, verified) {
// I'm not sure how what should happen if a async verification
// errors. if it's an key not found - that is a verification fail,
// not a error. if it's genunie error, really you should queue and
// try again? or replay the message later
// -- this should be done my the security plugin though, not scuttlebutt.
if(err)
return emit.call(self, 'error', err)
if(!verified)
return emit.call(self, 'unverified_data', update)
if(self.applyUpdate(update))
emit.call(self, '_update', update) //write to stream.
}
if(source !== this.id) {
if(this._verify)
this._verify(update, didVerification)
else
didVerification(null, true)
} else {
if(this._sign) {
//could make this async easily enough.
update[3] = this._sign(update)
}
didVerification(null, true)
}
return true
}
sb.createStream = function (opts) {
var self = this
//the sources for the remote end.
var sources = {}, other
var syncSent = false, syncRecv = false
this._streams ++
opts = opts || {}
var d = duplex()
d.name = opts.name
var outer = serializer(opts && opts.wrapper)(d)
outer.inner = d
d.writable = opts.writable !== false
d.readable = opts.readable !== false
syncRecv = !d.writable
syncSent = !d.readable
var tail = opts.tail !== false //default to tail=true
function start (data) {
//when the digest is recieved from the other end,
//send the history.
//merge with the current list of sources.
if (!data || !data.clock) {
d.emit('error');
return d._end()
}
sources = data.clock
i.each(self.history(sources), function (data) {d._data(data)})
//the _update listener must be set after the history is queued.
//otherwise there is a race between the first client message
//and the next update (which may come in on another stream)
//this problem will probably not be encountered until you have
//thousands of scuttlebutts.
self.on('_update', onUpdate)
d._data('SYNC')
syncSent = true
//when we have sent all history
outer.emit('header', data)
outer.emit('syncSent')
//when we have recieved all histoyr
//emit 'synced' when this stream has synced.
if(syncRecv) outer.emit('sync'), outer.emit('synced')
if(!tail) d._end()
}
d
.on('_data', function (data) {
//if it's an array, it's an update.
if(Array.isArray(data)) {
//check whether we are accepting writes.
if(!d.writable)
return
if(validate(data))
return self._update(data)
}
//if it's an object, it's a scuttlebut digest.
else if('object' === typeof data && data)
start(data)
else if('string' === typeof data && data == 'SYNC') {
syncRecv = true
outer.emit('syncRecieved')
if(syncSent) outer.emit('sync'), outer.emit('synced')
}
}).on('_end', function () {
d._end()
})
.on('close', function () {
self.removeListener('_update', onUpdate)
self.removeListener('dispose', dispose)
//emit the number of streams that are remaining...
//this will be used for memory management...
self._streams --
emit.call(self, 'unstream', self._streams)
})
if(opts && opts.tail === false) {
outer.on('sync', function () {
process.nextTick(function () {
d._end()
})
})
}
function onUpdate (update) { //value, source, ts
if(!validate(update) || !u.filter(update, sources))
return
d._data(update)
//really, this should happen before emitting.
var ts = update[1]
var source = update[2]
sources[source] = ts
}
function dispose () {
d.end()
}
var outgoing = { id : self.id, clock : self.sources }
if (opts && opts.meta) outgoing.meta = opts.meta
if(d.readable) {
d._data(outgoing)
if(!d.writable && !opts.clock)
start({clock:{}})
} else if (opts.sendClock) {
//send my current clock.
//so the other side knows what to send
d._data(outgoing)
}
self.once('dispose', dispose)
return outer
}
sb.createWriteStream = function (opts) {
opts = opts || {}
opts.writable = true; opts.readable = false
return this.createStream(opts)
}
sb.createReadStream = function (opts) {
opts = opts || {}
opts.writable = false; opts.readable = true
return this.createStream(opts)
}
sb.dispose = function () {
emit.call(this, 'dispose')
}
sb.setId = function (id) {
if('__proto__' === id) throw new Error('__proto__ is invalid id')
if(id == null) throw new Error('null is not invalid id')
this.id = id
return this
}
function streamDone(stream, listener) {
function remove () {
stream.removeListener('end', onDone)
stream.removeListener('error', onDone)
stream.removeListener('close', onDone)
}
function onDone (arg) {
remove()
listener.call(this, arg)
}
//this makes emitter.removeListener(event, listener) still work
onDone.listener = listener
stream.on('end', onDone)
stream.on('error', onDone)
stream.on('close', onDone)
}
//create another instance of this scuttlebutt,
//that is in sync and attached to this instance.
sb.clone = function () {
var A = this
var B = new (A.constructor)
B.setId(A.id) //same id. think this will work...
A._clones = (A._clones || 0) + 1
var a = A.createStream({wrapper: 'raw'})
var b = B.createStream({wrapper: 'raw'})
//all updates must be sync, so make sure pause never happens.
a.pause = b.pause = function noop(){}
streamDone(b, function () {
A._clones--
emit.call(A, 'unclone', A._clones)
})
a.pipe(b).pipe(a)
//resume both streams, so that the new instance is brought up to date immediately.
a.resume()
b.resume()
return B
}