hypercore 10.0.0-alpha.33 → 10.0.0-alpha.36
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 +7 -0
- package/index.js +32 -1
- package/lib/replicator.js +4 -0
- package/package.json +1 -1
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) {
|
|
@@ -123,7 +124,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
123
124
|
}
|
|
124
125
|
if (opts.keepAlive !== false) {
|
|
125
126
|
noiseStream.setKeepAlive(5000)
|
|
126
|
-
noiseStream.setTimeout(
|
|
127
|
+
noiseStream.setTimeout(10000)
|
|
127
128
|
}
|
|
128
129
|
noiseStream.userData = protocol
|
|
129
130
|
}
|
|
@@ -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()
|
|
@@ -322,6 +329,13 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
322
329
|
const noiseStream = protocolStream.noiseStream
|
|
323
330
|
const protocol = noiseStream.userData
|
|
324
331
|
|
|
332
|
+
// If the user wants to, we can make this replication run in a session
|
|
333
|
+
// that way the core wont close "under them" during replication
|
|
334
|
+
if (opts.session) {
|
|
335
|
+
const s = this.session()
|
|
336
|
+
protocolStream.on('close', () => s.close().catch(noop))
|
|
337
|
+
}
|
|
338
|
+
|
|
325
339
|
if (this.opened) {
|
|
326
340
|
this.replicator.attachTo(protocol)
|
|
327
341
|
} else {
|
|
@@ -432,6 +446,22 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
432
446
|
return null
|
|
433
447
|
}
|
|
434
448
|
|
|
449
|
+
findingPeers () {
|
|
450
|
+
this._findingPeers++
|
|
451
|
+
if (this.replicator !== null && !this.closing) this.replicator.findingPeers++
|
|
452
|
+
|
|
453
|
+
let once = true
|
|
454
|
+
|
|
455
|
+
return () => {
|
|
456
|
+
if (this.closing || !once) return
|
|
457
|
+
once = false
|
|
458
|
+
this._findingPeers--
|
|
459
|
+
if (this.replicator !== null && --this.replicator.findingPeers === 0) {
|
|
460
|
+
this.replicator.updateAll()
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
435
465
|
async update (opts) {
|
|
436
466
|
if (this.opened === false) await this.opening
|
|
437
467
|
|
|
@@ -441,6 +471,7 @@ module.exports = class Hypercore extends EventEmitter {
|
|
|
441
471
|
const activeRequests = (opts && opts.activeRequests) || this.activeRequests
|
|
442
472
|
const req = this.replicator.addUpgrade(activeRequests)
|
|
443
473
|
|
|
474
|
+
// TODO: if snapshot, also update the length/byteLength to latest
|
|
444
475
|
return req.promise
|
|
445
476
|
}
|
|
446
477
|
|
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)
|