hypercore 10.31.9 → 10.31.11
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/lib/core.js +12 -0
- package/lib/replicator.js +35 -16
- package/lib/streams.js +1 -1
- package/package.json +1 -1
package/lib/core.js
CHANGED
|
@@ -230,6 +230,8 @@ module.exports = class Core {
|
|
|
230
230
|
}
|
|
231
231
|
|
|
232
232
|
try {
|
|
233
|
+
const updates = []
|
|
234
|
+
|
|
233
235
|
let pos = 0
|
|
234
236
|
|
|
235
237
|
while (pos < length) {
|
|
@@ -252,6 +254,12 @@ module.exports = class Core {
|
|
|
252
254
|
this.bitfield.setRange(segmentStart, segmentEnd - segmentStart, true)
|
|
253
255
|
|
|
254
256
|
pos = segmentEnd + 1
|
|
257
|
+
|
|
258
|
+
updates.push({
|
|
259
|
+
drop: false,
|
|
260
|
+
start: segmentStart,
|
|
261
|
+
length: segmentEnd - segmentStart
|
|
262
|
+
})
|
|
255
263
|
}
|
|
256
264
|
|
|
257
265
|
for (let i = 0; i < length * 2; i++) {
|
|
@@ -295,6 +303,10 @@ module.exports = class Core {
|
|
|
295
303
|
this.header.userData = src.header.userData.slice(0)
|
|
296
304
|
this.header.hints.contiguousLength = Math.min(src.header.hints.contiguousLength, this.header.tree.length)
|
|
297
305
|
|
|
306
|
+
for (const bitfield of updates) {
|
|
307
|
+
this.onupdate(0b0001, bitfield, null, null)
|
|
308
|
+
}
|
|
309
|
+
|
|
298
310
|
await this._flushOplog()
|
|
299
311
|
} finally {
|
|
300
312
|
src._mutex.unlock()
|
package/lib/replicator.js
CHANGED
|
@@ -11,7 +11,7 @@ const caps = require('./caps')
|
|
|
11
11
|
const DEFAULT_MAX_INFLIGHT = [32, 512]
|
|
12
12
|
const SCALE_LATENCY = 50
|
|
13
13
|
const DEFAULT_SEGMENT_SIZE = 256 * 1024 * 8 // 256 KiB in bits
|
|
14
|
-
const NOT_DOWNLOADING_SLACK =
|
|
14
|
+
const NOT_DOWNLOADING_SLACK = 20000 + (Math.random() * 20000) | 0
|
|
15
15
|
|
|
16
16
|
const PRIORITY = {
|
|
17
17
|
NORMAL: 0,
|
|
@@ -305,6 +305,8 @@ class Peer {
|
|
|
305
305
|
this.needsSync = false
|
|
306
306
|
this.syncsProcessing = 0
|
|
307
307
|
|
|
308
|
+
this._remoteContiguousLength = 0
|
|
309
|
+
|
|
308
310
|
// TODO: tweak pipelining so that data sent BEFORE remoteOpened is not cap verified!
|
|
309
311
|
// we might wanna tweak that with some crypto, ie use the cap to encrypt it...
|
|
310
312
|
// or just be aware of that, to only push non leaky data
|
|
@@ -334,7 +336,7 @@ class Peer {
|
|
|
334
336
|
}
|
|
335
337
|
|
|
336
338
|
get remoteContiguousLength () {
|
|
337
|
-
return this.remoteBitfield.findFirst(false,
|
|
339
|
+
return this.remoteBitfield.findFirst(false, this._remoteContiguousLength)
|
|
338
340
|
}
|
|
339
341
|
|
|
340
342
|
getMaxInflight () {
|
|
@@ -736,6 +738,7 @@ class Peer {
|
|
|
736
738
|
}
|
|
737
739
|
|
|
738
740
|
onbitfield ({ start, bitfield }) {
|
|
741
|
+
if (start < this._remoteContiguousLength) this._remoteContiguousLength = start // bitfield is always the truth
|
|
739
742
|
this.remoteBitfield.insert(start, bitfield)
|
|
740
743
|
this.missingBlocks.insert(start, bitfield)
|
|
741
744
|
this._clearLocalRange(start, bitfield.byteLength * 8)
|
|
@@ -744,7 +747,7 @@ class Peer {
|
|
|
744
747
|
|
|
745
748
|
_clearLocalRange (start, length) {
|
|
746
749
|
if (length === 1) {
|
|
747
|
-
this.missingBlocks.set(start, this.
|
|
750
|
+
this.missingBlocks.set(start, this._remoteHasBlock(start) && !this.core.bitfield.get(start))
|
|
748
751
|
return
|
|
749
752
|
}
|
|
750
753
|
|
|
@@ -776,7 +779,7 @@ class Peer {
|
|
|
776
779
|
|
|
777
780
|
_unclearLocalRange (start, length) {
|
|
778
781
|
if (length === 1) {
|
|
779
|
-
this.missingBlocks.set(start, this.
|
|
782
|
+
this.missingBlocks.set(start, this._remoteHasBlock(start) && !this.core.bitfield.get(start))
|
|
780
783
|
return
|
|
781
784
|
}
|
|
782
785
|
|
|
@@ -804,7 +807,13 @@ class Peer {
|
|
|
804
807
|
onrange ({ drop, start, length }) {
|
|
805
808
|
const has = drop === false
|
|
806
809
|
|
|
807
|
-
if (
|
|
810
|
+
if (drop === true && start < this._remoteContiguousLength) {
|
|
811
|
+
this._remoteContiguousLength = start
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
if (start === 0 && drop === false && length > this._remoteContiguousLength) {
|
|
815
|
+
this._remoteContiguousLength = length
|
|
816
|
+
} else if (length === 1) {
|
|
808
817
|
this.remoteBitfield.set(start, has)
|
|
809
818
|
this.missingBlocks.set(start, has && !this.core.bitfield.get(start))
|
|
810
819
|
} else {
|
|
@@ -947,11 +956,6 @@ class Peer {
|
|
|
947
956
|
return false
|
|
948
957
|
}
|
|
949
958
|
|
|
950
|
-
// mb turn this into a YES/NO/MAYBE enum, could simplify ifavail logic
|
|
951
|
-
_blockAvailable (b) { // TODO: fork also
|
|
952
|
-
return this.remoteBitfield.get(b.index)
|
|
953
|
-
}
|
|
954
|
-
|
|
955
959
|
_hasTreeParent (index) {
|
|
956
960
|
if (this.remoteLength >= this.core.tree.length) return true
|
|
957
961
|
|
|
@@ -984,13 +988,18 @@ class Peer {
|
|
|
984
988
|
return false
|
|
985
989
|
}
|
|
986
990
|
|
|
991
|
+
_remoteHasBlock (index) {
|
|
992
|
+
return index < this._remoteContiguousLength || this.remoteBitfield.get(index) === true
|
|
993
|
+
}
|
|
994
|
+
|
|
987
995
|
_requestBlock (b) {
|
|
988
996
|
const { length, fork } = this.core.tree
|
|
989
997
|
|
|
990
|
-
if (this.
|
|
998
|
+
if (this._remoteHasBlock(b.index) === false || fork !== this.remoteFork) {
|
|
991
999
|
this._maybeWant(b.index)
|
|
992
1000
|
return false
|
|
993
1001
|
}
|
|
1002
|
+
|
|
994
1003
|
if (!this._hasTreeParent(b.index)) {
|
|
995
1004
|
return false
|
|
996
1005
|
}
|
|
@@ -1031,6 +1040,16 @@ class Peer {
|
|
|
1031
1040
|
return true
|
|
1032
1041
|
}
|
|
1033
1042
|
|
|
1043
|
+
_findNext (i) {
|
|
1044
|
+
if (i < this._remoteContiguousLength) {
|
|
1045
|
+
i = this.core.bitfield.findFirst(false, i)
|
|
1046
|
+
if (i < this._remoteContiguousLength && i > -1) return i
|
|
1047
|
+
i = this._remoteContiguousLength
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
return this.missingBlocks.findFirst(true, i)
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1034
1053
|
_requestRange (r) {
|
|
1035
1054
|
const { length, fork } = this.core.tree
|
|
1036
1055
|
|
|
@@ -1042,7 +1061,8 @@ class Peer {
|
|
|
1042
1061
|
const index = r.blocks[i]
|
|
1043
1062
|
if (min === -1 || index < min) min = index
|
|
1044
1063
|
if (max === -1 || index > max) max = index
|
|
1045
|
-
|
|
1064
|
+
const has = index < this._remoteContiguousLength || this.missingBlocks.get(index) === true
|
|
1065
|
+
if (has === true && this._requestRangeBlock(index, length)) return true
|
|
1046
1066
|
}
|
|
1047
1067
|
|
|
1048
1068
|
if (min > -1) this._maybeWant(min, max - min)
|
|
@@ -1058,7 +1078,7 @@ class Peer {
|
|
|
1058
1078
|
let i = off
|
|
1059
1079
|
|
|
1060
1080
|
while (true) {
|
|
1061
|
-
i = this.
|
|
1081
|
+
i = this._findNext(i)
|
|
1062
1082
|
if (i === -1 || i >= end) break
|
|
1063
1083
|
|
|
1064
1084
|
if (this._requestRangeBlock(i, length)) return true
|
|
@@ -1068,8 +1088,7 @@ class Peer {
|
|
|
1068
1088
|
i = r.start
|
|
1069
1089
|
|
|
1070
1090
|
while (true) {
|
|
1071
|
-
i = this.
|
|
1072
|
-
|
|
1091
|
+
i = this._findNext(i)
|
|
1073
1092
|
if (i === -1 || i >= off) break
|
|
1074
1093
|
|
|
1075
1094
|
if (this._requestRangeBlock(i, length)) return true
|
|
@@ -1103,7 +1122,7 @@ class Peer {
|
|
|
1103
1122
|
let index = off + i
|
|
1104
1123
|
if (index >= end) index -= len
|
|
1105
1124
|
|
|
1106
|
-
if (this.
|
|
1125
|
+
if (this._remoteHasBlock(index) === false) continue
|
|
1107
1126
|
|
|
1108
1127
|
const req = this._makeRequest(false, 0)
|
|
1109
1128
|
|
package/lib/streams.js
CHANGED
|
@@ -105,7 +105,7 @@ class ByteStream extends Readable {
|
|
|
105
105
|
}
|
|
106
106
|
|
|
107
107
|
this._predownload(this._index + 1)
|
|
108
|
-
data = await this._core.get(this._index
|
|
108
|
+
data = await this._core.get(this._index++, { valueEncoding: 'binary' })
|
|
109
109
|
|
|
110
110
|
if (relativeOffset > 0) data = data.subarray(relativeOffset)
|
|
111
111
|
|