hypercore 11.38.0 → 11.39.1
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 +2 -2
- package/lib/core.js +25 -0
- package/lib/session-state.js +11 -6
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -711,7 +711,7 @@ class Hypercore extends EventEmitter {
|
|
|
711
711
|
|
|
712
712
|
async recover() {
|
|
713
713
|
if (this.opened === false) await this.opening
|
|
714
|
-
return this.state.waitForRecovery()
|
|
714
|
+
return this.state.waitForRecovery(this, false)
|
|
715
715
|
}
|
|
716
716
|
|
|
717
717
|
async setUserData(key, value) {
|
|
@@ -955,7 +955,7 @@ class Hypercore extends EventEmitter {
|
|
|
955
955
|
}
|
|
956
956
|
|
|
957
957
|
async purge() {
|
|
958
|
-
await this.
|
|
958
|
+
if (this.opened === false) await this.opening
|
|
959
959
|
await this.core.purge()
|
|
960
960
|
}
|
|
961
961
|
|
package/lib/core.js
CHANGED
|
@@ -939,6 +939,31 @@ module.exports = class Core {
|
|
|
939
939
|
await Promise.allSettled(all)
|
|
940
940
|
}
|
|
941
941
|
|
|
942
|
+
// closes every session and deletes the core from storage
|
|
943
|
+
async purge() {
|
|
944
|
+
if (this.closing !== null) throw new Error('Cannot purge a closed core')
|
|
945
|
+
|
|
946
|
+
const db = this.db
|
|
947
|
+
const ptr = this.storage.core
|
|
948
|
+
const autoClose = this.autoClose
|
|
949
|
+
|
|
950
|
+
// closing the last session would close the storage along with the core
|
|
951
|
+
this.autoClose = false
|
|
952
|
+
|
|
953
|
+
try {
|
|
954
|
+
await this.closeAllSessions(null)
|
|
955
|
+
if (this.hasSession() === true) throw new Error('Cannot purge while sessions are open')
|
|
956
|
+
await this.close()
|
|
957
|
+
} catch (err) {
|
|
958
|
+
this.autoClose = autoClose
|
|
959
|
+
throw err
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
await db.deleteCore(ptr)
|
|
963
|
+
|
|
964
|
+
if (autoClose) await db.close()
|
|
965
|
+
}
|
|
966
|
+
|
|
942
967
|
async destroy() {
|
|
943
968
|
if (this.destroyed === true) return
|
|
944
969
|
this.destroyed = true
|
package/lib/session-state.js
CHANGED
|
@@ -612,11 +612,11 @@ class SessionState {
|
|
|
612
612
|
}
|
|
613
613
|
|
|
614
614
|
// simple tool to wait for other peers to catch us up
|
|
615
|
-
async waitForRecovery() {
|
|
615
|
+
async waitForRecovery(session, writable) {
|
|
616
616
|
const length = this.length
|
|
617
617
|
const replicator = this.core.replicator
|
|
618
618
|
|
|
619
|
-
while (
|
|
619
|
+
while (session === null ? this.core.closing === null : session.closing === null) {
|
|
620
620
|
let peers = getRecoveryPeers(replicator.peers)
|
|
621
621
|
|
|
622
622
|
if (peers.length < 1) {
|
|
@@ -631,7 +631,8 @@ class SessionState {
|
|
|
631
631
|
|
|
632
632
|
peers = getRecoveryPeers(replicator.peers)
|
|
633
633
|
|
|
634
|
-
|
|
634
|
+
const closing = session === null ? this.core.closing !== null : session.closing !== null
|
|
635
|
+
if (closing || !validateRecoveryPeers(peers, this.length)) {
|
|
635
636
|
restart = true
|
|
636
637
|
break
|
|
637
638
|
}
|
|
@@ -639,7 +640,7 @@ class SessionState {
|
|
|
639
640
|
|
|
640
641
|
if (restart) continue
|
|
641
642
|
|
|
642
|
-
if (this.length !== length) {
|
|
643
|
+
if (this.length !== length && writable) {
|
|
643
644
|
throw SESSION_NOT_WRITABLE('core recovered during append')
|
|
644
645
|
}
|
|
645
646
|
|
|
@@ -661,7 +662,9 @@ class SessionState {
|
|
|
661
662
|
async append(values, { signature, keyPair, preappend, postappend, maxLength = -1 } = {}) {
|
|
662
663
|
if (!keyPair && this.isDefault()) keyPair = this.core.header.keyPair
|
|
663
664
|
|
|
664
|
-
if (this.isDefault() && this.core.header.hints.recovering)
|
|
665
|
+
if (this.isDefault() && this.core.header.hints.recovering) {
|
|
666
|
+
await this.waitForRecovery(null, true)
|
|
667
|
+
}
|
|
665
668
|
|
|
666
669
|
await this.mutex.lock()
|
|
667
670
|
|
|
@@ -1050,7 +1053,9 @@ class SessionState {
|
|
|
1050
1053
|
throw ASSERTION('Cannot commit while repair mode is on')
|
|
1051
1054
|
}
|
|
1052
1055
|
|
|
1053
|
-
if (this.core.header.hints.recovering)
|
|
1056
|
+
if (this.core.header.hints.recovering) {
|
|
1057
|
+
await this.waitForRecovery(null, true)
|
|
1058
|
+
}
|
|
1054
1059
|
|
|
1055
1060
|
let srcLocked = false
|
|
1056
1061
|
await this.mutex.lock()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hypercore",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.39.1",
|
|
4
4
|
"description": "Hypercore is a secure, distributed append-only log",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"hypercore-crypto": "^3.2.1",
|
|
55
55
|
"hypercore-errors": "^1.5.0",
|
|
56
56
|
"hypercore-id-encoding": "^1.2.0",
|
|
57
|
-
"hypercore-storage": "^3.2
|
|
57
|
+
"hypercore-storage": "^3.3.2",
|
|
58
58
|
"is-options": "^1.0.1",
|
|
59
59
|
"nanoassert": "^2.0.0",
|
|
60
60
|
"protomux": "^3.5.0",
|