hypercore-storage 1.8.6 → 1.9.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.
Files changed (2) hide show
  1. package/index.js +49 -4
  2. package/package.json +2 -1
package/index.js CHANGED
@@ -1,6 +1,9 @@
1
1
  const RocksDB = require('rocksdb-native')
2
2
  const rrp = require('resolve-reject-promise')
3
3
  const ScopeLock = require('scope-lock')
4
+ const DeviceFile = require('device-file')
5
+ const path = require('path')
6
+ const fs = require('fs')
4
7
  const View = require('./lib/view.js')
5
8
 
6
9
  const VERSION = 1
@@ -346,10 +349,19 @@ class HypercoreStorage {
346
349
  }
347
350
 
348
351
  class CorestoreStorage {
349
- constructor (db, opts) {
350
- this.path = typeof db === 'string' ? db : db.path
351
- this.rocks = typeof db === 'string' ? new RocksDB(db, opts) : db
352
+ constructor (db, opts = {}) {
353
+ const storage = typeof db === 'string' ? db : null
354
+
355
+ this.bootstrap = storage !== null
356
+ this.path = storage !== null ? storage : path.join(db.path, '..')
357
+ this.readOnly = !!opts.readOnly
358
+
359
+ // tmp sync fix for simplicty since not super deployed yet
360
+ if (this.bootstrap && !this.readOnly) tmpFixStorage(this.path)
361
+
362
+ this.rocks = storage === null ? db : new RocksDB(path.join(this.path, 'db'), opts)
352
363
  this.db = createColumnFamily(this.rocks, opts)
364
+ this.id = opts.id || null
353
365
  this.view = null
354
366
  this.enters = 0
355
367
  this.lock = new ScopeLock()
@@ -426,6 +438,14 @@ class CorestoreStorage {
426
438
  try {
427
439
  if (this.version === VERSION) return
428
440
 
441
+ if (this.bootstrap && this.readOnly) {
442
+ const corestoreFile = path.join(this.path, 'CORESTORE')
443
+
444
+ if (!(await DeviceFile.resume(corestoreFile, { id: this.id }))) {
445
+ await DeviceFile.create(corestoreFile, { id: this.id })
446
+ }
447
+ }
448
+
429
449
  const rx = new CorestoreRX(this.db, view)
430
450
  const headPromise = rx.getHead()
431
451
 
@@ -557,6 +577,10 @@ class CorestoreStorage {
557
577
  return new Atom(this.db)
558
578
  }
559
579
 
580
+ async flush () {
581
+ await this.rocks.flush()
582
+ }
583
+
560
584
  async close () {
561
585
  if (this.db.closed) return
562
586
  await this._flush()
@@ -678,7 +702,7 @@ class CorestoreStorage {
678
702
  const core = await promise
679
703
 
680
704
  if (core === null) return false
681
- if (core.version !== VERSION && !ifMigrated) return false
705
+ if (core.version !== VERSION && ifMigrated) return false
682
706
 
683
707
  return true
684
708
  }
@@ -850,3 +874,24 @@ function createColumnFamily (db, opts = {}) {
850
874
 
851
875
  return db.columnFamily(col)
852
876
  }
877
+
878
+ // TODO: remove in like 3-6 mo
879
+ function tmpFixStorage (p) {
880
+ let files = []
881
+
882
+ try {
883
+ files = fs.readdirSync(p)
884
+ } catch {}
885
+
886
+ const notRocks = new Set(['CORESTORE', 'primary-key', 'cores', 'app-preferences', 'cache', 'preferences.json', 'db'])
887
+
888
+ for (const f of files) {
889
+ if (notRocks.has(f)) continue
890
+
891
+ try {
892
+ fs.mkdirSync(path.join(p, 'db'))
893
+ } catch {}
894
+
895
+ fs.renameSync(path.join(p, f), path.join(p, 'db', f))
896
+ }
897
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "1.8.6",
3
+ "version": "1.9.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",
@@ -31,6 +31,7 @@
31
31
  "bare-fs": "^4.0.1",
32
32
  "bare-path": "^3.0.0",
33
33
  "compact-encoding": "^2.16.0",
34
+ "device-file": "^1.2.2",
34
35
  "flat-tree": "^1.12.1",
35
36
  "hypercore-crypto": "^3.4.2",
36
37
  "hyperschema": "^1.7.0",