hypercore-storage 1.16.0 → 1.18.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 +201 -104
- 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 +8 -4
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,13 +419,16 @@ 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
|
|
405
426
|
this.path = storage !== null ? storage : path.join(db.path, '..')
|
|
406
427
|
this.readOnly = !!opts.readOnly
|
|
407
428
|
this.allowBackup = !!opts.allowBackup
|
|
429
|
+
this.deviceFile = null
|
|
430
|
+
this.wait = !!opts.wait
|
|
431
|
+
this.lock = !!opts.lock || this.wait
|
|
408
432
|
|
|
409
433
|
// tmp sync fix for simplicty since not super deployed yet
|
|
410
434
|
if (this.bootstrap && !this.readOnly) tmpFixStorage(this.path)
|
|
@@ -420,24 +444,24 @@ class CorestoreStorage {
|
|
|
420
444
|
this.migrating = null
|
|
421
445
|
}
|
|
422
446
|
|
|
423
|
-
get opened
|
|
447
|
+
get opened() {
|
|
424
448
|
return this.db.opened
|
|
425
449
|
}
|
|
426
450
|
|
|
427
|
-
get closed
|
|
451
|
+
get closed() {
|
|
428
452
|
return this.db.closed
|
|
429
453
|
}
|
|
430
454
|
|
|
431
|
-
async ready
|
|
455
|
+
async ready() {
|
|
432
456
|
if (this.version === 0) await this._migrateStore()
|
|
433
457
|
return this.db.ready()
|
|
434
458
|
}
|
|
435
459
|
|
|
436
|
-
compact
|
|
460
|
+
compact() {
|
|
437
461
|
return this.db.compactRange()
|
|
438
462
|
}
|
|
439
463
|
|
|
440
|
-
async audit
|
|
464
|
+
async audit() {
|
|
441
465
|
for await (const { core } of this.createCoreStream()) {
|
|
442
466
|
const coreRx = new CoreRX(core, this.db, EMPTY)
|
|
443
467
|
const authPromise = coreRx.getAuth()
|
|
@@ -456,7 +480,7 @@ class CorestoreStorage {
|
|
|
456
480
|
}
|
|
457
481
|
}
|
|
458
482
|
|
|
459
|
-
async deleteCore
|
|
483
|
+
async deleteCore(ptr) {
|
|
460
484
|
const rx = new CoreRX(ptr, this.db, EMPTY)
|
|
461
485
|
|
|
462
486
|
const authPromise = rx.getAuth()
|
|
@@ -490,16 +514,16 @@ class CorestoreStorage {
|
|
|
490
514
|
return tx.flush()
|
|
491
515
|
}
|
|
492
516
|
|
|
493
|
-
static isCoreStorage
|
|
517
|
+
static isCoreStorage(db) {
|
|
494
518
|
return isCorestoreStorage(db)
|
|
495
519
|
}
|
|
496
520
|
|
|
497
|
-
static from
|
|
521
|
+
static from(db) {
|
|
498
522
|
if (isCorestoreStorage(db)) return db
|
|
499
523
|
return new this(db)
|
|
500
524
|
}
|
|
501
525
|
|
|
502
|
-
async _flush
|
|
526
|
+
async _flush() {
|
|
503
527
|
while (this.enters > 0) {
|
|
504
528
|
await this.lock.lock()
|
|
505
529
|
await this.lock.unlock()
|
|
@@ -507,7 +531,7 @@ class CorestoreStorage {
|
|
|
507
531
|
}
|
|
508
532
|
|
|
509
533
|
// runs pre any other mutation and read
|
|
510
|
-
async _migrateStore
|
|
534
|
+
async _migrateStore() {
|
|
511
535
|
const view = await this._enter()
|
|
512
536
|
|
|
513
537
|
try {
|
|
@@ -518,9 +542,13 @@ class CorestoreStorage {
|
|
|
518
542
|
if (this.bootstrap && !this.readOnly && !this.allowBackup) {
|
|
519
543
|
const corestoreFile = path.join(this.path, 'CORESTORE')
|
|
520
544
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
545
|
+
this.deviceFile = new DeviceFile(corestoreFile, {
|
|
546
|
+
wait: this.wait,
|
|
547
|
+
lock: this.lock,
|
|
548
|
+
data: { id: this.id }
|
|
549
|
+
})
|
|
550
|
+
|
|
551
|
+
await this.deviceFile.ready()
|
|
524
552
|
}
|
|
525
553
|
|
|
526
554
|
const rx = new CorestoreRX(this.db, view)
|
|
@@ -543,7 +571,9 @@ class CorestoreStorage {
|
|
|
543
571
|
break
|
|
544
572
|
}
|
|
545
573
|
default: {
|
|
546
|
-
throw new Error(
|
|
574
|
+
throw new Error(
|
|
575
|
+
'Unsupported version: ' + version + ' - you should probably upgrade your dependencies'
|
|
576
|
+
)
|
|
547
577
|
}
|
|
548
578
|
}
|
|
549
579
|
|
|
@@ -554,7 +584,7 @@ class CorestoreStorage {
|
|
|
554
584
|
}
|
|
555
585
|
|
|
556
586
|
// runs pre the core is returned to the user
|
|
557
|
-
async _migrateCore
|
|
587
|
+
async _migrateCore(core, discoveryKey, version, locked) {
|
|
558
588
|
const view = locked ? this.view : await this._enter()
|
|
559
589
|
try {
|
|
560
590
|
if (version === VERSION) return
|
|
@@ -567,7 +597,9 @@ class CorestoreStorage {
|
|
|
567
597
|
break
|
|
568
598
|
}
|
|
569
599
|
default: {
|
|
570
|
-
throw new Error(
|
|
600
|
+
throw new Error(
|
|
601
|
+
'Unsupported version: ' + version + ' - you should probably upgrade your dependencies'
|
|
602
|
+
)
|
|
571
603
|
}
|
|
572
604
|
}
|
|
573
605
|
|
|
@@ -589,14 +621,14 @@ class CorestoreStorage {
|
|
|
589
621
|
}
|
|
590
622
|
}
|
|
591
623
|
|
|
592
|
-
async _enter
|
|
624
|
+
async _enter() {
|
|
593
625
|
this.enters++
|
|
594
626
|
await this.lock.lock()
|
|
595
627
|
if (this.view === null) this.view = new View()
|
|
596
628
|
return this.view
|
|
597
629
|
}
|
|
598
630
|
|
|
599
|
-
async _exit
|
|
631
|
+
async _exit() {
|
|
600
632
|
this.enters--
|
|
601
633
|
|
|
602
634
|
if (this.flushing === null) this.flushing = rrp()
|
|
@@ -620,7 +652,7 @@ class CorestoreStorage {
|
|
|
620
652
|
|
|
621
653
|
// when used with core catches this isnt transactional for simplicity, HOWEVER, its just a number
|
|
622
654
|
// so worth the tradeoff
|
|
623
|
-
async _allocData
|
|
655
|
+
async _allocData() {
|
|
624
656
|
let dataPointer = 0
|
|
625
657
|
|
|
626
658
|
const view = await this._enter()
|
|
@@ -641,7 +673,7 @@ class CorestoreStorage {
|
|
|
641
673
|
}
|
|
642
674
|
|
|
643
675
|
// exposes here so migrations can easily access the head in an init state
|
|
644
|
-
async _getHead
|
|
676
|
+
async _getHead(view) {
|
|
645
677
|
const rx = new CorestoreRX(this.db, view)
|
|
646
678
|
const headPromise = rx.getHead()
|
|
647
679
|
rx.tryFlush()
|
|
@@ -650,22 +682,23 @@ class CorestoreStorage {
|
|
|
650
682
|
return head === null ? initStoreHead() : head
|
|
651
683
|
}
|
|
652
684
|
|
|
653
|
-
createAtom
|
|
685
|
+
createAtom() {
|
|
654
686
|
return new Atom(this.db)
|
|
655
687
|
}
|
|
656
688
|
|
|
657
|
-
async flush
|
|
689
|
+
async flush() {
|
|
658
690
|
await this.rocks.flush()
|
|
659
691
|
}
|
|
660
692
|
|
|
661
|
-
async close
|
|
693
|
+
async close() {
|
|
662
694
|
if (this.db.closed) return
|
|
663
695
|
await this._flush()
|
|
664
696
|
await this.db.close()
|
|
665
697
|
await this.rocks.close()
|
|
698
|
+
if (this.deviceFile) await this.deviceFile.close()
|
|
666
699
|
}
|
|
667
700
|
|
|
668
|
-
async clear
|
|
701
|
+
async clear() {
|
|
669
702
|
if (this.version === 0) await this._migrateStore()
|
|
670
703
|
|
|
671
704
|
const view = await this._enter()
|
|
@@ -677,21 +710,21 @@ class CorestoreStorage {
|
|
|
677
710
|
await this._exit()
|
|
678
711
|
}
|
|
679
712
|
|
|
680
|
-
createCoreStream
|
|
713
|
+
createCoreStream() {
|
|
681
714
|
// TODO: be nice to run the mgiration here also, but too much plumbing atm
|
|
682
715
|
return createCoreStream(this.db, EMPTY)
|
|
683
716
|
}
|
|
684
717
|
|
|
685
|
-
createAliasStream
|
|
718
|
+
createAliasStream(namespace) {
|
|
686
719
|
// TODO: be nice to run the mgiration here also, but too much plumbing atm
|
|
687
720
|
return createAliasStream(this.db, EMPTY, namespace)
|
|
688
721
|
}
|
|
689
722
|
|
|
690
|
-
createDiscoveryKeyStream
|
|
723
|
+
createDiscoveryKeyStream(namespace) {
|
|
691
724
|
return createDiscoveryKeyStream(this.db, EMPTY, namespace)
|
|
692
725
|
}
|
|
693
726
|
|
|
694
|
-
async getAlias
|
|
727
|
+
async getAlias(alias) {
|
|
695
728
|
if (this.version === 0) await this._migrateStore()
|
|
696
729
|
|
|
697
730
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -700,7 +733,7 @@ class CorestoreStorage {
|
|
|
700
733
|
return discoveryKeyPromise
|
|
701
734
|
}
|
|
702
735
|
|
|
703
|
-
async getSeed
|
|
736
|
+
async getSeed() {
|
|
704
737
|
if (this.version === 0) await this._migrateStore()
|
|
705
738
|
|
|
706
739
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -712,7 +745,7 @@ class CorestoreStorage {
|
|
|
712
745
|
return head === null ? null : head.seed
|
|
713
746
|
}
|
|
714
747
|
|
|
715
|
-
async setSeed
|
|
748
|
+
async setSeed(seed, { overwrite = true } = {}) {
|
|
716
749
|
if (this.version === 0) await this._migrateStore()
|
|
717
750
|
|
|
718
751
|
const view = await this._enter()
|
|
@@ -736,7 +769,7 @@ class CorestoreStorage {
|
|
|
736
769
|
}
|
|
737
770
|
}
|
|
738
771
|
|
|
739
|
-
async getDefaultDiscoveryKey
|
|
772
|
+
async getDefaultDiscoveryKey() {
|
|
740
773
|
if (this.version === 0) await this._migrateStore()
|
|
741
774
|
|
|
742
775
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -748,7 +781,7 @@ class CorestoreStorage {
|
|
|
748
781
|
return head === null ? null : head.defaultDiscoveryKey
|
|
749
782
|
}
|
|
750
783
|
|
|
751
|
-
async setDefaultDiscoveryKey
|
|
784
|
+
async setDefaultDiscoveryKey(discoveryKey, { overwrite = true } = {}) {
|
|
752
785
|
if (this.version === 0) await this._migrateStore()
|
|
753
786
|
|
|
754
787
|
const view = await this._enter()
|
|
@@ -772,7 +805,7 @@ class CorestoreStorage {
|
|
|
772
805
|
}
|
|
773
806
|
}
|
|
774
807
|
|
|
775
|
-
async has
|
|
808
|
+
async has(discoveryKey, { ifMigrated = false } = {}) {
|
|
776
809
|
if (this.version === 0) await this._migrateStore()
|
|
777
810
|
|
|
778
811
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -788,7 +821,7 @@ class CorestoreStorage {
|
|
|
788
821
|
return true
|
|
789
822
|
}
|
|
790
823
|
|
|
791
|
-
async getAuth
|
|
824
|
+
async getAuth(discoveryKey) {
|
|
792
825
|
if (this.version === 0) await this._migrateStore()
|
|
793
826
|
|
|
794
827
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
@@ -799,15 +832,47 @@ class CorestoreStorage {
|
|
|
799
832
|
const core = await corePromise
|
|
800
833
|
if (core === null) return null
|
|
801
834
|
|
|
802
|
-
const
|
|
803
|
-
const authPromise =
|
|
835
|
+
const read = this.db.read({ autoDestroy: true })
|
|
836
|
+
const authPromise = CoreRX.getAuth(read, core)
|
|
804
837
|
|
|
805
|
-
|
|
838
|
+
read.tryFlush()
|
|
806
839
|
|
|
807
840
|
return authPromise
|
|
808
841
|
}
|
|
809
842
|
|
|
810
|
-
async
|
|
843
|
+
async getInfo(discoveryKey, opts) {
|
|
844
|
+
return (await this.getInfos([discoveryKey], opts))[0]
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
async getInfos(discoveryKeys, { auth = true, head = true, hints = true } = {}) {
|
|
848
|
+
if (this.version === 0) await this._migrateStore()
|
|
849
|
+
|
|
850
|
+
const rx = new CorestoreRX(this.db, EMPTY)
|
|
851
|
+
const corePromises = new Array(discoveryKeys.length)
|
|
852
|
+
|
|
853
|
+
for (let i = 0; i < discoveryKeys.length; i++) {
|
|
854
|
+
corePromises[i] = rx.getCore(discoveryKeys[i])
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
rx.tryFlush()
|
|
858
|
+
|
|
859
|
+
const cores = await Promise.all(corePromises)
|
|
860
|
+
const read = this.db.read({ autoDestroy: true })
|
|
861
|
+
|
|
862
|
+
const resultPromises = new Array(cores.length)
|
|
863
|
+
|
|
864
|
+
for (let i = 0; i < cores.length; i++) {
|
|
865
|
+
resultPromises[i] = cores[i]
|
|
866
|
+
? getInfoFromBatch(read, cores[i], discoveryKeys[i], auth, head, hints)
|
|
867
|
+
: null
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
read.tryFlush()
|
|
871
|
+
|
|
872
|
+
return Promise.all(resultPromises)
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
async resume(discoveryKey) {
|
|
811
876
|
if (this.version === 0) await this._migrateStore()
|
|
812
877
|
|
|
813
878
|
if (!discoveryKey) {
|
|
@@ -825,7 +890,7 @@ class CorestoreStorage {
|
|
|
825
890
|
return this._resumeFromPointers(EMPTY, discoveryKey, false, core)
|
|
826
891
|
}
|
|
827
892
|
|
|
828
|
-
async export
|
|
893
|
+
async export(discoveryKey, opts) {
|
|
829
894
|
const rx = new CorestoreRX(this.db, EMPTY)
|
|
830
895
|
const corePromise = rx.getCore(discoveryKey)
|
|
831
896
|
|
|
@@ -855,7 +920,7 @@ class CorestoreStorage {
|
|
|
855
920
|
}
|
|
856
921
|
}
|
|
857
922
|
|
|
858
|
-
async _resumeFromPointers
|
|
923
|
+
async _resumeFromPointers(view, discoveryKey, create, { version, corePointer, dataPointer }) {
|
|
859
924
|
const core = { corePointer, dataPointer, dependencies: [] }
|
|
860
925
|
|
|
861
926
|
while (true) {
|
|
@@ -875,7 +940,7 @@ class CorestoreStorage {
|
|
|
875
940
|
}
|
|
876
941
|
|
|
877
942
|
// not allowed to throw validation errors as its a shared tx!
|
|
878
|
-
async _create
|
|
943
|
+
async _create(view, { key, manifest, keyPair, encryptionKey, discoveryKey, alias, userData }) {
|
|
879
944
|
const rx = new CorestoreRX(this.db, view)
|
|
880
945
|
const tx = new CorestoreTX(view)
|
|
881
946
|
|
|
@@ -921,7 +986,7 @@ class CorestoreStorage {
|
|
|
921
986
|
return new HypercoreStorage(this, this.db.session(), ptr, EMPTY, null)
|
|
922
987
|
}
|
|
923
988
|
|
|
924
|
-
async create
|
|
989
|
+
async create(data) {
|
|
925
990
|
if (this.version === 0) await this._migrateStore()
|
|
926
991
|
|
|
927
992
|
const view = await this._enter()
|
|
@@ -936,7 +1001,7 @@ class CorestoreStorage {
|
|
|
936
1001
|
|
|
937
1002
|
module.exports = CorestoreStorage
|
|
938
1003
|
|
|
939
|
-
function initStoreHead
|
|
1004
|
+
function initStoreHead() {
|
|
940
1005
|
return {
|
|
941
1006
|
version: 0, // cause we wanna run the migration
|
|
942
1007
|
allocated: {
|
|
@@ -948,7 +1013,7 @@ function initStoreHead () {
|
|
|
948
1013
|
}
|
|
949
1014
|
}
|
|
950
1015
|
|
|
951
|
-
function getBatch
|
|
1016
|
+
function getBatch(sessions, name, alloc) {
|
|
952
1017
|
for (let i = 0; i < sessions.length; i++) {
|
|
953
1018
|
if (sessions[i].name === name) return sessions[i]
|
|
954
1019
|
}
|
|
@@ -960,11 +1025,11 @@ function getBatch (sessions, name, alloc) {
|
|
|
960
1025
|
return result
|
|
961
1026
|
}
|
|
962
1027
|
|
|
963
|
-
function isCorestoreStorage
|
|
1028
|
+
function isCorestoreStorage(s) {
|
|
964
1029
|
return typeof s === 'object' && !!s && typeof s.setDefaultDiscoveryKey === 'function'
|
|
965
1030
|
}
|
|
966
1031
|
|
|
967
|
-
function createColumnFamily
|
|
1032
|
+
function createColumnFamily(db, opts = {}) {
|
|
968
1033
|
const {
|
|
969
1034
|
tableCacheIndexAndFilterBlocks = true,
|
|
970
1035
|
blockCache = true,
|
|
@@ -987,7 +1052,7 @@ function createColumnFamily (db, opts = {}) {
|
|
|
987
1052
|
}
|
|
988
1053
|
|
|
989
1054
|
// TODO: remove in like 3-6 mo
|
|
990
|
-
function tmpFixStorage
|
|
1055
|
+
function tmpFixStorage(p) {
|
|
991
1056
|
// if CORESTORE file is written, new format
|
|
992
1057
|
if (fs.existsSync(path.join(p, 'CORESTORE'))) return
|
|
993
1058
|
|
|
@@ -997,7 +1062,18 @@ function tmpFixStorage (p) {
|
|
|
997
1062
|
files = fs.readdirSync(p)
|
|
998
1063
|
} catch {}
|
|
999
1064
|
|
|
1000
|
-
const notRocks = new Set([
|
|
1065
|
+
const notRocks = new Set([
|
|
1066
|
+
'CORESTORE',
|
|
1067
|
+
'primary-key',
|
|
1068
|
+
'cores',
|
|
1069
|
+
'app-preferences',
|
|
1070
|
+
'cache',
|
|
1071
|
+
'preferences.json',
|
|
1072
|
+
'db',
|
|
1073
|
+
'clone',
|
|
1074
|
+
'core',
|
|
1075
|
+
'notifications'
|
|
1076
|
+
])
|
|
1001
1077
|
|
|
1002
1078
|
for (const f of files) {
|
|
1003
1079
|
if (notRocks.has(f)) continue
|
|
@@ -1010,7 +1086,7 @@ function tmpFixStorage (p) {
|
|
|
1010
1086
|
}
|
|
1011
1087
|
}
|
|
1012
1088
|
|
|
1013
|
-
async function exportData
|
|
1089
|
+
async function exportData(ptr, db, opts) {
|
|
1014
1090
|
// just need dataPointer
|
|
1015
1091
|
const reads = [
|
|
1016
1092
|
toArray(createBlockStream(ptr, db, EMPTY, opts)),
|
|
@@ -1027,8 +1103,29 @@ async function exportData (ptr, db, opts) {
|
|
|
1027
1103
|
}
|
|
1028
1104
|
}
|
|
1029
1105
|
|
|
1030
|
-
async function toArray
|
|
1106
|
+
async function toArray(stream) {
|
|
1031
1107
|
const all = []
|
|
1032
1108
|
for await (const e of stream) all.push(e)
|
|
1033
1109
|
return all
|
|
1034
1110
|
}
|
|
1111
|
+
|
|
1112
|
+
function noop() {}
|
|
1113
|
+
|
|
1114
|
+
async function getInfoFromBatch(db, c, discoveryKey, getAuth, getHead, getHints) {
|
|
1115
|
+
const authPromise = getAuth ? CoreRX.getAuth(db, c) : null
|
|
1116
|
+
const headPromise = getHead ? CoreRX.getHead(db, c) : null
|
|
1117
|
+
const hintsPromise = getHints ? CoreRX.getHints(db, c) : null
|
|
1118
|
+
|
|
1119
|
+
// ensure no uncaughts
|
|
1120
|
+
if (authPromise) authPromise.catch(noop)
|
|
1121
|
+
if (headPromise) headPromise.catch(noop)
|
|
1122
|
+
if (hintsPromise) hintsPromise.catch(noop)
|
|
1123
|
+
|
|
1124
|
+
return {
|
|
1125
|
+
discoveryKey,
|
|
1126
|
+
core: c,
|
|
1127
|
+
auth: await authPromise,
|
|
1128
|
+
head: await headPromise,
|
|
1129
|
+
hints: await hintsPromise
|
|
1130
|
+
}
|
|
1131
|
+
}
|