hypercore 11.9.0 → 11.10.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 +1 -1
- package/lib/core.js +1 -1
- package/lib/replicator.js +16 -0
- package/lib/session-state.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -165,7 +165,7 @@ Make a read stream to read a range of data out at once.
|
|
|
165
165
|
// read the full core
|
|
166
166
|
const fullStream = core.createReadStream()
|
|
167
167
|
|
|
168
|
-
// read from block 10-
|
|
168
|
+
// read from block 10-14
|
|
169
169
|
const partialStream = core.createReadStream({ start: 10, end: 15 })
|
|
170
170
|
|
|
171
171
|
// pipe the stream somewhere using the .pipe method on Node.js or consume it as
|
package/lib/core.js
CHANGED
|
@@ -469,8 +469,8 @@ module.exports = class Core {
|
|
|
469
469
|
for (let i = 0; i < verifies.length; i++) {
|
|
470
470
|
const bitfield = verifies[i] && verifies[i].bitfield
|
|
471
471
|
if (bitfield) {
|
|
472
|
-
this.replicator.onhave(bitfield.start, bitfield.length, bitfield.drop)
|
|
473
472
|
this.updateContiguousLength(bitfield)
|
|
473
|
+
this.replicator.onhave(bitfield.start, bitfield.length, bitfield.drop)
|
|
474
474
|
}
|
|
475
475
|
}
|
|
476
476
|
} finally {
|
package/lib/replicator.js
CHANGED
|
@@ -39,6 +39,8 @@ const SCALE_LATENCY = 50
|
|
|
39
39
|
const DEFAULT_SEGMENT_SIZE = 256 * 1024 * 8 // 256 KiB in bits
|
|
40
40
|
const NOT_DOWNLOADING_SLACK = 20000 + (Math.random() * 20000) | 0
|
|
41
41
|
const MAX_PEERS_UPGRADE = 3
|
|
42
|
+
const LAST_BLOCKS = 256
|
|
43
|
+
const MAX_REMOTE_SEGMENTS = 2048
|
|
42
44
|
|
|
43
45
|
const MAX_RANGES = 64
|
|
44
46
|
|
|
@@ -371,6 +373,7 @@ class Peer {
|
|
|
371
373
|
this.remotePublicKey = this.stream.remotePublicKey
|
|
372
374
|
this.remoteSupportsSeeks = false
|
|
373
375
|
this.inflightRange = inflightRange
|
|
376
|
+
this.remoteSegmentsWanted = new Set()
|
|
374
377
|
|
|
375
378
|
this.paused = false
|
|
376
379
|
this.removed = false
|
|
@@ -480,6 +483,16 @@ class Peer {
|
|
|
480
483
|
if (drop) this._unclearLocalRange(start, length)
|
|
481
484
|
else this._clearLocalRange(start, length)
|
|
482
485
|
|
|
486
|
+
const i = Math.floor(start / DEFAULT_SEGMENT_SIZE)
|
|
487
|
+
const contig = this.core.header.hints.contiguousLength === this.core.state.length
|
|
488
|
+
|
|
489
|
+
if (start + LAST_BLOCKS < this.core.state.length && !this.remoteSegmentsWanted.has(i) && !drop && !contig) return
|
|
490
|
+
|
|
491
|
+
if (contig && !drop) {
|
|
492
|
+
start = 0
|
|
493
|
+
length = this.core.state.length
|
|
494
|
+
}
|
|
495
|
+
|
|
483
496
|
// TODO: consider also adding early-returns on the drop===true case
|
|
484
497
|
if (!drop) {
|
|
485
498
|
// No need to broadcast if the remote already has this range
|
|
@@ -952,6 +965,9 @@ class Peer {
|
|
|
952
965
|
}
|
|
953
966
|
|
|
954
967
|
onwant ({ start, length }) {
|
|
968
|
+
const i = Math.floor(start / DEFAULT_SEGMENT_SIZE)
|
|
969
|
+
if (this.remoteSegmentsWanted.size >= MAX_REMOTE_SEGMENTS) this.remoteSegmentsWanted.clear() // just in case of abuse
|
|
970
|
+
this.remoteSegmentsWanted.add(i)
|
|
955
971
|
this.replicator._onwant(this, start, length)
|
|
956
972
|
}
|
|
957
973
|
|
package/lib/session-state.js
CHANGED
|
@@ -509,7 +509,7 @@ module.exports = class SessionState {
|
|
|
509
509
|
await this.mutex.lock()
|
|
510
510
|
|
|
511
511
|
try {
|
|
512
|
-
if (maxLength >= 0 && (this.length + values.length)
|
|
512
|
+
if (maxLength >= 0 && (this.length + values.length) > maxLength) {
|
|
513
513
|
return { length: this.length, byteLength: this.byteLength }
|
|
514
514
|
}
|
|
515
515
|
|