hypercore-storage 1.15.0 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +196 -100
- package/lib/block-dependency-stream.js +13 -13
- package/lib/close-error-stream.js +2 -2
- package/lib/keys.js +1 -1
- package/lib/streams.js +55 -21
- package/lib/tx.js +83 -58
- package/lib/view.js +34 -36
- package/package.json +5 -3
package/index.js
CHANGED
|
@@ -11,12 +11,7 @@ const COLUMN_FAMILY = 'corestore'
|
|
|
11
11
|
|
|
12
12
|
const { store, core } = require('./lib/keys.js')
|
|
13
13
|
|
|
14
|
-
const {
|
|
15
|
-
CorestoreRX,
|
|
16
|
-
CorestoreTX,
|
|
17
|
-
CoreTX,
|
|
18
|
-
CoreRX
|
|
19
|
-
} = require('./lib/tx.js')
|
|
14
|
+
const { CorestoreRX, CorestoreTX, CoreTX, CoreRX } = require('./lib/tx.js')
|
|
20
15
|
|
|
21
16
|
const {
|
|
22
17
|
createCoreStream,
|
|
@@ -32,7 +27,7 @@ const {
|
|
|
32
27
|
const EMPTY = new View()
|
|
33
28
|
|
|
34
29
|
class Atom {
|
|
35
|
-
constructor
|
|
30
|
+
constructor(db) {
|
|
36
31
|
this.db = db
|
|
37
32
|
this.view = new View()
|
|
38
33
|
this.flushedPromise = null
|
|
@@ -40,24 +35,24 @@ class Atom {
|
|
|
40
35
|
this.flushes = []
|
|
41
36
|
}
|
|
42
37
|
|
|
43
|
-
onflush
|
|
38
|
+
onflush(fn) {
|
|
44
39
|
this.flushes.push(fn)
|
|
45
40
|
}
|
|
46
41
|
|
|
47
|
-
flushed
|
|
42
|
+
flushed() {
|
|
48
43
|
if (!this.flushing) return Promise.resolve()
|
|
49
44
|
if (this.flushedPromise !== null) return this.flushedPromise.promise
|
|
50
45
|
this.flushedPromise = rrp()
|
|
51
46
|
return this.flushedPromise.promise
|
|
52
47
|
}
|
|
53
48
|
|
|
54
|
-
_resolve
|
|
49
|
+
_resolve() {
|
|
55
50
|
const f = this.flushedPromise
|
|
56
51
|
this.flushedPromise = null
|
|
57
52
|
f.resolve()
|
|
58
53
|
}
|
|
59
54
|
|
|
60
|
-
async flush
|
|
55
|
+
async flush() {
|
|
61
56
|
if (this.flushing) throw new Error('Atom already flushing')
|
|
62
57
|
this.flushing = true
|
|
63
58
|
|
|
@@ -78,7 +73,7 @@ class Atom {
|
|
|
78
73
|
}
|
|
79
74
|
|
|
80
75
|
class HypercoreStorage {
|
|
81
|
-
constructor
|
|
76
|
+
constructor(store, db, core, view, atom) {
|
|
82
77
|
this.store = store
|
|
83
78
|
this.db = db
|
|
84
79
|
this.core = core
|
|
@@ -88,17 +83,17 @@ class HypercoreStorage {
|
|
|
88
83
|
this.view.readStart()
|
|
89
84
|
}
|
|
90
85
|
|
|
91
|
-
get dependencies
|
|
86
|
+
get dependencies() {
|
|
92
87
|
return this.core.dependencies
|
|
93
88
|
}
|
|
94
89
|
|
|
95
|
-
getDependencyLength
|
|
90
|
+
getDependencyLength() {
|
|
96
91
|
return this.core.dependencies.length
|
|
97
92
|
? this.core.dependencies[this.core.dependencies.length - 1].length
|
|
98
93
|
: -1
|
|
99
94
|
}
|
|
100
95
|
|
|
101
|
-
getDependency
|
|
96
|
+
getDependency(length) {
|
|
102
97
|
for (let i = this.core.dependencies.length - 1; i >= 0; i--) {
|
|
103
98
|
const dep = this.core.dependencies[i]
|
|
104
99
|
if (dep.length < length) return dep
|
|
@@ -107,7 +102,7 @@ class HypercoreStorage {
|
|
|
107
102
|
return null
|
|
108
103
|
}
|
|
109
104
|
|
|
110
|
-
setDependencyHead
|
|
105
|
+
setDependencyHead(dep) {
|
|
111
106
|
const deps = this.core.dependencies
|
|
112
107
|
|
|
113
108
|
for (let i = deps.length - 1; i >= 0; i--) {
|
|
@@ -130,14 +125,16 @@ class HypercoreStorage {
|
|
|
130
125
|
}
|
|
131
126
|
}
|
|
132
127
|
|
|
133
|
-
this.core.dependencies = [
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
128
|
+
this.core.dependencies = [
|
|
129
|
+
{
|
|
130
|
+
dataPointer: dep.dataPointer,
|
|
131
|
+
length: dep.length
|
|
132
|
+
}
|
|
133
|
+
]
|
|
137
134
|
}
|
|
138
135
|
|
|
139
136
|
// TODO: this might have to be async if the dependents have changed, but prop ok for now
|
|
140
|
-
updateDependencyLength
|
|
137
|
+
updateDependencyLength(length, truncated) {
|
|
141
138
|
const deps = this.core.dependencies
|
|
142
139
|
|
|
143
140
|
const i = this.findDependencyIndex(length, truncated)
|
|
@@ -157,7 +154,7 @@ class HypercoreStorage {
|
|
|
157
154
|
}
|
|
158
155
|
}
|
|
159
156
|
|
|
160
|
-
findDependencyIndex
|
|
157
|
+
findDependencyIndex(length, truncated) {
|
|
161
158
|
const deps = this.core.dependencies
|
|
162
159
|
|
|
163
160
|
if (truncated) {
|
|
@@ -175,44 +172,58 @@ class HypercoreStorage {
|
|
|
175
172
|
return -1
|
|
176
173
|
}
|
|
177
174
|
|
|
178
|
-
get snapshotted
|
|
175
|
+
get snapshotted() {
|
|
179
176
|
return this.db._snapshot !== null
|
|
180
177
|
}
|
|
181
178
|
|
|
182
|
-
snapshot
|
|
183
|
-
return new HypercoreStorage(
|
|
179
|
+
snapshot() {
|
|
180
|
+
return new HypercoreStorage(
|
|
181
|
+
this.store,
|
|
182
|
+
this.db.snapshot(),
|
|
183
|
+
this.core,
|
|
184
|
+
this.view.snapshot(),
|
|
185
|
+
this.atom
|
|
186
|
+
)
|
|
184
187
|
}
|
|
185
188
|
|
|
186
|
-
|
|
187
|
-
|
|
189
|
+
compact() {
|
|
190
|
+
return Promise.all([
|
|
191
|
+
this.db.compactRange(core.core(this.core.corePointer), core.core(this.core.corePointer)),
|
|
192
|
+
this.db.compactRange(core.data(this.core.dataPointer), core.data(this.core.dataPointer))
|
|
193
|
+
])
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
atomize(atom) {
|
|
197
|
+
if (this.atom && this.atom !== atom)
|
|
198
|
+
throw new Error('Cannot atomize and atomized session with a new atom')
|
|
188
199
|
return new HypercoreStorage(this.store, this.db.session(), this.core, atom.view, atom)
|
|
189
200
|
}
|
|
190
201
|
|
|
191
|
-
createAtom
|
|
202
|
+
createAtom() {
|
|
192
203
|
return this.store.createAtom()
|
|
193
204
|
}
|
|
194
205
|
|
|
195
|
-
createBlockStream
|
|
206
|
+
createBlockStream(opts) {
|
|
196
207
|
return createBlockStream(this.core, this.db, this.view, opts)
|
|
197
208
|
}
|
|
198
209
|
|
|
199
|
-
createTreeNodeStream
|
|
210
|
+
createTreeNodeStream(opts) {
|
|
200
211
|
return createTreeNodeStream(this.core, this.db, this.view, opts)
|
|
201
212
|
}
|
|
202
213
|
|
|
203
|
-
createBitfieldStream
|
|
214
|
+
createBitfieldStream(opts) {
|
|
204
215
|
return createBitfieldStream(this.core, this.db, this.view, opts)
|
|
205
216
|
}
|
|
206
217
|
|
|
207
|
-
createUserDataStream
|
|
218
|
+
createUserDataStream(opts) {
|
|
208
219
|
return createUserDataStream(this.core, this.db, this.view, opts)
|
|
209
220
|
}
|
|
210
221
|
|
|
211
|
-
createLocalStream
|
|
222
|
+
createLocalStream(opts) {
|
|
212
223
|
return createLocalStream(this.core, this.db, this.view, opts)
|
|
213
224
|
}
|
|
214
225
|
|
|
215
|
-
async resumeSession
|
|
226
|
+
async resumeSession(name) {
|
|
216
227
|
const rx = this.read()
|
|
217
228
|
const existingSessionsPromise = rx.getSessions()
|
|
218
229
|
|
|
@@ -238,10 +249,16 @@ class HypercoreStorage {
|
|
|
238
249
|
const dependency = await dependencyPromise
|
|
239
250
|
if (dependency) core.dependencies = this._addDependency(dependency)
|
|
240
251
|
|
|
241
|
-
return new HypercoreStorage(
|
|
252
|
+
return new HypercoreStorage(
|
|
253
|
+
this.store,
|
|
254
|
+
this.db.session(),
|
|
255
|
+
core,
|
|
256
|
+
this.atom ? this.view : new View(),
|
|
257
|
+
this.atom
|
|
258
|
+
)
|
|
242
259
|
}
|
|
243
260
|
|
|
244
|
-
async createSession
|
|
261
|
+
async createSession(name, head) {
|
|
245
262
|
const rx = this.read()
|
|
246
263
|
|
|
247
264
|
const existingSessionsPromise = rx.getSessions()
|
|
@@ -249,7 +266,10 @@ class HypercoreStorage {
|
|
|
249
266
|
|
|
250
267
|
rx.tryFlush()
|
|
251
268
|
|
|
252
|
-
const [existingSessions, existingHead] = await Promise.all([
|
|
269
|
+
const [existingSessions, existingHead] = await Promise.all([
|
|
270
|
+
existingSessionsPromise,
|
|
271
|
+
existingHeadPromise
|
|
272
|
+
])
|
|
253
273
|
if (head === null) head = existingHead
|
|
254
274
|
|
|
255
275
|
if (existingHead !== null && head.length > existingHead.length) {
|
|
@@ -272,7 +292,10 @@ class HypercoreStorage {
|
|
|
272
292
|
const core = {
|
|
273
293
|
corePointer: this.core.corePointer,
|
|
274
294
|
dataPointer: session.dataPointer,
|
|
275
|
-
dependencies: this._addDependency({
|
|
295
|
+
dependencies: this._addDependency({
|
|
296
|
+
dataPointer: this.core.dataPointer,
|
|
297
|
+
length
|
|
298
|
+
})
|
|
276
299
|
}
|
|
277
300
|
|
|
278
301
|
const coreTx = new CoreTX(core, this.db, tx.view, tx.changes)
|
|
@@ -289,10 +312,16 @@ class HypercoreStorage {
|
|
|
289
312
|
|
|
290
313
|
await tx.flush()
|
|
291
314
|
|
|
292
|
-
return new HypercoreStorage(
|
|
315
|
+
return new HypercoreStorage(
|
|
316
|
+
this.store,
|
|
317
|
+
this.db.session(),
|
|
318
|
+
core,
|
|
319
|
+
this.atom ? this.view : new View(),
|
|
320
|
+
this.atom
|
|
321
|
+
)
|
|
293
322
|
}
|
|
294
323
|
|
|
295
|
-
async createAtomicSession
|
|
324
|
+
async createAtomicSession(atom, head) {
|
|
296
325
|
const length = head === null ? 0 : head.length
|
|
297
326
|
const core = {
|
|
298
327
|
corePointer: this.core.corePointer,
|
|
@@ -309,7 +338,7 @@ class HypercoreStorage {
|
|
|
309
338
|
return this.atomize(atom)
|
|
310
339
|
}
|
|
311
340
|
|
|
312
|
-
_addDependency
|
|
341
|
+
_addDependency(dep) {
|
|
313
342
|
const deps = []
|
|
314
343
|
|
|
315
344
|
for (let i = 0; i < this.core.dependencies.length; i++) {
|
|
@@ -325,21 +354,24 @@ class HypercoreStorage {
|
|
|
325
354
|
deps.push(d)
|
|
326
355
|
}
|
|
327
356
|
|
|
328
|
-
if (
|
|
357
|
+
if (
|
|
358
|
+
dep !== null &&
|
|
359
|
+
(deps.length === 0 || deps[deps.length - 1].dataPointer !== dep.dataPointer)
|
|
360
|
+
) {
|
|
329
361
|
deps.push(dep)
|
|
330
362
|
}
|
|
331
363
|
return deps
|
|
332
364
|
}
|
|
333
365
|
|
|
334
|
-
read
|
|
366
|
+
read() {
|
|
335
367
|
return new CoreRX(this.core, this.db, this.view)
|
|
336
368
|
}
|
|
337
369
|
|
|
338
|
-
write
|
|
370
|
+
write() {
|
|
339
371
|
return new CoreTX(this.core, this.db, this.atom ? this.view : null, [])
|
|
340
372
|
}
|
|
341
373
|
|
|
342
|
-
close
|
|
374
|
+
close() {
|
|
343
375
|
if (this.view !== null) {
|
|
344
376
|
this.view.readStop()
|
|
345
377
|
this.view = null
|
|
@@ -348,7 +380,7 @@ class HypercoreStorage {
|
|
|
348
380
|
return this.db.close()
|
|
349
381
|
}
|
|
350
382
|
|
|
351
|
-
static async export
|
|
383
|
+
static async export(ptr, db, { batches = false } = {}) {
|
|
352
384
|
const rx = new CoreRX(ptr, db, EMPTY)
|
|
353
385
|
|
|
354
386
|
const core = {
|
|
@@ -364,15 +396,11 @@ class HypercoreStorage {
|
|
|
364
396
|
|
|
365
397
|
rx.tryFlush()
|
|
366
398
|
|
|
367
|
-
const [sessions, head, auth] = await Promise.all([
|
|
368
|
-
sessionsPromise,
|
|
369
|
-
headPromise,
|
|
370
|
-
authPromise
|
|
371
|
-
])
|
|
399
|
+
const [sessions, head, auth] = await Promise.all([sessionsPromise, headPromise, authPromise])
|
|
372
400
|
|
|
373
401
|
core.head = head
|
|
374
402
|
core.auth = { ...auth, keyPair: null }
|
|
375
|
-
if (sessions) core.sessions = sessions.map(s => s.name)
|
|
403
|
+
if (sessions) core.sessions = sessions.map((s) => s.name)
|
|
376
404
|
|
|
377
405
|
const data = []
|
|
378
406
|
|
|
@@ -391,7 +419,7 @@ class HypercoreStorage {
|
|
|
391
419
|
}
|
|
392
420
|
|
|
393
421
|
class CorestoreStorage {
|
|
394
|
-
constructor
|
|
422
|
+
constructor(db, opts = {}) {
|
|
395
423
|
const storage = typeof db === 'string' ? db : null
|
|
396
424
|
|
|
397
425
|
this.bootstrap = storage !== null
|
|
@@ -413,24 +441,24 @@ class CorestoreStorage {
|
|
|
413
441
|
this.migrating = null
|
|
414
442
|
}
|
|
415
443
|
|
|
416
|
-
get opened
|
|
444
|
+
get opened() {
|
|
417
445
|
return this.db.opened
|
|
418
446
|
}
|
|
419
447
|
|
|
420
|
-
get closed
|
|
448
|
+
get closed() {
|
|
421
449
|
return this.db.closed
|
|
422
450
|
}
|
|
423
451
|
|
|
424
|
-
async ready
|
|
452
|
+
async ready() {
|
|
425
453
|
if (this.version === 0) await this._migrateStore()
|
|
426
454
|
return this.db.ready()
|
|
427
455
|
}
|
|
428
456
|
|
|
429
|
-
compact
|
|
457
|
+
compact() {
|
|
430
458
|
return this.db.compactRange()
|
|
431
459
|
}
|
|
432
460
|
|
|
433
|
-
async audit
|
|
461
|
+
async audit() {
|
|
434
462
|
for await (const { core } of this.createCoreStream()) {
|
|
435
463
|
const coreRx = new CoreRX(core, this.db, EMPTY)
|
|
436
464
|
const authPromise = coreRx.getAuth()
|
|
@@ -449,7 +477,7 @@ class CorestoreStorage {
|
|
|
449
477
|
}
|
|
450
478
|
}
|
|
451
479
|
|
|
452
|
-
async deleteCore
|
|
480
|
+
async deleteCore(ptr) {
|
|
453
481
|
const rx = new CoreRX(ptr, this.db, EMPTY)
|
|
454
482
|
|
|
455
483
|
const authPromise = rx.getAuth()
|
|
@@ -483,16 +511,16 @@ class CorestoreStorage {
|
|
|
483
511
|
return tx.flush()
|
|
484
512
|
}
|
|
485
513
|
|
|
486
|
-
static isCoreStorage
|
|
514
|
+
static isCoreStorage(db) {
|
|
487
515
|
return isCorestoreStorage(db)
|
|
488
516
|
}
|
|
489
517
|
|
|
490
|
-
static from
|
|
518
|
+
static from(db) {
|
|
491
519
|
if (isCorestoreStorage(db)) return db
|
|
492
520
|
return new this(db)
|
|
493
521
|
}
|
|
494
522
|
|
|
495
|
-
async _flush
|
|
523
|
+
async _flush() {
|
|
496
524
|
while (this.enters > 0) {
|
|
497
525
|
await this.lock.lock()
|
|
498
526
|
await this.lock.unlock()
|
|
@@ -500,7 +528,7 @@ class CorestoreStorage {
|
|
|
500
528
|
}
|
|
501
529
|
|
|
502
530
|
// runs pre any other mutation and read
|
|
503
|
-
async _migrateStore
|
|
531
|
+
async _migrateStore() {
|
|
504
532
|
const view = await this._enter()
|
|
505
533
|
|
|
506
534
|
try {
|
|
@@ -536,7 +564,9 @@ class CorestoreStorage {
|
|
|
536
564
|
break
|
|
537
565
|
}
|
|
538
566
|
default: {
|
|
539
|
-
throw new Error(
|
|
567
|
+
throw new Error(
|
|
568
|
+
'Unsupported version: ' + version + ' - you should probably upgrade your dependencies'
|
|
569
|
+
)
|
|
540
570
|
}
|
|
541
571
|
}
|
|
542
572
|
|
|
@@ -547,7 +577,7 @@ class CorestoreStorage {
|
|
|
547
577
|
}
|
|
548
578
|
|
|
549
579
|
// runs pre the core is returned to the user
|
|
550
|
-
async _migrateCore
|
|
580
|
+
async _migrateCore(core, discoveryKey, version, locked) {
|
|
551
581
|
const view = locked ? this.view : await this._enter()
|
|
552
582
|
try {
|
|
553
583
|
if (version === VERSION) return
|
|
@@ -560,7 +590,9 @@ class CorestoreStorage {
|
|
|
560
590
|
break
|
|
561
591
|
}
|
|
562
592
|
default: {
|
|
563
|
-
throw new Error(
|
|
593
|
+
throw new Error(
|
|
594
|
+
'Unsupported version: ' + version + ' - you should probably upgrade your dependencies'
|
|
595
|
+
)
|
|
564
596
|
}
|
|
565
597
|
}
|
|
566
598
|
|
|
@@ -582,14 +614,14 @@ class CorestoreStorage {
|
|
|
582
614
|
}
|
|
583
615
|
}
|
|
584
616
|
|
|
585
|
-
async _enter
|
|
617
|
+
async _enter() {
|
|
586
618
|
this.enters++
|
|
587
619
|
await this.lock.lock()
|
|
588
620
|
if (this.view === null) this.view = new View()
|
|
589
621
|
return this.view
|
|
590
622
|
}
|
|
591
623
|
|
|
592
|
-
async _exit
|
|
624
|
+
async _exit() {
|
|
593
625
|
this.enters--
|
|
594
626
|
|
|
595
627
|
if (this.flushing === null) this.flushing = rrp()
|
|
@@ -613,7 +645,7 @@ class CorestoreStorage {
|
|
|
613
645
|
|
|
614
646
|
// when used with core catches this isnt transactional for simplicity, HOWEVER, its just a number
|
|
615
647
|
// so worth the tradeoff
|
|
616
|
-
async _allocData
|
|
648
|
+
async _allocData() {
|
|
617
649
|
let dataPointer = 0
|
|
618
650
|
|
|
619
651
|
const view = await this._enter()
|
|
@@ -634,7 +666,7 @@ class CorestoreStorage {
|
|
|
634
666
|
}
|
|
635
667
|
|
|
636
668
|
// exposes here so migrations can easily access the head in an init state
|
|
637
|
-
async _getHead
|
|
669
|
+
async _getHead(view) {
|
|
638
670
|
const rx = new CorestoreRX(this.db, view)
|
|
639
671
|
const headPromise = rx.getHead()
|
|
640
672
|
rx.tryFlush()
|
|
@@ -643,22 +675,22 @@ class CorestoreStorage {
|
|
|
643
675
|
return head === null ? initStoreHead() : head
|
|
644
676
|
}
|
|
645
677
|
|
|
646
|
-
createAtom
|
|
678
|
+
createAtom() {
|
|
647
679
|
return new Atom(this.db)
|
|
648
680
|
}
|
|
649
681
|
|
|
650
|
-
async flush
|
|
682
|
+
async flush() {
|
|
651
683
|
await this.rocks.flush()
|
|
652
684
|
}
|
|
653
685
|
|
|
654
|
-
async close
|
|
686
|
+
async close() {
|
|
655
687
|
if (this.db.closed) return
|
|
656
688
|
await this._flush()
|
|
657
689
|
await this.db.close()
|
|
658
690
|
await this.rocks.close()
|
|
659
691
|
}
|
|
660
692
|
|
|
661
|
-
async clear
|
|
693
|
+
async clear() {
|
|
662
694
|
if (this.version === 0) await this._migrateStore()
|
|
663
695
|
|
|
664
696
|
const view = await this._enter()
|
|
@@ -670,21 +702,21 @@ class CorestoreStorage {
|
|
|
670
702
|
await this._exit()
|
|
671
703
|
}
|
|
672
704
|
|
|
673
|
-
createCoreStream
|
|
705
|
+
createCoreStream() {
|
|
674
706
|
// TODO: be nice to run the mgiration here also, but too much plumbing atm
|
|
675
707
|
return createCoreStream(this.db, EMPTY)
|
|
676
708
|
}
|
|
677
709
|
|
|
678
|
-
createAliasStream
|
|
710
|
+
createAliasStream(namespace) {
|
|
679
711
|
// TODO: be nice to run the mgiration here also, but too much plumbing atm
|
|
680
712
|
return createAliasStream(this.db, EMPTY, namespace)
|
|
681
713
|
}
|
|
682
714
|
|
|
683
|
-
createDiscoveryKeyStream
|
|
715
|
+
createDiscoveryKeyStream(namespace) {
|
|
684
716
|
return createDiscoveryKeyStream(this.db, EMPTY, namespace)
|
|
685
717
|
}
|
|
686
718
|
|
|
687
|
-
async getAlias
|
|
719
|
+
async getAlias(alias) {
|
|
688
720
|
if (this.version === 0) await this._migrateStore()
|
|
689
721
|
|
|
690
722
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -693,7 +725,7 @@ class CorestoreStorage {
|
|
|
693
725
|
return discoveryKeyPromise
|
|
694
726
|
}
|
|
695
727
|
|
|
696
|
-
async getSeed
|
|
728
|
+
async getSeed() {
|
|
697
729
|
if (this.version === 0) await this._migrateStore()
|
|
698
730
|
|
|
699
731
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -705,7 +737,7 @@ class CorestoreStorage {
|
|
|
705
737
|
return head === null ? null : head.seed
|
|
706
738
|
}
|
|
707
739
|
|
|
708
|
-
async setSeed
|
|
740
|
+
async setSeed(seed, { overwrite = true } = {}) {
|
|
709
741
|
if (this.version === 0) await this._migrateStore()
|
|
710
742
|
|
|
711
743
|
const view = await this._enter()
|
|
@@ -729,7 +761,7 @@ class CorestoreStorage {
|
|
|
729
761
|
}
|
|
730
762
|
}
|
|
731
763
|
|
|
732
|
-
async getDefaultDiscoveryKey
|
|
764
|
+
async getDefaultDiscoveryKey() {
|
|
733
765
|
if (this.version === 0) await this._migrateStore()
|
|
734
766
|
|
|
735
767
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -741,7 +773,7 @@ class CorestoreStorage {
|
|
|
741
773
|
return head === null ? null : head.defaultDiscoveryKey
|
|
742
774
|
}
|
|
743
775
|
|
|
744
|
-
async setDefaultDiscoveryKey
|
|
776
|
+
async setDefaultDiscoveryKey(discoveryKey, { overwrite = true } = {}) {
|
|
745
777
|
if (this.version === 0) await this._migrateStore()
|
|
746
778
|
|
|
747
779
|
const view = await this._enter()
|
|
@@ -765,7 +797,7 @@ class CorestoreStorage {
|
|
|
765
797
|
}
|
|
766
798
|
}
|
|
767
799
|
|
|
768
|
-
async has
|
|
800
|
+
async has(discoveryKey, { ifMigrated = false } = {}) {
|
|
769
801
|
if (this.version === 0) await this._migrateStore()
|
|
770
802
|
|
|
771
803
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -781,7 +813,7 @@ class CorestoreStorage {
|
|
|
781
813
|
return true
|
|
782
814
|
}
|
|
783
815
|
|
|
784
|
-
async getAuth
|
|
816
|
+
async getAuth(discoveryKey) {
|
|
785
817
|
if (this.version === 0) await this._migrateStore()
|
|
786
818
|
|
|
787
819
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -792,15 +824,47 @@ class CorestoreStorage {
|
|
|
792
824
|
const core = await corePromise
|
|
793
825
|
if (core === null) return null
|
|
794
826
|
|
|
795
|
-
const
|
|
796
|
-
const authPromise =
|
|
827
|
+
const read = this.db.read({ autoDestroy: true })
|
|
828
|
+
const authPromise = CoreRX.getAuth(read, core)
|
|
797
829
|
|
|
798
|
-
|
|
830
|
+
read.tryFlush()
|
|
799
831
|
|
|
800
832
|
return authPromise
|
|
801
833
|
}
|
|
802
834
|
|
|
803
|
-
async
|
|
835
|
+
async getInfo(discoveryKey, opts) {
|
|
836
|
+
return (await this.getInfos([discoveryKey], opts))[0]
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
async getInfos(discoveryKeys, { auth = true, head = true, hints = true } = {}) {
|
|
840
|
+
if (this.version === 0) await this._migrateStore()
|
|
841
|
+
|
|
842
|
+
const rx = new CorestoreRX(this.db, EMPTY)
|
|
843
|
+
const corePromises = new Array(discoveryKeys.length)
|
|
844
|
+
|
|
845
|
+
for (let i = 0; i < discoveryKeys.length; i++) {
|
|
846
|
+
corePromises[i] = rx.getCore(discoveryKeys[i])
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
rx.tryFlush()
|
|
850
|
+
|
|
851
|
+
const cores = await Promise.all(corePromises)
|
|
852
|
+
const read = this.db.read({ autoDestroy: true })
|
|
853
|
+
|
|
854
|
+
const resultPromises = new Array(cores.length)
|
|
855
|
+
|
|
856
|
+
for (let i = 0; i < cores.length; i++) {
|
|
857
|
+
resultPromises[i] = cores[i]
|
|
858
|
+
? getInfoFromBatch(read, cores[i], discoveryKeys[i], auth, head, hints)
|
|
859
|
+
: null
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
read.tryFlush()
|
|
863
|
+
|
|
864
|
+
return Promise.all(resultPromises)
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
async resume(discoveryKey) {
|
|
804
868
|
if (this.version === 0) await this._migrateStore()
|
|
805
869
|
|
|
806
870
|
if (!discoveryKey) {
|
|
@@ -818,7 +882,7 @@ class CorestoreStorage {
|
|
|
818
882
|
return this._resumeFromPointers(EMPTY, discoveryKey, false, core)
|
|
819
883
|
}
|
|
820
884
|
|
|
821
|
-
async export
|
|
885
|
+
async export(discoveryKey, opts) {
|
|
822
886
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
823
887
|
const corePromise = rx.getCore(discoveryKey)
|
|
824
888
|
|
|
@@ -848,7 +912,7 @@ class CorestoreStorage {
|
|
|
848
912
|
}
|
|
849
913
|
}
|
|
850
914
|
|
|
851
|
-
async _resumeFromPointers
|
|
915
|
+
async _resumeFromPointers(view, discoveryKey, create, { version, corePointer, dataPointer }) {
|
|
852
916
|
const core = { corePointer, dataPointer, dependencies: [] }
|
|
853
917
|
|
|
854
918
|
while (true) {
|
|
@@ -868,7 +932,7 @@ class CorestoreStorage {
|
|
|
868
932
|
}
|
|
869
933
|
|
|
870
934
|
// not allowed to throw validation errors as its a shared tx!
|
|
871
|
-
async _create
|
|
935
|
+
async _create(view, { key, manifest, keyPair, encryptionKey, discoveryKey, alias, userData }) {
|
|
872
936
|
const rx = new CorestoreRX(this.db, view)
|
|
873
937
|
const tx = new CorestoreTX(view)
|
|
874
938
|
|
|
@@ -914,7 +978,7 @@ class CorestoreStorage {
|
|
|
914
978
|
return new HypercoreStorage(this, this.db.session(), ptr, EMPTY, null)
|
|
915
979
|
}
|
|
916
980
|
|
|
917
|
-
async create
|
|
981
|
+
async create(data) {
|
|
918
982
|
if (this.version === 0) await this._migrateStore()
|
|
919
983
|
|
|
920
984
|
const view = await this._enter()
|
|
@@ -929,7 +993,7 @@ class CorestoreStorage {
|
|
|
929
993
|
|
|
930
994
|
module.exports = CorestoreStorage
|
|
931
995
|
|
|
932
|
-
function initStoreHead
|
|
996
|
+
function initStoreHead() {
|
|
933
997
|
return {
|
|
934
998
|
version: 0, // cause we wanna run the migration
|
|
935
999
|
allocated: {
|
|
@@ -941,7 +1005,7 @@ function initStoreHead () {
|
|
|
941
1005
|
}
|
|
942
1006
|
}
|
|
943
1007
|
|
|
944
|
-
function getBatch
|
|
1008
|
+
function getBatch(sessions, name, alloc) {
|
|
945
1009
|
for (let i = 0; i < sessions.length; i++) {
|
|
946
1010
|
if (sessions[i].name === name) return sessions[i]
|
|
947
1011
|
}
|
|
@@ -953,11 +1017,11 @@ function getBatch (sessions, name, alloc) {
|
|
|
953
1017
|
return result
|
|
954
1018
|
}
|
|
955
1019
|
|
|
956
|
-
function isCorestoreStorage
|
|
1020
|
+
function isCorestoreStorage(s) {
|
|
957
1021
|
return typeof s === 'object' && !!s && typeof s.setDefaultDiscoveryKey === 'function'
|
|
958
1022
|
}
|
|
959
1023
|
|
|
960
|
-
function createColumnFamily
|
|
1024
|
+
function createColumnFamily(db, opts = {}) {
|
|
961
1025
|
const {
|
|
962
1026
|
tableCacheIndexAndFilterBlocks = true,
|
|
963
1027
|
blockCache = true,
|
|
@@ -980,7 +1044,7 @@ function createColumnFamily (db, opts = {}) {
|
|
|
980
1044
|
}
|
|
981
1045
|
|
|
982
1046
|
// TODO: remove in like 3-6 mo
|
|
983
|
-
function tmpFixStorage
|
|
1047
|
+
function tmpFixStorage(p) {
|
|
984
1048
|
// if CORESTORE file is written, new format
|
|
985
1049
|
if (fs.existsSync(path.join(p, 'CORESTORE'))) return
|
|
986
1050
|
|
|
@@ -990,7 +1054,18 @@ function tmpFixStorage (p) {
|
|
|
990
1054
|
files = fs.readdirSync(p)
|
|
991
1055
|
} catch {}
|
|
992
1056
|
|
|
993
|
-
const notRocks = new Set([
|
|
1057
|
+
const notRocks = new Set([
|
|
1058
|
+
'CORESTORE',
|
|
1059
|
+
'primary-key',
|
|
1060
|
+
'cores',
|
|
1061
|
+
'app-preferences',
|
|
1062
|
+
'cache',
|
|
1063
|
+
'preferences.json',
|
|
1064
|
+
'db',
|
|
1065
|
+
'clone',
|
|
1066
|
+
'core',
|
|
1067
|
+
'notifications'
|
|
1068
|
+
])
|
|
994
1069
|
|
|
995
1070
|
for (const f of files) {
|
|
996
1071
|
if (notRocks.has(f)) continue
|
|
@@ -1003,7 +1078,7 @@ function tmpFixStorage (p) {
|
|
|
1003
1078
|
}
|
|
1004
1079
|
}
|
|
1005
1080
|
|
|
1006
|
-
async function exportData
|
|
1081
|
+
async function exportData(ptr, db, opts) {
|
|
1007
1082
|
// just need dataPointer
|
|
1008
1083
|
const reads = [
|
|
1009
1084
|
toArray(createBlockStream(ptr, db, EMPTY, opts)),
|
|
@@ -1020,8 +1095,29 @@ async function exportData (ptr, db, opts) {
|
|
|
1020
1095
|
}
|
|
1021
1096
|
}
|
|
1022
1097
|
|
|
1023
|
-
async function toArray
|
|
1098
|
+
async function toArray(stream) {
|
|
1024
1099
|
const all = []
|
|
1025
1100
|
for await (const e of stream) all.push(e)
|
|
1026
1101
|
return all
|
|
1027
1102
|
}
|
|
1103
|
+
|
|
1104
|
+
function noop() {}
|
|
1105
|
+
|
|
1106
|
+
async function getInfoFromBatch(db, c, discoveryKey, getAuth, getHead, getHints) {
|
|
1107
|
+
const authPromise = getAuth ? CoreRX.getAuth(db, c) : null
|
|
1108
|
+
const headPromise = getHead ? CoreRX.getHead(db, c) : null
|
|
1109
|
+
const hintsPromise = getHints ? CoreRX.getHints(db, c) : null
|
|
1110
|
+
|
|
1111
|
+
// ensure no uncaughts
|
|
1112
|
+
if (authPromise) authPromise.catch(noop)
|
|
1113
|
+
if (headPromise) headPromise.catch(noop)
|
|
1114
|
+
if (hintsPromise) hintsPromise.catch(noop)
|
|
1115
|
+
|
|
1116
|
+
return {
|
|
1117
|
+
discoveryKey,
|
|
1118
|
+
core: c,
|
|
1119
|
+
auth: await authPromise,
|
|
1120
|
+
head: await headPromise,
|
|
1121
|
+
hints: await hintsPromise
|
|
1122
|
+
}
|
|
1123
|
+
}
|