hypercore-storage 0.0.24 → 0.0.26
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 +21 -33
- package/lib/memory-overlay.js +1 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -263,12 +263,9 @@ class ReadBatch {
|
|
|
263
263
|
}
|
|
264
264
|
|
|
265
265
|
module.exports = class CoreStorage {
|
|
266
|
-
constructor (dir
|
|
266
|
+
constructor (dir) {
|
|
267
267
|
this.db = new RocksDB(dir)
|
|
268
268
|
this.mutex = new RW()
|
|
269
|
-
this.autoClose = !!autoClose
|
|
270
|
-
|
|
271
|
-
this.sessions = 0
|
|
272
269
|
}
|
|
273
270
|
|
|
274
271
|
// just a helper to make tests easier
|
|
@@ -361,7 +358,7 @@ module.exports = class CoreStorage {
|
|
|
361
358
|
return new HypercoreStorage(this, discoveryKey, core, data, null)
|
|
362
359
|
}
|
|
363
360
|
|
|
364
|
-
async create ({ key, manifest, keyPair, encryptionKey, discoveryKey }) {
|
|
361
|
+
async create ({ key, manifest, keyPair, encryptionKey, discoveryKey, userData }) {
|
|
365
362
|
await this.mutex.write.lock()
|
|
366
363
|
|
|
367
364
|
try {
|
|
@@ -393,7 +390,7 @@ module.exports = class CoreStorage {
|
|
|
393
390
|
const batch = new WriteBatch(storage, write)
|
|
394
391
|
|
|
395
392
|
initialiseCoreInfo(batch, { key, manifest, keyPair, encryptionKey })
|
|
396
|
-
initialiseCoreData(batch)
|
|
393
|
+
initialiseCoreData(batch, { userData })
|
|
397
394
|
|
|
398
395
|
await batch.flush()
|
|
399
396
|
return storage
|
|
@@ -401,11 +398,6 @@ module.exports = class CoreStorage {
|
|
|
401
398
|
this.mutex.write.unlock()
|
|
402
399
|
}
|
|
403
400
|
}
|
|
404
|
-
|
|
405
|
-
_onclose () {
|
|
406
|
-
if (--this.sessions > 0 || !this.autoClose) return Promise.resolve()
|
|
407
|
-
return this.close()
|
|
408
|
-
}
|
|
409
401
|
}
|
|
410
402
|
|
|
411
403
|
class HypercoreStorage {
|
|
@@ -415,8 +407,6 @@ class HypercoreStorage {
|
|
|
415
407
|
this.dbSnapshot = snapshot
|
|
416
408
|
this.mutex = root.mutex
|
|
417
409
|
|
|
418
|
-
this.root.sessions++
|
|
419
|
-
|
|
420
410
|
this.discoveryKey = discoveryKey
|
|
421
411
|
|
|
422
412
|
this.dependencies = []
|
|
@@ -425,7 +415,7 @@ class HypercoreStorage {
|
|
|
425
415
|
this.corePointer = core
|
|
426
416
|
this.dataPointer = data
|
|
427
417
|
|
|
428
|
-
this.
|
|
418
|
+
this.destroyed = false
|
|
429
419
|
}
|
|
430
420
|
|
|
431
421
|
get snapshotted () {
|
|
@@ -486,30 +476,30 @@ class HypercoreStorage {
|
|
|
486
476
|
}
|
|
487
477
|
|
|
488
478
|
snapshot () {
|
|
489
|
-
assert(this.
|
|
479
|
+
assert(this.destroyed === false)
|
|
490
480
|
return new HypercoreStorage(this.root, this.discoveryKey, this.corePointer, this.dataPointer, this.db.snapshot())
|
|
491
481
|
}
|
|
492
482
|
|
|
493
483
|
createReadBatch (opts) {
|
|
494
|
-
assert(this.
|
|
484
|
+
assert(this.destroyed === false)
|
|
495
485
|
|
|
496
486
|
const snapshot = this.dbSnapshot
|
|
497
487
|
return new ReadBatch(this, this.db.read({ snapshot }))
|
|
498
488
|
}
|
|
499
489
|
|
|
500
490
|
createWriteBatch () {
|
|
501
|
-
assert(this.
|
|
491
|
+
assert(this.destroyed === false)
|
|
502
492
|
|
|
503
493
|
return new WriteBatch(this, this.db.write())
|
|
504
494
|
}
|
|
505
495
|
|
|
506
496
|
createBlockStream (opts = {}) {
|
|
507
|
-
assert(this.
|
|
497
|
+
assert(this.destroyed === false)
|
|
508
498
|
return createStream(this, createBlockStream, opts)
|
|
509
499
|
}
|
|
510
500
|
|
|
511
501
|
createUserDataStream (opts = {}) {
|
|
512
|
-
assert(this.
|
|
502
|
+
assert(this.destroyed === false)
|
|
513
503
|
|
|
514
504
|
const r = encodeIndexRange(this.dataPointer, DATA.USER_DATA, this.dbSnapshot, opts)
|
|
515
505
|
const s = this.db.iterator(r)
|
|
@@ -518,7 +508,7 @@ class HypercoreStorage {
|
|
|
518
508
|
}
|
|
519
509
|
|
|
520
510
|
createTreeNodeStream (opts = {}) {
|
|
521
|
-
assert(this.
|
|
511
|
+
assert(this.destroyed === false)
|
|
522
512
|
|
|
523
513
|
const r = encodeIndexRange(this.dataPointer, DATA.TREE, this.dbSnapshot, opts)
|
|
524
514
|
const s = this.db.iterator(r)
|
|
@@ -527,7 +517,7 @@ class HypercoreStorage {
|
|
|
527
517
|
}
|
|
528
518
|
|
|
529
519
|
createBitfieldPageStream (opts = {}) {
|
|
530
|
-
assert(this.
|
|
520
|
+
assert(this.destroyed === false)
|
|
531
521
|
|
|
532
522
|
const r = encodeIndexRange(this.dataPointer, DATA.BITFIELD, this.dbSnapshot, opts)
|
|
533
523
|
const s = this.db.iterator(r)
|
|
@@ -536,7 +526,7 @@ class HypercoreStorage {
|
|
|
536
526
|
}
|
|
537
527
|
|
|
538
528
|
async peekLastTreeNode () {
|
|
539
|
-
assert(this.
|
|
529
|
+
assert(this.destroyed === false)
|
|
540
530
|
|
|
541
531
|
const last = await this.db.peek(encodeIndexRange(this.dataPointer, DATA.TREE, this.dbSnapshot, { reverse: true }))
|
|
542
532
|
if (last === null) return null
|
|
@@ -544,7 +534,7 @@ class HypercoreStorage {
|
|
|
544
534
|
}
|
|
545
535
|
|
|
546
536
|
async peekLastBitfieldPage () {
|
|
547
|
-
assert(this.
|
|
537
|
+
assert(this.destroyed === false)
|
|
548
538
|
|
|
549
539
|
const last = await this.db.peek(encodeIndexRange(this.dataPointer, DATA.BITFIELD, this.dbSnapshot, { reverse: true }))
|
|
550
540
|
if (last === null) return null
|
|
@@ -552,18 +542,11 @@ class HypercoreStorage {
|
|
|
552
542
|
}
|
|
553
543
|
|
|
554
544
|
destroy () {
|
|
555
|
-
if (this.
|
|
556
|
-
this.
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
close () {
|
|
560
|
-
if (this.closed) return Promise.resolve()
|
|
561
|
-
this.closed = true
|
|
545
|
+
if (this.destroyed) return
|
|
546
|
+
this.destroyed = true
|
|
562
547
|
|
|
563
548
|
if (this.dbSnapshot) this.dbSnapshot.destroy()
|
|
564
549
|
this.dbSnapshot = null
|
|
565
|
-
|
|
566
|
-
return this.root._onclose()
|
|
567
550
|
}
|
|
568
551
|
}
|
|
569
552
|
|
|
@@ -762,6 +745,11 @@ function initialiseCoreInfo (db, { key, manifest, keyPair, encryptionKey }) {
|
|
|
762
745
|
if (encryptionKey) db.setEncryptionKey(encryptionKey)
|
|
763
746
|
}
|
|
764
747
|
|
|
765
|
-
function initialiseCoreData (db) {
|
|
748
|
+
function initialiseCoreData (db, { userData } = {}) {
|
|
766
749
|
db.setDataInfo({ version: 0 })
|
|
750
|
+
if (userData) {
|
|
751
|
+
for (const { key, value } of userData) {
|
|
752
|
+
db.setUserData(key, value)
|
|
753
|
+
}
|
|
754
|
+
}
|
|
767
755
|
}
|
package/lib/memory-overlay.js
CHANGED