hypercore 10.0.0-alpha.26 → 10.0.0-alpha.29

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
@@ -51,7 +51,7 @@ module.exports = class Hypercore extends EventEmitter {
51
51
  this.core = null
52
52
  this.replicator = null
53
53
  this.encryption = null
54
- this.extensions = opts.extensions || new Map()
54
+ this.extensions = new Map()
55
55
  this.cache = opts.cache === true ? new Xache({ maxSize: 65536, maxAge: 0 }) : (opts.cache || null)
56
56
 
57
57
  this.valueEncoding = null
@@ -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 = {}) {
@@ -157,7 +157,6 @@ module.exports = class Hypercore extends EventEmitter {
157
157
  const Clz = opts.class || Hypercore
158
158
  const s = new Clz(this.storage, this.key, {
159
159
  ...opts,
160
- extensions: this.extensions,
161
160
  _opening: this.opening,
162
161
  _sessions: this.sessions
163
162
  })
@@ -182,12 +181,7 @@ module.exports = class Hypercore extends EventEmitter {
182
181
  async _openFromExisting (from, opts) {
183
182
  await from.opening
184
183
 
185
- for (const [name, ext] of this.extensions) {
186
- from.extensions.register(name, null, ext)
187
- }
188
-
189
184
  this._passCapabilities(from)
190
- this.extensions = from.extensions
191
185
  this.sessions = from.sessions
192
186
  this.storage = from.storage
193
187
 
@@ -392,7 +386,7 @@ module.exports = class Hypercore extends EventEmitter {
392
386
  }
393
387
  }
394
388
 
395
- this.replicator.signalUpgrade()
389
+ this.replicator.localUpgrade()
396
390
  }
397
391
 
398
392
  if (bitfield) {
@@ -411,14 +405,14 @@ module.exports = class Hypercore extends EventEmitter {
411
405
  _onpeerupdate (added, peer) {
412
406
  const name = added ? 'peer-add' : 'peer-remove'
413
407
 
414
- if (added) {
415
- for (const ext of this.extensions.values()) {
416
- peer.extensions.set(ext.name, ext)
417
- }
418
- }
419
-
420
408
  for (let i = 0; i < this.sessions.length; i++) {
421
409
  this.sessions[i].emit(name, peer)
410
+
411
+ if (added) {
412
+ for (const ext of this.sessions[i].extensions.values()) {
413
+ peer.extensions.set(ext.name, ext)
414
+ }
415
+ }
422
416
  }
423
417
  }
424
418
 
@@ -610,7 +604,7 @@ module.exports = class Hypercore extends EventEmitter {
610
604
  },
611
605
  destroy () {
612
606
  for (const peer of this.session.peers) {
613
- peer.extensions.delete(name)
607
+ if (peer.extensions.get(name) === ext) peer.extensions.delete(name)
614
608
  }
615
609
  this.session.extensions.delete(name)
616
610
  },
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
@@ -1090,11 +1124,13 @@ module.exports = class Replicator {
1090
1124
  }
1091
1125
 
1092
1126
  _clearInflightBlock (tracker, req) {
1093
- const b = tracker.get(req.fork, req.block.index)
1127
+ const isBlock = tracker === this._blocks
1128
+ const index = isBlock === true ? req.block.index : req.hash.index / 2
1129
+ const b = tracker.get(req.fork, index)
1094
1130
 
1095
1131
  if (b === null || removeInflight(b.inflight, req) === false) return
1096
1132
 
1097
- if (b.refs.length > 0 && tracker === this._blocks) {
1133
+ if (b.refs.length > 0 && isBlock === true) {
1098
1134
  this._queueBlock(b)
1099
1135
  return
1100
1136
  }
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.29",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {