hypercore-storage 1.16.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 +190 -101
- 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,51 +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
|
-
compact
|
|
189
|
+
compact() {
|
|
187
190
|
return Promise.all([
|
|
188
191
|
this.db.compactRange(core.core(this.core.corePointer), core.core(this.core.corePointer)),
|
|
189
192
|
this.db.compactRange(core.data(this.core.dataPointer), core.data(this.core.dataPointer))
|
|
190
193
|
])
|
|
191
194
|
}
|
|
192
195
|
|
|
193
|
-
atomize
|
|
194
|
-
if (this.atom && this.atom !== atom)
|
|
196
|
+
atomize(atom) {
|
|
197
|
+
if (this.atom && this.atom !== atom)
|
|
198
|
+
throw new Error('Cannot atomize and atomized session with a new atom')
|
|
195
199
|
return new HypercoreStorage(this.store, this.db.session(), this.core, atom.view, atom)
|
|
196
200
|
}
|
|
197
201
|
|
|
198
|
-
createAtom
|
|
202
|
+
createAtom() {
|
|
199
203
|
return this.store.createAtom()
|
|
200
204
|
}
|
|
201
205
|
|
|
202
|
-
createBlockStream
|
|
206
|
+
createBlockStream(opts) {
|
|
203
207
|
return createBlockStream(this.core, this.db, this.view, opts)
|
|
204
208
|
}
|
|
205
209
|
|
|
206
|
-
createTreeNodeStream
|
|
210
|
+
createTreeNodeStream(opts) {
|
|
207
211
|
return createTreeNodeStream(this.core, this.db, this.view, opts)
|
|
208
212
|
}
|
|
209
213
|
|
|
210
|
-
createBitfieldStream
|
|
214
|
+
createBitfieldStream(opts) {
|
|
211
215
|
return createBitfieldStream(this.core, this.db, this.view, opts)
|
|
212
216
|
}
|
|
213
217
|
|
|
214
|
-
createUserDataStream
|
|
218
|
+
createUserDataStream(opts) {
|
|
215
219
|
return createUserDataStream(this.core, this.db, this.view, opts)
|
|
216
220
|
}
|
|
217
221
|
|
|
218
|
-
createLocalStream
|
|
222
|
+
createLocalStream(opts) {
|
|
219
223
|
return createLocalStream(this.core, this.db, this.view, opts)
|
|
220
224
|
}
|
|
221
225
|
|
|
222
|
-
async resumeSession
|
|
226
|
+
async resumeSession(name) {
|
|
223
227
|
const rx = this.read()
|
|
224
228
|
const existingSessionsPromise = rx.getSessions()
|
|
225
229
|
|
|
@@ -245,10 +249,16 @@ class HypercoreStorage {
|
|
|
245
249
|
const dependency = await dependencyPromise
|
|
246
250
|
if (dependency) core.dependencies = this._addDependency(dependency)
|
|
247
251
|
|
|
248
|
-
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
|
+
)
|
|
249
259
|
}
|
|
250
260
|
|
|
251
|
-
async createSession
|
|
261
|
+
async createSession(name, head) {
|
|
252
262
|
const rx = this.read()
|
|
253
263
|
|
|
254
264
|
const existingSessionsPromise = rx.getSessions()
|
|
@@ -256,7 +266,10 @@ class HypercoreStorage {
|
|
|
256
266
|
|
|
257
267
|
rx.tryFlush()
|
|
258
268
|
|
|
259
|
-
const [existingSessions, existingHead] = await Promise.all([
|
|
269
|
+
const [existingSessions, existingHead] = await Promise.all([
|
|
270
|
+
existingSessionsPromise,
|
|
271
|
+
existingHeadPromise
|
|
272
|
+
])
|
|
260
273
|
if (head === null) head = existingHead
|
|
261
274
|
|
|
262
275
|
if (existingHead !== null && head.length > existingHead.length) {
|
|
@@ -279,7 +292,10 @@ class HypercoreStorage {
|
|
|
279
292
|
const core = {
|
|
280
293
|
corePointer: this.core.corePointer,
|
|
281
294
|
dataPointer: session.dataPointer,
|
|
282
|
-
dependencies: this._addDependency({
|
|
295
|
+
dependencies: this._addDependency({
|
|
296
|
+
dataPointer: this.core.dataPointer,
|
|
297
|
+
length
|
|
298
|
+
})
|
|
283
299
|
}
|
|
284
300
|
|
|
285
301
|
const coreTx = new CoreTX(core, this.db, tx.view, tx.changes)
|
|
@@ -296,10 +312,16 @@ class HypercoreStorage {
|
|
|
296
312
|
|
|
297
313
|
await tx.flush()
|
|
298
314
|
|
|
299
|
-
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
|
+
)
|
|
300
322
|
}
|
|
301
323
|
|
|
302
|
-
async createAtomicSession
|
|
324
|
+
async createAtomicSession(atom, head) {
|
|
303
325
|
const length = head === null ? 0 : head.length
|
|
304
326
|
const core = {
|
|
305
327
|
corePointer: this.core.corePointer,
|
|
@@ -316,7 +338,7 @@ class HypercoreStorage {
|
|
|
316
338
|
return this.atomize(atom)
|
|
317
339
|
}
|
|
318
340
|
|
|
319
|
-
_addDependency
|
|
341
|
+
_addDependency(dep) {
|
|
320
342
|
const deps = []
|
|
321
343
|
|
|
322
344
|
for (let i = 0; i < this.core.dependencies.length; i++) {
|
|
@@ -332,21 +354,24 @@ class HypercoreStorage {
|
|
|
332
354
|
deps.push(d)
|
|
333
355
|
}
|
|
334
356
|
|
|
335
|
-
if (
|
|
357
|
+
if (
|
|
358
|
+
dep !== null &&
|
|
359
|
+
(deps.length === 0 || deps[deps.length - 1].dataPointer !== dep.dataPointer)
|
|
360
|
+
) {
|
|
336
361
|
deps.push(dep)
|
|
337
362
|
}
|
|
338
363
|
return deps
|
|
339
364
|
}
|
|
340
365
|
|
|
341
|
-
read
|
|
366
|
+
read() {
|
|
342
367
|
return new CoreRX(this.core, this.db, this.view)
|
|
343
368
|
}
|
|
344
369
|
|
|
345
|
-
write
|
|
370
|
+
write() {
|
|
346
371
|
return new CoreTX(this.core, this.db, this.atom ? this.view : null, [])
|
|
347
372
|
}
|
|
348
373
|
|
|
349
|
-
close
|
|
374
|
+
close() {
|
|
350
375
|
if (this.view !== null) {
|
|
351
376
|
this.view.readStop()
|
|
352
377
|
this.view = null
|
|
@@ -355,7 +380,7 @@ class HypercoreStorage {
|
|
|
355
380
|
return this.db.close()
|
|
356
381
|
}
|
|
357
382
|
|
|
358
|
-
static async export
|
|
383
|
+
static async export(ptr, db, { batches = false } = {}) {
|
|
359
384
|
const rx = new CoreRX(ptr, db, EMPTY)
|
|
360
385
|
|
|
361
386
|
const core = {
|
|
@@ -371,15 +396,11 @@ class HypercoreStorage {
|
|
|
371
396
|
|
|
372
397
|
rx.tryFlush()
|
|
373
398
|
|
|
374
|
-
const [sessions, head, auth] = await Promise.all([
|
|
375
|
-
sessionsPromise,
|
|
376
|
-
headPromise,
|
|
377
|
-
authPromise
|
|
378
|
-
])
|
|
399
|
+
const [sessions, head, auth] = await Promise.all([sessionsPromise, headPromise, authPromise])
|
|
379
400
|
|
|
380
401
|
core.head = head
|
|
381
402
|
core.auth = { ...auth, keyPair: null }
|
|
382
|
-
if (sessions) core.sessions = sessions.map(s => s.name)
|
|
403
|
+
if (sessions) core.sessions = sessions.map((s) => s.name)
|
|
383
404
|
|
|
384
405
|
const data = []
|
|
385
406
|
|
|
@@ -398,7 +419,7 @@ class HypercoreStorage {
|
|
|
398
419
|
}
|
|
399
420
|
|
|
400
421
|
class CorestoreStorage {
|
|
401
|
-
constructor
|
|
422
|
+
constructor(db, opts = {}) {
|
|
402
423
|
const storage = typeof db === 'string' ? db : null
|
|
403
424
|
|
|
404
425
|
this.bootstrap = storage !== null
|
|
@@ -420,24 +441,24 @@ class CorestoreStorage {
|
|
|
420
441
|
this.migrating = null
|
|
421
442
|
}
|
|
422
443
|
|
|
423
|
-
get opened
|
|
444
|
+
get opened() {
|
|
424
445
|
return this.db.opened
|
|
425
446
|
}
|
|
426
447
|
|
|
427
|
-
get closed
|
|
448
|
+
get closed() {
|
|
428
449
|
return this.db.closed
|
|
429
450
|
}
|
|
430
451
|
|
|
431
|
-
async ready
|
|
452
|
+
async ready() {
|
|
432
453
|
if (this.version === 0) await this._migrateStore()
|
|
433
454
|
return this.db.ready()
|
|
434
455
|
}
|
|
435
456
|
|
|
436
|
-
compact
|
|
457
|
+
compact() {
|
|
437
458
|
return this.db.compactRange()
|
|
438
459
|
}
|
|
439
460
|
|
|
440
|
-
async audit
|
|
461
|
+
async audit() {
|
|
441
462
|
for await (const { core } of this.createCoreStream()) {
|
|
442
463
|
const coreRx = new CoreRX(core, this.db, EMPTY)
|
|
443
464
|
const authPromise = coreRx.getAuth()
|
|
@@ -456,7 +477,7 @@ class CorestoreStorage {
|
|
|
456
477
|
}
|
|
457
478
|
}
|
|
458
479
|
|
|
459
|
-
async deleteCore
|
|
480
|
+
async deleteCore(ptr) {
|
|
460
481
|
const rx = new CoreRX(ptr, this.db, EMPTY)
|
|
461
482
|
|
|
462
483
|
const authPromise = rx.getAuth()
|
|
@@ -490,16 +511,16 @@ class CorestoreStorage {
|
|
|
490
511
|
return tx.flush()
|
|
491
512
|
}
|
|
492
513
|
|
|
493
|
-
static isCoreStorage
|
|
514
|
+
static isCoreStorage(db) {
|
|
494
515
|
return isCorestoreStorage(db)
|
|
495
516
|
}
|
|
496
517
|
|
|
497
|
-
static from
|
|
518
|
+
static from(db) {
|
|
498
519
|
if (isCorestoreStorage(db)) return db
|
|
499
520
|
return new this(db)
|
|
500
521
|
}
|
|
501
522
|
|
|
502
|
-
async _flush
|
|
523
|
+
async _flush() {
|
|
503
524
|
while (this.enters > 0) {
|
|
504
525
|
await this.lock.lock()
|
|
505
526
|
await this.lock.unlock()
|
|
@@ -507,7 +528,7 @@ class CorestoreStorage {
|
|
|
507
528
|
}
|
|
508
529
|
|
|
509
530
|
// runs pre any other mutation and read
|
|
510
|
-
async _migrateStore
|
|
531
|
+
async _migrateStore() {
|
|
511
532
|
const view = await this._enter()
|
|
512
533
|
|
|
513
534
|
try {
|
|
@@ -543,7 +564,9 @@ class CorestoreStorage {
|
|
|
543
564
|
break
|
|
544
565
|
}
|
|
545
566
|
default: {
|
|
546
|
-
throw new Error(
|
|
567
|
+
throw new Error(
|
|
568
|
+
'Unsupported version: ' + version + ' - you should probably upgrade your dependencies'
|
|
569
|
+
)
|
|
547
570
|
}
|
|
548
571
|
}
|
|
549
572
|
|
|
@@ -554,7 +577,7 @@ class CorestoreStorage {
|
|
|
554
577
|
}
|
|
555
578
|
|
|
556
579
|
// runs pre the core is returned to the user
|
|
557
|
-
async _migrateCore
|
|
580
|
+
async _migrateCore(core, discoveryKey, version, locked) {
|
|
558
581
|
const view = locked ? this.view : await this._enter()
|
|
559
582
|
try {
|
|
560
583
|
if (version === VERSION) return
|
|
@@ -567,7 +590,9 @@ class CorestoreStorage {
|
|
|
567
590
|
break
|
|
568
591
|
}
|
|
569
592
|
default: {
|
|
570
|
-
throw new Error(
|
|
593
|
+
throw new Error(
|
|
594
|
+
'Unsupported version: ' + version + ' - you should probably upgrade your dependencies'
|
|
595
|
+
)
|
|
571
596
|
}
|
|
572
597
|
}
|
|
573
598
|
|
|
@@ -589,14 +614,14 @@ class CorestoreStorage {
|
|
|
589
614
|
}
|
|
590
615
|
}
|
|
591
616
|
|
|
592
|
-
async _enter
|
|
617
|
+
async _enter() {
|
|
593
618
|
this.enters++
|
|
594
619
|
await this.lock.lock()
|
|
595
620
|
if (this.view === null) this.view = new View()
|
|
596
621
|
return this.view
|
|
597
622
|
}
|
|
598
623
|
|
|
599
|
-
async _exit
|
|
624
|
+
async _exit() {
|
|
600
625
|
this.enters--
|
|
601
626
|
|
|
602
627
|
if (this.flushing === null) this.flushing = rrp()
|
|
@@ -620,7 +645,7 @@ class CorestoreStorage {
|
|
|
620
645
|
|
|
621
646
|
// when used with core catches this isnt transactional for simplicity, HOWEVER, its just a number
|
|
622
647
|
// so worth the tradeoff
|
|
623
|
-
async _allocData
|
|
648
|
+
async _allocData() {
|
|
624
649
|
let dataPointer = 0
|
|
625
650
|
|
|
626
651
|
const view = await this._enter()
|
|
@@ -641,7 +666,7 @@ class CorestoreStorage {
|
|
|
641
666
|
}
|
|
642
667
|
|
|
643
668
|
// exposes here so migrations can easily access the head in an init state
|
|
644
|
-
async _getHead
|
|
669
|
+
async _getHead(view) {
|
|
645
670
|
const rx = new CorestoreRX(this.db, view)
|
|
646
671
|
const headPromise = rx.getHead()
|
|
647
672
|
rx.tryFlush()
|
|
@@ -650,22 +675,22 @@ class CorestoreStorage {
|
|
|
650
675
|
return head === null ? initStoreHead() : head
|
|
651
676
|
}
|
|
652
677
|
|
|
653
|
-
createAtom
|
|
678
|
+
createAtom() {
|
|
654
679
|
return new Atom(this.db)
|
|
655
680
|
}
|
|
656
681
|
|
|
657
|
-
async flush
|
|
682
|
+
async flush() {
|
|
658
683
|
await this.rocks.flush()
|
|
659
684
|
}
|
|
660
685
|
|
|
661
|
-
async close
|
|
686
|
+
async close() {
|
|
662
687
|
if (this.db.closed) return
|
|
663
688
|
await this._flush()
|
|
664
689
|
await this.db.close()
|
|
665
690
|
await this.rocks.close()
|
|
666
691
|
}
|
|
667
692
|
|
|
668
|
-
async clear
|
|
693
|
+
async clear() {
|
|
669
694
|
if (this.version === 0) await this._migrateStore()
|
|
670
695
|
|
|
671
696
|
const view = await this._enter()
|
|
@@ -677,21 +702,21 @@ class CorestoreStorage {
|
|
|
677
702
|
await this._exit()
|
|
678
703
|
}
|
|
679
704
|
|
|
680
|
-
createCoreStream
|
|
705
|
+
createCoreStream() {
|
|
681
706
|
// TODO: be nice to run the mgiration here also, but too much plumbing atm
|
|
682
707
|
return createCoreStream(this.db, EMPTY)
|
|
683
708
|
}
|
|
684
709
|
|
|
685
|
-
createAliasStream
|
|
710
|
+
createAliasStream(namespace) {
|
|
686
711
|
// TODO: be nice to run the mgiration here also, but too much plumbing atm
|
|
687
712
|
return createAliasStream(this.db, EMPTY, namespace)
|
|
688
713
|
}
|
|
689
714
|
|
|
690
|
-
createDiscoveryKeyStream
|
|
715
|
+
createDiscoveryKeyStream(namespace) {
|
|
691
716
|
return createDiscoveryKeyStream(this.db, EMPTY, namespace)
|
|
692
717
|
}
|
|
693
718
|
|
|
694
|
-
async getAlias
|
|
719
|
+
async getAlias(alias) {
|
|
695
720
|
if (this.version === 0) await this._migrateStore()
|
|
696
721
|
|
|
697
722
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -700,7 +725,7 @@ class CorestoreStorage {
|
|
|
700
725
|
return discoveryKeyPromise
|
|
701
726
|
}
|
|
702
727
|
|
|
703
|
-
async getSeed
|
|
728
|
+
async getSeed() {
|
|
704
729
|
if (this.version === 0) await this._migrateStore()
|
|
705
730
|
|
|
706
731
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -712,7 +737,7 @@ class CorestoreStorage {
|
|
|
712
737
|
return head === null ? null : head.seed
|
|
713
738
|
}
|
|
714
739
|
|
|
715
|
-
async setSeed
|
|
740
|
+
async setSeed(seed, { overwrite = true } = {}) {
|
|
716
741
|
if (this.version === 0) await this._migrateStore()
|
|
717
742
|
|
|
718
743
|
const view = await this._enter()
|
|
@@ -736,7 +761,7 @@ class CorestoreStorage {
|
|
|
736
761
|
}
|
|
737
762
|
}
|
|
738
763
|
|
|
739
|
-
async getDefaultDiscoveryKey
|
|
764
|
+
async getDefaultDiscoveryKey() {
|
|
740
765
|
if (this.version === 0) await this._migrateStore()
|
|
741
766
|
|
|
742
767
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -748,7 +773,7 @@ class CorestoreStorage {
|
|
|
748
773
|
return head === null ? null : head.defaultDiscoveryKey
|
|
749
774
|
}
|
|
750
775
|
|
|
751
|
-
async setDefaultDiscoveryKey
|
|
776
|
+
async setDefaultDiscoveryKey(discoveryKey, { overwrite = true } = {}) {
|
|
752
777
|
if (this.version === 0) await this._migrateStore()
|
|
753
778
|
|
|
754
779
|
const view = await this._enter()
|
|
@@ -772,7 +797,7 @@ class CorestoreStorage {
|
|
|
772
797
|
}
|
|
773
798
|
}
|
|
774
799
|
|
|
775
|
-
async has
|
|
800
|
+
async has(discoveryKey, { ifMigrated = false } = {}) {
|
|
776
801
|
if (this.version === 0) await this._migrateStore()
|
|
777
802
|
|
|
778
803
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -788,7 +813,7 @@ class CorestoreStorage {
|
|
|
788
813
|
return true
|
|
789
814
|
}
|
|
790
815
|
|
|
791
|
-
async getAuth
|
|
816
|
+
async getAuth(discoveryKey) {
|
|
792
817
|
if (this.version === 0) await this._migrateStore()
|
|
793
818
|
|
|
794
819
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -799,15 +824,47 @@ class CorestoreStorage {
|
|
|
799
824
|
const core = await corePromise
|
|
800
825
|
if (core === null) return null
|
|
801
826
|
|
|
802
|
-
const
|
|
803
|
-
const authPromise =
|
|
827
|
+
const read = this.db.read({ autoDestroy: true })
|
|
828
|
+
const authPromise = CoreRX.getAuth(read, core)
|
|
804
829
|
|
|
805
|
-
|
|
830
|
+
read.tryFlush()
|
|
806
831
|
|
|
807
832
|
return authPromise
|
|
808
833
|
}
|
|
809
834
|
|
|
810
|
-
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) {
|
|
811
868
|
if (this.version === 0) await this._migrateStore()
|
|
812
869
|
|
|
813
870
|
if (!discoveryKey) {
|
|
@@ -825,7 +882,7 @@ class CorestoreStorage {
|
|
|
825
882
|
return this._resumeFromPointers(EMPTY, discoveryKey, false, core)
|
|
826
883
|
}
|
|
827
884
|
|
|
828
|
-
async export
|
|
885
|
+
async export(discoveryKey, opts) {
|
|
829
886
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
830
887
|
const corePromise = rx.getCore(discoveryKey)
|
|
831
888
|
|
|
@@ -855,7 +912,7 @@ class CorestoreStorage {
|
|
|
855
912
|
}
|
|
856
913
|
}
|
|
857
914
|
|
|
858
|
-
async _resumeFromPointers
|
|
915
|
+
async _resumeFromPointers(view, discoveryKey, create, { version, corePointer, dataPointer }) {
|
|
859
916
|
const core = { corePointer, dataPointer, dependencies: [] }
|
|
860
917
|
|
|
861
918
|
while (true) {
|
|
@@ -875,7 +932,7 @@ class CorestoreStorage {
|
|
|
875
932
|
}
|
|
876
933
|
|
|
877
934
|
// not allowed to throw validation errors as its a shared tx!
|
|
878
|
-
async _create
|
|
935
|
+
async _create(view, { key, manifest, keyPair, encryptionKey, discoveryKey, alias, userData }) {
|
|
879
936
|
const rx = new CorestoreRX(this.db, view)
|
|
880
937
|
const tx = new CorestoreTX(view)
|
|
881
938
|
|
|
@@ -921,7 +978,7 @@ class CorestoreStorage {
|
|
|
921
978
|
return new HypercoreStorage(this, this.db.session(), ptr, EMPTY, null)
|
|
922
979
|
}
|
|
923
980
|
|
|
924
|
-
async create
|
|
981
|
+
async create(data) {
|
|
925
982
|
if (this.version === 0) await this._migrateStore()
|
|
926
983
|
|
|
927
984
|
const view = await this._enter()
|
|
@@ -936,7 +993,7 @@ class CorestoreStorage {
|
|
|
936
993
|
|
|
937
994
|
module.exports = CorestoreStorage
|
|
938
995
|
|
|
939
|
-
function initStoreHead
|
|
996
|
+
function initStoreHead() {
|
|
940
997
|
return {
|
|
941
998
|
version: 0, // cause we wanna run the migration
|
|
942
999
|
allocated: {
|
|
@@ -948,7 +1005,7 @@ function initStoreHead () {
|
|
|
948
1005
|
}
|
|
949
1006
|
}
|
|
950
1007
|
|
|
951
|
-
function getBatch
|
|
1008
|
+
function getBatch(sessions, name, alloc) {
|
|
952
1009
|
for (let i = 0; i < sessions.length; i++) {
|
|
953
1010
|
if (sessions[i].name === name) return sessions[i]
|
|
954
1011
|
}
|
|
@@ -960,11 +1017,11 @@ function getBatch (sessions, name, alloc) {
|
|
|
960
1017
|
return result
|
|
961
1018
|
}
|
|
962
1019
|
|
|
963
|
-
function isCorestoreStorage
|
|
1020
|
+
function isCorestoreStorage(s) {
|
|
964
1021
|
return typeof s === 'object' && !!s && typeof s.setDefaultDiscoveryKey === 'function'
|
|
965
1022
|
}
|
|
966
1023
|
|
|
967
|
-
function createColumnFamily
|
|
1024
|
+
function createColumnFamily(db, opts = {}) {
|
|
968
1025
|
const {
|
|
969
1026
|
tableCacheIndexAndFilterBlocks = true,
|
|
970
1027
|
blockCache = true,
|
|
@@ -987,7 +1044,7 @@ function createColumnFamily (db, opts = {}) {
|
|
|
987
1044
|
}
|
|
988
1045
|
|
|
989
1046
|
// TODO: remove in like 3-6 mo
|
|
990
|
-
function tmpFixStorage
|
|
1047
|
+
function tmpFixStorage(p) {
|
|
991
1048
|
// if CORESTORE file is written, new format
|
|
992
1049
|
if (fs.existsSync(path.join(p, 'CORESTORE'))) return
|
|
993
1050
|
|
|
@@ -997,7 +1054,18 @@ function tmpFixStorage (p) {
|
|
|
997
1054
|
files = fs.readdirSync(p)
|
|
998
1055
|
} catch {}
|
|
999
1056
|
|
|
1000
|
-
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
|
+
])
|
|
1001
1069
|
|
|
1002
1070
|
for (const f of files) {
|
|
1003
1071
|
if (notRocks.has(f)) continue
|
|
@@ -1010,7 +1078,7 @@ function tmpFixStorage (p) {
|
|
|
1010
1078
|
}
|
|
1011
1079
|
}
|
|
1012
1080
|
|
|
1013
|
-
async function exportData
|
|
1081
|
+
async function exportData(ptr, db, opts) {
|
|
1014
1082
|
// just need dataPointer
|
|
1015
1083
|
const reads = [
|
|
1016
1084
|
toArray(createBlockStream(ptr, db, EMPTY, opts)),
|
|
@@ -1027,8 +1095,29 @@ async function exportData (ptr, db, opts) {
|
|
|
1027
1095
|
}
|
|
1028
1096
|
}
|
|
1029
1097
|
|
|
1030
|
-
async function toArray
|
|
1098
|
+
async function toArray(stream) {
|
|
1031
1099
|
const all = []
|
|
1032
1100
|
for await (const e of stream) all.push(e)
|
|
1033
1101
|
return all
|
|
1034
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
|
+
}
|