-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.js
615 lines (495 loc) · 15.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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
const b4a = require('b4a')
const Hypercore = require('hypercore')
const ReadyResource = require('ready-resource')
const sodium = require('sodium-universal')
const crypto = require('hypercore-crypto')
const ID = require('hypercore-id-encoding')
const { STORAGE_EMPTY } = require('hypercore-errors')
const auditStore = require('./lib/audit.js')
const [NS] = crypto.namespace('corestore', 1)
const DEFAULT_NAMESPACE = b4a.alloc(32) // This is meant to be 32 0-bytes
class StreamTracker {
constructor () {
this.records = []
}
add (stream, isExternal) {
const record = { index: 0, stream, isExternal }
record.index = this.records.push(record) - 1
return record
}
remove (record) {
const popped = this.records.pop()
if (popped === record) return
this.records[(popped.index = record.index)] = popped
}
attachAll (core) {
for (let i = 0; i < this.records.length; i++) {
const record = this.records[i]
const muxer = record.stream.noiseStream.userData
if (!core.replicator.attached(muxer)) core.replicator.attachTo(muxer)
}
}
destroy () {
// reverse is safer cause we delete mb
for (let i = this.records.length - 1; i >= 0; i--) {
const record = this.records[i]
if (!record.isExternal) record.stream.destroy()
}
}
}
class SessionTracker {
constructor () {
this.map = new Map()
}
get size () {
return this.map.size
}
get (id) {
const existing = this.map.get(id)
if (existing !== undefined) return existing
const fresh = []
this.map.set(id, fresh)
return fresh
}
gc (id) {
this.map.delete(id)
}
list (id) {
return id ? (this.map.get(id) || []) : [...this]
}
* [Symbol.iterator] () {
for (const sessions of this.map.values()) {
yield * sessions[Symbol.iterator]()
}
}
}
class CoreTracker {
constructor () {
this.map = new Map()
this.watching = []
this._gcing = new Set()
this._gcInterval = null
this._gcCycleBound = this._gcCycle.bind(this)
}
get size () {
return this.map.size
}
watch (store) {
if (store.watchIndex !== -1) return
store.watchIndex = this.watching.push(store) - 1
}
unwatch (store) {
if (store.watchIndex === -1) return
const head = this.watching.pop()
if (head !== store) this.watching[(head.watchIndex = store.watchIndex)] = head
store.watchIndex = -1
}
resume (id) {
const core = this.map.get(id)
if (!core) return null
// signal back that we have a closing one stored
if (core.closing) return core
if (core.gc) {
this._gcing.delete(core)
if (this._gcing.size === 0) this._stopGC()
core.gc = 0
}
return core
}
opened (id) {
const core = this.map.get(id)
return !!(core && core.opened && !core.closing)
}
get (id) {
// we allow you do call this from the outside, so support normal buffers also
if (b4a.isBuffer(id)) id = b4a.toString(id, 'hex')
const core = this.map.get(id)
if (!core || core.closing) return null
return core
}
set (id, core) {
this.map.set(id, core)
if (this.watching.length > 0) this._emit(core)
}
_emit (core) {
for (let i = this.watching.length - 1; i >= 0; i--) {
const store = this.watching[i]
for (const fn of store.watchers) fn(core)
}
}
_gc (core) {
const id = toHex(core.discoveryKey)
if (this.map.get(id) === core) this.map.delete(id)
}
_gcCycle () {
for (const core of this._gcing) {
if (++core.gc < 4) continue
const gc = this._gc.bind(this, core)
core.close().then(gc, gc)
this._gcing.delete(core)
}
if (this._gcing.size === 0) this._stopGC()
}
gc (core) {
core.gc = 1 // first strike
this._gcing.add(core)
if (this._gcing.size === 1) this._startGC()
}
_stopGC () {
clearInterval(this._gcInterval)
this._gcInterval = null
}
_startGC () {
if (this._gcInterval) return
this._gcInterval = setInterval(this._gcCycleBound, 2000)
if (this._gcInterval.unref) this._gcInterval.unref()
}
close () {
this._stopGC()
this._gcing.clear()
const all = []
for (const core of this.map.values()) {
core.onidle = noop // no reentry
all.push(core.close())
}
this.map.clear()
return Promise.all(all)
}
* [Symbol.iterator] () {
for (const core of this.map.values()) {
if (!core.closing) yield core
}
}
}
class FindingPeers {
constructor () {
this.count = 0
this.pending = []
}
add (core) {
if (this.count === 0) return
this.pending.push(core.findingPeers())
}
inc (sessions) {
if (++this.count !== 1) return
for (const core of sessions) {
this.pending.push(core.findingPeers())
}
}
dec (sessions) {
if (--this.count !== 0) return
while (this.pending.length > 0) this.pending.pop()()
}
}
class Corestore extends ReadyResource {
constructor (storage, opts = {}) {
super()
this.root = opts.root || null
this.storage = this.root ? this.root.storage : Hypercore.defaultStorage(storage, { id: opts.id, allowBackup: opts.allowBackup })
this.streamTracker = this.root ? this.root.streamTracker : new StreamTracker()
this.cores = this.root ? this.root.cores : new CoreTracker()
this.sessions = new SessionTracker()
this.corestores = this.root ? this.root.corestores : new Set()
this.readOnly = opts.writable === false
this.globalCache = this.root ? this.root.globalCache : (opts.globalCache || null)
this.primaryKey = this.root ? this.root.primaryKey : (opts.primaryKey || null)
this.ns = opts.namespace || DEFAULT_NAMESPACE
this.manifestVersion = opts.manifestVersion || 1
this.watchers = null
this.watchIndex = -1
this._findingPeers = null // here for legacy
this._ongcBound = this._ongc.bind(this)
if (this.root) this.corestores.add(this)
this.ready().catch(noop)
}
watch (fn) {
if (this.watchers === null) {
this.watchers = new Set()
this.cores.watch(this)
}
this.watchers.add(fn)
}
unwatch (fn) {
if (this.watchers === null) return
this.watchers.delete(fn)
if (this.watchers.size === 0) {
this.watchers = null
this.cores.unwatch(this)
}
}
findingPeers () {
if (this._findingPeers === null) this._findingPeers = new FindingPeers()
this._findingPeers.inc(this.sessions)
let done = false
return () => {
if (done) return
done = true
this._findingPeers.dec(this.sessions)
}
}
audit (opts = {}) {
return auditStore(this, opts)
}
async suspend ({ log = noop } = {}) {
log('Flushing db...')
await this.storage.db.flush()
log('Suspending db...')
await this.storage.db.suspend()
log('Corestore suspended')
}
resume () {
return this.storage.db.resume()
}
session (opts) {
this._maybeClosed()
const root = this.root || this
return new Corestore(null, { manifestVersion: this.manifestVersion, ...opts, root })
}
namespace (name, opts) {
return this.session({ ...opts, namespace: generateNamespace(this.ns, name) })
}
list (namespace) {
return this.storage.createDiscoveryKeyStream(namespace)
}
getAuth (discoveryKey) {
return this.storage.getAuth(discoveryKey)
}
_ongc (session) {
if (session.sessions.length === 0) this.sessions.gc(session.id)
}
async _getOrSetSeed () {
const seed = await this.storage.getSeed()
if (seed !== null) return seed
return await this.storage.setSeed(this.primaryKey || crypto.randomBytes(32))
}
async _open () {
if (this.root !== null) {
if (this.root.opened === false) await this.root.ready()
this.primaryKey = this.root.primaryKey
return
}
const primaryKey = await this._getOrSetSeed()
if (this.primaryKey === null) {
this.primaryKey = primaryKey
return
}
if (!b4a.equals(primaryKey, this.primaryKey)) {
throw new Error('Another corestore is stored here')
}
}
async _close () {
const closing = []
const hanging = [...this.sessions]
for (const sess of hanging) closing.push(sess.close())
if (this.watchers !== null) this.cores.unwatch(this)
if (this.root !== null) {
await Promise.all(closing)
return
}
for (const store of this.corestores) {
closing.push(store.close())
}
await Promise.all(closing)
await this.cores.close()
await this.storage.close()
}
async _attachMaybe (muxer, discoveryKey) {
if (this.opened === false) await this.ready()
if (!this.cores.opened(toHex(discoveryKey)) && !(await this.storage.has(discoveryKey, { ifMigrated: true }))) return
if (this.closing) return
const core = this._openCore(discoveryKey, { createIfMissing: false })
if (!core) return
if (!core.opened) await core.ready()
if (!core.replicator.attached(muxer)) {
core.replicator.attachTo(muxer)
}
core.checkIfIdle()
}
replicate (isInitiator, opts) {
this._maybeClosed()
const isExternal = isStream(isInitiator)
const stream = Hypercore.createProtocolStream(isInitiator, {
...opts,
ondiscoverykey: discoveryKey => {
if (this.closing) return
const muxer = stream.noiseStream.userData
return this._attachMaybe(muxer, discoveryKey)
}
})
if (this.cores.size > 0) {
const muxer = stream.noiseStream.userData
const uncork = muxer.uncork.bind(muxer)
muxer.cork()
for (const core of this.cores) {
if (!core.replicator.downloading || core.replicator.attached(muxer) || !core.opened) continue
core.replicator.attachTo(muxer)
}
stream.noiseStream.opened.then(uncork)
}
const record = this.streamTracker.add(stream, isExternal)
stream.once('close', () => this.streamTracker.remove(record))
return stream
}
_maybeClosed () {
if (this.closing || (this.root !== null && this.root.closing)) {
throw new Error('Corestore is closed')
}
}
get (opts) {
this._maybeClosed()
if (b4a.isBuffer(opts) || typeof opts === 'string') opts = { key: opts }
if (!opts) opts = {}
const conf = {
preload: null,
sessions: null,
ongc: null,
core: null,
active: opts.active !== false,
encryption: opts.encryption || null,
encryptionKey: opts.encryptionKey || null, // back compat, should remove
isBlockKey: !!opts.isBlockKey, // back compat, should remove
valueEncoding: opts.valueEncoding || null,
exclusive: !!opts.exclusive,
manifest: opts.manifest || null,
keyPair: opts.keyPair || null,
onwait: opts.onwait || null,
wait: opts.wait !== false,
timeout: opts.timeout || 0,
draft: !!opts.draft,
writable: opts.writable === undefined && this.readOnly ? false : opts.writable
}
// name requires us to rt to storage + ready, so needs preload
// same goes if user has defined async preload obvs
if (opts.name || opts.preload) {
conf.preload = this._preload(opts)
return this._makeSession(conf)
}
if (opts.discoveryKey && !opts.key && !opts.manifest) {
conf.preload = this._preloadCheckIfExists(opts)
return this._makeSession(conf)
}
// if not not we can sync create it, which just is easier for the
// upstream user in terms of guarantees (key is there etc etc)
const core = this._openCore(null, opts)
conf.core = core
conf.sessions = this.sessions.get(core.id)
conf.ongc = this._ongcBound
return this._makeSession(conf)
}
_makeSession (conf) {
const session = new Hypercore(null, null, conf)
if (this._findingPeers !== null) this._findingPeers.add(session)
return session
}
async createKeyPair (name, ns = this.ns) {
if (this.opened === false) await this.ready()
return createKeyPair(this.primaryKey, ns, name)
}
async _preloadCheckIfExists (opts) {
const has = await this.storage.has(opts.discoveryKey)
if (!has) throw STORAGE_EMPTY('No Hypercore is stored here')
return this._preload(opts)
}
async _preload (opts) {
if (opts.preload) opts = { ...opts, ...(await opts.preload) }
if (this.opened === false) await this.ready()
const discoveryKey = opts.name ? await this.storage.getAlias({ name: opts.name, namespace: this.ns }) : null
this._maybeClosed()
const core = this._openCore(discoveryKey, opts)
return {
core,
sessions: this.sessions.get(core.id),
ongc: this._ongcBound,
encryption: opts.encryption || null,
encryptionKey: opts.encryptionKey || null, // back compat, should remove
isBlockKey: !!opts.isBlockKey // back compat, should remove
}
}
_auth (discoveryKey, opts) {
const result = {
keyPair: null,
key: null,
discoveryKey,
manifest: null
}
if (opts.name) {
result.keyPair = createKeyPair(this.primaryKey, this.ns, opts.name)
} else if (opts.keyPair) {
result.keyPair = opts.keyPair
}
if (opts.manifest) {
result.manifest = opts.manifest
} else if (result.keyPair && !result.discoveryKey) {
result.manifest = { version: this.manifestVersion, signers: [{ publicKey: result.keyPair.publicKey }] }
}
if (opts.key) result.key = ID.decode(opts.key)
else if (result.manifest) result.key = Hypercore.key(result.manifest)
if (result.discoveryKey) return result
if (opts.discoveryKey) result.discoveryKey = ID.decode(opts.discoveryKey)
else if (result.key) result.discoveryKey = crypto.discoveryKey(result.key)
else throw new Error('Could not derive discovery from input')
return result
}
_openCore (discoveryKey, opts) {
const auth = this._auth(discoveryKey, opts)
const id = toHex(auth.discoveryKey)
const existing = this.cores.resume(id)
if (existing && !existing.closing) return existing
const core = Hypercore.createCore(this.storage, {
preopen: (existing && existing.opened) ? existing.closing : null, // always wait for the prev one to close first in any case...
eagerUpgrade: true,
notDownloadingLinger: opts.notDownloadingLinger,
allowFork: opts.allowFork !== false,
inflightRange: opts.inflightRange,
compat: false, // no compat for now :)
force: opts.force,
createIfMissing: opts.createIfMissing,
discoveryKey: auth.discoveryKey,
overwrite: opts.overwrite,
key: auth.key,
keyPair: auth.keyPair,
legacy: opts.legacy,
manifest: auth.manifest,
globalCache: opts.globalCache || this.globalCache || null,
alias: opts.name ? { name: opts.name, namespace: this.ns } : null
})
core.onidle = () => {
this.cores.gc(core)
}
core.replicator.ondownloading = () => {
this.streamTracker.attachAll(core)
}
this.cores.set(id, core)
return core
}
}
module.exports = Corestore
function isStream (s) {
return typeof s === 'object' && s && typeof s.pipe === 'function'
}
function generateNamespace (namespace, name) {
if (!b4a.isBuffer(name)) name = b4a.from(name)
const out = b4a.allocUnsafeSlow(32)
sodium.crypto_generichash_batch(out, [namespace, name])
return out
}
function deriveSeed (primaryKey, namespace, name) {
if (!b4a.isBuffer(name)) name = b4a.from(name)
const out = b4a.alloc(32)
sodium.crypto_generichash_batch(out, [NS, namespace, name], primaryKey)
return out
}
function createKeyPair (primaryKey, namespace, name) {
const seed = deriveSeed(primaryKey, namespace, name)
const buf = b4a.alloc(sodium.crypto_sign_PUBLICKEYBYTES + sodium.crypto_sign_SECRETKEYBYTES)
const keyPair = {
publicKey: buf.subarray(0, sodium.crypto_sign_PUBLICKEYBYTES),
secretKey: buf.subarray(sodium.crypto_sign_PUBLICKEYBYTES)
}
sodium.crypto_sign_seed_keypair(keyPair.publicKey, keyPair.secretKey, seed)
return keyPair
}
function noop () {}
function toHex (discoveryKey) {
return b4a.toString(discoveryKey, 'hex')
}