hypercore-storage 1.17.0 → 2.0.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/README.md CHANGED
@@ -18,11 +18,11 @@ const Storage = require('hypercore-storage')
18
18
 
19
19
  Make a new storage engine.
20
20
 
21
- #### `core = await store.create({ key, discoveyKey, manifest?, keyPair?, encryptionKey?, userData? })`
21
+ #### `core = await store.createCore({ key, discoveyKey, manifest?, keyPair?, encryptionKey?, userData? })`
22
22
 
23
23
  Create a new core, returns a storage instance for that core.
24
24
 
25
- #### `core = await store.resume(discoveryKey)`
25
+ #### `core = await store.resumeCore(discoveryKey)`
26
26
 
27
27
  Resume a previously make core. If it doesn't exist it returns `null`.
28
28
 
@@ -33,7 +33,7 @@ When you wanna flush your changes to the underlying storage, use `await atom.flu
33
33
 
34
34
  Internally to "listen" for when that happens you can add an sync hook with `atom.onflush(fn)`
35
35
 
36
- #### `bool = await store.has(discoveryKey)`
36
+ #### `bool = await store.hasCore(discoveryKey)`
37
37
 
38
38
  Check if a core exists.
39
39
 
package/index.js CHANGED
@@ -426,6 +426,9 @@ class CorestoreStorage {
426
426
  this.path = storage !== null ? storage : path.join(db.path, '..')
427
427
  this.readOnly = !!opts.readOnly
428
428
  this.allowBackup = !!opts.allowBackup
429
+ this.deviceFile = null
430
+ this.wait = !!opts.wait
431
+ this.lock = !!opts.lock || this.wait
429
432
 
430
433
  // tmp sync fix for simplicty since not super deployed yet
431
434
  if (this.bootstrap && !this.readOnly) tmpFixStorage(this.path)
@@ -539,9 +542,13 @@ class CorestoreStorage {
539
542
  if (this.bootstrap && !this.readOnly && !this.allowBackup) {
540
543
  const corestoreFile = path.join(this.path, 'CORESTORE')
541
544
 
542
- if (!(await DeviceFile.resume(corestoreFile, { id: this.id }))) {
543
- await DeviceFile.create(corestoreFile, { id: this.id })
544
- }
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()
545
552
  }
546
553
 
547
554
  const rx = new CorestoreRX(this.db, view)
@@ -688,6 +695,7 @@ class CorestoreStorage {
688
695
  await this._flush()
689
696
  await this.db.close()
690
697
  await this.rocks.close()
698
+ if (this.deviceFile) await this.deviceFile.close()
691
699
  }
692
700
 
693
701
  async clear() {
@@ -797,7 +805,7 @@ class CorestoreStorage {
797
805
  }
798
806
  }
799
807
 
800
- async has(discoveryKey, { ifMigrated = false } = {}) {
808
+ async hasCore(discoveryKey, { ifMigrated = false } = {}) {
801
809
  if (this.version === 0) await this._migrateStore()
802
810
 
803
811
  const rx = new CorestoreRX(this.db, EMPTY)
@@ -864,7 +872,17 @@ class CorestoreStorage {
864
872
  return Promise.all(resultPromises)
865
873
  }
866
874
 
867
- async resume(discoveryKey) {
875
+ async suspend() {
876
+ await this.db.suspend()
877
+ if (this.deviceFile) await this.deviceFile.suspend()
878
+ }
879
+
880
+ async resume() {
881
+ if (this.deviceFile) await this.deviceFile.resume()
882
+ await this.db.resume()
883
+ }
884
+
885
+ async resumeCore(discoveryKey) {
868
886
  if (this.version === 0) await this._migrateStore()
869
887
 
870
888
  if (!discoveryKey) {
@@ -978,7 +996,7 @@ class CorestoreStorage {
978
996
  return new HypercoreStorage(this, this.db.session(), ptr, EMPTY, null)
979
997
  }
980
998
 
981
- async create(data) {
999
+ async createCore(data) {
982
1000
  if (this.version === 0) await this._migrateStore()
983
1001
 
984
1002
  const view = await this._enter()
@@ -622,6 +622,7 @@ function getCached (read, cache, index) {
622
622
  async function getByteRangeFromStorage (read, index, roots, cache) {
623
623
  const promises = [getCached(read, cache, index), getByteOffsetFromStorage(read, index, roots, cache)]
624
624
  const [node, offset] = await Promise.all(promises)
625
+ if (!node) throw new Error('Node not found during migration: ' + index)
625
626
  return [offset, node.size]
626
627
  }
627
628
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore-storage",
3
- "version": "1.17.0",
3
+ "version": "2.0.0",
4
4
  "main": "index.js",
5
5
  "files": [
6
6
  "index.js",
@@ -10,7 +10,8 @@
10
10
  ],
11
11
  "scripts": {
12
12
  "format": "prettier --write .",
13
- "test": "prettier --check . && node test/all.js",
13
+ "lint": "prettier --check . && lunte",
14
+ "test": "node test/all.js",
14
15
  "test:bare": "bare test/all.js",
15
16
  "test:generate": "brittle -r test/all.js test/*.js"
16
17
  },
@@ -32,7 +33,7 @@
32
33
  "bare-fs": "^4.0.1",
33
34
  "bare-path": "^3.0.0",
34
35
  "compact-encoding": "^2.16.0",
35
- "device-file": "^1.2.2",
36
+ "device-file": "^2.0.0",
36
37
  "flat-tree": "^1.12.1",
37
38
  "hypercore-crypto": "^3.4.2",
38
39
  "hyperschema": "^1.7.0",
@@ -44,6 +45,7 @@
44
45
  },
45
46
  "devDependencies": {
46
47
  "brittle": "^3.7.0",
48
+ "lunte": "^1.2.0",
47
49
  "prettier": "^3.6.2",
48
50
  "prettier-config-holepunch": "^2.0.0",
49
51
  "test-tmp": "^1.3.1"