hypercore 10.34.0 → 10.35.0

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
@@ -74,7 +74,8 @@ Note that `tree`, `data`, and `bitfield` are normally heavily sparse files.
74
74
  encryptionKey: k, // optionally pass an encryption key to enable block encryption
75
75
  onwait: () => {}, // hook that is called if gets are waiting for download
76
76
  timeout: 0, // wait at max some milliseconds (0 means no timeout)
77
- writable: true // disable appends and truncates
77
+ writable: true, // disable appends and truncates
78
+ inflightRange: null // Advanced option. Set to [minInflight, maxInflight] to change the min and max inflight blocks per peer when downloading.
78
79
  }
79
80
  ```
80
81
 
package/index.js CHANGED
@@ -60,6 +60,7 @@ module.exports = class Hypercore extends EventEmitter {
60
60
  this.crypto = opts.crypto || hypercoreCrypto
61
61
  this.core = null
62
62
  this.replicator = null
63
+ this.inflightRange = opts.inflightRange || null
63
64
  this.encryption = null
64
65
  this.extensions = new Map()
65
66
  this.cache = createCache(opts.cache)
@@ -404,6 +405,7 @@ module.exports = class Hypercore extends EventEmitter {
404
405
  eagerUpgrade: true,
405
406
  notDownloadingLinger: opts.notDownloadingLinger,
406
407
  allowFork: opts.allowFork !== false,
408
+ inflightRange: this.inflightRange,
407
409
  onpeerupdate: this._onpeerupdate.bind(this),
408
410
  onupload: this._onupload.bind(this),
409
411
  oninvalid: this._oninvalid.bind(this)
package/lib/replicator.js CHANGED
@@ -1,3 +1,27 @@
1
+ /* DEV DOCS
2
+ Every hypercore has one Replicator object managing its connections to other peers.
3
+ There is one Peer object per peer connected to the Hypercore.
4
+ Hypercores do not know about other hypercores, so when a peer is connected to multiple cores, there exists one Peer object per core.
5
+
6
+ Hypercore indicates block should be downloaded through methods like Replicator.addRange or Replicator.addBlock
7
+ Hypercore calls Replicator.updateActivity every time a hypercore session opens/closes
8
+ Replicator.updateActivity ensures the Hypercore is downloading blocks as expected
9
+ Replicator keeps track of:
10
+ - Which blocks need to be downloaded (Replicator._blocks)
11
+ - Which blocks currently have inflight requests (Replicator._inflight)
12
+
13
+ Blocks are requested from remote peers by Peer objects. The flow is:
14
+ - The replicator's updatePeer method gets called
15
+ - The replicator detects whether the Peer can accept more requests (for example by checking if it's maxed out on inflight blocks)
16
+ - The replicator then tells the Peer what to request (e.g. Peer_requestRange or Peer._requestBlock methods)
17
+
18
+ The Peer object is responsible for tracking
19
+ - Which blocks does the Peer have available (tracked in remoteBitfield)
20
+ - Which blocks are you actively looking for from this peer (tracked in missingBlocks)
21
+ - How many blocks are currently inflight (tracked in inflight)
22
+ The Peer uses this information to decide which blocks to request form the peer in response to _requestRange requests and the like.
23
+ */
24
+
1
25
  const b4a = require('b4a')
2
26
  const safetyCatch = require('safety-catch')
3
27
  const RandomIterator = require('random-array-iterator')
@@ -268,7 +292,7 @@ class BlockTracker {
268
292
  }
269
293
 
270
294
  class Peer {
271
- constructor (replicator, protomux, channel, session) {
295
+ constructor (replicator, protomux, channel, session, inflightRange) {
272
296
  this.tracer = createTracer(this, { parent: replicator.core.tracer })
273
297
  this.core = replicator.core
274
298
  this.replicator = replicator
@@ -276,6 +300,7 @@ class Peer {
276
300
  this.protomux = protomux
277
301
  this.remotePublicKey = this.stream.remotePublicKey
278
302
  this.remoteSupportsSeeks = false
303
+ this.inflightRange = inflightRange
279
304
 
280
305
  this.paused = false
281
306
 
@@ -299,7 +324,6 @@ class Peer {
299
324
  this.receiverBusy = false
300
325
 
301
326
  this.inflight = 0
302
- this.inflightRange = DEFAULT_MAX_INFLIGHT
303
327
  this.dataProcessing = 0
304
328
 
305
329
  this.canUpgrade = true
@@ -1087,7 +1111,7 @@ class Peer {
1087
1111
  return false
1088
1112
  }
1089
1113
 
1090
- const end = Math.min(r.end === -1 ? this.remoteLength : r.end, this.remoteLength)
1114
+ const end = Math.min(this.core.tree.length, Math.min(r.end === -1 ? this.remoteLength : r.end, this.remoteLength))
1091
1115
  if (end <= r.start || fork !== this.remoteFork) return false
1092
1116
 
1093
1117
  const len = end - r.start
@@ -1215,10 +1239,13 @@ class Peer {
1215
1239
  }
1216
1240
 
1217
1241
  module.exports = class Replicator {
1242
+ static Peer = Peer // hack to be able to access Peer from outside this module
1243
+
1218
1244
  constructor (core, key, {
1219
1245
  notDownloadingLinger = NOT_DOWNLOADING_SLACK,
1220
1246
  eagerUpgrade = true,
1221
1247
  allowFork = true,
1248
+ inflightRange = null,
1222
1249
  onpeerupdate = noop,
1223
1250
  onupload = noop,
1224
1251
  oninvalid = noop
@@ -1239,6 +1266,8 @@ module.exports = class Replicator {
1239
1266
  this.downloading = false
1240
1267
  this.activeSessions = 0
1241
1268
 
1269
+ this.inflightRange = inflightRange || DEFAULT_MAX_INFLIGHT
1270
+
1242
1271
  this._attached = new Set()
1243
1272
  this._inflight = new InflightTracker()
1244
1273
  this._blocks = new BlockTracker()
@@ -2088,7 +2117,7 @@ module.exports = class Replicator {
2088
2117
 
2089
2118
  if (channel === null) return onnochannel()
2090
2119
 
2091
- const peer = new Peer(replicator, protomux, channel, session)
2120
+ const peer = new Peer(replicator, protomux, channel, session, this.inflightRange)
2092
2121
  const stream = protomux.stream
2093
2122
 
2094
2123
  peer.channel.open({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hypercore",
3
- "version": "10.34.0",
3
+ "version": "10.35.0",
4
4
  "description": "Hypercore is a secure, distributed append-only log",
5
5
  "main": "index.js",
6
6
  "scripts": {