hypercore 10.0.0-alpha.33 → 10.0.0-alpha.34

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
@@ -280,6 +280,13 @@ const socket = net.connect(...)
280
280
  socket.pipe(localCore.replicate(true)).pipe(socket)
281
281
  ```
282
282
 
283
+ #### `const done = core.findingPeers()`
284
+
285
+ Create a hook that tells Hypercore you are finding peers for this core in the background. Call `done` when your current discovery iteration is done.
286
+ If you're using Hyperswarm, you'd normally call this after a `swarm.flush()` finishes.
287
+
288
+ This allows `core.update` to wait for either the `findingPeers` hook to finish or one peer to appear before deciding whether it should wait for a merkle tree update before returning.
289
+
283
290
  #### `core.on('append')`
284
291
 
285
292
  Emitted when the core has been appended to (i.e. has a new length / byteLength), either locally or remotely.
package/index.js CHANGED
@@ -74,6 +74,7 @@ module.exports = class Hypercore extends EventEmitter {
74
74
 
75
75
  this._preappend = preappend.bind(this)
76
76
  this._snapshot = opts.snapshot || null
77
+ this._findingPeers = 0
77
78
  }
78
79
 
79
80
  [inspect] (depth, opts) {
@@ -184,6 +185,7 @@ module.exports = class Hypercore extends EventEmitter {
184
185
  this._passCapabilities(from)
185
186
  this.sessions = from.sessions
186
187
  this.storage = from.storage
188
+ this.replicator.findingPeers += this._findingPeers
187
189
 
188
190
  this.sessions.push(this)
189
191
  }
@@ -271,6 +273,8 @@ module.exports = class Hypercore extends EventEmitter {
271
273
  onupload: this._onupload.bind(this)
272
274
  })
273
275
 
276
+ this.replicator.findingPeers += this._findingPeers
277
+
274
278
  if (!this.encryption && opts.encryptionKey) {
275
279
  this.encryption = new BlockEncryption(opts.encryptionKey, this.key)
276
280
  }
@@ -301,9 +305,12 @@ module.exports = class Hypercore extends EventEmitter {
301
305
  for (const ext of gc) ext.destroy()
302
306
 
303
307
  if (this.replicator !== null) {
308
+ this.replicator.findingPeers -= this._findingPeers
304
309
  this.replicator.clearRequests(this.activeRequests)
305
310
  }
306
311
 
312
+ this._findingPeers = 0
313
+
307
314
  if (this.sessions.length) {
308
315
  // if this is the last session and we are auto closing, trigger that first to enforce error handling
309
316
  if (this.sessions.length === 1 && this.autoClose) await this.sessions[0].close()
@@ -432,6 +439,22 @@ module.exports = class Hypercore extends EventEmitter {
432
439
  return null
433
440
  }
434
441
 
442
+ findingPeers () {
443
+ this._findingPeers++
444
+ if (this.replicator !== null && !this.closing) this.replicator.findingPeers++
445
+
446
+ let once = true
447
+
448
+ return () => {
449
+ if (this.closing || !once) return
450
+ once = false
451
+ this._findingPeers--
452
+ if (this.replicator !== null && --this.replicator.findingPeers === 0) {
453
+ this.replicator.updateAll()
454
+ }
455
+ }
456
+ }
457
+
435
458
  async update (opts) {
436
459
  if (this.opened === false) await this.opening
437
460
 
@@ -441,6 +464,7 @@ module.exports = class Hypercore extends EventEmitter {
441
464
  const activeRequests = (opts && opts.activeRequests) || this.activeRequests
442
465
  const req = this.replicator.addUpgrade(activeRequests)
443
466
 
467
+ // TODO: if snapshot, also update the length/byteLength to latest
444
468
  return req.promise
445
469
  }
446
470
 
package/lib/replicator.js CHANGED
@@ -842,6 +842,7 @@ module.exports = class Replicator {
842
842
  this.onpeerupdate = onpeerupdate
843
843
  this.onupload = onupload
844
844
  this.peers = []
845
+ this.findingPeers = 0 // updateable from the outside
845
846
 
846
847
  this._inflight = new InflightTracker()
847
848
  this._blocks = new BlockTracker(core)
@@ -854,6 +855,7 @@ module.exports = class Replicator {
854
855
  this._reorgs = []
855
856
  this._ranges = []
856
857
 
858
+ this._hadPeers = false
857
859
  this._ifAvailable = 0
858
860
  this._updatesPending = 0
859
861
  this._applyingReorg = false
@@ -959,6 +961,7 @@ module.exports = class Replicator {
959
961
  // Do this when we have more tests.
960
962
  _checkUpgradeIfAvailable () {
961
963
  if (this._ifAvailable > 0 || this._upgrade === null || this._upgrade.refs.length === 0) return
964
+ if (this._hadPeers === false && this.findingPeers > 0) return
962
965
 
963
966
  // check if a peer can upgrade us
964
967
 
@@ -1047,6 +1050,7 @@ module.exports = class Replicator {
1047
1050
  }
1048
1051
 
1049
1052
  _addPeer (peer) {
1053
+ this._hadPeers = true
1050
1054
  this.peers.push(peer)
1051
1055
  this.updatePeer(peer)
1052
1056
  this.onpeerupdate(true, peer)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.0.0-alpha.33",
3
+ "version": "10.0.0-alpha.34",
4
4
  "description": "Hypercore 10",
5
5
  "main": "index.js",
6
6
  "scripts": {