hypercore 10.31.6 → 10.31.7
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/bitfield.js +1 -1
- package/lib/remote-bitfield.js +7 -3
- package/lib/replicator.js +22 -12
- package/package.json +1 -1
package/lib/bitfield.js
CHANGED
|
@@ -43,7 +43,7 @@ class BitfieldPage {
|
|
|
43
43
|
let i = Math.floor(start / 128)
|
|
44
44
|
const n = i + Math.ceil(length / 128)
|
|
45
45
|
|
|
46
|
-
while (i
|
|
46
|
+
while (i <= n) this.tree.update(this.offset * 8 + i++ * 128)
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
findFirst (val, position) {
|
package/lib/remote-bitfield.js
CHANGED
|
@@ -38,7 +38,7 @@ class RemoteBitfieldPage {
|
|
|
38
38
|
let i = Math.floor(start / 128)
|
|
39
39
|
const n = i + Math.ceil(length / 128)
|
|
40
40
|
|
|
41
|
-
while (i
|
|
41
|
+
while (i <= n) this.tree.update(this.offset * 8 + i++ * 128)
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
findFirst (val, position) {
|
|
@@ -65,6 +65,7 @@ class RemoteBitfieldSegment {
|
|
|
65
65
|
this.offset = index * BYTES_PER_SEGMENT
|
|
66
66
|
this.tree = quickbit.Index.from([], BYTES_PER_SEGMENT)
|
|
67
67
|
this.pages = new Array(PAGES_PER_SEGMENT)
|
|
68
|
+
this.pagesLength = 0
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
get chunks () {
|
|
@@ -76,7 +77,10 @@ class RemoteBitfieldSegment {
|
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
add (page) {
|
|
79
|
-
|
|
80
|
+
const pageIndex = page.index - this.index * PAGES_PER_SEGMENT
|
|
81
|
+
if (pageIndex >= this.pagesLength) this.pagesLength = pageIndex + 1
|
|
82
|
+
|
|
83
|
+
this.pages[pageIndex] = page
|
|
80
84
|
|
|
81
85
|
const chunk = { field: page.bitfield, offset: page.offset }
|
|
82
86
|
|
|
@@ -98,7 +102,7 @@ class RemoteBitfieldSegment {
|
|
|
98
102
|
|
|
99
103
|
if (i >= PAGES_PER_SEGMENT) return -1
|
|
100
104
|
|
|
101
|
-
while (i < this.
|
|
105
|
+
while (i < this.pagesLength) {
|
|
102
106
|
const p = this.pages[i]
|
|
103
107
|
|
|
104
108
|
let index = -1
|
package/lib/replicator.js
CHANGED
|
@@ -743,20 +743,23 @@ class Peer {
|
|
|
743
743
|
|
|
744
744
|
_clearLocalRange (start, length) {
|
|
745
745
|
if (length === 1) {
|
|
746
|
-
this.missingBlocks.set(start,
|
|
746
|
+
this.missingBlocks.set(start, this.remoteBitfield.get(start) && !this.core.bitfield.get(start))
|
|
747
747
|
return
|
|
748
748
|
}
|
|
749
749
|
|
|
750
750
|
const contig = Math.min(this.core.tree.length, this.core.header.hints.contiguousLength)
|
|
751
751
|
|
|
752
|
-
if (start < contig) {
|
|
752
|
+
if (start + length < contig) {
|
|
753
753
|
const delta = contig - start
|
|
754
|
-
this.missingBlocks.setRange(start, delta)
|
|
755
|
-
|
|
756
|
-
length -= delta
|
|
754
|
+
this.missingBlocks.setRange(start, delta, false)
|
|
755
|
+
return
|
|
757
756
|
}
|
|
758
757
|
|
|
759
|
-
|
|
758
|
+
const rem = start & 32767
|
|
759
|
+
if (rem > 0) {
|
|
760
|
+
start -= rem
|
|
761
|
+
length += rem
|
|
762
|
+
}
|
|
760
763
|
|
|
761
764
|
const end = start + Math.min(length, this.core.tree.length)
|
|
762
765
|
while (start < end) {
|
|
@@ -772,11 +775,17 @@ class Peer {
|
|
|
772
775
|
|
|
773
776
|
_unclearLocalRange (start, length) {
|
|
774
777
|
if (length === 1) {
|
|
775
|
-
this.missingBlocks.set(start, this.remoteBitfield.get(start))
|
|
778
|
+
this.missingBlocks.set(start, this.remoteBitfield.get(start) && !this.core.bitfield.get(start))
|
|
776
779
|
return
|
|
777
780
|
}
|
|
778
781
|
|
|
779
|
-
|
|
782
|
+
const rem = start & 2097151
|
|
783
|
+
if (rem > 0) {
|
|
784
|
+
start -= rem
|
|
785
|
+
length += rem
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
const fixedStart = start
|
|
780
789
|
|
|
781
790
|
const end = start + Math.min(length, this.remoteLength)
|
|
782
791
|
while (start < end) {
|
|
@@ -788,14 +797,14 @@ class Peer {
|
|
|
788
797
|
start += 2097152
|
|
789
798
|
}
|
|
790
799
|
|
|
791
|
-
this._clearLocalRange(
|
|
800
|
+
this._clearLocalRange(fixedStart, length)
|
|
792
801
|
}
|
|
793
802
|
|
|
794
803
|
onrange ({ drop, start, length }) {
|
|
795
804
|
const has = drop === false
|
|
796
805
|
|
|
797
806
|
if (length === 1) {
|
|
798
|
-
this.remoteBitfield.
|
|
807
|
+
this.remoteBitfield.set(start, has)
|
|
799
808
|
this.missingBlocks.set(start, has && !this.core.bitfield.get(start))
|
|
800
809
|
} else {
|
|
801
810
|
const rangeStart = this.remoteBitfield.findFirst(!has, start)
|
|
@@ -1027,12 +1036,14 @@ class Peer {
|
|
|
1027
1036
|
if (r.blocks) {
|
|
1028
1037
|
let min = -1
|
|
1029
1038
|
let max = -1
|
|
1039
|
+
|
|
1030
1040
|
for (let i = r.start; i < r.end; i++) {
|
|
1031
1041
|
const index = r.blocks[i]
|
|
1032
1042
|
if (min === -1 || index < min) min = index
|
|
1033
1043
|
if (max === -1 || index > max) max = index
|
|
1034
1044
|
if (this.missingBlocks.get(index) === true && this._requestRangeBlock(index, length)) return true
|
|
1035
1045
|
}
|
|
1046
|
+
|
|
1036
1047
|
if (min > -1) this._maybeWant(min, max - min)
|
|
1037
1048
|
return false
|
|
1038
1049
|
}
|
|
@@ -1047,7 +1058,6 @@ class Peer {
|
|
|
1047
1058
|
|
|
1048
1059
|
while (true) {
|
|
1049
1060
|
i = this.missingBlocks.findFirst(true, i)
|
|
1050
|
-
|
|
1051
1061
|
if (i === -1 || i >= end) break
|
|
1052
1062
|
|
|
1053
1063
|
if (this._requestRangeBlock(i, length)) return true
|
|
@@ -1061,7 +1071,7 @@ class Peer {
|
|
|
1061
1071
|
|
|
1062
1072
|
if (i === -1 || i >= off) break
|
|
1063
1073
|
|
|
1064
|
-
if (this.
|
|
1074
|
+
if (this._requestRangeBlock(i, length)) return true
|
|
1065
1075
|
i++
|
|
1066
1076
|
}
|
|
1067
1077
|
|