hypercore 10.0.0-alpha.26 → 10.0.0-alpha.27

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 CHANGED
@@ -94,8 +94,8 @@ module.exports = class Hypercore extends EventEmitter {
94
94
  indent + ')'
95
95
  }
96
96
 
97
- static protomux (stream, opts) {
98
- return stream.noiseStream.userData.open(opts)
97
+ static getProtocolMuxer (stream) {
98
+ return stream.noiseStream.userData
99
99
  }
100
100
 
101
101
  static createProtocolStream (isInitiator, opts = {}) {
@@ -392,7 +392,7 @@ module.exports = class Hypercore extends EventEmitter {
392
392
  }
393
393
  }
394
394
 
395
- this.replicator.signalUpgrade()
395
+ this.replicator.localUpgrade()
396
396
  }
397
397
 
398
398
  if (bitfield) {
package/lib/replicator.js CHANGED
@@ -213,6 +213,10 @@ class BlockTracker {
213
213
  yield * this._additional
214
214
  }
215
215
 
216
+ isEmpty () {
217
+ return this._indexed.size === 0 && this._additional.length === 0
218
+ }
219
+
216
220
  has (fork, index) {
217
221
  return this.get(fork, index) !== null
218
222
  }
@@ -863,14 +867,16 @@ module.exports = class Replicator {
863
867
  for (const peer of this.peers) peer.protomux.uncork()
864
868
  }
865
869
 
866
- signalUpgrade () {
867
- for (const peer of this.peers) peer.signalUpgrade()
868
- }
869
-
870
870
  broadcastRange (start, length, drop = false) {
871
871
  for (const peer of this.peers) peer.broadcastRange(start, length, drop)
872
872
  }
873
873
 
874
+ localUpgrade () {
875
+ for (const peer of this.peers) peer.signalUpgrade()
876
+ if (this._blocks.isEmpty() === false) this._resolveBlocksLocally()
877
+ if (this._upgrade !== null) this._resolveUpgradeRequest(null)
878
+ }
879
+
874
880
  addUpgrade (session) {
875
881
  if (this._upgrade !== null) {
876
882
  const ref = this._upgrade.attach(session)
@@ -1065,6 +1071,34 @@ module.exports = class Replicator {
1065
1071
  this._queued.push(b)
1066
1072
  }
1067
1073
 
1074
+ // Runs in the background - not allowed to throw
1075
+ async _resolveBlocksLocally () {
1076
+ // TODO: check if fork compat etc. Requires that we pass down truncation info
1077
+
1078
+ let clear = null
1079
+
1080
+ for (const b of this._blocks) {
1081
+ if (this.core.bitfield.get(b.index) === false) continue
1082
+
1083
+ try {
1084
+ b.resolve(await this.core.blocks.get(b.index))
1085
+ } catch (err) {
1086
+ b.reject(err)
1087
+ }
1088
+
1089
+ if (clear === null) clear = []
1090
+ clear.push(b)
1091
+ }
1092
+
1093
+ if (clear === null) return
1094
+
1095
+ // Currently the block tracker does not support deletes during iteration, so we make
1096
+ // sure to clear them afterwards.
1097
+ for (const b of clear) {
1098
+ this._blocks.remove(b.fork, b.index)
1099
+ }
1100
+ }
1101
+
1068
1102
  _resolveBlockRequest (tracker, fork, index, value, req) {
1069
1103
  const b = tracker.remove(fork, index)
1070
1104
  if (b === null) return false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.26",
3
+ "version": "10.0.0-alpha.27",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {