hypercore 10.30.3 → 10.30.5
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 +8 -1
- package/lib/batch.js +9 -0
- package/lib/replicator.js +17 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -391,7 +391,8 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
391
391
|
eagerUpgrade: true,
|
|
392
392
|
allowFork: opts.allowFork !== false,
|
|
393
393
|
onpeerupdate: this._onpeerupdate.bind(this),
|
|
394
|
-
onupload: this._onupload.bind(this)
|
|
394
|
+
onupload: this._onupload.bind(this),
|
|
395
|
+
oninvalid: this._oninvalid.bind(this)
|
|
395
396
|
})
|
|
396
397
|
|
|
397
398
|
this.replicator.findingPeers += this._findingPeers
|
|
@@ -624,6 +625,12 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
624
625
|
}
|
|
625
626
|
}
|
|
626
627
|
|
|
628
|
+
_oninvalid (err, req, res, from) {
|
|
629
|
+
for (let i = 0; i < this.sessions.length; i++) {
|
|
630
|
+
this.sessions[i].emit('verification-error', err, req, res, from)
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
627
634
|
async _oncoreconflict (proof, from) {
|
|
628
635
|
await this.replicator.onconflict(from)
|
|
629
636
|
|
package/lib/batch.js
CHANGED
|
@@ -317,6 +317,15 @@ module.exports = class HypercoreBatch extends EventEmitter {
|
|
|
317
317
|
const flushingLength = Math.min(length - this._sessionLength, this._appends.length)
|
|
318
318
|
if (flushingLength <= 0) return true
|
|
319
319
|
|
|
320
|
+
if (this.session.replicator._upgrade) {
|
|
321
|
+
for (const req of this.session.replicator._upgrade.inflight) {
|
|
322
|
+
// yield to the remote inflight upgrade, TODO: if the remote upgrade fails, retry flushing...
|
|
323
|
+
if (req.upgrade && (req.upgrade.start + req.upgrade.length) > length) {
|
|
324
|
+
return false
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
320
329
|
const batch = this.createTreeBatch(this._sessionLength + flushingLength)
|
|
321
330
|
if (batch === null) return false
|
|
322
331
|
|
package/lib/replicator.js
CHANGED
|
@@ -636,11 +636,11 @@ class Peer {
|
|
|
636
636
|
this.wireCancel.send({ request: id })
|
|
637
637
|
}
|
|
638
638
|
|
|
639
|
-
_checkIfConflict (
|
|
639
|
+
_checkIfConflict () {
|
|
640
640
|
this.paused = true
|
|
641
641
|
|
|
642
642
|
const length = Math.min(this.core.tree.length, this.remoteLength)
|
|
643
|
-
if (length === 0)
|
|
643
|
+
if (length === 0) return // pause and ignore
|
|
644
644
|
|
|
645
645
|
this.wireRequest.send({
|
|
646
646
|
id: 0, // TODO: use an more explicit id for this eventually...
|
|
@@ -675,7 +675,14 @@ class Peer {
|
|
|
675
675
|
this.replicator._removeInflight(req.id)
|
|
676
676
|
}
|
|
677
677
|
|
|
678
|
-
|
|
678
|
+
try {
|
|
679
|
+
if (reorg === true) return await this.replicator._onreorgdata(this, req, data)
|
|
680
|
+
} catch (err) {
|
|
681
|
+
safetyCatch(err)
|
|
682
|
+
this.paused = true
|
|
683
|
+
this.replicator.oninvalid(err, req, data, this)
|
|
684
|
+
return
|
|
685
|
+
}
|
|
679
686
|
|
|
680
687
|
this.dataProcessing++
|
|
681
688
|
|
|
@@ -688,9 +695,12 @@ class Peer {
|
|
|
688
695
|
safetyCatch(err)
|
|
689
696
|
if (this.core.closed && !isCriticalError(err)) return
|
|
690
697
|
|
|
691
|
-
|
|
692
|
-
|
|
698
|
+
if (err.code !== 'INVALID_OPERATION') {
|
|
699
|
+
// might be a fork, verify
|
|
700
|
+
this._checkIfConflict()
|
|
701
|
+
}
|
|
693
702
|
this.replicator._onnodata(this, req)
|
|
703
|
+
this.replicator.oninvalid(err, req, data, this)
|
|
694
704
|
return
|
|
695
705
|
} finally {
|
|
696
706
|
this.dataProcessing--
|
|
@@ -1102,7 +1112,7 @@ class Peer {
|
|
|
1102
1112
|
}
|
|
1103
1113
|
|
|
1104
1114
|
module.exports = class Replicator {
|
|
1105
|
-
constructor (core, key, { eagerUpgrade = true, allowFork = true, onpeerupdate = noop, onupload = noop } = {}) {
|
|
1115
|
+
constructor (core, key, { eagerUpgrade = true, allowFork = true, onpeerupdate = noop, onupload = noop, oninvalid = noop } = {}) {
|
|
1106
1116
|
this.key = key
|
|
1107
1117
|
this.discoveryKey = core.crypto.discoveryKey(key)
|
|
1108
1118
|
this.core = core
|
|
@@ -1110,6 +1120,7 @@ module.exports = class Replicator {
|
|
|
1110
1120
|
this.allowFork = allowFork
|
|
1111
1121
|
this.onpeerupdate = onpeerupdate
|
|
1112
1122
|
this.onupload = onupload
|
|
1123
|
+
this.oninvalid = oninvalid
|
|
1113
1124
|
this.ondownloading = null // optional external hook for monitoring downloading status
|
|
1114
1125
|
this.peers = []
|
|
1115
1126
|
this.findingPeers = 0 // updateable from the outside
|