forked from hypercore-protocol/hypertrie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
354 lines (286 loc) · 9.86 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
346
347
348
349
350
351
352
353
354
const events = require('events')
const mutexify = require('mutexify')
const thunky = require('thunky')
const codecs = require('codecs')
const bulk = require('bulk-write-stream')
const toStream = require('nanoiterator/to-stream')
const isOptions = require('is-options')
const hypercore = require('hypercore')
const inherits = require('inherits')
const alru = require('array-lru')
const set = require('unordered-set')
const Extension = require('./lib/extension')
const Node = require('./lib/node')
const Get = require('./lib/get')
const Put = require('./lib/put')
const Batch = require('./lib/batch')
const Delete = require('./lib/del')
const History = require('./lib/history')
const Iterator = require('./lib/iterator')
const Watch = require('./lib/watch')
const Diff = require('./lib/diff')
const { Header } = require('./lib/messages')
module.exports = HyperTrie
function HyperTrie (storage, key, opts) {
if (!(this instanceof HyperTrie)) return new HyperTrie(storage, key, opts)
if (isOptions(key)) {
opts = key
key = null
}
if (!opts) opts = {}
events.EventEmitter.call(this)
this.id = null
this.key = null
this.discoveryKey = null
this.secretKey = null
this.metadata = opts.metadata || null
this.hash = opts.hash || null
this.valueEncoding = opts.valueEncoding ? codecs(opts.valueEncoding) : null
this.alwaysUpdate = !!opts.alwaysUpdate
this.alwaysReconnect = !!opts.alwaysReconnect
this.subtype = opts.subtype
const feedOpts = Object.assign({}, opts, { valueEncoding: 'binary' })
this.feed = opts.feed || hypercore(storage, key, feedOpts)
this.feed.maxRequests = opts.maxRequests || 256 // set max requests higher since the payload is small
this.opened = false
this.ready = thunky(this._ready.bind(this))
this._extension = opts.extension === false ? null : ((opts.extension === true ? null : opts.extension) || new Extension(this))
if (this._extension && !this._extension.outgoing) this._extension.outgoing = this.feed.registerExtension('hypertrie', this._extension)
this._watchers = []
this._checkout = (opts && opts.checkout) || 0
this._cache = alru((opts && opts.cacheSize) || 32768)
this._lock = mutexify()
if (this.feed !== opts.feed) this.feed.on('error', this._onerror.bind(this))
if (!this._checkout) this.feed.on('append', this._onappend.bind(this))
}
inherits(HyperTrie, events.EventEmitter)
Object.defineProperty(HyperTrie.prototype, 'version', {
enumerable: true,
get: function () {
return this._checkout || this.feed.length
}
})
HyperTrie.prototype._removeWatch = function (w) {
set.remove(this._watchers, w)
}
HyperTrie.prototype._addWatch = function (w) {
const self = this
set.add(this._watchers, w)
if (this._watchers.length > 1 || !this.feed.sparse) return
this.feed.update({ ifAvailable: false }, function loop () {
if (self._watchers.length === 0) return
self.feed.update({ ifAvailable: false }, loop)
})
}
HyperTrie.prototype.reconnect = function (from, opts) {
opts = opts ? Object.assign({}, opts, { reconnect: true }) : { reconnect: true }
return this.diff(from, opts)
}
HyperTrie.prototype._onerror = function (err) {
this.emit('error', err)
}
HyperTrie.prototype._onappend = function () {
for (var i = 0; i < this._watchers.length; i++) {
this._watchers[i].update()
}
this.emit('append')
}
HyperTrie.prototype._ready = function (cb) {
const self = this
this.feed.ready(function (err) {
if (err) return done(err)
if (self.feed.length || !self.feed.writable) return done(null)
self.feed.append(Header.encode({
type: 'hypertrie',
metadata: self.metadata,
subtype: self.subtype
}), done)
function done (err) {
if (err) return cb(err)
if (self._checkout === -1) self._checkout = self.feed.length
self.id = self.feed.id
self.key = self.feed.key
self.discoveryKey = self.feed.discoveryKey
self.secretKey = self.feed.secretKey
self.opened = true
self.emit('ready')
if (self.alwaysReconnect) {
var from = self.feed.length
var active = null
self.feed.on('append', function () {
if (!from) {
from = self.feed.length
return
}
if (active) active.destroy()
self.emit('reconnecting')
const r = active = self.reconnect(from)
active.next(function loop (err, data) {
if (r !== active) return
if (err || !data) {
active = null
from = self.feed.length
if (!err) self.emit('reconnected')
return
}
active.next(loop)
})
})
}
cb(null)
}
})
}
HyperTrie.getMetadata = function (feed, cb) {
feed.get(0, (err, msg) => {
if (err) return cb(err)
try {
var header = Header.decode(msg)
} catch (err) {
return cb(err)
}
cb(null, header.metadata)
})
}
HyperTrie.prototype.getMetadata = function (cb) {
HyperTrie.getMetadata(this.feed, cb)
}
HyperTrie.prototype.setMetadata = function (metadata) {
// setMetadata can only be called before this.ready is first called.
if (this.feed.length || !this.feed.writable) throw new Error('The metadata must be set before any puts have occurred.')
this.metadata = metadata
}
HyperTrie.prototype.replicate = function (isInitiator, opts) {
return this.feed.replicate(isInitiator, opts)
}
HyperTrie.prototype.checkout = function (version) {
if (version === 0) version = 1
return new HyperTrie(null, null, {
checkout: version || 1,
valueEncoding: this.valueEncoding,
feed: this.feed,
extension: this._extension === null ? false : this._extension
})
}
HyperTrie.prototype.snapshot = function () {
return this.checkout(this.version)
}
HyperTrie.prototype.headSeq = function (opts, cb) {
const self = this
if (!this.opened) return readyAndHeadSeq(this, opts, cb)
if (this._checkout !== 0) return process.nextTick(cb, null, this._checkout - 1)
if (this.alwaysUpdate && (!opts || opts.wait !== false)) this.feed.update({ hash: false, ifAvailable: true }, onupdated)
else process.nextTick(onupdated)
function onupdated () {
if (self.feed.length < 2) return cb(null, 0)
cb(null, self.feed.length - 1)
}
}
HyperTrie.prototype.head = function (opts, cb) {
if (typeof opts === 'function') return this.head(null, opts)
const self = this
this.headSeq(opts, function (err, seq) {
if (err) return cb(err)
if (!seq) return cb(null, null)
self.getBySeq(seq, opts, cb)
})
}
HyperTrie.prototype.list = function (prefix, opts, cb) {
if (typeof prefix === 'function') return this.list('', null, prefix)
if (typeof opts === 'function') return this.list(prefix, null, opts)
const ite = this.iterator(prefix, opts)
const res = []
ite.next(function loop (err, node) {
if (err) return cb(err)
if (!node) return cb(null, res)
res.push(node)
ite.next(loop)
})
}
HyperTrie.prototype.iterator = function (prefix, opts) {
if (isOptions(prefix)) return this.iterator('', prefix)
return new Iterator(this, prefix, opts)
}
HyperTrie.prototype.createReadStream = function (prefix, opts) {
return toStream(this.iterator(prefix, opts))
}
HyperTrie.prototype.history = function (opts) {
return new History(this, opts)
}
HyperTrie.prototype.createHistoryStream = function (opts) {
return toStream(this.history(opts))
}
HyperTrie.prototype.diff = function (other, prefix, opts) {
if (Buffer.isBuffer(other)) return this.diff(0, prefix, Object.assign(opts || {}, { checkpoint: other }))
if (isOptions(prefix)) return this.diff(other, null, prefix)
const checkout = (typeof other === 'number' || !other) ? this.checkout(other) : other
return new Diff(this, checkout, prefix, opts)
}
HyperTrie.prototype.createDiffStream = function (other, prefix, opts) {
return toStream(this.diff(other, prefix, opts))
}
HyperTrie.prototype.get = function (key, opts, cb) {
if (typeof opts === 'function') return this.get(key, null, opts)
return new Get(this, key, opts, cb)
}
HyperTrie.prototype.watch = function (key, onchange) {
if (typeof key === 'function') return this.watch('', key)
return new Watch(this, key, onchange)
}
HyperTrie.prototype.batch = function (ops, cb) {
return new Batch(this, ops, cb || noop)
}
HyperTrie.prototype.put = function (key, value, opts, cb) {
if (typeof opts === 'function') return this.put(key, value, null, opts)
opts = Object.assign({}, opts, {
batch: null,
del: 0
})
return new Put(this, key, value, opts, cb || noop)
}
HyperTrie.prototype.del = function (key, opts, cb) {
if (typeof opts === 'function') return this.del(key, null, opts)
opts = Object.assign({}, opts, {
batch: null
})
return new Delete(this, key, opts, cb)
}
HyperTrie.prototype.createWriteStream = function (opts) {
const self = this
return bulk.obj(write)
function write (batch, cb) {
if (batch.length && Array.isArray(batch[0])) batch = flatten(batch)
self.batch(batch, cb)
}
}
HyperTrie.prototype.getBySeq = function (seq, opts, cb) {
if (typeof opts === 'function') return this.getBySeq(seq, null, opts)
if (seq < 1) return process.nextTick(cb, null, null)
const self = this
const cached = this._cache.get(seq)
if (cached) return process.nextTick(onnode, null, cached)
this.feed.get(seq, opts, onnode)
function onnode (err, val) {
if (err) return cb(err)
const node = Node.decode(val, seq, self.valueEncoding, self.hash)
self._cache.set(seq, val)
// early exit for the key: '' nodes we write to reset the db
if (!node.value && !node.key) return cb(null, null)
cb(null, node)
}
}
function noop () {}
function readyAndHeadSeq (self, opts, cb) {
self.ready(function (err) {
if (err) return cb(err)
self.headSeq(opts, cb)
})
}
function flatten (list) {
const result = []
for (var i = 0; i < list.length; i++) {
const next = list[i]
for (var j = 0; j < next.length; j++) result.push(next[j])
}
return result
}